面向对象-汽车租赁系统

最近学了面向对象知识,用汽车租赁系统作为练习,如有写得不好之处请指正。

首先,创建一个汽车类作为父类,汽车类里包含了三个属性和一个方法

 1 package com.ketang.rental;
 2 
 3 /**
 4  * 汽车类
 5  * @author 
 6  *
 7  */
 8 public abstract class Automobile {
 9     private String brand;  //品牌
10     private String plateNum;  //车牌号
11     private int dayRent;  //日租金
12     
13     //无参构造方法
14     public Automobile() {}
15     
16     //包含汽车品牌、车牌号、日租金三个参数的构造方法
17     public Automobile(String brand, String plateNum, int dayRent) {
18         this.brand = brand;
19         this.plateNum = plateNum;
20         this.dayRent = dayRent;
21     }
22     
23     public String getBrand() {
24         return brand;
25     }
26     public void setBrand(String brand) {
27         this.brand = brand;
28     }
29     public String getPlateNum() {
30         return plateNum;
31     }
32     public void setPlateNum(String plateNum) {
33         this.plateNum = plateNum;
34     }
35     public int getDayRent() {
36         return dayRent;
37     }
38     public void setDayRent(int dayRent) {
39         this.dayRent = dayRent;
40     }
41     
42     //计算租金
43     public abstract float Rent(int days);
44     
45 }

创建完父类,接着建两个子类:轿车类和客车类,轿车类和客车类都有特有的属性,因各自计算租金的方法不一样,所以重新父类方法

 1 package com.ketang.rental;
 2 
 3 /**
 4  * 轿车类
 5  * @author 
 6  *
 7  */
 8 public class Sedan extends Automobile {
 9 
10     private String type;  //轿车型号
11     
12     //无参构造方法
13     public Sedan() {
14         super();
15     }
16 
17     //轿车类带品牌、车牌号、日租金、型号四个参数的构造方法
18     public Sedan(String brand, String plateNum, int dayRent, String type) {
19         super(brand, plateNum, dayRent);
20         this.type=type;
21     }
22 
23     public String getType() {
24         return type;
25     }
26 
27     public void setType(String type) {
28         this.type = type;
29     }
30 
31     //轿车类重写父类计算租金方法
32     public float Rent(int days) {
33         float price=super.getDayRent()*days;
34         if(days>7 && days<=30) {
35             price*=0.9f;
36         }else if(days>30 && days<=150) {
37             price*=0.8f;
38         }else if(days>150) {
39             price*=0.7f;
40         }
41         return price;
42     }
43 
44 }
 1 package com.ketang.rental;
 2 
 3 /**
 4  * 客车类
 5  * @author 
 6  *
 7  */
 8 public class Bus extends Automobile {
 9 
10     private int seatCount;  //座位数
11     
12     public Bus() {
13         super();
14     }
15 
16     //包含汽车品牌、车牌号、日租金、座位数四个参数的构造方法
17     public Bus(String brand, String plateNum, int dayRent,int seatCount) {
18         super(brand, plateNum, dayRent);
19         this.seatCount=seatCount;
20     }
21 
22     public int getSeatCount() {
23         return seatCount;
24     }
25 
26     public void setSeatCount(int seatCount) {
27         this.seatCount = seatCount;
28     }
29 
30     //客车类重写父类计算租金的方法
31     public float Rent(int days) {
32         float price=super.getDayRent()*days;
33         if(days>=3 && days<7) {
34             price*=0.9f;
35         }else if(days>=7 && days<30) {
36             price*=0.8f;
37         }else if(days>=30 && days<150) {
38             price*=0.7f;
39         }else if(days>=150){
40             price*=0.6f;
41         }
42         return price;
43     }
44 
45 }

接着是汽车业务类,业务类是存储汽车信息并初始化

 1 package com.ketang.rental;
 2 
 3 /**
 4  * 汽车业务类
 5  * @author 
 6  *
 7  */
 8 public class CarOperation {
 9     //创建汽车类型数组,将该数组生命为父类类型
10     public Automobile[] ats=new Automobile[8];
11     
12     //初始化汽车信息
13     public void init() {
14         ats[0]=new Sedan("别克","粤B89894",300,"林荫大道");
15         ats[1]=new Sedan("别克","粤B96968",600,"GL8");
16         ats[2]=new Sedan("宝马","粤B88888",800,"X6");
17         ats[3]=new Sedan("宝马","粤B89894",600,"550i");
18         ats[4]=new Bus("金杯","粤B11111",800,16);
19         ats[5]=new Bus("金杯","粤B22222",1500,34);
20         ats[6]=new Bus("金龙","粤B33333",800,16);
21         ats[7]=new Bus("金龙","粤B44444",1500,34);
22     }
23 
24     //租车:根据用户提供的条件去汽车数组中查找相应车辆并返回
25     //如果租赁的是客车  需要的条件:品牌  座位数  型号null
26     //如果租赁的是轿车  需要的条件:品牌  型号  座位数0
27     public Automobile rentCar(String brand,String type,int seat) {
28         Automobile auto=null;
29         for( Automobile at:ats) {
30             if(at instanceof Sedan) {
31                 Sedan sedan=(Sedan)at;  //向下转型
32                 if(sedan.getBrand().equals(brand) && sedan.getType().equals(type)) {
33                     auto=sedan;
34                     break;
35                 }
36             }else if(at instanceof Bus) {
37                 Bus bus=(Bus)at;
38                 if(bus.getBrand().equals(brand) && bus.getSeatCount()==seat) {
39                     auto=bus;
40                     break;
41                 }
42             }
43             
44         }
45         return auto;  // 返回一个汽车对象
46     }
47     
48     //计算租多台车总价格方法
49     public float totalPrice(Automobile[] automobile,int day ) {
50         float sumPrice=0;
51         for(Automobile auto:automobile) {
52             if(auto!=null) {
53                 sumPrice+=auto.Rent(day);
54             }
55         }
56         return sumPrice;
57     }
58 }

最后一个是汽车租赁管理类,实现租车管理

 1 package com.ketang.rental;
 2 
 3 import java.util.Scanner;
 4 
 5 /**
 6  * 汽车租赁管理
 7  * @author 
 8  *
 9  */
10 public class CarRetalMrg2 {
11     public static void main(String[] args) {
12         Scanner input=new Scanner(System.in);
13         CarOperation co=new CarOperation();
14         Automobile[] automobile=new Automobile[3];
15         //租赁公司首先需要购置汽车
16         co.init();
17         
18         System.out.println("********************欢迎光临快租汽车租赁公司*******************");
19         //客户租车的条件
20         String brand="";  //品牌
21         String type="";  //型号
22         int seat=0;  //座位数
23         String answer="";
24         int i=0;
25         //收集用户条件
26         do {
27             System.out.println("1、轿车   2、客车");
28             System.out.print("请选择你要租赁的汽车类型:");
29             int chooseCar=input.nextInt();
30             switch(chooseCar){
31             //租赁轿车
32             case 1:
33                 System.out.print("请选择你要租赁的汽车品牌:1、别克  2、宝马");
34                 int chooseBrand=input.nextInt();
35                 if(chooseBrand==1) {
36                     brand="别克";
37                     System.out.print("请选择你要租赁的汽车型号:1、林荫大道  2、GL8");
38                     type=input.nextInt()==1?"林荫大道":"GL8";
39                 }else if(chooseBrand==2) {
40                     brand="宝马";
41                     System.out.print("请选择你要租赁的汽车型号:1、X6  2、550i");
42                     type=input.nextInt()==1?"X6":"550i";
43                 }
44                 break;
45             //租赁客车
46             case 2:
47                 System.out.print("请选择你要租赁的汽车品牌:1、金龙  2、金杯");
48                 brand=input.nextInt()==1?"金龙":"金杯";
49                 System.out.print("请选择你要租赁的汽车座位数:1、16座  2、34座");
50                 seat=input.nextInt()==1?16:34;
51                 break;
52             default:
53                 break;
54             }
55             //每租一辆车就放到租车数组里
56             automobile[i]=co.rentCar(brand, type, seat);
57             System.out.println("租车成功,你的车牌号是:"+automobile[i].getPlateNum());
58             i++;
59             System.out.print("是否继续租赁?(y/n)");
60             answer=input.next();
61             System.out.println();
62         }while("y".equalsIgnoreCase(answer));
63 
64         System.out.print("请输入要租赁的天数:");
65         int day=input.nextInt();
66 
67         float sum=co.totalPrice(automobile, day);
68         System.out.println("租赁总费用是"+sum+"元");
69     }
70 }

以下是运行结果,本次选择租赁一辆别克GL8,一辆宝马X6,一辆34座金杯,一起租赁2天,总计价格是5800元。

本案例涉及到了继承、封装、多态、对象数组等知识。谢谢观看!

转载于:https://www.cnblogs.com/baichang/p/10056038.html

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
汽车租赁系统是一个典型的面向对象设计问题。首先,我们需要考虑哪些对象是必需的。以下是一些可能的对象: 1. 汽车类(Car):该类应该包含汽车的基本信息,如车型、车牌号、颜色、租金等。 2. 租车店类(RentalStore):该类应该包含租车店的基本信息,如店名、地址、电话等。此外,租车店应该有一个汽车库(CarInventory),以便记录汽车的数量和可用性。 3. 客户类(Customer):该类应该包含客户的基本信息,如姓名、地址、电话等。客户可以通过租车店租用汽车,因此客户类应该有一个租车方法。 4. 订单类(Order):此类应该包含有关客户租车的详细信息,如租车日期、还车日期、租用的汽车等。 接下来,我们需要考虑这些对象之间的关系。以下是一些可能的关系: 1. 租车店和汽车之间是一对多的关系,因为一个租车店可以有多个汽车。 2. 客户和租车店之间是一对多的关系,因为一个客户可以在多个租车店租车。 3. 客户和汽车之间是多对多的关系,因为一个客户可以租用多个汽车,而一辆汽车也可以被多个客户租用。 4. 订单和客户、汽车之间是一对多的关系,因为一个客户可以有多个订单,一辆汽车也可以有多个订单。 在实现时,我们需要定义上述类,并确保它们之间的关系正确。此外,我们还需要考虑如何处理错误情况,例如租车店没有足够的汽车供客户租用等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值