租車信息:
輸出結果:
代碼:
1、先定義抽象類(汽車類:Moto)
1 package cn.aura.demo01; 2 3 public abstract class Moto { 4 //公共屬性 5 private String id;//車牌號 6 private String brand;//品牌 7 private int preRent;//日租金 8 //構造方法 9 public Moto(String id, String brand, int preRent) { 10 this.id = id; 11 this.brand = brand; 12 this.preRent = preRent; 13 } 14 //set和get方法 15 public String getId() { 16 return id; 17 } 18 public void setId(String id) { 19 this.id = id; 20 } 21 public String getBrand() { 22 return brand; 23 } 24 public void setBrand(String brand) { 25 this.brand = brand; 26 } 27 public int getPreRent() { 28 return preRent; 29 } 30 public void setPreRent(int preRent) { 31 this.preRent = preRent; 32 } 33 //計算租金方法 34 public abstract double calRent(int days); 35 }
2.定義轎車類繼承汽車類
1 package cn.aura.demo01; 2 /** 3 * 轎車類 4 */ 5 public class Car extends Moto{ 6 //私有屬性 7 private String type;//型號 8 //set和get方法 9 public String getType() { 10 return type; 11 } 12 public void setType(String type) { 13 this.type = type; 14 } 15 //構造方法 16 public Car(String id, String brand, String type,int preRent) { 17 super(id, brand, preRent); 18 this.type = type; 19 } 20 21 //重寫計算租金的算法 22 @Override 23 public double calRent(int days) { 24 if(days >0 && days <= 7) { 25 //days小於7天,不打折 26 return getPreRent()*days; 27 }else if (days > 7 && days <= 30) { 28 //days大於7天,9折 29 return getPreRent()*days*0.9; 30 }else if (days > 30 && days <= 150) { 31 //days大於30天,8折 32 return getPreRent()*days*0.8; 33 }else if (days > 150) { 34 //days大於150天,7折 35 return getPreRent()*days*0.7; 36 } 37 return 0; 38 } 39 }
3.定義客車類
1 package cn.aura.demo01; 2 /** 3 * 客車類 4 */ 5 public class Bus extends Moto{ 6 //私有屬性 7 private int seatCount;//座位數 8 //set和get方法 9 public int getSeatCount() { 10 return seatCount; 11 } 12 public void setSeatCount(int seatCount) { 13 this.seatCount = seatCount; 14 } 15 //構造方法 16 public Bus(String id, String brand, int seatCount, int preRent) { 17 super(id, brand, preRent); 18 this.seatCount = seatCount; 19 } 20 //重寫計算租金的算法 21 @Override 22 public double calRent(int days) { 23 if(days >0 && days <3) { 24 //days小於3天,不打折 25 return getPreRent()*days; 26 }else if (days >= 3 && days < 7) { 27 //days大於3天,9折 28 return getPreRent()*days*0.9; 29 }else if (days >= 7 && days <30) { 30 //days大於3天,8折 31 return getPreRent()*days*0.8; 32 }else if (days >= 30 && days <150) { 33 //days大於30天,7折 34 return getPreRent()*days*0.7; 35 }else if (days >= 150) { 36 //days大於150天,6折 37 return getPreRent()*days*0.6; 38 } 39 return 0; 40 } 41 }
4.定義汽車業務類
1 package cn.aura.demo01; 2 /** 3 * 定義汽車業務類 4 */ 5 public class MotoOperation { 6 //初始化數組 7 public static Moto[] motos = new Moto[8]; 8 //創建8個汽車對象 9 public void init() { 10 motos[0] = new Car("京NY28558", "寶馬", "X6", 800); 11 motos[1] = new Car("京CNY3284", "寶馬", "550i", 600); 12 motos[2] = new Car("京NT37465", "別克", "林蔭大道", 300); 13 motos[3] = new Car("京NT96968", "別克", "GL8", 600); 14 motos[4] = new Bus("京6566754", "金杯", 16, 800); 15 motos[5] = new Bus("京8696997", "金龍", 16, 800); 16 motos[6] = new Bus("京9696996", "金杯", 34, 1500); 17 motos[7] = new Bus("京8696998", "金龍", 34, 1500); 18 } 19 //租賃汽車的方法 20 public Moto motoLeaseOut(String brand,String type,int seat) { 21 //for循環遍歷數組motos 22 for(int i=0;i<motos.length;i++){ 23 if (brand.equals(motos[i].getBrand())) { 24 // 判斷Moto類的motos[i]的類型是否和Car一樣 25 if (motos[i] instanceof Car) { 26 // 強轉成小汽車Car 27 Car car = (Car) motos[i]; 28 if (type.equals(car.getType())) { 29 return car; 30 } 31 } 32 if(motos[i] instanceof Bus){ 33 // 強轉成大客車Bus 34 Bus bus = (Bus) motos[i]; 35 if (seat == bus.getSeatCount()) { 36 return bus; 37 } 38 } 39 } 40 41 } 42 //如果沒有就返回空 43 return null; 44 } 45 }
5.定義測試類
1 package cn.aura.demo01; 2 3 import java.util.Scanner; 4 5 /** 6 * 汽車租賃管理類 7 */ 8 public class TestRent { 9 //主函數 10 public static void main(String[] args) { 11 MotoOperation motoOperation = new MotoOperation(); 12 // 初始化汽車 13 motoOperation.init(); 14 //設置一個空容器 15 Moto moto = null; 16 // 提示信息 17 System.out.println("***********歡迎光臨騰飛汽車租賃公司***********"); 18 System.out.println("1、轎車 2、客車"); 19 System.out.println("請輸入你要租賃的汽車類型:"); 20 // 獲取用戶輸入 21 Scanner scanner = new Scanner(System.in); 22 int flag = scanner.nextInt(); 23 if (flag == 1) { 24 //轎車 25 System.out.println("請選擇你要租賃的汽車品牌:1、別克 2、寶馬"); 26 int brand = scanner.nextInt(); 27 if (brand == 1) { 28 System.out.println("請選擇你要租賃的汽車類型:1、林蔭大道 2、GL8"); 29 int type = scanner.nextInt(); 30 if (type == 1) { 31 moto = motoOperation.motoLeaseOut("別克", "林蔭大道", 4); 32 } else if (type == 2) { 33 moto = motoOperation.motoLeaseOut("別克", "GL8", 4); 34 } 35 } else if (brand == 2) { 36 System.out.println("請選擇你要租賃的汽車類型:1、X6 2、550i"); 37 int type = scanner.nextInt(); 38 if (type == 1) { 39 moto = motoOperation.motoLeaseOut("寶馬", "X6", 4); 40 } else if (type == 2) { 41 moto = motoOperation.motoLeaseOut("別克", "550i", 4); 42 } 43 } 44 } else if (flag == 2) { 45 //客車 46 System.out.println("請選擇你要租賃的汽車品牌:1、金龍 2、金杯"); 47 int brand = scanner.nextInt(); 48 if (brand == 1) { 49 System.out.println("請選擇你要租賃的汽車座位數:1、16座 2、34座"); 50 int type = scanner.nextInt(); 51 if (type == 1) { 52 moto = motoOperation.motoLeaseOut("金龍", "", 16); 53 } else if (type == 2) { 54 moto = motoOperation.motoLeaseOut("金龍", "", 34); 55 } 56 } else if (brand == 2) { 57 System.out.println("請選擇你要租賃的汽車座位數:1、16座 2、34座"); 58 int type = scanner.nextInt(); 59 if (type == 1) { 60 moto = motoOperation.motoLeaseOut("金杯", "", 16); 61 } else if (type == 2) { 62 moto = motoOperation.motoLeaseOut("金杯", "", 34); 63 } 64 } 65 } 66 //客戶汽車匹配完成 67 if (moto != null) { 68 // 獲取用戶租車天數 69 System.out.println("請輸入您的租車天數:"); 70 int days = scanner.nextInt(); 71 // 計算租車錢 72 double money = moto.calRent(days); 73 System.out.println("分配給您的汽車牌號是:" + moto.getId()); 74 System.out.println("您需要支付的租賃費用是:" + money + "元"); 75 }else { 76 System.out.println("很抱歉先生:當前的公司內,暫無您要的汽車類型!請重新選擇!"); 77 } 78 79 } 80 81 82 }