レンタカー:
は乗用車とセダンの 2 つのタイプに分かれています:
座席数 20 未満の乗用車: 1 日あたり 500 台、20 席以上: 1 日あたり 900 台。
車は高級車と普通車に分かれており、高級車は 1 日あたり 600、普通車は 1 日あたり 200 です。
レンダリング:
コードは次のとおりです:自動車カテゴリ:package busTest; /* 机动车类 */ public abstract class MotoVehicle { private String carNumber; //车牌号 private String carBrand; // 车品牌 //构造方法 public MotoVehicle(){} public MotoVehicle(String carNumber, String carBrand) { this.carNumber = carNumber; this.carBrand = carBrand; } // get/set public String getCarNumber(){ return carNumber; } public void setCarNumber(String carNumber){ this.carNumber = carNumber; } public String getCarBrand(){ return carBrand; } public void setCarBrand(String carBrand){ this.carNumber = carNumber; } /* 计算租赁的方法 */ public abstract int calRent(int days); }乗用車カテゴリ:
package busTest; public class Bus extends MotoVehicle { private int setCount; //座位数 //通过构造方法初始化对象 public Bus(String carNUmber, String brand, int setCount) { super(carNUmber, brand); this.setCount = setCount; } @Override public int calRent(int days) { //根据座位数量来判断租赁的金额 if (this.setCount < 20) { return days * 500; } else { return days * 900; } } public void showBusInfo(int days) { System.out.println("*"); System.out.println("\t车牌号:" + super.getCarNumber()); System.out.println("\t车品牌:" + super.getCarBrand()); System.out.println("\t座位数:" + this.setCount); System.out.println("\t租赁天数:" + days); System.out.println("\t金额:" + calRent(days)); } }乗用車カテゴリ:
package busTest; public class Car extends MotoVehicle { private String type; //汽车类型 普通/豪华 //通过构造方法初始化对象 public Car(String carNUmber, String brand, String type) { super(carNUmber, brand); this.type = type; } @Override public int calRent(int days) { //根据类型来决定价格 if ("豪车".equals(type)) { return days * 600; } else { return days * 200; } } public void showCarInfo(int days) { System.out.println("*"); System.out.println("\t车牌号:" + super.getCarNumber()); System.out.println("\t车品牌:" + super.getCarBrand()); System.out.println("\t车类型:" + this.type); System.out.println("\t租赁天数:" + days); System.out.println("\t金额:" + calRent(days)); } }レンタカー顧客カテゴリ:
package busTest; /* 顾客类 */ import java.util.Scanner; public class Customer { private String name; private int sum = 0; //当不确定我的购物车内具体是轿车还是客车,那就以父亲类类型创建对象数组 MotoVehicle[] motos = new MotoVehicle[10]; Scanner input = new Scanner(System.in); public void showMenu() { //定义一个父类机动车的对象,在下面可以接收 MotoVehicle moto = null; System.out.println("******汽车租赁系统*******"); String answer; do { System.out.println("1、租赁客车 2、租赁轿车"); System.out.print("请输入编号:"); int num = input.nextInt(); if (num == 1) { //创建租赁的客车对象 moto = rentBus(); } else if (num == 2) { //创建租赁的轿车对象 moto = rentCar(); } for (int i = 0; i < motos.length; i++) { if (motos[i] == null) { motos[i] = moto; break; } } System.out.print("是否继续租赁?:y/n"); answer = input.next(); } while (!"n".equals(answer)); System.out.print("请输入你的姓名:"); this.name = input.next(); System.out.print("租赁的天数:"); int days = input.nextInt(); //根据天数来统计租赁金额 calTotalRent(days); //显示租赁的信息 showInfo(days); } private void showInfo(int days) { System.out.println("---------------------租赁汽车信息---------------------"); for (int i = 0; i < motos.length; i++) { MotoVehicle moto = this.motos[i]; if (moto != null) { if (moto instanceof Bus) { Bus bus = (Bus) moto; bus.showBusInfo(days); } else if (moto instanceof Car) { Car car = (Car) moto; car.showCarInfo(days); } } } System.out.println("\t顾客:" + this.name + "\t\t总金额:" + sum); System.out.println("----------------------------------------------------"); } private void calTotalRent(int days) { int total = 0; for (MotoVehicle moto : motos) { if (moto != null) { int rent = moto.calRent(days); total += rent; //累加总金额 } this.sum = total;// 把总金额复制给全局变量 sum } } //轿车 private MotoVehicle rentCar() { System.out.print("请输入轿车品牌:"); String brand = input.next(); System.out.print("请输入轿车车牌号:"); String carNumber = input.next(); System.out.print("请选择轿车类型[1、豪车 2、普通车:]"); int choise = input.nextInt(); String type; if (choise == 1) { type = "豪车"; } else { type = "普通型"; } return new Car(carNumber, brand, type); } //客车 private MotoVehicle rentBus() { System.out.print("请输入客车品牌:"); String brand = input.next(); System.out.print("请输入客车车牌号:"); String carNumber = input.next(); System.out.print("请输入客车座位数:"); int seatCount = input.nextInt(); return new Bus(carNumber, brand, seatCount); } }テスト カテゴリ:
package busTest; public class TestMain { public static void main(String[] args) { Customer customer = new Customer(); customer.showMenu(); } }
以上がJavaでレンタカーシステムを実装する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。