Mujey
2020-01-27 01:55
//: Vehicle.java package com.faraj.car_rental; public class Vehicle { public int price; public String name; public String cargo; public String manned; public int id; public Vehicle(int i, int p, String n) { id = i; price = p; name = n; } @Override public String toString() { return name; } }
//: Bus.java package com.faraj.car_rental; public class Bus extends Vehicle { public Bus(int i,int p, String n, int m) { super(i,p, n); manned = m + "People"; } }
//: Truck.java package com.faraj.car_rental; public class Truck extends Vehicle { public Truck(int i,int p, String n, int c) { super(i,p, n); cargo = c + "t"; } }
//: Pickup.java package com.faraj.car_rental; public class Pickup extends Vehicle { public Pickup(int i,int p, String n, int c, int m) { super(i,p, n); cargo = c + "t & "; manned = m + "People"; } }
//: Startup.java package com.faraj.car_rental; import java.util.Scanner; public class Startup { public static void output(Vehicle vehicle) { if (vehicle.name.length() > 8) { System.out.print(vehicle.id + "\t" + vehicle.name + "\t" + "$" + vehicle.price + "\t\t"); } else if (vehicle.name.length() < 4) { System.out.print(vehicle.id + "\t" + vehicle.name + "\t\t\t" + "$" + vehicle.price + "\t\t"); } else { System.out.print(vehicle.id + "\t" + vehicle.name + "\t\t" + "$" + vehicle.price + "\t\t"); } if (vehicle.cargo != null) { if (vehicle.manned != null) { System.out.println(vehicle.cargo + vehicle.manned); } else { System.out.println(vehicle.cargo); } } else { System.out.println(vehicle.manned); } } public void startSystem() { Vehicle[] vehicles = new Vehicle[]{ new Bus(1, 2000, "Smart Bus", 40), new Bus(2, 3000, "Large Bus", 120), new Truck(3, 1500, "Van", 15), new Truck(4, 3500, "Large Truck", 60), new Pickup(5, 2500, "Pickup", 10, 20) }; Scanner scanner = new Scanner(System.in); System.out.println("是否进入租车系统?"); if (scanner.hasNextInt()) { int isStart = scanner.nextInt(); if (isStart == 1) { System.out.println("ID\tName\t\tPrice\\Day\tVolume"); System.out.println("==========================================="); for (Vehicle vehicle : vehicles) { output(vehicle); } System.out.println("您需要租几辆车?"); int num = scanner.nextInt(); System.out.println("请选择你要租的车的ID"); int[] vehicleId = new int[num]; for (int i = 1; i < vehicleId.length + 1; i++) { System.out.println("请输入第" + i + "辆车的Id"); vehicleId[i - 1] = scanner.nextInt(); } System.out.println("您需要租多少天"); int days = scanner.nextInt(); int sumPrice = 0; for (int i = 0; i < vehicleId.length; i++) { sumPrice += vehicles[vehicleId[i] - 1].price; sumPrice *= days; } System.out.println("++++++++++++++++++++++++++++++"); System.out.print("您租了 "); for (int id : vehicleId) { System.out.print(vehicles[id - 1] + "*"); System.out.print(days + "天\t"); } System.out.println(); System.out.println("您总共需要支付:$" + sumPrice); } else { System.out.println("已退出程序"); } } } }
//: DriveTest.java package com.faraj.car_rental; public class DriveTest { public static void main(String[] args) { new Startup().startSystem(); } }
错了,你最后的测试类没有调用前面的类。
Java入门第二季 升级版
530655 学习 · 6091 问题
相似问题