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

Java入门第二季——答答租车系统

小丷
关注TA
已关注
手记 5
粉丝 8
获赞 49

父类:

package com.lb;

public class ACar {

}

子类:

package com.lb;

public class Aodi extends ACar{

    private String name;
    private int peopleNum;
    private int cargoNum;
    private int rentMoney;
    public Aodi(String name,int peopleNum,int cargoNum,int rentMoney){
        this.name=name;
        this.peopleNum=peopleNum;
        this.cargoNum=cargoNum;
        this.rentMoney=rentMoney;
    }
    @Override
    public String toString() {
        return name + "     载客" + peopleNum + " 载货" + cargoNum + "   租金" + rentMoney;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getPeopleNum() {
        return peopleNum;
    }
    public void setPeopleNum(int peopleNum) {
        this.peopleNum = peopleNum;
    }
    public int getCargoNum() {
        return cargoNum;
    }
    public void setCargoNum(int cargoNum) {
        this.cargoNum = cargoNum;
    }
    public int getRentMoney() {
        return rentMoney;
    }
    public void setRentMoney(int rentMoney) {
        this.rentMoney = rentMoney;
    }

}

实现类:

package com.lb;

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        System.out.println("欢迎使用租车系统:");
        System.out.println("您是否要租车:1是 0否");
        Scanner sc=new Scanner(System.in);
        int in=sc.nextInt();
        if(in==1){
            System.out.println("您可以租车的类型及价目表:");
            System.out.println("序号  汽车名称   容量             租金");
            ACar[] carForRent={new Aodi("奥迪",4,0,500),new Mazida("马自达",4,3,400),
                            new Pikaxue("皮卡雪",4,2,450),new Jinlong("金龙",20,0,800)};

            for(int i=0;i<carForRent.length;i++){
                System.out.println((i+1)+"  "+carForRent[i].toString());
            }
            System.out.println("请输入租车的数量:");
            int num=sc.nextInt();
            int[] a=new int[5];
            for(int i=0;i<num;i++){
                System.out.println("请输入第"+(i+1)+"辆车的序号:");
                a[i]=sc.nextInt();
            }
            System.out.println("请输入租车天数:");
            int day=sc.nextInt();
            int cargoNum=0,peopleNum=0,money=0;
            String peopleName="",cargoName="";
            for(int i=0;i<num;i++){
                if(carForRent[a[i]-1] instanceof Aodi){
                    Aodi aoid= (Aodi)carForRent[a[i]-1];
                    if(aoid.getPeopleNum()!=0){
                        peopleName+=(aoid.getName()+"  ");
                        peopleNum += aoid.getPeopleNum();
                    }
                    if(aoid.getCargoNum()!=0){
                        cargoName+=(aoid.getName()+"  ");
                        cargoNum += aoid.getCargoNum();
                    }
                    money+=aoid.getRentMoney();
                }
                if(carForRent[a[i]-1] instanceof Jinlong){
                    Jinlong jinlong= (Jinlong)carForRent[a[i]-1];
                    if(jinlong.getPeopleNum()!=0){
                        peopleName+=(jinlong.getName()+"  ");
                        peopleNum += jinlong.getPeopleNum();
                    }
                    if(jinlong.getCargoNum()!=0){
                        cargoName+=(jinlong.getName()+"  ");
                        cargoNum += jinlong.getCargoNum();
                    }
                    money+=jinlong.getRentMoney();
                }
                if(carForRent[a[i]-1] instanceof Mazida){
                    Mazida mazida= (Mazida)carForRent[a[i]-1];
                    if(mazida.getPeopleNum()!=0){
                        peopleName+=(mazida.getName()+"  ");
                        peopleNum += mazida.getPeopleNum();
                    }
                    if(mazida.getCargoNum()!=0){
                        cargoName+=(mazida.getName()+"  ");
                        cargoNum += mazida.getCargoNum();
                    }
                    money+=mazida.getRentMoney();
                }
                if(carForRent[a[i]-1] instanceof Pikaxue){
                    Pikaxue pikaxue= (Pikaxue)carForRent[a[i]-1];
                    if(pikaxue.getPeopleNum()!=0){
                        peopleName+=(pikaxue.getName()+"  ");
                        peopleNum += pikaxue.getPeopleNum();
                    }
                    if(pikaxue.getCargoNum()!=0){
                        cargoName+=(pikaxue.getName()+"  ");
                        cargoNum += pikaxue.getCargoNum();
                    }
                    money+=pikaxue.getRentMoney();
                }
            }
            System.out.println("可以载人的车有:\n"+peopleName+"    共"+peopleNum+"人。");
            System.out.println("可以载货的车有:\n"+cargoName+"    共"+cargoNum+"吨。");
            System.out.println("租车总价格:"+money*day+"元!");
            sc.close();
        }
    }
}
打开App,阅读手记
2人推荐
发表评论
随时随地看视频慕课网APP