慕九州8084167
2018-04-12 16:57
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Test { public static void main(String[] args) { System.out.println("~~~~~~~欢迎使用哒哒租车~~~~~~~~ "); System.out.println("您是否要租车:1.是 >> 点击其他键退出"); Test test = new Test(); while (test.test1()) { System.out.println(">>>您可选择车的类型及其价目表:"); System.out.println("-------------------------------------------"); Car[] cars = {new PassengerCar(1, "奥迪A8L", 600, 4), new PassengerCar(2, "奔驰G", 800, 4), new Pickup(3, "福特猛禽", 1300, 4, 2), new PassengerCar(4, "宝马M5", 700, 20), new Truck(5, "斯柯达", 500, 4), new Truck(6, "依维柯", 1000, 20) }; System.out.println("序号" + " " + "\t" + "车型" + " " + "\t" + "租金" + " " + "\t" + "容量"); for (Car car : cars) { //判断左边的类是否是右边的实例类 if (car.getClass().equals(PassengerCar.class)) { //如果是客车的实例类 System.out.println(car.getId() + "\t" + "\t" + car.getName() + "\t" + "\t" + car.getPrice() + "/天" + "\t" + "\t" + "载客" + ((PassengerCar) car).getCapacity() + "/人"); } if (car.getClass().equals(Truck.class)) { System.out.println(car.getId() + "\t" + "\t" + car.getName() + "\t" + "\t" + car.getPrice() + "/天" + "\t" + "\t" + "载货" + ((Truck) car).getCargoVolume() + "/吨"); } if (car.getClass().equals(Pickup.class)) { System.out.println(car.getId() + "\t" + "\t" + car.getName() + "\t" + "\t" + car.getPrice() + "/天" + "\t" + "\t" + "载客" + ((Pickup) car).getCapacity() + "/人" + " " + "载货" + ((Pickup) car).getCargoVolume() + "/吨"); } } System.out.println("-------------------------------------------"); System.out.println("-->请输入你要租的汽车的数量:"); Scanner z = new Scanner(System.in); int carNum = z.nextInt();//用于接收租车的数量 List<Car> carList = new ArrayList<Car>(); int add = 0; List<Car> carListPassenger = new ArrayList<>(); //用来储存可载人的车辆 List<Car> carListTrunk = new ArrayList<>(); //用来储存可载货的车辆 int passengerCar = 0; //载人数 int carCargo = 0; //载货量 int carPrice = 0; //总资金 for (int i = 0; i < carNum; i++) { System.out.println(">>请输入第" + (i + 1) + "辆车的序号:"); int num = z.nextInt(); //用于接收输入的汽车的序号 carList.add(cars[num - 1]); System.out.println("----成功添加:" + carList.get(add).getName()); if (cars[num - 1].getClass().equals(PassengerCar.class)) { passengerCar += ((PassengerCar) carList.get(add)).getCapacity(); carPrice += ((PassengerCar) carList.get(add)).getPrice(); carListPassenger.add(carList.get(add)); } if (cars[num - 1].getClass().equals(Pickup.class)) { passengerCar += ((Pickup) carList.get(add)).getCapacity(); carCargo += ((Pickup) carList.get(add)).getCargoVolume(); carPrice += ((Pickup) carList.get(add)).getPrice(); carListPassenger.add(carList.get(add)); carListTrunk.add(carList.get(add)); } if (cars[num - 1].getClass().equals(Truck.class)) { carCargo += ((Truck) carList.get(add)).getCargoVolume(); carPrice += ((Truck) carList.get(add)).getPrice(); carListTrunk.add(carList.get(add)); } add++; } System.out.println(">>请输入租车的天数:"); Scanner g = new Scanner(System.in); int carDay = g.nextInt(); carPrice = carPrice * carDay; System.out.println("------------租车选车完成------------" + "\n" + "下面开始统计数据.........."); //租车完成,开始统计数据并输出 System.out.print("您租的车共有" + carNum + "辆:" + " "); for (Car car : carList) { System.out.print(car.getName() + " "); } System.out.println(); System.out.println("共租用:" + carDay + " 天"); System.out.println("可载人:" + passengerCar + " 人"); System.out.println("可载货:" + carCargo + " 吨"); System.out.println("需要付款:" + carPrice + " 元"); System.out.println("->请输入付款金额:"); System.out.println("------------"); Scanner x = new Scanner(System.in); while (carPrice != x.nextInt()) System.out.println("------------" + "\n" + "输入错误,请重新输入金额!"); System.out.println("------------"); System.out.println("->请输入付款密码:"); Scanner y = new Scanner(System.in); while (y.nextInt() != 123456) System.out.println("------------" + "\n" + "密码错误,请重新输入!"); System.out.println("------------"); System.out.println(" 交易成功!"); System.out.println(); System.out.println("------------感谢您的使用--------------"); System.out.println("………………继续租车请按1,退出请按其他键………………"); } System.out.println("***欢迎您再次使用,再见!***"); } // 捕获输入参数不正确异常 public boolean test1() { try { Scanner z = new Scanner(System.in); if (z.nextInt() == 1) { return true; } else { return false; } } catch (Exception e1) { return false; } } }
这一段代码看不太懂,可以解释一下么?
我照着你的在eclipse里面试了一下,for
(Car car : cars)
这句报错了,然而刚学java的我,不会改,网上查了一些资料,但讲的和我想知道的不一样
car.getClass().equals(PassengerCar.
class
)
这句可以用 car instanceof PassengerCar 代替吗
说实话,只学了入门第一季和入门第二季的话很多看不明白,单单是cars数组那块还可以这样创建,cars数组类型可以是Car,数组里面还可以有各子类的构造方法
捕获只要输入1之外的所有包括字母 负数 小数点会报错的异常,只要输入不是1都会退出程序
。。。
一直运行的时候
public class Truck extends Car { private int cargoVolume; //载货量 public Truck(int id, String name, int price, int cargoVolume) { this.setId(id); this.setName(name); this.setPrice(price); this.cargoVolume = cargoVolume; } public int getCargoVolume() { return cargoVolume; } public void setCargoVolume(int cargoVolume) { this.cargoVolume = cargoVolume; } }
public class Pickup extends Car { private int capacity; //定义载客量 private int cargoVolume; //定义载货量 public Pickup(int id, String name, int price, int capacity, int cargoVolume) { this.setId(id); this.setName(name); this.setPrice(price); this.capacity = capacity; this.cargoVolume = cargoVolume; } public int getCapacity() { return capacity; } public void setCapacity(int capacity) { this.capacity = capacity; } public int getCargoVolume() { return cargoVolume; } public void setCargoVolume(int cargoVolume) { this.cargoVolume = cargoVolume; } }
public class PassengerCar extends Car { private int capacity; //载客量 public PassengerCar(int id, String name, int price, int capacity) { this.setId(id); this.setName(name); this.setPrice(price); this.capacity = capacity; } public int getCapacity() { return capacity; } public void setCapacity(int capacity) { this.capacity = capacity; } }
public class Car { private int id; //序号 private String name; //车名 private int price; //租金 public int getId() { return id; } public void setId(int id) { this.id = id; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Java入门第二季 升级版
530655 学习 · 6091 问题
相似问题