Java入门第二季租车系统

释放双眼,带上耳机,听听看~!

定义父类Auto

package com.Imooc;

public abstract class Auto {
    private  String model;//汽车型号
    private  int rent;//汽车租金
    private  int capacity;//汽车载客量
    private  int cargo;//汽车载货量

    //以下为各个变量的设置与获取方法

    public String getModel() {
        return model;
    }
    public void setModel(String model) {
        this.model = model;
    }
    public double getRent() {
        return rent;
    }
    public void setRent(int rent) {
        this.rent = rent;
    }
    public int getCapacity() {
        return capacity;
    }
    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }
    public int getCargo() {
        return cargo;
    }
    public void setCargo(int cargo) {
        this.cargo = cargo;
    }

}

定义子类Bus

package com.Imooc;
public class Bus extends Auto{
    //载客用车初始化
        public Bus(String newmodel, int newrent, int capacity) {
            // TODO Auto-generated method stub
            this.setModel(newmodel);
            this.setRent(newrent);
            this.setCapacity(capacity);
        }
}

定义子类Truck

package com.Imooc;

public class Truck extends Auto {

    //卡车的初始化
    public  Truck(String newmodel,int newrent,int newcargo){
        this.setModel(newmodel);
        this.setRent(newrent);
        this.setCargo(newcargo);
    }

}

定义子类Pickup

package com.Imooc;

public class Pickup extends Auto {

    //皮卡车的初始化
    public  Pickup(String newmodel,int newrent, int newcapacity,int cargo){
        this.setModel(newmodel);
        this.setRent(newrent);
        this.setCapacity(newcapacity);
        this.setCargo(newcapacity);
    }
}

测试函数

package com.Imooc;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Auto[] carsForRent = {new Bus("五菱",100,7),new Pickup("福特",150,2,2),new Truck("东风", 150, 5)};
        System.out.println("欢迎来到租车系统,请问您要租车吗?");
        System.out.println("租车请输入1,退出请输入0");
        Scanner scan = new Scanner(System.in);
        String input = scan.next();
        if(input.equals("1")){//
            System.out.println("您可租车的类型及其价目表:");
            System.out.println("序号/t汽车名称/t租金/t载客量/t载货量");
            for(Auto currentCar:carsForRent){//遍历已存数组内的数据
                if(currentCar instanceof Bus){
                    System.out.println(""+1+"/t"+currentCar.getModel()+"/t"+currentCar.getRent()+"/t"+currentCar.getCapacity());
                }
                else if(currentCar instanceof Pickup){
                    System.out.println(""+2+"/t"+currentCar.getModel()+"/t"+currentCar.getRent()+"/t"+currentCar.getCapacity()+"/t"+currentCar.getCargo());
                }
                else{
                    System.out.println(""+3+"/t"+currentCar.getModel()+"/t"+currentCar.getRent()+"/t/t"+currentCar.getCargo());
                }
            }
        }
        else{
            System.out.println("谢谢光临租车系统,再见!");
        }
        System.out.println("请输入序号选择您所需要的车");
        String input2 = scan.next();
        switch (input2){
        case "1":System.out.println("您选择了"+carsForRent[0].getModel()+",租金为每日"+carsForRent[0].getRent()+"元,"+"载客量为"+carsForRent[0].getCapacity()+"人");
        break;
        case "2":System.out.println("您选择了"+carsForRent[1].getModel()+",租金为每日"+carsForRent[1].getRent()+"元,"+"载客量为"+carsForRent[1].getCapacity()+"人,载货量为"+carsForRent[1].getCargo()+"吨。");
        break;
        case "3":System.out.println("您选择了"+carsForRent[2].getModel()+",租金为每日"+carsForRent[2].getRent()+"元,"+"载货量为"+carsForRent[1].getCargo()+"吨");
        }
        System.out.println("谢谢惠顾");
    }
}

主函数部分参考了Java入门第二季最后一个视频里的代码

【转自慕课】https://www.imooc.com

JAVA

【servlet+mybatis实现一个仓库管理系统】之工作日志

2022-3-3 19:01:02

JAVA

Thinking in java基础之 I/O系统

2022-3-3 19:01:31

搜索