public interface CargoCapacity {
float getWeight();
}
public interface MannedCapacity {
int getNumber();
}
public class Car {
private int id;
private String name;
private float price;
public Car(int id, String name, float price) {
this.id = id;
this.name = name;
this.price = price;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public float getPrice() {
return price;
}
}
public class CargoCar extends Car implements CargoCapacity {
private float weight;
public CargoCar(int id, String name, float price, float weight) {
super(id, name, price);
this.weight = weight;
}
@Override
public float getWeight() {
return this.weight;
}
@Override
public String toString() {
return this.getId() + "\t" + this.getName() + "\t " + this.getPrice() + "元/天\t载货:" + getWeight() + "吨";
}
}
public class MannedCar extends Car implements MannedCapacity {
private int number;
public MannedCar(int id, String name, float price, int number) {
super(id, name, price);
this.number = number;
}
@Override
public int getNumber() {
return this.number;
}
@Override
public String toString() {
return this.getId() + "\t" + this.getName() + "\t " + this.getPrice() + "元/天\t载人:" + this.getNumber() + "人";
}
}
public class MannedCargoCar extends Car implements MannedCapacity, CargoCapacity {
private int number;
private float weight;
public MannedCargoCar(int id, String name, float price, int number, float weight) {
super(id, name, price);
this.number = number;
this.weight = weight;
}
@Override
public int getNumber() {
return this.number;
}
@Override
public float getWeight() {
return this.weight;
}
@Override
public String toString() {
return this.getId() + "\t" + this.getName() + "\t " + this.getPrice() + "元/天\t载人:" + getNumber() + "人\t载货:" + getWeight() + "吨";
}
}
public class RentalItem {
private Car car;
private Integer days;
private Integer quantity;
public RentalItem(Car car, Integer days, Integer quantity) {
this.car = car;
this.days = days;
this.quantity = quantity;
}
public float getRent() {
return this.car.getPrice() * this.days * this.quantity;
}
public int getNumber() {
if (isMannedCar()) {
return ((MannedCapacity) car).getNumber();
}
return 0;
}
public float getWeight() {
if (isCargoCar()) {
return ((CargoCapacity) car).getWeight();
}
return 0;
}
public boolean isMannedCar() {
return car instanceof MannedCapacity;
}
public boolean isCargoCar() {
return car instanceof CargoCapacity;
}
public String getName() {
return car.getName();
}
}
import java.util.ArrayList;
import java.util.List;
public class CarManager {
private List<Car> cars = new ArrayList<>();
public CarManager() {
init();
}
private void init() {
add(new MannedCar(getCarId(), "奥迪A4", 500, 4));
add(new MannedCar(getCarId(), "马自达6", 400, 4));
add(new MannedCargoCar(getCarId(), "皮卡雪6", 450, 4, 2));
add(new MannedCar(getCarId(), "金龙", 800, 20));
add(new CargoCar(getCarId(), "松花江", 400, 4));
add(new CargoCar(getCarId(), "依维柯", 1000, 20));
}
public void add(Car car) {
cars.add(car);
}
public void showMenu() {
System.out.println("========可租车的类型及其价目表========");
System.out.println("序号\t" + "汽车名称 " + "租金\t\t " + "容量");
cars.forEach(System.out::println);
}
public Car get(int index) {
return cars.get(index - 1);
}
public boolean hasIndex(Integer carId) {
return carId > 0 && carId <= cars.size();
}
private int getCarId() {
return cars.size() + 1;
}
}
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class RentalBill {
private Map<Integer, RentalItem> itemMap = new HashMap<>();
public void add(RentalItem item) {
if (itemMap.containsKey(item.getId())) {
RentalItem existItem = itemMap.get(item.getId());
existItem.setDays(existItem.getDays() + item.getDays());
existItem.setQuantity(existItem.getQuantity() + item.getQuantity());
} else {
itemMap.put(item.getId(), item);
}
}
public void showBill() {
System.out.println("=============您的账单=============");
System.out.println("***可载人的车有:" + "\n\t" + mannedCarNames() + " 共载人:" + totalNumber() + "人");
System.out.println("***可载货的车有:" + "\n\t" + cargoCarNames() + " 共载货:" + totalWeight() + "吨");
System.out.println("***租车总价格:" + totalRent() + "元");
}
private List<String> mannedCarNames() {
return getCarNames(RentalItem::isMannedCar);
}
private List<String> cargoCarNames() {
return getCarNames(RentalItem::isCargoCar);
}
private List<String> getCarNames(Function<RentalItem, Boolean> condition) {
return itemMap.values().stream().filter(item -> condition.apply(item)).map(RentalItem::getName).collect(Collectors.toList());
}
private Integer totalNumber() {
return itemMap.values().stream().filter(RentalItem::isMannedCar).map(RentalItem::getNumber).reduce(Integer::sum).get();
}
private Float totalWeight() {
return itemMap.values().stream().filter(RentalItem::isCargoCar).map(RentalItem::getWeight).reduce(Float::sum).get();
}
private Float totalRent() {
return itemMap.values().stream().map(RentalItem::getRent).reduce(0f, (sum, rent) -> sum += rent);
}
}
import java.util.Scanner;
import java.util.function.Function;
public class CarRentalSystem {
private Scanner scanner = new Scanner(System.in);
private RentalBill rentalBill = new RentalBill();
private CarManager carManager = new CarManager();
public void launch() {
System.out.println("欢迎使用答答租车系统:");
if (goOn()) {
showMenu();
collectRentalItems();
showBill();
} else {
System.out.println("谢谢使用!客官慢走!");
}
}
private void showMenu() {
carManager.showMenu();
}
private void showBill() {
rentalBill.showBill();
}
private void collectRentalItems() {
while (true) {
int carId = getInputCarId();
Car car = carManager.get(carId);
int quantity = getInputQuantity(car.getName());
int days = getInputDays(car.getName());
rentalBill.add(new RentalItem(car, days, quantity));
System.out.print("是否选择继续租赁操作?(n:退出,并打印租赁清单;其他任意键继续):");
String input = scanner.next();
if ("N".equalsIgnoreCase(input.trim())) {
break;
}
}
}
private boolean goOn() {
int key = getInput("您是否要租车?(1是 0否):", input -> {
if (input != 0 && input != 1) {
System.out.println("请输入数字1或0!");
return false;
}
return true;
});
return key == 1;
}
private int getInputDays(String name) {
return getInput("请输入["+name+"]的租赁天数:", days -> {
if (days < 1) {
System.out.println("不合法的租赁天数,请重新输入!");
return false;
}
return true;
});
}
private int getInputQuantity(String name) {
return getInput("请输入["+name+"]的租赁数量:", quantity -> {
if (quantity < 1 ) {
System.out.println("不合法的租赁数量,请重新输入!");
return false;
}
return true;
});
}
private int getInputCarId() {
return getInput("请输入要租赁的车辆序号:", carId -> {
if (!carManager.hasIndex(carId)) {
System.out.println("不合法的车辆序号,请重新输入!");
return false;
}
return true;
});
}
private int getInput(String hint, Function function) {
while (true) {
System.out.print(hint);
try {
int input = scanner.nextInt();
if (!function.apply(input)) {
continue;
}
return input;
} catch (Exception e) {
System.out.println("输入不合法!请重新输入!");
scanner = scanner.skip(".*");
}
}
}
}
public class Test {
public static void main(String[] args) {
new CarRentalSystem().launch();
}
}
打开App,阅读手记