Day22 汽车租赁系统

本文回顾了面向对象编程的概念,强调了其封装、继承和多态的特性。通过模拟真实世界的对象,定义类和对象,阐述了面向对象在软件开发中的应用。文章通过创建Car父类和轿车、客车子类,演示了如何设计汽车租赁系统的租金计算方法,并介绍了业务逻辑的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、面向对象回顾

1、面向对想象的软件开发方法

        软件系统即各种对象的集合

        按对象设计出来的系统结构较稳定

        子系统相对独立,提高了软件的可维护性

        支持封装、继承和多态,提高了软件的可重用性和可扩展性

2、万物皆对象

        软件系统所模拟的真实世界中,所有的实体都可以抽象为对象

        每个对象都是唯一的

3、对象

        对象具有属性和行为(方法)

        对象具有状态

                状态指某个瞬间对象各种属性的取值         

                对象的方法可以改变对象自身的状态

        对象都属于某个类,每个对象都是某个类的实例

4、类

        类是一组具有相同属性和行为的对象的抽象

        开发人员自定义数据类型

        面向对象编程的主要任务就是定义各个类

        对象是类的实例,类是对象的模板

5、面向对象的三大特征

        封装

                隐藏对象的属性和实现细节,仅仅对外公开接口

                便于使用者正确方便的理解和使用系统

                有助于各系统之间的松耦合,提高系统独立性

                提高软件的可重用性

                把尽可能多的东西藏起来,对外提供便捷的接口

                把所有的属性藏起来

        继承

                子类继承了父类的部分属性和方法

                子类还可以扩展出新的属性和方法

                子类还可以覆盖父类中方法的实现方式

        注意:

                继承的层次不可太多,尽量两到三层

                继承的最上层最好抽象

        多态

                多态:多种实现方式提供服务

                动态绑定

                        向上转型:父类的引用指向子类的实例

                        向下转型:子类的引用指向父类的实例

                

 二、面向对象的应用

 1、设计步骤

        

2、抽象出类

        

3、类的属性

         

 4、类的方法

        

 5、优化设计

        

 三、实例

1、创建一个Car父类,并创建一个抽象方法

package cn.bdqn.demo;

public abstract class Car {
    private String brand;
    private String licenseNumber;
    private int dayRent;

    public Car() {
    }

    public Car(String brand, String licenseNumber, int dayRent) {
        this.brand = brand;
        this.licenseNumber = licenseNumber;
        this.dayRent = dayRent;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getLicenseNumber() {
        return licenseNumber;
    }

    public void setLicenseNumber(String licenseNumber) {
        this.licenseNumber = licenseNumber;
    }

    public int getDayRent() {
        return dayRent;
    }

    public void setDayRent(int dayRent) {
        this.dayRent = dayRent;
    }

    public abstract double carRent(int days);
}

2、创建一个轿车类saloonCar,重写父类Car中的抽象方法(计算租金)

package cn.bdqn.demo;
//轿车
public class saloonCar extends Car{
    private String type;

    public saloonCar() {
    }

    public saloonCar(String brand, String licenseNumber, int dayRent, String type) {
        super(brand, licenseNumber, dayRent);
        this.type = type;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public double carRent(int days) {
        double price=this.getDayRent()*days;
        if (days>150){
            price*=0.7;
        } else if (days>30) {
            price*=0.8;
        } else if (days>7) {
            price*=0.9;
        }else{
            price*=1;
        }
        return price;
    }
}

   3、创建一个客车类Coach,重写父类中的抽象方法(计算租金)

        

package cn.bdqn.demo;
//客车
public class Coach extends Car{
    //座位数
    private int seating;

    public Coach() {
    }

    @Override
    public double carRent(int days) {
        double price=super.getDayRent()*days;
        if(days>150){
            price*=0.6;
        } else if (days>=30) {
            price*=0.7;
        } else if (days>=7) {
            price*=0.8;
        }else if (days>=3){
            price*=0.9;
        }else{
            price*=1;
        }
        return price;
    }

    public Coach(String brand, String licenseNumber, int dayRent, int seating) {
        super(brand, licenseNumber, dayRent);
        this.seating = seating;
    }

    public int getSeating() {
        return seating;
    }

    public void setSeating(int seating) {
        this.seating = seating;
    }

}

4、创建一个汽车业务类,定义一个数组,初始化数据,并创建一个一查找相应车辆的方法

package cn.bdqn.demo;
//汽车业务类
public class CarOperation {
    Car cars[]=new Car[8];
    
    public void init(){
        cars[0]=new saloonCar("宝马","京NY28588",800,"X6");
        cars[1]=new saloonCar("宝马","京CNY3284",600,"550i");
        cars[2]=new saloonCar("别克","京NT37465",300,"林荫大道");
        cars[3]=new saloonCar("别克","京NT96968",600,"GLB");
        cars[4]=new Coach("金杯","京6566754",800,16);
        cars[5]=new Coach("金龙","京8696997",800,16);
        cars[6]=new Coach("金杯","京9696996",1500,34);
        cars[7]=new Coach("金龙","京8696998",1500,34);
    }

    //根据用户提供的数据去数组中查找相应车辆并返回
    public Car carOut(String brand,String type,int seating){
        Car car=null;
        for (Car mycar : cars) {
            if(mycar instanceof saloonCar){
                saloonCar car1=(saloonCar) mycar;
                if(car1.getBrand().equals(brand)&&car1.getType().equals(type)){
                    car=car1;
                    break;
                }
            }else {
                Coach car2=(Coach) mycar;
                if(car2.getBrand().equals(brand)&&car2.getSeating()==seating){
                    car=car2;
                    break;
                }
            }
        }
        return car;
    }

}

5、测试类

package cn.bdqn.demo;

import java.util.Scanner;

public class CarManage {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        CarOperation carMan=new CarOperation();
        carMan.init();
        Car car =null;//汽车
        System.out.println("**********欢迎光临杰斌汽车租赁有限公司**********");
        System.out.println("1、轿车 \t 2、客车");
        System.out.println("请输入你要租赁的汽车类型");
        int count= sc.nextInt();
        String brand= " ";
        String type=" ";
        int seating=0;
        if (count==1){
            System.out.println("请选择您要租赁的品牌:1、别克 2、宝马");
            int count1= sc.nextInt();
            if (count==1){
                brand="别克";
                System.out.println("请选择您要租赁的汽车型号:1、林荫大道 2、GLB");
                type = (sc.nextInt()==1)?"林荫大道":"GLB";
            } else if (count1==2) {
                brand="宝马";
                System.out.println("请输入您要租赁的汽车型号:1、X6 2、550i");
                type=(sc.nextInt()==1)?"X6":"550i";
            }
        } else if (count==2) {
            type=" ";
            System.out.println("请选择您要租赁的客车品牌:1、金龙 2、金杯");
            brand=(sc.nextInt()==1)?"金龙":"金杯";
            System.out.println("请选择您要租赁的客车座位数:1、16 2、34");
            seating=(sc.nextInt()==2)?16:34;

        }
        car=carMan.carOut(brand,type,seating);
        System.out.println("请输入您要租赁的天数:");
        int days= sc.nextInt();
        double money=car.carRent(days);
        System.out.println("分配给您的汽车牌号是:"+car.getLicenseNumber());
        System.out.println("您想要支付的租赁金额是:"+money+"元");

    }
}

6、部分结果

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值