定义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;
}
}
}
热门评论
一样的头像哈哈。不过不知道是不是我太菜,看得有点晕。。。
inputCommand方法中么? 因为当输入非数字的时候,sn.nextInt()会异常,sn不能继续读取接下来的键盘输入。
假如没有这一行,运行程序并在该输入数字的时候输个字母,这时候会直接return -1, 本来想让程序提示错误信息然后让用户重新输入数字(即再次调用inputCommand), 结果 sn.nextInt();的时候不等用户输入内容就直接异常然后return -1 了,导致死循环,根本不能重新读取键盘输入;
所以再出现异常的时候,重新赋值sn,使程序能继续读取键盘输入信息,然后再返回表示异常的-1。
可以拷贝程序,然后注释这一行,运行的时候提示输入时,输个字母,比较下两次运行的效果
我想问一下为什么要 sn = new Scanner(System.in);不是sn已经初始化了吗,这句删掉有什么影响