本文實例為大家分享了Java實現簡單汽車租賃系統的具體代碼,供大家參考,具體內容如下
需求如下:
問題分析:
首先應當構建一個MotoVehicle的抽象(abstract)類,類里面包含一個brand屬性,表示汽車品牌;還包含一個no屬性,表示汽車牌號;
package cn.jbit.car; public abstract class MotoVehicle { private String no; private String brand; /** * 無參構造方法 */ public MotoVehicle() { } /** * 有參構造方法 * @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); }
其次,應有Car類繼承自MotoVehicle類,并有一個type屬性,表示轎車型號,應有一個計算租金的方法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; } } }
再次,應有Bus類繼承自MotoVehicle類,并有一個CountSet屬性,表示客車的容量,同樣的,應有一個計算租金的方法calRent();
package cn.jbit.car; public class Bus extends MotoVehicle { int CountSet; public Bus() { } /** * 帶參構造函數 */ 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; } } }
最后,以上三類應在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("請輸入天數:"); 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("請輸入輛數:"); int count=input.nextInt(); Car car=new Car("遼B000",brand,mtype); System.out.println("您需支付:"+count*car.calRent(days)); } else { System.out.println("1、別克商務GL8:600 3、別克林蔭大道:300"); mtype=input.next(); System.out.println("請輸入輛數:"); 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("請輸入座位數:"); countSet=input.nextInt(); System.out.println("請輸入輛數:"); int count=input.nextInt(); Bus b=new Bus(brand,"遼B000",countSet); System.out.println("您需支付:"+b.calRent(days)*count); } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。