继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

JAVA入门第二季项目(答答租车系统)

Francis_Lee
关注TA
已关注
手记 3
粉丝 3
获赞 64

父类:Car

package com.dada;

public class Car {
    private String name;
    private int price;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
}

子类:PassengerCar

package com.dada;

public class PassengerCar extends Car {
    private int passengerLoad;
    public PassengerCar(String name,int price,int passengerLoad){
        super();
        super.setName(name);
        super.setPrice(price);
        this.passengerLoad=passengerLoad;
    }
    public int getPassengerLoad() {
        return passengerLoad;
    }
    public void setPassengerLoad(int passengerLoad) {
        this.passengerLoad = passengerLoad;
    }
    @Override
    public String toString() {
        return this.getName()+"     "+this.getPrice()+"元/天      载客"+passengerLoad+"人";
    }

}

子类:Truck

package com.dada;

public class Truck extends Car {
    private int goodsLoad;
    public Truck(String name,int price,int goodsLoad){
        super();
        super.setName(name);
        super.setPrice(price);
        this.goodsLoad=goodsLoad;
    }
    @Override
    public String toString() {
        return this.getName()+"     "+this.getPrice()+"元/天      载货"+goodsLoad+"吨";
    }
    public int getGoodsLoad() {
        return goodsLoad;
    }
    public void setGoodsLoad(int goodsLoad) {
        this.goodsLoad = goodsLoad;
    }
}

子类:PickUp

package com.dada;

public class PickUp extends Car {
    private int passengerLoad;
    private int goodsLoad;
    public PickUp(String name,int price,int passengerLoad,int goodsLoad){
        super();
        super.setName(name);
        super.setPrice(price);
        this.passengerLoad=passengerLoad;
        this.goodsLoad=goodsLoad;
    }
    @Override
    public String toString() {
        return this.getName()+"     "+this.getPrice()+"元/天      载客"+passengerLoad+"人/载货"+goodsLoad+"吨";
    }
    public int getPassengerLoad() {
        return passengerLoad;
    }
    public void setPassengerLoad(int passengerLoad) {
        this.passengerLoad = passengerLoad;
    }
    public int getGoodsLoad() {
        return goodsLoad;
    }
    public void setGoodsLoad(int goodsLoad) {
        this.goodsLoad = goodsLoad;
    }
}

系统类:Initial

package com.dada;

import java.util.Scanner;

public class Initial {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("欢迎使用答答租车系统");
        System.out.println("您是否要租车? 是 按1  否 按0");
        Scanner input=new Scanner(System.in);
        int key=input.nextInt();
        if(key==0){
            System.exit(0);
        }else if(key==1){
            System.out.println("您可租车的类型及其价目表");
            System.out.println("序号      汽车名称        租金      容量");
            Car[] cars={new PassengerCar("奥迪A4",500,4),new PassengerCar("马自达6",400,4),new 
                    PickUp("皮卡雪6",450,4,2),new PassengerCar("金龙",800,20),new Truck("松花江",400,
                            4),new Truck("依维柯",900,20)};
            for(int i=0;i<6;i++){
                System.out.println(i+1+".       "+cars[i]);
            }
            System.out.println("请输入您要租车的数量:");
            int num=input.nextInt();
            int totalPrice=0;
            int totalPassengerLoad=0;
            int totalGoodsLoad=0;
            int[] passengerCarNo = new int[7];
            int pCNCount=0;
            int[] goodsCarNo = new int[7];
            int gCNCount=0;
            for(int i=0;i<num;i++){
                System.out.println("请输入第"+(i+1)+"辆车的序号:");
                int j=input.nextInt();
                totalPrice+=cars[j-1].getPrice();
                if(cars[j-1] instanceof PassengerCar){
                    PassengerCar passengerCar=(PassengerCar)cars[j-1];
                    totalPassengerLoad+=passengerCar.getPassengerLoad();
                    passengerCarNo[pCNCount++]=j-1;
                }else if(cars[j-1] instanceof Truck){
                    Truck truck=(Truck)cars[j-1];
                    totalGoodsLoad+=truck.getGoodsLoad();
                    goodsCarNo[gCNCount++]=j-1;
                }else if(cars[j-1] instanceof PickUp){
                    PickUp pickUp=(PickUp)cars[j-1];
                    totalPassengerLoad+=pickUp.getPassengerLoad();
                    totalGoodsLoad+=pickUp.getGoodsLoad();
                    passengerCarNo[pCNCount++]=j-1;
                    goodsCarNo[gCNCount++]=j-1;
                }
            }
            String passengerCars="";
            String goodsCars="";
            for(int k=0;k<pCNCount;k++){
                passengerCars=passengerCars+cars[passengerCarNo[k]].getName()+"  ";
            }
            for(int k=0;k<gCNCount;k++){
                goodsCars=goodsCars+cars[goodsCarNo[k]].getName()+"  ";
            }
            System.out.println("请输入租车天数:");
            int days=input.nextInt();
            System.out.println("您的账单:");
            System.out.println("***可载人的车有:"+passengerCars+"共载人:"+totalPassengerLoad+"人");
            System.out.println("***可载货的车有:"+goodsCars+"共载货:"+totalGoodsLoad+"吨");
            System.out.println("***租车总价格:"+totalPrice*days+"元");
        }else{
            System.exit(0);
        }

    }

}
打开App,阅读手记
22人推荐
发表评论
随时随地看视频慕课网APP

热门评论

借见了同学你的,真的很好

查看全部评论