###Car 抽象类
//Car 类
public abstract class Car {
private int price;//车的租金
private String name;//车的名字
private int passengerLoad;//载客人数
private double goodsLoad;//载客数量
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;
}
public int getPassengerLoad() {
return passengerLoad;
}
public void setPassengerLoad(int passengerLoad) {
this.passengerLoad = passengerLoad;
}
public double getGoodsLoad() {
return goodsLoad;
}
public void setGoodsLoad(double goodsLoad) {
this.goodsLoad = goodsLoad;
}
}
###客车
//只载人的KeCar类
public class KeCar extends Car {
public KeCar(String name, int price, int passengerLoad) {
// TODO Auto-generated constructor stub
this.setName(name);
this.setPrice(price);
this.setPassengerLoad(passengerLoad);
}
@Override
public String toString() {
return this.getName()+"----"+this.getPrice()+"元/天----载客"+this.getPassengerLoad()+"人";
}
}
货车
//货车Truck类
public class Truck extends Car {
public Truck(String name, int price, int goodsLoad) {
// TODO Auto-generated constructor stub
this.setName(name);
this.setPrice(price);
this.setPassengerLoad(goodsLoad);
}
@Override
public String toString() {
return this.getName()+"----"+this.getPrice()+"元/天----载货"+this.getGoodsLoad()+"屯";
}
}
###即载人又载货
//载人和货的BothCarry类
public class BothCarry extends Car {
public BothCarry(String name, int price,int passengerLoad, double goodsLoad) {
// TODO Auto-generated constructor stub
this.setName(name);
this.setPrice(price);
this.setPassengerLoad(passengerLoad);
this.setGoodsLoad(goodsLoad);
}
@Override
public String toString() {
return this.getName()+"----"+this.getPrice()+"元/天"+this.getPassengerLoad()+"人/货物"+this.getGoodsLoad()+"屯";
}
}
###测试类
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
System.out.println("****欢迎使用**租车系统****");
System.out.println("*******租车请按 (1) 退出请按 (0)*******");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
Car[] cars={
new KeCar("大众",300,5),
new KeCar("宝马",800,4),
new KeCar("宝骏巴士",800,45),
new Truck("东风Rl",1000,10),
new Truck("依维柯S",1500,25),
new BothCarry("皮卡雪Z",600,2,5),
new BothCarry("皮卡雪Z",600,2,15)
};
if (num != 1) {
System.out.println("****欢迎下次光临****");
System.exit(0);
}else{
for (int i = 1; i <= cars.length; i++) {
System.out.println("序号"+i+"----"+cars[i-1]);
}
}
System.out.println("请输入预定数量");
int carNum = input.nextInt();
int priceSum = 0;//价格总量
int passengerLoadSum = 0;//载客总量
double goodsLoadSum = 0;//载货总量
for (int i = 1; i <= carNum; i++) {
System.out.println("请输入第"+i+"辆要预定车辆的序号");
int index = input.nextInt();
priceSum=priceSum+cars[index-1].getPrice();
passengerLoadSum=passengerLoadSum+cars[index-1].getPassengerLoad();
goodsLoadSum=goodsLoadSum+cars[index-1].getGoodsLoad();
}
System.out.println("一共需要"+priceSum+"元/天"+" "+"共载货量:"+goodsLoadSum+"吨 "+"共载客量:"+ passengerLoadSum+"人");
System.out.println("***********谢谢惠顾!*************");
}
}