렌트카:
승용과 세단 2가지 종류로 나누어집니다.
20석 미만 승용차: 1일 500대, 20석 이상: 900대.
자동차는 고급차와 일반차로 구분됩니다. 고급차는 하루 600원, 일반차는 하루 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); } }
테스트 카테고리 :
rr 리위 내용은 Java로 자동차 렌탈 시스템을 구현하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!