JAVA入门第二季 6-1综合练习

331 查看

package com.demo.dada;

import java.util.Scanner;

public class Rent {

    public static void main(String[] args) {

        int input;
        Scanner scanner = new Scanner(System.in);

        // TODO Auto-generated method stub
        for(;;){
            Car[] daDaRent = {new Truck(10,5,"解放",550),new autoMobile(10,5,"思域",200),new autoMobile(10,5,"雷凌",180),new Pickup(10,5,5,"福特",250)};
            System.out.println("欢迎来到Dada租车~");
            System.out.println("确认租车请按1,退出请按0.");
            input = scanner.nextInt();
            if(input == 1){
                for(int i=0;i<daDaRent.length;i++){
                    if(daDaRent[i] instanceof Truck){
                        System.out.println((i+1)+". "+daDaRent[i].getCarName()+" 拉货量:"+((Truck)daDaRent[i]).getCargoLoad()+"吨"+" 剩余数量:"+daDaRent[i].getCarCount()+"辆"+" 租金:¥"+daDaRent[i].getRent());
                    }
                    else if(daDaRent[i] instanceof autoMobile){
                        System.out.println((i+1)+". "+daDaRent[i].getCarName()+" 载客量:"+((autoMobile)daDaRent[i]).getPersons()+"人"+" 剩余数量:"+daDaRent[i].getCarCount()+"辆"+" 租金:¥"+daDaRent[i].getRent());                    
                    }else{
                        System.out.println((i+1)+". "+daDaRent[i].getCarName()+" 拉货量:"+((Pickup)daDaRent[i]).getCargoLoad()+"吨"+" 载客量:"+((Pickup)daDaRent[i]).getPersons()+"人"+" 剩余数量:"+daDaRent[i].getCarCount()+"辆"+" 租金:¥"+daDaRent[i].getRent());                                       
                    }
                }
            }else if(input == 0){
                System.out.println("成功退出!");
                break;
//              System.exit(0);
            }
            else{
                System.out.println("您的输入有误,请重新输入!");
                continue;
            }
            input = scanner.nextInt();
            switch(input){
            case 1:
                System.out.println("订单信息:"+daDaRent[input-1].getCarName()+" 拉货量:"+((Truck)daDaRent[input-1]).getCargoLoad()+"吨"+" 租金:¥"+daDaRent[input-1].getRent());
                break;
            case 2:
                System.out.println("订单信息:"+daDaRent[input-1].getCarName()+" 载客量:"+((autoMobile)daDaRent[input-1]).getPersons()+"人"+" 租金:¥"+daDaRent[input-1].getRent());                    
                break;
            case 3:
                System.out.println("订单信息:"+daDaRent[input-1].getCarName()+" 载客量:"+((autoMobile)daDaRent[input-1]).getPersons()+"人"+" 租金:¥"+daDaRent[input-1].getRent());                    
                break;
            default:
                System.out.println("订单信息:"+daDaRent[input-1].getCarName()+" 拉货量:"+((Pickup)daDaRent[input-1]).getCargoLoad()+"吨"+" 载客量:"+((Pickup)daDaRent[input-1]).getPersons()+"人"+" 租金:¥"+daDaRent[input-1].getRent());                                     
            }
//          scanner.close();
        }
    }

}
package com.demo.dada;

public class Car {
    private int leftCount = 0;
    private String name = null;
    private int rent = 0;
    public Car(int count,String name,int rent){
        this.leftCount = count;
        this.name = name;
        this.rent = rent;
    }
    public int getCarCount(){
        return leftCount;
    }
    public String getCarName(){
        return name;
    }
    public int getRent(){
        return rent;
    }
    public void rentCar(int count){
        if(leftCount<count){
            System.out.println("抱歉,该车型仅剩"+leftCount+",您可减少租赁数量或选择其他车型。");
        }
        else{
            leftCount = leftCount-count;
            System.out.print("租车成功!");
        }
    }
}
package com.demo.dada;

public class Truck extends Car {

    private int cargoLoad = 0;

    public Truck(int count,int load,String name,int rent) {
        super(count,name,rent);
        this.cargoLoad = load;
    }
    public int getCargoLoad(){
        return cargoLoad;
    }
}
package com.demo.dada;

public class autoMobile extends Car {

    private int persons = 0;

    public autoMobile(int count,int persons,String name,int rent) {
        super(count,name,rent);
        this.persons = persons;
    }
    public int getPersons(){
        return persons;
    }
}
package com.demo.dada;

public class Pickup extends Car {

    private int persons = 0;
    private int cargoLoad = 0;

    public Pickup(int count,int persons,int load,String name,int rent) {
        super(count,name,rent);
        this.persons = persons;
        this.cargoLoad = load;
    }
    public int getCargoLoad(){
        return cargoLoad;
    }
    public int getPersons(){
        return persons;
    }
}