JAVA多態練習題_某汽車租賃公司出租多種車輛,車型及租金情況如下:


某汽車租賃公司出租多種車輛,車型及租金情況如下:

 

 

 

 

編寫程序實現計算租賃價  MotoVehicle

分析

 

 

 

 

 

 

 

4、需求說明:

輸入提示需求,計算出對應租金

 

5、需求說明:

新購置了卡車,根據噸位,租金每噸每天50

對系統進行擴展,計算汽車租賃的總租金

 

 

 

 

 1 package day13;
 2 //機動車類
 3 public abstract class MotoVehicle {
 4     //車牌號    
 5     String no=null;
 6     //品牌
 7     String brand=null;
 8     //顏色
 9     char color;
10     //里程
11     int mileage;
12     //總金額
13     int num;
14     
15     
16     
17     public abstract int CalcRent(int day);
18     
19 }
 1 package day13;
 2 //大車類
 3 public class Bus extends MotoVehicle{
 4     
 5     private int seatCount;
 6     
 7 
 8     
 9     public Bus(String no,int seatCount) {
10         super();
11         this.no=no;
12         this.seatCount = seatCount;
13     }
14 
15 
16      //方法重寫
17     @Override
18     public int CalcRent(int day) {
19         
20         return 0;
21     }
22     //方法重載
23     public int CalcRent(int days,int seatCount){
24         if(seatCount<=16){
25             num=800*days;
26         }
27         else if(seatCount>16){
28             num=1500*days;
29         }
30         System.out.println("費用為:"+num);
31         return num;
32     }
33 
34     
35     
36 }
 1 package day13;
 2 //小車類
 3 public class Car extends MotoVehicle{
 4     
 5     private String type;
 6     
 7     public Car(String no,String type) {
 8         super();
 9         this.no=no;
10         this.type = type;
11     }
12     //方法重寫
13     @Override
14     public int CalcRent(int day) {
15         
16         return 0;
17     }
18     //方法重載
19     public int CalcRent(int days,String type){
20         //
21         switch (type) {
22         case "1":
23             num=600*days;
24             break;
25         case "2":
26             num=500*days;
27             break;
28         case "3":
29             num=300*days;
30             break;
31         
32         default:
33             System.out.println("輸入有誤");
34             break;
35         }
36         System.out.println("費用為"+num);
37         return num;
38     }
39 }
 1 package day13;
 2 
 3 public class Truck extends MotoVehicle{
 4     int  tonnage;
 5     
 6     public Truck(String no,int tonnage) {
 7         super();
 8         this.no=no;
 9         this.tonnage = tonnage;
10     }
11 
12     //重寫方法
13     @Override
14     public int CalcRent(int day) {
15         
16         return 0;
17     }
18     //重載方法
19     public int CalcRent(int day,int tonnage){
20         num=tonnage*day*50;
21         System.out.println("費用為"+num);
22         return num;
23     }
24     
25 
26 }
package day13;

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        Scanner reader=new Scanner(System.in);
        System.out.println("請輸入數字選擇車型:(1轎車),(2客車),(3.卡車)");
        int m=reader.nextInt();
        if(m==1){
            System.out.println("1.別克商務艙GL8");
            System.out.println("2.寶馬55i");
            System.out.println("3.別克林蔭大道");
            System.out.println("請輸入轎車品牌數字:");
            String b=reader.next();
            
        
            System.out.println("請輸入租賃天數:");
            int days=reader.nextInt();
            
            //創建對象
            Car car=new Car("0",b);
            car.CalcRent(days, b);
            
        }else if(m==2){
            System.out.println("請輸入座位數:");
            int c=reader.nextInt();
            System.out.println("請輸入租賃天數:");
            int  days=reader.nextInt();
            
            //創建對象並調用
            Bus bus=new Bus("0",c);
            bus.CalcRent(days, c);
        }else if(m==3){
            System.out.println("請輸入噸位:");
            int d=reader.nextInt();
            System.out.println("請輸入要租賃的天數:");
            int days=reader.nextInt();
            
            //創建對象並使用
            Truck truck=new Truck("0", d);
            truck.CalcRent(days, d);
        }
        else{
            System.out.println("輸入有誤!");
        }

    }

}

運行截圖:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM