为了账号安全,请及时绑定邮箱和手机立即绑定

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

标签:
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);

    }

}
点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 1
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消