遇見你的美
2016-06-27 18:41
package com.dada; public class Car { private int number; private String name;//车辆型号 private int rent;//元/一天 public boolean whetherCarryCargo;//是否可以载货 private int cargoCapacity;//载货量 public boolean whetherCarryPassengers;//是否可以载客 private int busload;//载客量 public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getRent() { return rent; } public void setRent(int rent) { this.rent = rent; } public int getCargoCapacity() { return cargoCapacity; } public void setCargoCapacity(int cargoCapacity) { this.cargoCapacity = cargoCapacity; } public int getBusload() { return busload; } public void setBusload(int busload) { this.busload = busload; } }
package com.dada; /* * 载客的客车 */ public class passengerCar extends Car { public passengerCar(int number,String name,int rent,int busload){ this.whetherCarryPassengers = true; this.whetherCarryCargo = false; this.setNumber(number); this.setName(name); this.setRent(rent); this.setBusload(busload); } }
package com.dada; /* * 可载货又可载客的皮卡 */ public class pickUp extends Car { public pickUp(int number,String name,int rent,int busload,int cargoCapacity){ this.whetherCarryPassengers = true; this.whetherCarryCargo = true; this.setNumber(number); this.setName(name); this.setRent(rent); this.setBusload(busload); this.setCargoCapacity(cargoCapacity); } }
package com.dada; public class trunk extends Car { public trunk(int number,String name,int rent,int cargoCapacity){ this.whetherCarryPassengers = false; this.whetherCarryCargo = true; this.setNumber(number); this.setName(name); this.setRent(rent); this.setCargoCapacity(cargoCapacity); } }
package com.dada; import java.util.Scanner; public class initialTest { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("欢迎使用嗒嗒租车系统!"); System.out.println("您是否要租车? :1 是 0 否"); Scanner input = new Scanner(System.in); int k = input.nextInt(); if (k == 1) { System.out.println("您可租车的类型及其价目表:"); Car[] carForRent = { new passengerCar(1, "奥迪A4", 500, 4), new passengerCar(2, "马自达6", 400, 4), new pickUp(3, "皮卡雪6", 450, 4, 2), new passengerCar(4, "金龙", 800, 20), new trunk(5, "松花江", 400, 4), new trunk(6, "依维柯", 1000, 20) }; System.out.println("序号" + "\t" + "汽车名称" + "\t" + "租金" + "\t" + "容量"); /* * 打印车辆清单 */ for (Car car : carForRent) { if (car instanceof passengerCar) { System.out.println(car.getNumber() + "\t" + car.getName() + "\t" + car.getRent() + "/天" + "\t" + "载人:" + car.getBusload() + "人"); } if (car instanceof pickUp) { System.out.println(car.getNumber() + "\t" + car.getName() + "\t" + car.getRent() + "/天" + "\t" + "载人:" + car.getBusload() + "人" + "\t" + "载货:" + car.getCargoCapacity() + "吨"); } if (car instanceof trunk) { System.out.println(car.getNumber()+"\t"+car.getName()+"\t"+car.getRent() +"/天"+"\t"+"载货:"+car.getCargoCapacity()+"吨"); } } System.out.println("请输入您要租的汽车的数量"); int totalNum=input.nextInt(); //用于接收汽车的总数量 String totalPassengerCar="";//载人的车 String totalTrunk="";//载货的车 int totalPassenger=0;; //用来存储总的载人数 int totalCarrayCargo=0; //用来存储总的载货量 int totalMoney=0; //用来计算租车的总资金 for (int i = 1; i <= totalNum; i++) { System.out.println("请输入第"+i+"辆车的序号"); int num = input.nextInt(); if(carForRent[num-1].whetherCarryCargo){ totalCarrayCargo = totalCarrayCargo + carForRent[num-1].getCargoCapacity(); totalTrunk = totalTrunk + "\t"+carForRent[num-1].getName(); } if(carForRent[num-1].whetherCarryPassengers){ totalPassenger = totalPassenger + carForRent[num-1].getBusload(); totalPassengerCar = totalPassengerCar + "\t"+carForRent[num-1].getName(); } totalMoney = totalMoney + carForRent[num-1].getRent(); } System.out.println("请输入租车的天数:"); int carRenDay=input.nextInt(); totalMoney=totalMoney*carRenDay; System.out.println("********************可载人的车有********************"); System.out.println(totalPassengerCar + "\t共载人:" + totalPassenger + "人"); System.out.println("********************可载货的车有********************"); System.out.println(totalTrunk + "\t共载货:" + totalCarrayCargo + "吨"); System.out.println("********************租车的总价格********************"); System.out.println(totalMoney+"元"); }else if (k == 0) { System.out.println("感谢你的再次使用,再见。"); }else { System.out.println("您的输入有误。"); } } }
感觉你的思路更清晰点(泪哭)价目表那里我没分车型直接丢了个二维数组.....
static String[][] car=new String[][]{ {"1","奥迪A4","500","载人:4人"}, {"2","马自达6","400","载人:4人"}, {"3","皮卡雪6","450","载人:4 载货:2吨"}, {"4","金龙 " ,"800","载人:20人"}, {"5","松花江","400","载货:4吨"}, {"6","依维柯","1000","载货:20吨"} };
载货量和载客量不是公用的属性 应该写在各自的子类中
Java入门第二季 升级版
530652 学习 · 6091 问题
相似问题