定义Car类:
package com.imooc.mika.dadarent;
public class Car {
private String carName;
private double price;
public Car(String carName, double price) {
this.carName = carName;
this.price = price;
}
public String getCarName() {
return carName;
}
public void setCarName(String carName) {
this.carName = carName;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return carName + "\t" + price;
}
}
创建3个继承Car的子类:
客车BusCar :
package com.imooc.mika.dadarent;
public class BusCar extends Car {
private int busLoad;
public BusCar(String carName, double price) {
super(carName, price);
}
public BusCar(String carName, int busLoad, double price) {
super(carName, price);
this.busLoad = busLoad;
}
public int getBusLoad() {
return busLoad;
}
public void setBusLoad(int busLoad) {
this.busLoad = busLoad;
}
@Override
public String toString() {
return super.toString() + "\t" + busLoad + "\t-";
}
}
货车TrunkCar :
package com.imooc.mika.dadarent;
public class TrunkCar extends Car {
private double deadWeight;
public TrunkCar(String carName, double price) {
super(carName, price);
}
public TrunkCar(String carName, double deadWeight, double price) {
super(carName, price);
this.deadWeight = deadWeight;
}
public double getDeadWeight() {
return deadWeight;
}
public void setDeadWeight(double deadWeight) {
this.deadWeight = deadWeight;
}
@Override
public String toString() {
return super.toString() + "\t-\t" + deadWeight;
}
}
客货两用PickUpCar :
package com.imooc.mika.dadarent;
public class PickUpCar extends Car {
private int busLoad;
private double deadWeight;
public PickUpCar(String carName, double price) {
super(carName, price);
}
public PickUpCar(String carName, int busLoad, double deadWeight, double price) {
super(carName, price);
this.busLoad = busLoad;
this.deadWeight = deadWeight;
}
public int getBusLoad() {
return busLoad;
}
public void setBusLoad(int busLoad) {
this.busLoad = busLoad;
}
public double getDeadWeight() {
return deadWeight;
}
public void setDeadWeight(double deadWeight) {
this.deadWeight = deadWeight;
}
@Override
public String toString() {
return super.toString() + "\t" + busLoad + "\t" + deadWeight;
}
}
主程序:
package com.imooc.mika.dadarent;
import java.util.HashSet;
import java.util.Scanner;
public class DaDaRent {
private static Scanner sn = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("欢迎使用答答租车系统!");
boolean quit = false;
while (!quit) {
System.out.println("租车请输入数字'1',退出系统请输入数字'0'");
switch (inputCommand()) {
case 0:
quit = true;
break;
case 1:
rentCars();
break;
default:
System.out.println("命令输入错误!请根据提示输入命令值!");
break;
}
}
sn.close();
System.out.println("退出系统~");
}
private static void rentCars() {
// 可选车型初始化
Car[] carList = initCarList();
// 打印可选车型信息
printCarList(carList);
// 输入租车数量
int rentCount = setRentCount();
// 选择具体车型
Car[] selectCars = selectCars(carList, rentCount);
// 输入租车天数
int days = setRentDays();
// 打印账单
printBill(selectCars, days);
}
private static Car[] initCarList() {
Car[] carList = new Car[5];
carList[0] = new TrunkCar("货车1号", 5, 500);
carList[1] = new TrunkCar("货车2号", 10, 800);
carList[2] = new BusCar("客车1号", 4, 400);
carList[3] = new BusCar("客车2号", 20, 1500);
carList[4] = new PickUpCar("客货两用", 4, 8, 1000);
return carList;
}
private static void printCarList(Car[] carList) {
System.out.println("可选车型一览表:");
System.out.println("编号\t车型\t日租金(元)\t载客量(人)\t载重量(吨)");
for (int i = 0; i < carList.length; i++) {
System.out.println((i + 1) + "\t" + carList[i].toString());
}
}
private static int setRentCount() {
int rentCount = 0;
do {
System.out.println("请输入租车数量(1-100):");
rentCount = inputCommand();
} while (!(rentCount > 0 && rentCount <= 100));
return rentCount;
}
private static Car[] selectCars(Car[] carList, int rentCount) {
System.out.println("请输入每辆车辆的编号:");
Car[] selectCars = new Car[rentCount];
int carNo = 0;
for (int i = 0; i < rentCount; i++) {
do {
System.out.println("第" + (i + 1) + "辆车的编号(1-" + carList.length + "):");
carNo = inputCommand();
} while (!(carNo > 0 && carNo <= carList.length));
selectCars[i] = carList[carNo - 1];
}
return selectCars;
}
private static int setRentDays() {
int days = 0;
do {
System.out.println("请输入租车的天数(1-365):");
days = inputCommand();
} while (!(days > 0 && days <= 365));
return days;
}
private static void printBill(Car[] selectCars, int days) {
System.out.println("账单:————————————————————————");
System.out.println("租用车辆数量:" + selectCars.length);
System.out.println("租用天数:" + days);
// 计算选择车辆的总载客数、载重量、总租用金额、车型名称
HashSet<String> carNames = new HashSet<String>();
int sumBusLoad = 0;
double sumDeadWeight = 0;
double sumPrice = 0;
for (Car car : selectCars) {
carNames.add(car.getCarName());
sumPrice += car.getPrice();
if (car instanceof BusCar)
sumBusLoad += ((BusCar) car).getBusLoad();
if (car instanceof TrunkCar)
sumDeadWeight += ((TrunkCar) car).getDeadWeight();
if (car instanceof PickUpCar) {
sumBusLoad += ((PickUpCar) car).getBusLoad();
sumDeadWeight += ((PickUpCar) car).getDeadWeight();
}
}
System.out.println("总载客数(人):" + sumBusLoad);
System.out.println("总载重量(吨):" + sumDeadWeight);
System.out.println("总租用金额(元):" + sumPrice * days);
System.out.println("租用车辆型号:" + carNames.toString());
System.out.println("————————————————————————————");
}
private static int inputCommand() {
try {
return sn.nextInt();
} catch (Exception e) {
sn = new Scanner(System.in);
return -1;
}
}
}
点击查看更多内容
13人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦