Java實現(xiàn)簡單汽車租賃系統(tǒng)
本文實例為大家分享了Java實現(xiàn)簡單汽車租賃系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
需求如下:
問題分析:
首先應(yīng)當(dāng)構(gòu)建一個MotoVehicle的抽象(abstract)類,類里面包含一個brand屬性,表示汽車品牌;還包含一個no屬性,表示汽車牌號;
package cn.jbit.car; public abstract class MotoVehicle { private String no; private String brand; /** * 無參構(gòu)造方法 */ public MotoVehicle() { } /** * 有參構(gòu)造方法 * @param no 汽車牌號 * @param brand 汽車品牌 */ public MotoVehicle(String no,String brand) { this.no=no; this.brand=brand; } public String getNo() { return no; } public String getBrand() { return brand; } public abstract int calRent(int days); }
其次,應(yīng)有Car類繼承自MotoVehicle類,并有一個type屬性,表示轎車型號,應(yīng)有一個計算租金的方法calRent()
package cn.jbit.car; public class Car extends MotoVehicle{ private String type; public Car() { } public Car (String no,String brand,String type) { super(no,brand); this.type=type; } public String getType() { return type; } public void setType(String type) { this.type = type; } @Override public int calRent(int days) { // TODO Auto-generated method stub if("2".equals(type)) { return days*500; } else if ("1".equals(type)) { return days*600; } else { return 300*days; } } }
再次,應(yīng)有Bus類繼承自MotoVehicle類,并有一個CountSet屬性,表示客車的容量,同樣的,應(yīng)有一個計算租金的方法calRent();
package cn.jbit.car; public class Bus extends MotoVehicle { int CountSet; public Bus() { } /** * 帶參構(gòu)造函數(shù) */ public Bus(String brand,String no,int CountSet) { super(brand,no); this.CountSet=CountSet; } public int getCountSet() { return CountSet; } public void setCountSet(int countSet) { CountSet = countSet; } @Override public int calRent(int days) { // TODO Auto-generated method stub if(CountSet<16) { return 800*days; } else { return 1600*days; } } }
最后,以上三類應(yīng)在test類中測試;
package cn.jbit.car; import java.util.Scanner; public class Test { public static void main(String[] args) { String no,brand,mtype; int countSet,days; Scanner input=new Scanner(System.in); System.out.println("*****歡迎來到汽車租賃公司!******"); System.out.println("請輸入天數(shù):"); days=input.nextInt(); System.out.println("請輸入車輛類型:"); System.out.println("1、轎車 2、客車"); mtype=input.next(); if("1".equals(mtype)) { System.out.println("請輸入轎車品牌:"); System.out.println("1、寶馬 2、別克"); brand=input.next(); if("1".equals(brand)) { System.out.println("2、寶馬550i:500"); System.out.println("請輸入轎車型號:"); mtype=input.next(); System.out.println("請輸入輛數(shù):"); int count=input.nextInt(); Car car=new Car("遼B000",brand,mtype); System.out.println("您需支付:"+count*car.calRent(days)); } else { System.out.println("1、別克商務(wù)GL8:600 3、別克林蔭大道:300"); mtype=input.next(); System.out.println("請輸入輛數(shù):"); int count=input.nextInt(); Car car=new Car("遼B000",brand,mtype); System.out.println("您需支付:"+count*car.calRent(days)); } } else { System.out.println("請輸入品牌:"); System.out.println("1、金杯 2、金龍"); brand=input.next(); System.out.println("請輸入座位數(shù):"); countSet=input.nextInt(); System.out.println("請輸入輛數(shù):"); int count=input.nextInt(); Bus b=new Bus(brand,"遼B000",countSet); System.out.println("您需支付:"+b.calRent(days)*count); } } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
MyBatis中使用分頁插件PageHelper實現(xiàn)分頁功能
分頁是經(jīng)常使用的功能,本文主要介紹了Mybatis中處理特殊SQL處理邏輯,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06MyBatis?Plus如何實現(xiàn)獲取自動生成主鍵值
這篇文章主要介紹了MyBatis?Plus如何實現(xiàn)獲取自動生成主鍵值問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09spring?boot?mybatis日志輸出到控制臺的方法實踐
在開發(fā)過程中我們往往需要打印出SQL語句,這樣就方便我們監(jiān)控問題,本文主要介紹了spring?boot?mybatis日志輸出到控制臺的方法實踐,具有一定的參考價值,感興趣的可以了解一下2024-05-05