// 车车的父类
package rentCar;
public class AutoCar {
private boolean flagPerson;
private boolean flagGoods;
private int capacityPer;
private int capacityGoods;
private int price;
private String name;
public AutoCar(boolean flagPerson, boolean flagGoods, int capacityPer, int capacityGoods, int price, String name) {
this.flagPerson = flagPerson;
this.flagGoods = flagGoods;
his.capacityPer = capacityPer;
this.capacityGoods = capacityGoods;
this.price = price;
this.name = name;
}
// getter & setter 方法
public boolean isFlagPerson() { return flagPerson; }
public void setFlagPerson(boolean flagPerson) { this.flagPerson = flagPerson; }
public boolean isFlagGoods() { return flagGoods; }
public void setFlagGoods(boolean flagGoods) { this.flagGoods = flagGoods; }
public int getCapacityPer() { return capacityPer; }
public void setCapacityPer(int capacityPer) { this.capacityPer = capacityPer; }
public int getCapacityGoods() { return capacityGoods; }
public void setCapacityGoods(int capacityGoods) { this.capacityGoods = capacityGoods; }
public int getPrice() { return price; } public void setPrice(int price) { this.price = price; }
public String getName() { return name; } public void setName(String name) { this.name = name; }
// toString 方法
@Override public String toString() { return "AutoCar [capacityPer=" + capacityPer + ", capacityGoods=" + capacityGoods + "]"; }}
// 6个子类,分别对应6种车车
package rentCar;
public class AodiA4 extends AutoCar{
public AodiA4() {
super(true,false,4,0,500,"奥迪A4");
}
}
package rentCar;
public class Mazida6 extends AutoCar {
public Mazida6() {
super(true,false,4,0,400,"马自达6");
}}
package rentCar;
public class Pikaxue6 extends AutoCar {
public Pikaxue6() {
super(true,true,4,2,450,"皮卡学6");
}}
package rentCar;
public class Jinlong extends AutoCar {
public Jinlong() {
super(true,false,20,0,800,"金龙");
}
}
package rentCar;
public class Songhuajiang extends AutoCar {
public Songhuajiang() {
super(false,true,0,4,400,"松花江");
}
}
package rentCar;
public class Yiweike extends AutoCar{
public Yiweike() {
super(false,true,0,20,1000,"依维柯");
}
}
//Main 方法
package rentCar;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 菜单
System.out.println("欢迎使用");
System.out.println("您是否要租车: 1是 0否");
Scanner in = new Scanner(System.in);
int choose = in.nextInt();
if( choose == 1 ) {
System.out.println("您可租的车车类型及价目表");
System.out.println("序号 汽车名称 租金 容量");
System.out.println("1 奥迪a4 500/天 载人4人\n"
+ "2 马自达6 400/天 载人4人 \n"
+ "3 金龙奥迪a4 800/天 载人20人 \n"
+ "4 松花江 400/天 载货4吨 \n"
+ "5 依维柯 1000/天 载货20吨 \n"
+ "6 皮卡雪 450/天 载货2吨载人4人 \n");
System.out.println("请输入你要租车的数量");
Scanner in2 = new Scanner(System.in);
int quantity = in2.nextInt();
List<AutoCar> listCar = new ArrayList<AutoCar>();
for( int i=1; i<=quantity; i++) {
System.out.println("请输入第" + i + "辆车的序号");
Scanner in3 = new Scanner(System.in);
int number = in3.nextInt();
switch(number) {
case 1: listCar.add(new AodiA4()); break;
case 2: listCar.add(new Mazida6()); break;
case 3: listCar.add(new Jinlong()); break;
case 4: listCar.add(new Songhuajiang()); break;
case 5: listCar.add(new Yiweike()); break;
case 6: listCar.add(new Pikaxue6()); break;
}
}
System.out.println("请输入租车天数");
Scanner in4 = new Scanner(System.in);
int days = in4.nextInt();
System.out.println("您的账单");
System.out.println("***可载人的车有:");
for(int i=0; i<quantity; i++) {
if(listCar.get(i).isFlagPerson())
System.out.print(listCar.get(i).getName() + " ");
}
System.out.println();
System.out.println("可载人:");
int totalPer = 0;
for(int i=0; i<quantity; i++) {
totalPer += listCar.get(i).getCapacityPer();
}
System.out.print(totalPer);
System.out.println();
System.out.println("***可载货的车有:");
for(int i=0; i<quantity; i++) {
if(listCar.get(i).isFlagGoods())
System.out.print(listCar.get(i).getName());
}
System.out.println();
System.out.println("可载货:");
int totalGoods = 0;
for(int i=0; i<quantity; i++) {
totalGoods += listCar.get(i).getCapacityGoods();
}
System.out.print(totalGoods);
System.out.println();
int totalPrice = 0;
for(int i=0; i<quantity; i++)
totalPrice += listCar.get(i).getPrice();
System.out.println("租车总价格:" + totalPrice + "元");
}
else
System.out.println("Bye~");
}
}