java基础练习0722:汽车租赁系统(含代码、改进点、关键点、出错点)

题目描述

在这里插入图片描述

出错点

  • 继承后的属性和构造函数
    在这里插入图片描述
    在这里插入图片描述

**

几个注意点

**

  1. 抽象方法的输入参数
    在这里插入图片描述

  2. 利用?”“:”“
    在这里插入图片描述

  3. 类中构造一个数组的属性:MotoVehicle类数组
    在这里插入图片描述
    在这里插入图片描述

  4. null与空”“:前者无内存空间,后者有内存空间但是没内容
    equal方法比较的时内容是否为空(不能把null和有内存空间的字符串进行比较)

    new一个Aotomobil类(引用类型)的数组,如果没有在构造时传属性的内容,实际为null,意思时只是声明可能有20个Aotomobil类的数组,内存没有给他分配空间。
    在这里插入图片描述

int[] num = new int[20];//没有初始化,默认值为0
String[] str = new String[20];//没有初始化,默认值为null
Aotomobil[] motos = new Aotomobil[20]//没有初始化,默认值为null
sout(motos[2].(Aotomobil类的某个属性));//出错  因为motos[2] = null;

在这里插入图片描述

待改进点

  1. 方法:可以返回一个父类(引用类型)(改进点:考虑car和bus的专有属性)
    在这里插入图片描述

  2. 改进点:将初始化方法改为用list集合存汽车对象

  3. 改进点:返回可以选择的品牌时或者汽车型号时,从初始化数据库中找到可选的,返回输出到菜单

Java代码

Aotomobil.java

package demoa;

public abstract class Aotomobil {
    private String License;
    private String brand;
    private String modelType;
    private double perMoney;
public Aotomobil(){

}
    public Aotomobil(String license, String brand, String modelType, double perMoney) {
        License = license;
        this.brand = brand;
        this.modelType = modelType;
        this.perMoney = perMoney;
    }

    public abstract double rentPrice(int day);
    //public abstract String showAllModelType(String Brand);
    //public abstract String showAllBrand();


    public String getLicense() {
        return License;
    }

    public void setLicense(String license) {
        this.License = license;
    }

    public String getBrand() {
        return brand;
    }

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

    public String getModelType() {
        return modelType;
    }

    public void setModelType(String modelType) {
        this.modelType = modelType;
    }

    public double getPerMoney() {
        return perMoney;
    }

    public void setPerMoney(double perMoney) {
        this.perMoney = perMoney;
    }
}

Bus.java

package demoa;

public class Bus extends Aotomobil {
    private String License;
    private String brand;
    private String modelType;
    private double perMoney;
    public Bus(){

    }
    public Bus(String license, String brand, String modelType, double perMoney) {
        this.License = license;
        this.brand = brand;
        this.modelType = modelType;
        this.perMoney = perMoney;
    }

    /*
    *重写方法:计算租赁的价格
     */
    public double rentPrice(int day) {
        double price = perMoney * day;
        if(day>=3 && day<7){
            price *= 0.9f;
        }else if(day>=7 && day<30){
            price *= 0.8f;
        }else if(day>=30 && day<150){
            price *= 0.7f;
        }else if(day>=150){
            price*= 0.6f;
        }
        return price;

    }
    public String showAllBrand(){
        return "1.金杯 2.金龙";
    }
    public String showAllModelType(String Brand){
        if(Brand == "金杯"){
            return "1.16 2.34";
        }else{
            return "1.16 2.34";
        }

    }

    public String getBrand() {
        return brand;
    }

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

    @Override
    public String getModelType() {
        return modelType;
    }

    @Override
    public void setModelType(String modelType) {
        this.modelType = modelType;
    }

    public String getLicense() {
        return License;
    }

    public void setLicense(String license) {
        this.License = license;
    }

    public double getPerMoney() {
        return perMoney;
    }

    public void setPerMoney(double perMoney) {
        this.perMoney = perMoney;
    }
}

Car.java

package demoa;

public class Car extends Aotomobil {

    private String License;
    private String brand;
    private String modelType;
    private double perMoney;

    public Car(){

    }

    public Car(String license, String brand, String modelType, double perMoney) {
        super(license, brand, modelType, perMoney);
    }

    /*
     *重写方法:计算租赁的价格
     */
    @Override
    public double rentPrice(int day) {
        double price = perMoney * day;
        if(day>7 && day<=30){
            price *= 0.9;
        }else if(day>30 && day<=150){
            price *= 0.8;
        }else if(day>150){
            price *= 0.7;
        }
        return price;

    }





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

    public String getModelType() {
        return modelType;
    }

    public void setModelType(String modelType) {
        this.modelType = modelType;
    }

    public void setLicense(String license) {
        this.License = license;
    }

    public double getPerMoney() {
        return perMoney;
    }

    public void setPerMoney(double perMoney) {
        this.perMoney = perMoney;
    }




}

OperateSystem

package demoa;

import java.util.Scanner;

public class OperateSystem {

    //***类中创建引用类型的数组属性:数组类型是Aotomobil类
    public Aotomobil[] motos = new Aotomobil[20];//数组对象
    public void initial(){
        motos[0] = new Car("京NY28588", "宝马", "X6",800);
        motos[1] = new Car("京CNY3284", "宝马", "550i",600);
        motos[2] = new Car("京NT37465", "别克", "林荫大道",300);
        motos[3] = new Car("京NT96968", "别克", "GL8",600);
        motos[4] = new Bus("京6566754", "金杯", "16",800);
        motos[5] = new Bus("京8696997", "金龙", "16",800);
        motos[6] = new Bus("京9696996", "金杯", "34",1500);
        motos[7] = new Bus("京8696998", "金龙", "34",1500);
    }
    public void operate(Scanner input){
        System.out.println("*****************欢迎光临腾飞汽车租赁公司***********************");
        System.out.println("1.轿车  2.客车");
        System.out.println("请选择你要租赁的汽车类型:");
        int motoType  = input.nextInt();
        String brand = null;
        String type = null;

        if(motoType==1){
            //aotomobil = new Car();
            System.out.print("请选择你要租赁的汽车品牌:1、别克 2、宝马");
            int choose2=input.nextInt();
            if(choose2==1){
                brand="别克";
                System.out.print("请选择你要租赁的汽车类型:1、林荫大道 2、GL8");
                type=(input.nextInt()==1)?"林荫大道":"GL8";
            }else if(choose2==2){
                brand="宝马";
                System.out.print("请选择你要租赁的汽车类型:1、X6  2、550i");
                type=(input.nextInt()==1)?"X6":"550i";
            }
        }else if(motoType==2){
            //aotomobil = new Bus();
            type="";
            System.out.print("请选择你要租赁的汽车品牌:1、金龙 2、金杯");
            brand=(input.nextInt()==1)?"金龙":"金杯";
            System.out.print("请选择你要租赁的汽车座位数:1、16座 2、34座");
            type=(input.nextInt()==1)?"16":"34";
        }

        //System.out.println(brand+type+"equals"+'abc'.equals);
        //获取ID
        String ID = null;
        double perMoney = 0;

        for (int i = 0; i < motos.length; i++) {
            System.out.println(i);
            if(motos[i].getBrand().equals(brand) && motos[i].getModelType().equals(type)){
                ID = motos[i].getLicense();
                System.out.println("测试ID是"+ID);
                perMoney = motos[i].getPerMoney();
                System.out.println("测试单价是"+perMoney);
                break;

            }

        }
        System.out.println("分配给您的汽车牌号是:"+ID);
        Aotomobil aotomobil = null;//?
        if(motoType==1){
            aotomobil = new Car();

        }else if(motoType==2){
            aotomobil = new Bus();
        }

        aotomobil.setBrand(brand);
        aotomobil.setModelType(type);
        aotomobil.setLicense(ID);
        aotomobil.setPerMoney(perMoney);
        System.out.println(aotomobil.getBrand());

        System.out.println("请输入你要租赁的天数");
        int day = input.nextInt();
        double price = aotomobil.rentPrice(day);
        System.out.println("您需要支付的租赁费用是:"+price+"元");

    }

}

Test1.java

package demoa;

import java.util.Scanner;

public class Test1 {
    public static void main(String[] args) {
        OperateSystem operateSystem = new OperateSystem();
        operateSystem.initial();
        Scanner input = new Scanner(System.in);

        operateSystem.operate(input);
    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值