Java租车系统
根据所学知识,编写一个控制台版的“答答租车系统”
功能:
1、展示所有可租车辆
2、选择车型、租车量
3、展示租车清单,包含:总金额、总载货量及其车型、总载人量及其车型
设计思路:
新建三个类分别表示客车,轿车,卡车。(当然也可以先定义一个车类作为父类,然后使用继承。)
基本的变量有租金,车名,序号,载货量,载客量,库存等。
根据实际需求设定他们的载客量以及载货量,并且将所有的变量进行封装。
设计难点:
客户每预定一辆车,每辆车的库存就要相应的减少,若库存不足则应弹出提示。(可以使用异常来处理,但演示代码未体现)。
租金 = 车型的每日租金*天数。若客户选择多辆车,则总金额就应该将多辆车的租金相加。
上图为运行样例,可根据自己想法添加个性化设置。
一、新建一个Bus类
public class Bus {
private int serialNumber; //序号
private String model; //车型
private double rent; //租金
private int passengerCapacity; //载客量
private double cargoCapacity; //载货量
private int stock; //库存
public Bus (int serialNumber,String model,double rent,int passengerCapacity,double cargoCapacity,int stock) {
this.serialNumber = serialNumber;
this.model = model;
this.rent = rent;
this.passengerCapacity = passengerCapacity;
this.cargoCapacity = cargoCapacity;
this.stock = stock;
}
//封装
public void setSerialNumber(int serialNumber) {
this.serialNumber = serialNumber;
}
public int getSerialNumber() {
return serialNumber;
}
public void setModel(String model) {
this.model = model;
}
public String getModel() {
return model;
}
public void setRent(double rent) {
this.rent = rent;
}
public double getRent() {
return rent;
}
public void setPassengerCapacity(int passengerCapacity) {
this.passengerCapacity = passengerCapacity;
}
public int getPassengerCapacity() {
return passengerCapacity;
}
public void setCargoCapacity(double cargoCapacity) {
this.cargoCapacity = cargoCapacity;
}
public double getCargoCapacity() {
return cargoCapacity;
}
public void setStock(int stock) {
this.stock = stock;
}
public int getStock() {
return stock;
}
}
二、新建Car类
public class Bus {
private int serialNumber; //序号
private String model; //车型
private double rent; //租金
private int passengerCapacity; //载客量
private double cargoCapacity; //载货量
private int stock; //库存
public Bus (int serialNumber,String model,double rent,int passengerCapacity,double cargoCapacity,int stock) {
this.serialNumber = serialNumber;
this.model = model;
this.rent = rent;
this.passengerCapacity = passengerCapacity;
this.cargoCapacity = cargoCapacity;
this.stock = stock;
}
//封装
public void setSerialNumber(int serialNumber) {
this.serialNumber = serialNumber;
}
public int getSerialNumber() {
return serialNumber;
}
public void setModel(String model) {
this.model = model;
}
public String getModel() {
return model;
}
public void setRent(double rent) {
this.rent = rent;
}
public double getRent() {
return rent;
}
public void setPassengerCapacity(int passengerCapacity) {
this.passengerCapacity = passengerCapacity;
}
public int getPassengerCapacity() {
return passengerCapacity;
}
public void setCargoCapacity(double cargoCapacity) {
this.cargoCapacity = cargoCapacity;
}
public double getCargoCapacity() {
return cargoCapacity;
}
public void setStock(int stock) {
this.stock = stock;
}
public int getStock() {
return stock;
}
}
三、新建Truck类
public class Truck {
private int serialNumber; //序号
private String model; //车型
private double rent; //租金
private int passengerCapacity; //载客量
private double cargoCapacity; //载货量
private int stock; //库存
public Truck(int serialNumber,String model,double rent,int passengerCapacity,double cargoCapacity,int stock) {
this.serialNumber = serialNumber;
this.model = model;
this.rent = rent;
this.passengerCapacity = passengerCapacity;
this.cargoCapacity = cargoCapacity;
this.stock = stock;
}
//封装
public void setSerialNumber(int serialNumber) {
this.serialNumber = serialNumber;
}
public int getSerialNumber() {
return serialNumber;
}
public void setModel(String model) {
this.model = model;
}
public String getModel() {
return model;
}
public void setRent(double rent) {
this.rent = rent;
}
public double getRent() {
return rent;
}
public void setPassengerCapacity(int passengerCapacity) {
this.passengerCapacity = passengerCapacity;
}
public int getPassengerCapacity() {
return passengerCapacity;
}
public void setCargoCapacity(double cargoCapacity) {
this.cargoCapacity = cargoCapacity;
}
public double getCargoCapacity() {
return cargoCapacity;
}
public void setStock(int stock) {
this.stock = stock;
}
public int getStock() {
return stock;
}
}
四、新建测试类
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Bus bus1 = new Bus(1," 小皮卡",200,5,2,10);
Bus bus2 = new Bus(2," 金杯面包车",300,12,0,10);
Bus bus3 = new Bus(3," 五菱宏光",100,7,0,10);
Car car1 = new Car(4," 宝马530",600,5,0,10);
Car car2 = new Car(5," 奥迪A6L",600,5,0,10);
Car car3 = new Car(6," 奔驰E00",500,5,0,10);
Truck truck1 = new Truck(6," 小卡车",100,2,20,10);
Truck truck2 = new Truck(7," 中卡车",150,2,30,10);
Truck truck3 = new Truck(8," 大卡车",200,2,35,10);
System.out.println("欢迎使用租车系统");
System.out.println("您是否需要租车 1:是,0退出系统");
int a = input.nextInt(); //判断是否需要租车
if (a == 1) {
System.out.println("已经进入租车界面。");
System.out.println("下面是车辆信息清单。");
System.out.println("序号;"+bus1.getSerialNumber()+" 车型;"+ bus1.getModel()+" 租金/日:"+bus1.getRent()+" 额定载客:"+bus1.getPassengerCapacity()+" 额定载重:"+bus1.getCargoCapacity()+" 库存:"+bus1.getStock());
System.out.println("序号;"+bus2.getSerialNumber()+" 车型;"+ bus2.getModel()+" 租金/日:"+bus2.getRent()+" 额定载客:"+bus2.getPassengerCapacity()+" 额定载重:"+bus2.getCargoCapacity()+" 库存:"+bus2.getStock());
System.out.println("序号;"+bus3.getSerialNumber()+" 车型;"+ bus3.getModel()+" 租金/日:"+bus3.getRent()+" 额定载客:"+bus3.getPassengerCapacity()+" 额定载重:"+bus3.getCargoCapacity()+" 库存:"+bus3.getStock());
System.out.println("序号;"+car1.getSerialNumber()+" 车型:"+ car1.getModel()+" 租金/日:"+car1.getRent()+" 额定载客:"+car1.getPassengerCapacity()+" 额定载重:"+car1.getCargoCapacity()+" 库存:"+car1.getStock());
System.out.println("序号;"+car2.getSerialNumber()+" 车型:"+ car2.getModel()+" 租金/日:"+car2.getRent()+" 额定载客:"+car2.getPassengerCapacity()+" 额定载重:"+car2.getCargoCapacity()+" 库存:"+car2.getStock());
System.out.println("序号;"+car3.getSerialNumber()+" 车型:"+ car3.getModel()+" 租金/日:"+car3.getRent()+" 额定载客:"+car3.getPassengerCapacity()+" 额定载重:"+car3.getCargoCapacity()+" 库存:"+car3.getStock());
System.out.println("序号;"+truck1.getSerialNumber()+" 车型:"+ truck1.getModel()+" 租金/日:"+truck1.getRent()+" 额定载客:"+truck1.getPassengerCapacity()+" 额定载重:"+truck1.getCargoCapacity()+" 库存:"+truck1.getStock());
System.out.println("序号;"+truck2.getSerialNumber()+" 车型:"+ truck2.getModel()+" 租金/日:"+truck2.getRent()+" 额定载客:"+truck2.getPassengerCapacity()+" 额定载重:"+truck2.getCargoCapacity()+" 库存:"+truck2.getStock());
System.out.println("序号;"+truck3.getSerialNumber()+" 车型:"+ truck3.getModel()+" 租金/日:"+truck3.getRent()+" 额定载客:"+truck3.getPassengerCapacity()+" 额定载重:"+truck3.getCargoCapacity()+" 库存:"+truck3.getStock());
System.out.println("请输入租车的数量:");
int quantity = input.nextInt();
if (quantity <= (car1.getStock()+car2.getStock()+car3.getStock()+truck1.getStock()+truck2.getStock()+truck3.getStock()+bus1.getStock()+bus2.getStock()+bus3.getStock())) {
double moneySum = 0;
System.out.println("请输入要租车的序号");
if (quantity > 1) {
for (int count = 0;count < quantity;count++) {
int number = input.nextInt();
switch (number)
{
case 1:
bus1.setStock(bus1.getStock()-1);
if (bus1.getRent() < 0) {
System.out.println("很抱歉,我们的库存不足。");
continue;
}else {
System.out.println("您已选择1号车,日租金:200");
System.out.println("请输入你想要租用的天数");
int days1 = input.nextInt();
moneySum = (moneySum+bus1.getRent()*days1);
continue;
}
case 2:
bus2.setStock(bus2.getStock()-1);
if (bus2.getRent() < 0) {
System.out.println("很抱歉,我们的库存不足。");
continue;
}else {
System.out.println("您已选择2号车,日租金:300");
System.out.println("请输入你想要租用的天数");
int days2 = input.nextInt();
moneySum = (moneySum+bus2.getRent()*days2);
continue;
}
case 3:
bus3.setStock(bus3.getStock()-1);
if (bus3.getRent() < 0) {
System.out.println("很抱歉,我们的库存不足。");
continue;
}else {
System.out.println("您已选择3号车,日租金:100");
System.out.println("请输入你想要租用的天数");
int days3 = input.nextInt();
moneySum = (moneySum+bus3.getRent()*days3);
continue;
}
case 4:
car1.setStock(car1.getStock()-1);
if (car1.getRent() < 0) {
System.out.println("很抱歉,我们的库存不足。");
continue;
}else {
System.out.println("您已选择4号车,日租金:600");
System.out.println("请输入你想要租用的天数");
int days1 = input.nextInt();
moneySum = (moneySum+car1.getRent()*days1);
continue;
}
case 5:
car2.setStock(car2.getStock()-1);
if (car2.getRent() < 0) {
System.out.println("很抱歉,我们的库存不足。");
continue;
}else {
System.out.println("您已选择5号车,日租金:600");
System.out.println("请输入你想要租用的天数");
int days5 = input.nextInt();
moneySum = (moneySum+car2.getRent()*days5);
continue;
}
case 6:
car3.setStock(car3.getStock()-1);
if (car3.getRent() < 0) {
System.out.println("很抱歉,我们的库存不足。");
continue;
}else {
System.out.println("您已选择1号车,日租金:200");
System.out.println("请输入你想要租用的天数");
int days6 = input.nextInt();
moneySum = (moneySum+car3.getRent()*days6);
continue;
}
case 7:
truck1.setStock(truck1.getStock()-1);
if (truck1.getRent() < 0) {
System.out.println("很抱歉,我们的库存不足。");
continue;
}else {
System.out.println("您已选择7号车,日租金:100");
System.out.println("请输入你想要租用的天数");
int days7 = input.nextInt();
moneySum = (moneySum+truck1.getRent()*days7);
System.out.println("您选择租用7号车"+days7+"天,当前租金为"+moneySum);
continue;
}
case 8:
truck2.setStock(truck2.getStock()-1);
if (truck2.getRent() < 0) {
System.out.println("很抱歉,我们的库存不足。");
continue;
}else {
System.out.println("您已选择8号车,日租金:150");
System.out.println("请输入你想要租用的天数");
int days8 = input.nextInt();
moneySum = (moneySum+truck2.getRent()*days8);
continue;
}
case 9:
truck3.setStock(truck3.getStock()-1);
if (truck3.getRent() < 0) {
System.out.println("很抱歉,我们的库存不足。");
continue;
}else {
System.out.println("您已选择1号车,日租金:200");
System.out.println("请输入你想要租用的天数");
int days9 = input.nextInt();
moneySum = (moneySum+truck3.getRent()*days9);
continue;
}
}
}
} else {
int number = input.nextInt();
switch (number)
{
case 1:
bus1.setStock(bus1.getStock()-1);
if (bus1.getRent() < 0) {
System.out.println("很抱歉,我们的库存不足。");
}else {
System.out.println("您已选择1号车,日租金:200");
System.out.println("请输入你想要租用的天数");
int days1 = input.nextInt();
moneySum = (moneySum+bus1.getRent()*days1);
}
case 2:
bus2.setStock(bus2.getStock()-1);
if (bus2.getRent() < 0) {
System.out.println("很抱歉,我们的库存不足。");
}else {
System.out.println("您已选择2号车,日租金:300");
System.out.println("请输入你想要租用的天数");
int days2 = input.nextInt();
moneySum = (moneySum+bus2.getRent()*days2);
}
case 3:
bus3.setStock(bus3.getStock()-1);
if (bus3.getRent() < 0) {
System.out.println("很抱歉,我们的库存不足。");
}else {
System.out.println("您已选择3号车,日租金:100");
System.out.println("请输入你想要租用的天数");
int days3 = input.nextInt();
moneySum = (moneySum+bus3.getRent()*days3);
}
case 4:
car1.setStock(car1.getStock()-1);
if (car1.getRent() < 0) {
System.out.println("很抱歉,我们的库存不足。");
}else {
System.out.println("您已选择4号车,日租金:600");
System.out.println("请输入你想要租用的天数");
int days1 = input.nextInt();
moneySum = (moneySum+car1.getRent()*days1);
}
case 5:
car2.setStock(car2.getStock()-1);
if (car2.getRent() < 0) {
System.out.println("很抱歉,我们的库存不足。");
}else {
System.out.println("您已选择5号车,日租金:600");
System.out.println("请输入你想要租用的天数");
int days5 = input.nextInt();
moneySum = (moneySum+car2.getRent()*days5);
}
case 6:
car3.setStock(car3.getStock()-1);
if (car3.getRent() < 0) {
System.out.println("很抱歉,我们的库存不足。");
}else {
System.out.println("您已选择1号车,日租金:200");
System.out.println("请输入你想要租用的天数");
int days6 = input.nextInt();
moneySum = (moneySum+car3.getRent()*days6);
}
case 7:
truck1.setStock(truck1.getStock()-1);
if (truck1.getRent() < 0) {
System.out.println("很抱歉,我们的库存不足。");
}else {
System.out.println("您已选择7号车,日租金:100");
System.out.println("请输入你想要租用的天数");
int days7 = input.nextInt();
moneySum = (moneySum+truck1.getRent()*days7);
System.out.println("您选择租用7号车"+days7+"天,当前租金为"+moneySum);
}
case 8:
truck2.setStock(truck2.getStock()-1);
if (truck2.getRent() < 0) {
System.out.println("很抱歉,我们的库存不足。");
}else {
System.out.println("您已选择8号车,日租金:150");
System.out.println("请输入你想要租用的天数");
int days8 = input.nextInt();
moneySum = (moneySum+truck2.getRent()*days8);
}
case 9:
truck3.setStock(truck3.getStock()-1);
if (truck3.getRent() < 0) {
System.out.println("很抱歉,我们的库存不足。");
}else {
System.out.println("您已选择1号车,日租金:200");
System.out.println("请输入你想要租用的天数");
int days9 = input.nextInt();
moneySum = (moneySum+truck3.getRent()*days9);
}
}
}
System.out.println("总租金:"+moneySum);
System.out.println("欢迎您下次使用");
} else {
System.out.println("很抱歉,您的订单需求量太大我们库存不足");
}
} else {
System.out.println("您已退出系统");
}
}
}
五、运行示例: