汽車租賃管理系統 源代碼 Java初級小項目


   今天再給大家分享一個小項目:汽車租賃管理系統。用的是Java語言開發的,代碼不多,大概260行左右吧,系統是實現圖書的新增圖書、刪除圖書、借閱圖書、歸還圖書、查看圖書等簡單的功能(后附源代碼)!!!

  首先展示一下運行界面效果圖:運行代碼后,會在控制台顯示如下界面:

然后讓用戶選擇,如果用戶不小心或者誤輸入了錯誤的數,會再次讓用戶進行輸入。

  當用戶輸入1后,可以選擇要租賃的汽車品牌:

包括別克、寶馬兩大選項。

  當用戶繼續輸入1后,可以選擇要租賃的汽車類型,效果如下:

 

  用戶繼續輸入1后,可以選擇要租賃的汽車的天數,效果如下:

  用戶繼續輸入要租賃的汽車的天數后,效果如下:

會顯示分配給自己的汽車牌號、以及需要支付的租賃費用:

 

  同理,初始時,用戶輸入2后,接着輸入租賃的汽車品牌,繼續選擇租賃的汽車車位數:

 

 

 

  源代碼如下:

1 . 汽車業務類

 1 package com.manage;
 2 
 3 import com.vehicles.Bus;
 4 import com.vehicles.Car;
 5 import com.vehicles.Vehicle;
 6 
 7 //汽車業務類
 8 public class VehicleOperation {
 9     //汽車數組
10     Vehicle[] vehicles = new Vehicle[8];
11     
12     //汽車信息初始化
13     public void init(){
14         //向上轉型
15         vehicles[0] = new Car("京N85764","寶馬",800,"X6"); //Vehicle v = new Car();
16         vehicles[1] = new Car("京L79654","寶馬",600,"550i"); //Vehicle v = new Car();
17         vehicles[2] = new Car("京Y96584","別克",400,"林蔭大道"); //Vehicle v = new Car();
18         vehicles[3] = new Car("京M36589","別克",500,"GL8"); //Vehicle v = new Car();
19         vehicles[4] = new Bus("京Y85754","金龍",1000,34); //Vehicle v = new Bus();
20         vehicles[5] = new Bus("京U88888","金龍",800,16); //Vehicle v = new Bus();
21         vehicles[6] = new Bus("京T66666","金杯",1200,34); //Vehicle v = new Bus();
22         vehicles[7] = new Bus("京P90876","金杯",700,16); //Vehicle v = new Bus();
23     }
24     
25     //租車:簡單工廠模式
26     //參數:品牌   座位數   型號  (客車:品牌  座位數   "";轎車:品牌  0  型號)
27     public Vehicle rentVehicle(String brand,int seatCount,String type){
28         Vehicle v = null;
29         //根據用戶提供的租車信息(方法參數)去遍歷汽車數組,找到相應車輛返回給用戶
30         for(Vehicle vehicle:vehicles){
31             if(vehicle instanceof Car){
32                 //轎車
33                 Car car = (Car)vehicle; //向下轉型
34                 //轎車的品牌和型號與用戶想要的轎車品牌與型號吻合
35                 if(car.getBrand().equals(brand) && car.getType().equals(type)){
36                     v = car;
37                     break;
38                 }
39             }else{
40                 //客車
41                 Bus bus = (Bus)vehicle; //向下轉型
42                 //客車的品牌和座位數與用戶想要的客車品牌與座位數吻合
43                 if(bus.getBrand().equals(brand) && bus.getSeatCount()==seatCount){
44                     v = bus;
45                     break;
46                 }
47             }
48         }
49         return v;
50     }
51 }

 

2 . 汽車租賃管理類:入口測試類

 1 package com.manage;
 2 
 3 import java.util.Scanner;
 4 
 5 import com.vehicles.Vehicle;
 6 
 7 //汽車租賃管理類:入口測試類
 8 public class VehicleRent {
 9     public static void main(String[] args) {
10         Scanner input = new Scanner(System.in);
11         VehicleOperation vehicleOpr = new VehicleOperation();
12         System.out.println("************歡迎光臨騰飛汽車租賃有限公司*****************");
13         System.out.print("請選擇您要租賃的車型:1、轎車  2、客車  ");
14         int vehicleType = input.nextInt();
15         
16         //獲取用戶租賃汽車的三個條件:品牌   座位數   型號
17         String brand = "";
18         int seatCount = 0;
19         String type = "";
20         
21         switch(vehicleType){
22             case 1: //租賃轎車   獲取到用戶想租賃的轎車的品牌及型號信息
23                 System.out.print("請選擇您要租賃的汽車品牌:1、別克  2、寶馬 ");
24                 int choose = input.nextInt();
25                 if(choose == 1){
26                     brand = "別克";
27                     System.out.print("請選擇您要租賃的汽車類型:1、林蔭大道  2、GL8");
28                     type = (input.nextInt() == 1 )?"林蔭大道":"GL8";
29                 }else{
30                     brand = "寶馬";
31                     System.out.print("請選擇您要租賃的汽車類型:1、X6  2、550i");
32                     type = (input.nextInt() == 1 )?"X6":"550i";
33                 }
34                 
35                 break;
36             case 2://租賃客車 獲取到用戶想租賃的客車的品牌及座位數信息
37                 System.out.print("請選擇您要租賃的汽車品牌:1、金杯  2、金龍 ");
38                 brand = (input.nextInt() == 1 )?"金杯":"金龍";
39                 System.out.print("請選擇您要租賃的汽車座位數:1、16座  2、34座 ");
40                 seatCount = (input.nextInt() == 1 )?16:34;
41                 break;
42         }
43         
44         //初始化汽車信息
45         vehicleOpr.init();
46         //租車
47         Vehicle v = vehicleOpr.rentVehicle(brand, seatCount, type);
48         //提示用戶租車的車牌號    計算租金(多態,會根據具體返回的汽車子類對象,調用重寫后的計算租金方法)
49         System.out.print("請輸入您要租賃汽車的天數:");
50         int days = input.nextInt();
51         float price = v.clacRent(days);
52         System.out.println("分配給您的汽車牌號為:"+v.getVehicleId());
53         System.out.println("您需要支付的租賃費用為:"+price+"元");
54     }
55 }

 

3 . 子類:客車類

 1 package com.vehicles;
 2 
 3 //子類:客車類
 4 public class Bus extends Vehicle {
 5     //座位數
 6     private int seatCount;
 7     
 8     public Bus(){}
 9     public Bus(String vehicleId, String brand, int perRent,int seatCount){
10         super(vehicleId,brand,perRent);
11         this.seatCount = seatCount;
12     }
13 
14     public int getSeatCount() {
15         return seatCount;
16     }
17     public void setSeatCount(int seatCount) {
18         this.seatCount = seatCount;
19     }
20     
21     //根據客車計算租金的規則重寫父類方法
22     public float clacRent(int days) {
23         //租金 = 日租金 * 租賃周期
24         float price = this.getPerRent() * days;
25         //折扣規則
26         if(days>=3 && days<7){
27             price *=  0.9f;
28         }else if(days>=7 &&days<30){
29             price *=  0.8f;
30         }else if(days>=30 && days<150){
31             price *=  0.7f;
32         }else if(days>=150){
33             price *= 0.6f;
34         }
35         return price;
36     }
37 
38 }

 

4 . 子類:轎車類

 1 package com.vehicles;
 2 
 3 //子類:轎車類
 4 public class Car extends Vehicle {
 5     //型號
 6     private String type;
 7     
 8     public Car(){}
 9     public Car(String vehicleId, String brand, int perRent,String type){
10         super(vehicleId,brand,perRent);
11         this.type = type;
12     }
13     
14     public String getType() {
15         return type;
16     }
17 
18     public void setType(String type) {
19         this.type = type;
20     }
21 
22     //根據轎車計算租金的規則重寫父類方法
23     public float clacRent(int days) {
24         //租金 = 日租金 * 租賃周期
25         float price = this.getPerRent() * days;
26         //折扣規則
27         if(days>7 && days<=30){
28             price *=  0.9f;
29         }else if(days>30 &&days<=150){
30             price *=  0.8f;
31         }else if(days>150){
32             price *=  0.7f;
33         }
34         return price;
35     }
36 
37 }

 

5 . 父類:汽車類

 1 package com.vehicles;
 2 
 3 //父類:汽車類
 4 public abstract class Vehicle {
 5     //車牌號   品牌   日租金
 6     private String vehicleId;
 7     private String brand;
 8     private int perRent;
 9     
10     public Vehicle(){}
11     
12     
13     public Vehicle(String vehicleId, String brand, int perRent) {
14         this.vehicleId = vehicleId;
15         this.brand = brand;
16         this.perRent = perRent;
17     }
18 
19 
20     public String getVehicleId() {
21         return vehicleId;
22     }
23     public void setVehicleId(String vehicleId) {
24         this.vehicleId = vehicleId;
25     }
26     public String getBrand() {
27         return brand;
28     }
29     public void setBrand(String brand) {
30         this.brand = brand;
31     }
32     public int getPerRent() {
33         return perRent;
34     }
35     public void setPerRent(int perRent) {
36         this.perRent = perRent;
37     }
38     //抽象方法:計算租金-->根據租賃周期來計算租金
39     public abstract float clacRent(int days);
40 }

 

  小伙伴可以多多互動,一起多交流交流!!!O(∩_∩)O

  喜歡前端、后端java開發的 可以到我的知乎主頁(更多詳細內容等你關注呦):https://www.zhihu.com/people/xing-chen-you-guang-pie-pie-pie,有詳細視頻、資料、教程,文檔,值得擁有!!!希望可以一起努力,加油ヾ(◍°∇°◍)ノ゙!!!

  B站有上傳的更多項目視頻,從Java基礎到面向對象、Java高級API,以及SSM等框架的視頻,地址:https://www.bilibili.com/video/BV15a411w7Jh

  


免責聲明!

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



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