汽车类,客车、货车、皮卡车继承这个类
public class Car {
String name;
int price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
客车类
public class PassengerCar extends Car {
private int passengerNum;
public PassengerCar(String name, int i, int passengerNum) {
super();
super.setName(name);
super.setPrice(i);
this.passengerNum = passengerNum;
}
public int getPassengerNum() {
return passengerNum;
}
public void setPassengerNum(int passengerNum) {
this.passengerNum = passengerNum;
}
@Override
public String toString() {
return this.getName() + "\t" + this.getPrice() + "元/天\t载客" + passengerNum + "人";
}
}
货车类
public class Truck extends Car {
private int goodsNum;
public Truck(String name, int price, int goodsNum) {
super();
super.setName(name);
super.setPrice(price);
this.goodsNum = goodsNum;
}
public int getGoodsNum() {
return goodsNum;
}
public void setGoodsNum(int goodsNum) {
this.goodsNum = goodsNum;
}
@Override
public String toString() {
return this.name + "\t" + price + "元/天\t载货" + goodsNum + "T";
}
}
皮卡车类
public class PickUp extends Car {
private int passengerNum;
private int goodsNum;
public PickUp(String name, int price, int passengerNum, int goodsNum) {
super();
super.setName(name);
super.setPrice(price);
this.passengerNum = passengerNum;
this.goodsNum = goodsNum;
}
public int getPassengerNum() {
return passengerNum;
}
public void setPassengerNum(int passengerNum) {
this.passengerNum = passengerNum;
}
public int getGoodsNum() {
return goodsNum;
}
public void setGoodsNum(int goodsNum) {
this.goodsNum = goodsNum;
}
@Override
public String toString() {
return this.getName() + "\t" + this.getPrice() + "元/天\t载客" + passengerNum + "人\t载货" + goodsNum + "T";
}
}
出租的逻辑代码
public class Lease {
@SuppressWarnings({ "null", "null", "null", "null" })
public static void main(String[] args) {
System.out.println("欢迎使用哒哒租车系统\n您是否要租车:1是 0否");
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
if (input.nextInt() != 1) {
return;
}
System.out.println("您可租车的类型及价目表");
System.out.println("序号 汽车名称 租金 容量");
// 初始化车辆信息
PassengerCar car = new PassengerCar("奥迪A4", 500, 4);
PassengerCar car2 = new PassengerCar("马自达6", 400, 4);
PickUp car3 = new PickUp("皮卡雪6", 450, 4, 2);
PassengerCar car4 = new PassengerCar("金龙", 800, 20);
Truck car5 = new Truck("松花江", 400, 4);
Truck car6 = new Truck("依维柯", 1000, 20);
Car[] cars = { car, car2, car3, car4, car5, car6 };
// 展示车辆信息
for (int i = 0; i < cars.length; i++) {
System.out.println((i + 1) + ".\t" + cars[i].toString());
}
System.out.println("请输入您要租汽车的数量:");
// 记录租车数量
int num = input.nextInt();
// 记录租车的序号
int[] carsNO = new int[num];
for (int i = 0; i < num; i++) {
System.out.println("请输入第" + (i + 1) + "辆车的序号:");
int key = input.nextInt();
if (key >= 1 && key <= 6) {
carsNO[i] = key;
} else {
System.out.println("您输入的序号有误,请重新输入");
i--;//输入错误序号后,增加一次循环
}
}
// 总金额,总载客量,总载货量
int totalPrice = 0, totalPassengerNum = 0, totalGoodsNum = 0;
// 记录租车的车名
StringBuffer passengerName = new StringBuffer();
StringBuffer truckName = new StringBuffer();
for (int j = 0; j < carsNO.length; j++) {
switch (carsNO[j]) {
case 1:
passengerName.append(car.getName() + " ");
totalPrice += car.getPrice();
totalPassengerNum += car.getPassengerNum();
break;
case 2:
passengerName.append(car2.getName() + " ");
totalPrice += car2.getPrice();
totalPassengerNum += car2.getPassengerNum();
break;
case 3:
passengerName.append(car3.getName() + " ");
truckName.append(car3.getName() + " ");
totalPrice += car3.getPrice();
totalPassengerNum += car3.getPassengerNum();
totalGoodsNum += car3.getGoodsNum();
break;
case 4:
passengerName.append(car4.getName() + " ");
totalPrice += car4.getPrice();
totalPassengerNum += car4.getPassengerNum();
break;
case 5:
truckName.append(car5.getName() + " ");
totalPrice += car5.getPrice();
totalGoodsNum += car5.getGoodsNum();
break;
case 6:
truckName.append(car6.getName() + " ");
totalPrice += car6.getPrice();
totalGoodsNum += car6.getGoodsNum();
break;
default:
break;
}
}
System.out.println("请输入租车天数:");
int days = input.nextInt();
// 输出最后信息
System.out.println("您的账单:\n***可载人的车有:");
System.out.println(passengerName + "共载人:" + totalPassengerNum + "人");
System.out.println("***载货的车有:");
System.out.println(truckName + "共载货:" + totalGoodsNum + "吨");
System.out.println("***租车的总价格:" + days * totalPrice + "元");
System.out.println("感谢您使用哒哒租车系统");
}
}
热门评论
想要什么样的批评建议?找错吗?这个就是基本的啦,自己检验下没问题就OK
把参数写入构造类不是更简洁吗
我作为一个小白,觉得结构还是很清晰的!