//一共创建了5辆车。。写的应该还算有条理,比较容易懂
//其中cc是载货量,pc是载人量,pr是租一天的价格
package project3.rentcars;
/**/define superclass**
public abstract class Carrent {
public int cc;
public int pc;
public int pr;
public abstract void descrip();
//get method,(get太长了,我用的m代替)
public abstract int mcc();
public abstract int mpc();
public abstract int mpr();
//public abstract void set(/*int cc,int pc,int pr*/);
//public abstract int mpr();
/*the method set is no necessary(我觉得并不要用set方法,用了反而后面重写的时候麻烦,我没有用private修饰变量,这样可能不太安全。。)*/
}
//subclass truck1
public class Truck1 extends Carrent {
public int mcc(){
int cc=5;
return cc;
}
public int mpc(){
int pc=0;
return pc;
}
public int mpr(){
int pr=100;
return pr;
};
@Override
public void descrip() {
// TODO Auto-generated method stub
System.out.println("truck1:");
System.out.println("cargo capality:"+mcc()+"T");
System.out.println("person capality:"+mpc());
System.out.println("rental:"+mpr()+"$ for one day");
}
}
//subclass truck2
public class Truck2 extends Carrent {
public int mcc(){
int cc=10;
return cc;
}
public int mpc(){
int pc=0;
return pc;
}
public int mpr(){
int pr=200;
return pr;
}; @Override
public void descrip() {
// TODO Auto-generated method stub
System.out.println("truck2:");
System.out.println("cargo capality:"+mcc()+"T");
System.out.println("person capality:"+mpc());
System.out.println("rental:"+mpr()+"$ for one day");
}
}
//subclass bus1
public class Bus1 extends Carrent {
int cc=0;
int pc=10;
int pr=150;
public int mcc(){
return cc;
}
public int mpc(){
return pc;
}
public int mpr(){
return pr;
};
@Override
public void descrip() {
// TODO Auto-generated method stub
System.out.println("bus1:");
System.out.println("cargo capality:"+mcc()+"T");
System.out.println("person capality:"+mpc());
System.out.println("rental:"+mpr()+"$ for one day");
}
}
//subclass bus2
public class Bus2 extends Carrent {
public int mcc(){
int cc=0;
return cc;
}
public int mpc(){
int pc=15;
return pc;
}
public int mpr(){
int pr=250;
return pr;
}; @Override
public void descrip() {
// TODO Auto-generated method stub
System.out.println("bus2:");
System.out.println("cargo capality:"+mcc()+"T");
System.out.println("person capality:"+mpc());
System.out.println("rental:"+mpr()+"$ for one day");
}
}
//subclass pick
public class Pick extends Carrent {
public int mcc(){
int cc=10;
return cc;
}
public int mpc(){
int pc=15;
return pc;
}
public int mpr(){
int pr=300;
return pr;
}; @Override
public void descrip() {
// TODO Auto-generated method stub
System.out.println("pick:");
System.out.println("cargo capality:"+mcc()+"T");
System.out.println("person capality:"+mpc());
System.out.println("rental:"+mpr()+"$ for one day");
}
}
// main method
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
//define some useful variables
int num;
int numt1;
int numt2;
int numb1;
int numb2;
int numpi;
int loadsum;
int persum;
int rentsum;
System.out.println("do you want to rent cars? ");
System.out.println("1.yes 2.no");
System.out.println("please input 1or2");
Scanner input=new Scanner(System.in);
num=input.nextInt();
//if you want rent cars(input num 1),用了一个选择结构
if(num==1){
//the description of cars
System.out.println("the cars description");
//polymorphism define some pointers point to the subobjects
Carrent t1=new Truck1();
Carrent t2=new Truck2();
Carrent b1=new Bus1();
Carrent b2=new Bus2();
Carrent pi=new Pick();
/*the code above can changed into another way like:Carrent car[]={new Truck1,new Truck2...}*/
t1.descrip();
t2.descrip();
b1.descrip();
b2.descrip();
pi.descrip();
//asking for car renting
System.out.println("how many t1 do you want rent?");
numt1=input.nextInt();
System.out.println("how many t2 do you want rent?");
numt2=input.nextInt();
System.out.println("how many b1 do you want rent?");
numb1=input.nextInt();
System.out.println("how many b2 do you wnat rent?");
numb2=input.nextInt();
System.out.println("how many pi do you wnat rent?");
numpi=input.nextInt();
//print your renting information
System.out.println("your information:");
System.out.println("you have rent");
System.out.println(+numt1 +" t1");
System.out.println(numt2 +" t2");
System.out.println(numb1 +" b1");
System.out.println(numb2 +" b2");
System.out.println(numpi +" pi");
loadsum=t1.mcc()*numt1+t2.mcc()*numt2+b1.mcc()*numb1+b2.mcc()*numb2+pi.mcc()*numpi;
persum=t1.mpc()*numt1+t2.mpc()*numt2+b1.mpc()*numb1+b2.mpc()*numb2+pi.mpc()*numpi;
rentsum=t1.mpr()*numt1+t2.mpr()*numt2+b1.mpr()*numb1+b2.mpr()*numb2+pi.mpr()*numpi;
System.out.println("the total load:"+loadsum+"T");
System.out.println("the total person volume:"+persum);
System.out.println("the total rental:"+rentsum+"$");
System.out.println("thanks for your patronage!");
}
else{
System.out.println("fuck you bitch! go away if you dont rent cars!");
}
}
}
打开App,阅读手记