Darren_Huang
2015-01-03 17:28
//Auto.java, 所有车型的父类 package com.imooc.dadacarrental; public class Auto { private String autoType; private String autoName; private int autoRent; protected int passengerCount; protected int cargoWeight; public void setAuto(String autoType, String autoName, int autoRent){ this.autoType = autoType; this.autoName = autoName; this.autoRent = autoRent; } public void printCar(){ System.out.println("车型:"+autoType); System.out.println("名称:"+autoName); } public String getAutoType(){ return autoType; } public String getAutoName(){ return autoName; } public int getAutoRent(){ return autoRent; } public int getPassengerCount(){ return passengerCount; } public int getCargoWeight(){ return cargoWeight; } }
//IPassengerAuto.java, 载客车型接口 package com.imooc.dadacarrental; public interface IPassengerAuto { void setPassengerCount(int passengerCount); void printPassengerCount(); }
//ICargoAuto.java, 载货车型接口 package com.imooc.dadacarrental; public interface ICargoAuto { void setCargoWeight(int cargoWeight); void printCargoWeight(); }
//Car.java, 小客车类 package com.imooc.dadacarrental; public class Car extends Auto implements IPassengerAuto { Car(String carName, int carRent, int passengerCount){ setPassengerCount(passengerCount); setAuto("小客车", carName, carRent); } @Override public void setPassengerCount(int passengerCount) { if(passengerCount > 5){ System.out.println("小客车载人数应为5人以下!"); } else{ this.passengerCount = passengerCount; } } @Override public void printPassengerCount() { System.out.println("载客人数:"+passengerCount); } }
//Bus.java, 大客车类 package com.imooc.dadacarrental; public class Bus extends Auto implements IPassengerAuto { Bus(String busName, int busRent, int passengerCount){ setPassengerCount(passengerCount); setAuto("大客车", busName, busRent); } @Override public void setPassengerCount(int passengerCount) { if (passengerCount < 10){ System.out.println("大客车载客数应为10人以上!"); } else{ this.passengerCount = passengerCount; } } @Override public void printPassengerCount() { System.out.println("载客人数:"+passengerCount); } }
//SmallVan.java, 小货车类 package com.imooc.dadacarrental; public class SmallVan extends Auto implements ICargoAuto { SmallVan(String vanName, int vanRent, int cargoWeight){ setCargoWeight(cargoWeight); setAuto("小货车", vanName, vanRent); } @Override public void setCargoWeight(int cargoWeight) { if (cargoWeight > 5){ System.out.println("小货车的载货量应为5吨以下!"); } else{ this.cargoWeight = cargoWeight; } } @Override public void printCargoWeight() { System.out.println("载货量:"+cargoWeight); } }
//BigVan.java, 大客车类 package com.imooc.dadacarrental; public class BigVan extends Auto implements ICargoAuto { BigVan(String vanName, int vanRent, int cargoWeight){ setCargoWeight(cargoWeight); setAuto("大货车", vanName, vanRent); } @Override public void setCargoWeight(int cargoWeight) { if (cargoWeight < 5 || cargoWeight > 20){ System.out.println("大货车的载货量应在5吨和20吨之间!"); } else{ this.cargoWeight = cargoWeight; } } @Override public void printCargoWeight() { // TODO Auto-generated method stub } }
//Pickup.java, 皮卡车类 package com.imooc.dadacarrental; public class Pickup extends Auto implements ICargoAuto, IPassengerAuto { Pickup(String pickupName, int pickupRent, int passengerCount, int cargoWeight){ setPassengerCount(passengerCount); setCargoWeight(cargoWeight); setAuto("皮卡", pickupName, pickupRent); } @Override public void setPassengerCount(int passengerCount) { this.passengerCount = passengerCount; } @Override public void printPassengerCount() { // TODO Auto-generated method stub } @Override public void setCargoWeight(int cargoWeight) { this.cargoWeight = cargoWeight; } @Override public void printCargoWeight() { // TODO Auto-generated method stub } }
//DisplayProcess.java, 显示和处理类 package com.imooc.dadacarrental; import java.io.*; public class DisplayProcess { private int[] autoNoArray; private int rentalDays = 0; private int totalPassengerCount = 0; private int totalCargoWeight = 0; private int totalRent = 0; private boolean welcome() throws IOException{ int result; System.out.println("欢迎使用答答租车系统!"); System.out.println("您是否要租车:1是 0否"); BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in)); result = Integer.parseInt(br1.readLine()); if (result != 1){ // System.out.println(result); return false; } return true; } private void listAuto(Auto[] autoModel){ System.out.println("您可租车的类型及价目表:"); System.out.println("序号\t" + "汽车名称\t" + "租金(元/天)\t" + "容量"); for (int i = 0; i < autoModel.length; i++){ if (autoModel[i] instanceof Car || autoModel[i] instanceof Bus){ System.out.println((i+1) + ".\t" + autoModel[i].getAutoName() + "\t" + autoModel[i].getAutoRent() + "元/天\t\t载人:" + autoModel[i].getPassengerCount() + "人"); } else if (autoModel[i] instanceof SmallVan || autoModel[i] instanceof BigVan){ System.out.println((i+1) + ".\t" + autoModel[i].getAutoName() + "\t" + autoModel[i].getAutoRent() + "元/天\t\t载货:" + autoModel[i].getCargoWeight() + "吨"); } else if (autoModel[i] instanceof Pickup){ System.out.println((i+1) + ".\t" + autoModel[i].getAutoName() + "\t" + autoModel[i].getAutoRent() + "元/天\t\t载人:" + autoModel[i].getPassengerCount() + "人 载货:" + autoModel[i].getCargoWeight() + "吨"); } } } private void rentAuto() throws IOException{ System.out.println("请选择租车数量:"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int i = Integer.parseInt(br.readLine()); autoNoArray = new int[i]; for (int j = 0; j < i; j++){ System.out.println("请输入第" + (j+1) + "辆车的序号:"); autoNoArray[j] = Integer.parseInt(br.readLine()) - 1; } System.out.println("请输入租车天数:"); rentalDays = Integer.parseInt(br.readLine()); } private void generateOrder(Auto[] autoModel){ System.out.println("您的账单:"); System.out.println("***可载人的车有:"); for (int i = 0; i < autoNoArray.length; i++){ if(autoModel[autoNoArray[i]] instanceof Car || autoModel[autoNoArray[i]] instanceof Bus || autoModel[autoNoArray[i]] instanceof Pickup){ totalPassengerCount += autoModel[autoNoArray[i]].getPassengerCount(); totalRent += autoModel[autoNoArray[i]].getAutoRent()*rentalDays; System.out.print(autoModel[autoNoArray[i]].getAutoName() + "\t"); } else{ continue; } } System.out.println("共载人:" + totalPassengerCount + "人"); System.out.println("***可载货的车有:"); for (int i = 0; i < autoNoArray.length; i++){ if(autoModel[autoNoArray[i]] instanceof SmallVan || autoModel[autoNoArray[i]] instanceof BigVan){ totalCargoWeight += autoModel[autoNoArray[i]].getCargoWeight(); totalRent += autoModel[autoNoArray[i]].getAutoRent()*rentalDays; System.out.print(autoModel[autoNoArray[i]].getAutoName() + "\t"); } else if(autoModel[autoNoArray[i]] instanceof Pickup){ totalCargoWeight += autoModel[autoNoArray[i]].getCargoWeight(); System.out.print(autoModel[autoNoArray[i]].getAutoName() + "\t"); } else{ continue; } } System.out.println("共载货:"+ totalCargoWeight + "吨"); System.out.println("***租车总价格:" + totalRent + "元"); } public void processing(Auto[] autoModel) throws IOException{ if (welcome()){ listAuto(autoModel); rentAuto(); generateOrder(autoModel); } else return; } }
//CreateAutoModel.java, 创建车型类 package com.imooc.dadacarrental; public class CreateAutoModel { private Auto[] autoModel = new Auto[6]; private void createModel(){ autoModel[0] = new Car("奥迪A4", 500, 4); autoModel[1] = new Car("马自达6", 400, 4); autoModel[2] = new Pickup("皮卡雪6", 450, 4, 2); autoModel[3] = new Bus("金龙", 800, 20); autoModel[4] = new SmallVan("松花江", 400, 4); autoModel[5] = new BigVan("依维柯", 1000, 20); } public Auto[] getModel(){ createModel(); return autoModel; } }
//DadaCarRental.java, 主类 package com.imooc.dadacarrental; import java.io.IOException; public class DadaCarRental { public static void main(String[] args) throws IOException{ Auto[] models = new CreateAutoModel().getModel(); new DisplayProcess().processing(models); } }
你这也太繁琐 了
厉害~~~
有点看不懂
Java入门第二季 升级版
530655 学习 · 6091 问题
相似问题