手记

java第二季综合练习,答答租车系统

父类 Car

public class Car extends Object {
    private String carName;
    private int carPrice;
    private int carPeople;
    private int carGoods;
    public String getCarName() {
        return carName;
    }
    public void setCarName(String carName) {
        this.carName = carName;
    }
    public int getCarPrice() {
        return carPrice;
    }
    public void setCarPrice(int carPrice) {
        this.carPrice = carPrice;
    }
    public int getCarPeople() {
        return carPeople;
    }
    public void setCarPeople(int carPeople) {
        this.carPeople = carPeople;
    }
    public int getCarGoods() {
        return carGoods;
    }
    public void setCarGoods(int carGoods) {
        this.carGoods = carGoods;
    }

}

子类PassengerCar

public class PassengerCar extends Car {
    public PassengerCar (String name,int price,int people){
        super.setCarName(name);
        super.setCarPrice(price);
        super.setCarPeople(people);
    }

    @Override
    public String toString() {
        return  this.getCarName() + "\t"+this.getCarPrice() + "\t载人:"+this.getCarPeople() ;
    }

}

子类Truck

public class Truck extends Car {
    public Truck(String name,int price,int goods){
        super.setCarName(name);
        super.setCarPrice(price);
        super.setCarGoods(goods);
    }

    @Override
    public String toString() {
        return this.getCarName() + "\t"+ this.getCarPrice()+"\t载货:" + this.getCarGoods() +"吨";
    }

}

子类 Pickup

public class Pickup extends Car {
    public Pickup(String name,int price,int people,int goods){
        super.setCarName(name);
        super.setCarPrice(price);
        super.setCarPeople(people);
        super.setCarGoods(goods);
    }

    @Override
    public String toString() {
        return this.getCarName() + "\t" + this.getCarPrice() + "\t载人:"+ this.getCarPeople() + "载货:"+ this.getCarGoods()+"吨" ;
    }

}

main

import java.util.*;
public class Dada {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("欢迎使用答答租车系统\n您是否要租车:1是 0否");
        Scanner input = new Scanner(System.in);
        int x = input.nextInt();
        if(x!=1){
            System.out.println("谢谢光临,期待您下次再来!");
            System.exit(0);
        }
        else{
            System.out.println("您可租的类型及其价目表:");
            System.out.println("序号\t汽车名称\t租金\t容量");
        }
        //到这为止,判定是否为真正租车的。

        PassengerCar car1 = new PassengerCar("奥迪A4",500,4);
        PassengerCar car2 = new PassengerCar("马自达6",400,4);
        Pickup car3 = new Pickup("皮卡雪6",450,4,2);
        PassengerCar car4 = new PassengerCar("金龙",800,20);
        Truck car5 = new Truck("松花江",400,4);
        Truck car6 = new Truck("依维柯",400,4);
        Car[] cars = {car1,car2,car3,car4,car5,car6};
        for(int i=0;i<cars.length;i++){
            System.out.println((i+1)+".\t"+cars[i].toString()); 
        }
        //  隔开一下,看的更清楚啦!

        System.out.println("请输入您要租汽车的数量:");
        int n = input.nextInt();       //记录租车数量
        Car [] carList = new Car[n];   //记录租车序号
        int totalPrice = 0;
        for(int j=0;j<n;j++){
            System.out.println("请输入第"+(j+1)+"辆车的序号(1——"+cars.length+"):");
            int num = input.nextInt();
            carList[j] = cars[num-1];
            totalPrice+=carList[j].getCarPrice();
        }       
        System.out.println("请输入租车天数:");
        int days = input.nextInt();
        totalPrice*=days;     //  租车总价格

        //下面开始计算载人的车辆以及载人数
        int totalPeople=0;
        System.out.println("您的账单:\n***可载人的车有:");
        for(int i=0;i<n;i++){
            if(carList[i] instanceof PassengerCarcarList[i] instanceof Pickup){
            System.out.print(carList[i].getCarName()+"\t");
            totalPeople+=carList[i].getCarPeople();
            }
        }
        System.out.print("共载人:"+totalPeople+"人\n***载货的车有:\n");

        //下面开始计算载货的车辆以及载货量
        int totalGoods=0;
        for(int o=0;o<n;o++){   
            if(carList[o] instanceof TruckcarList[o] instanceof Pickup){
            System.out.print(carList[o].getCarName()+"\t");
            totalGoods+=carList[o].getCarGoods();
            }
        }
        System.out.println("共载货:"+totalGoods+"吨");
        System.out.println("***租车总价格:"+totalPrice);

    }

}
0人推荐
随时随地看视频
慕课网APP

热门评论

你好,我想请问下下面的句子是什么用法?

carList[i] instanceof PassengerCarcarList[i] instanceof Pickup

carList[o] instanceof TruckcarList[o] instanceof Pickup

麻烦你啦。

查看全部评论