手记

JAVA第二季---答答租车系统

package com.imooc;

public class Car {
    private String name;
    private int rentMoney;

    public Car(){

    }
    public Car(String name, int rentMoney){
        this.name = name;
        this.rentMoney = rentMoney; 
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getRentMoney() {
        return rentMoney;
    }
    public void setRentMoney(int rentMoney) {
        this.rentMoney = rentMoney;
    }

}

package com.imooc;

public class PassagerCar extends Car{
private int passCapacity;

public PassagerCar(String name, int rentMoney, int passCapacity){
    super(name, rentMoney);
    this.passCapacity = passCapacity;   
}
public String toString(){
    return PassagerCar.this.getName()+" "+ PassagerCar.this.getRentMoney()+"元/天"+"      "+"载人:" +this.passCapacity+"人";
}
public int getPassCapacity() {
    return passCapacity;
}
public void setPassCapacity(int passCapacity) {
    this.passCapacity = passCapacity;
}

}

package com.imooc;

public class Truck extends Car {
    private int goodsCapacity;
    public Truck(String name, int rentMoney, int goodsCapacity){
        super(name, rentMoney);
        this.goodsCapacity = goodsCapacity;
    }
    public String toString(){
        return Truck.this.getName()+"   "+ Truck.this.getRentMoney()+"元/天"+"        "+ "载货:" +this.goodsCapacity+"吨";
    }
    public int getGoodsCapacity() {
        return goodsCapacity;
    }
    public void setGoodsCapacity(int goodsCapacity) {
        this.goodsCapacity = goodsCapacity;
    }

}

package com.imooc;

public class PickUp extends Car {
private int passCapacity;
private int goodsCapacity;
public PickUp(String name, int rentMoney, int passCapacity, int goodsCapacity){
super(name, rentMoney);
this.passCapacity = passCapacity;
this.goodsCapacity = goodsCapacity;
}
public String toString(){
return PickUp.this.getName()+" "+ PickUp.this.getRentMoney()+"元/天"+" "+ "载人:" +this.passCapacity+"人"+" "+ "载货:" +this.goodsCapacity+"吨";
}
public int getPassCapacity() {
return passCapacity;
}
public void setPassCapacity(int passCapacity) {
this.passCapacity = passCapacity;
}
public int getGoodsCapacity() {
return goodsCapacity;
}
public void setGoodsCapacity(int goodsCapacity) {
this.goodsCapacity = goodsCapacity;
}

}


package com.imooc;

import java.util.Scanner;

public class RentCar {

    public static void main(String[] args) {
        Car[] cars = new Car[]{new PassagerCar("奥迪A4",500,4), new PassagerCar("马自达6",400,4),
                new PickUp("皮卡雪6",450,4,2),new PassagerCar("金龙",800,20),new Truck("松花江",400,4),
                new Truck("依维柯",900,20)};
        System.out.println("欢迎使用答答租车系统");
        System.out.println("您是否要租车: 1是 2否");
        Scanner input = new Scanner(System.in);
        int choose = input.nextInt();
        if(choose == 1){
            System.out.println("您可租车的类型及其价目表:");
            System.out.println("序号  汽车名称    租金      容量");
            for(int i = 0; i < cars.length; i++){
                System.out.println(i+1 + "." + "        "+cars[i]);
            }
            System.out.println("请输入你要租汽车的数量:");
            int num = input.nextInt();
            int[] rentCarNum = new int[num];
            for(int i = 0; i < num; i++){
                System.out.println("请输入第"+ (i+1) +"辆车的序号");
                int carNum = input.nextInt();
                rentCarNum[i] = carNum;
            }
            System.out.println("请输入租车的天数:");
            int rentDay = input.nextInt();
            System.out.println("您的账单:");
            System.out.println("***可载人的车有:");
            int peopleNum = 0;
            for(int i = 0; i < rentCarNum.length; i++){
                if((cars[rentCarNum[i]-1] instanceof PassagerCar) || (cars[rentCarNum[i]-1] instanceof PickUp)){
//                  System.out.print(cars[rentCarNum[i]-1].getName()+"  ");

                    System.out.println(cars[rentCarNum[i]-1]);
                }                   
            }

            System.out.println();
            System.out.println("***可载货的车有:");
            for(int i = 0; i < rentCarNum.length; i++){
                if((cars[rentCarNum[i]-1] instanceof Truck) || (cars[rentCarNum[i]-1] instanceof PickUp)){
                    System.out.println(cars[rentCarNum[i]-1]);      
                }

            }
            int totalMoney = 0;
            for(int i = 0; i < rentCarNum.length; i++){
                totalMoney += cars[rentCarNum[i]-1].getRentMoney() * rentDay;                       
            }
            System.out.println("***租车总价格:"+totalMoney);

        }else{
            System.out.println("欢迎您再次使用");
        }
    }

}
![图片描述][1]

  [1]: http://img.mukewang.com/5830112e00016a1304210204.png
0人推荐
随时随地看视频
慕课网APP