java集合结构写汽车租赁系统

本项目使用java集合和面向对象知识去撰写一个汽车租赁系统,有一些细节需要注意

public abstract class Automobile {
//汽车父类
    private String plateNumber;//车牌号
    private String brand;//品牌
    private double dailyRent;//日租金

    abstract double DailyRent(int days,double dailyRent);//计算日租金
    abstract boolean equals(Automobile automobile);//重写equals方法进行比较
    public Automobile() {
    }

    @Override
    public String toString() {
        return "Automobile{" +
                "车牌号='" + plateNumber + '\'' +
                ", 品牌='" + brand + '\'' +
                ", 日租金=" + dailyRent +
                '}';
    }

    public String getPlateNumber() {
        return plateNumber;
    }

    public void setPlateNumber(String plateNumber) {
        this.plateNumber = plateNumber;
    }

    public String getBrand() {
        return brand;
    }

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

    public double getDailyRent() {
        return dailyRent;
    }

    public void setDailyRent(double dailyRent) {
        this.dailyRent = dailyRent;
    }

    public Automobile(String plateNumber, String brand, double dailyRent) {
        this.plateNumber = plateNumber;
        this.brand = brand;
        this.dailyRent = dailyRent;
    }
}
    public class Car extends Automobile {
    //轿车继承汽车父类,重写计算日租金方法和equals方法
        private String type;
    
        public String getType() {
            return type;
        }
       //带两个参数的构造方法方便后面创建对象
        public Car(String type,String brand) {
            super.setBrand(brand);
            this.type = type;
        }
    
        public void setType(String type) {
            this.type = type;
        }
    
        public Car(String plateNumber, String brand, double dailyRent, String type) {
            super(plateNumber, brand, dailyRent);
            this.type = type;
        }
    
        @Override
        double DailyRent(int days, double dailyRent) {
            if(days>150){
                return  days*dailyRent*0.7;
            }else if(days>30){
                return days*dailyRent*0.8;
            }else if(days>7){
                return days*dailyRent*0.9;
            }else return days*dailyRent;
        }
    //重写equals方法
        @Override
        boolean equals(Automobile automobile) {
            if(automobile instanceof Car){
                Car car=(Car)automobile;
               return  (this.getType().equals(car.getType())&&this.getBrand().equals(automobile.getBrand()));
            }
                return false;
        }
    }
public class Bus extends Automobile {
//客车同样继承父类
    private int seatNum;//座位数

    public int getSeatNum() {
        return seatNum;
    }

    public void setSeatNum(int seatNum) {
        this.seatNum = seatNum;
    }

    public Bus() {
    }
    //2个参数的构造方法
    public Bus(String brand,int seatNum){
        super.setBrand(brand);
        this.seatNum=seatNum;
    }
    public Bus(String plateNumber, String brand, double dailyRent, int seatNum) {
        super(plateNumber, brand, dailyRent);
        this.seatNum = seatNum;
    }

    @Override
    double DailyRent(int days, double dailyRent) {
        if(days>150){
            return days*dailyRent*0.6;
        }else if(days>30){
            return days*dailyRent*0.7;
        }else if(days>7){
            return days*dailyRent*0.8;
        }else if(days>3) {
            return days * dailyRent * 0.9;

        }else return days*dailyRent;
    }

    @Override
    boolean equals(Automobile automobile) {
        if(automobile instanceof Bus){
            Bus bus=(Bus)automobile;
            return  (this.getSeatNum()==(bus.getSeatNum())&&this.getBrand().equals(automobile.getBrand()));
        }
            return false;
    }
}
import java.util.Arrays;
import java.util.List;
//汽车业务类
public class AutomobileService {
    public static List init(){
        Automobile c1 = new Car("京NT37465","别克",300,"林荫大道");
        Automobile c2 = new Car("京NT96968","别克",600,"GLB");
        Automobile c3 = new Car("京NY28588","宝马",800,"X6");
        Automobile c4 = new Car("京CNY3284","宝马",600,"550i");
        Automobile p1 = new Bus("京6566754","金杯",800,16);
        Automobile p2 = new Bus("京9696996","金杯",1500,34);
        Automobile p3 = new Bus("京8696997","金龙",800,16);
        Automobile p4 = new Bus("京8696998","金龙",1500,34);
        //先装入数组
        Automobile[] automobile={c1,c2,c3,c4,p1,p2,p3,p4};
        //再使用数组工具类调用asList方法,装入集合
        List<Automobile> automobiles = Arrays.asList(automobile);//重点
        return automobiles;
    }
}

import java.util.List;
import java.util.Scanner;
public class TestAutomobile {
//汽车测试类
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //初始化集合,将8辆车存入集合中
        List<Automobile> automobiles = AutomobileService.init();//重点
        System.out.println("* * * * * * * * * *欢迎光临秋名山守望者汽车租赁公司* * * * * * * * * *");
        System.out.println("1、轿车  2、客车");
        System.out.print("请选择你要租赁的汽车类型:");
        int type = sc.nextInt();
        //定义3个属性备用,分别属于客车或汽车
        String brand;//品牌
        String model;//型号
        int b;
        //父类引用类型,接收子类对象
        Automobile automobile;
        if (type==1){//轿车
            System.out.println("请选择你要租赁的汽车品牌(1、别克 2、宝马):");
            b = sc.nextInt();
            brand = b==1?"别克":"宝马";
            if (b==1){
                System.out.println("请输入你要租赁的汽车类型:1、林荫大道 2、GLB");
                model = sc.nextInt()==1?"林荫大道":"GLB";
            }else {
                System.out.println("请输入你要租赁的汽车类型:1、X6 2、550i");
                model = sc.nextInt()==1?"X6":"550i";
            }
            automobile = new Car(brand,model);
        }else {//客车
            System.out.println("请选择你要租赁的汽车品牌:1、金杯 2、金龙");
            brand = sc.nextInt()==1?"金杯":"金龙";
            System.out.println("请选择你要租赁的汽车座位数;1、16座 2、34座");
            int seat = sc.nextInt()==1?16:34;
            automobile = new Bus(brand,seat);
        }
        //根据选好的车型,输出车牌和总价
        for (Automobile a:automobiles) {
            //根据用户输入的两个属性构建的对象和集合中的对象比较:a有完整的属性值,automobile只有2个属性值
            if (a.equals(automobile)) {
                System.out.println("请输入您要租赁的天数:");
                int days = sc.nextInt();
                System.out.println("分配给您的汽车牌号是:"+a.getPlateNumber());
                System.out.println("您需要支付的租赁费用是:"+a.DailyRent(days,a.getDailyRent()));
                break;
            }
        }
    }

}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值