您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“Java如何實現汽車租賃系統”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“Java如何實現汽車租賃系統”這篇文章吧。
汽車租賃:
分為客車和轎車兩種:
客車小于20座: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); } }
測試類:
package busTest; public class TestMain { public static void main(String[] args) { Customer customer = new Customer(); customer.showMenu(); } }
以上是“Java如何實現汽車租賃系統”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。