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

java入门第二季最后一题租车系统程序

小星星来也
关注TA
已关注
手记 2
粉丝 6
获赞 48

首先创建一个Car父类

package com.carsystem;

public class Car {
    String name;
    int price;
    int load;
    int pNum;
}

接着创建三个子类

package com.carsystem;

public class PassengerCar extends Car {

    PassengerCar(String name,int price,int pNum){
        this.name=name;
        this.price=price;
        this.pNum=pNum;     
    }
}
package com.carsystem;

public class GoodsVan extends Car {
    GoodsVan(String name,int price,int load){
        this.name=name;
        this.price=price;
        this.load=load;     
    }   
}
package com.carsystem;

public class Pickup extends Car {
    Pickup(String name,int price,int load,int pNum){
        this.name=name;
        this.price=price;
        this.load=load;
        this.pNum=pNum;     
    }
}

最后创建租车系统类

package com.carsystem;
import java.util.*;
public class RentSystem {
    Car cars[] ={new PassengerCar("奥迪A4",500,4),new PassengerCar("马自达",400,4),new Pickup("皮卡雪",450,2,4),new PassengerCar("金龙",800,20),new GoodsVan("松花江",400,4), new GoodsVan("依维柯",1000,20)};
    int count;
    int day;
    int money;
    //定义一个大数组,原来是cars.length,一旦租车数量大于6就不行
    Car[] rent = new Car[100];
    static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {
        RentSystem hello = new RentSystem();
        System.out.println("欢迎使用答答租车系统:");
        System.out.println("您是否要租车:1 是 0 否");
        int select = input.nextInt();
        if(select==0){
            input.close();
            System.exit(-1);
        }
        System.out.println("您可租车的类型及其价目表:");
        System.out.println("序号 汽车名称   租金            容量");
        System.out.println("1.  奥迪A4   500/天      载人:4人");
        System.out.println("2.  马自达       400/天      载人:4人");
        System.out.println("3.  皮卡雪       450/天      载人:4人,载货:2吨");
        System.out.println("4.  金龙           800/天     载人:20人");
        System.out.println("5.  松花江        500/天     载货:4吨");
        System.out.println("6.  依维柯        1000/天   载货:20吨");
        System.out.println("请输入您要租车额数量:");
        int rentNum = input.nextInt();
        for(int i=0;i<rentNum;i++){
            System.out.println("您要租的第"+(i+1)+"辆车的序号");
            int a = input.nextInt();
            while(a<1||a>6){
                System.out.println("您输入的数据有问题,请重新输入");
                int b = input.nextInt();
                a=b;
            }
            int p = hello.cars[a-1].price;
            hello.money +=p;
                    hello.rent[i] = hello.cars[a-1];
        }
        System.out.println("您要租车的天数:");
        int c = input.nextInt();

        hello.money*=c;
        System.out.println("您的租车账单");
        int loadAmount =0;
        int passengerAmount =0;
        for(int i=0;i<rentNum;i++){

        System.out.print(hello.rent[i].name+" ");
        loadAmount+=hello.rent[i].load;
        passengerAmount+=hello.rent[i].pNum;
        }

        System.out.println();
        System.out.println("您的租车载人数量"+passengerAmount+"人");
        System.out.println("您的租车载货量"+loadAmount+"吨");
        System.out.println("您的租车总费用:"+hello.money);
    }
}
打开App,阅读手记
26人推荐
发表评论
随时随地看视频慕课网APP

热门评论

http://img.mukewang.com/591c6da4000199e104000081.jpg老师我在这里就over了,不明白什么意思?

这里代码测试好像有个bug,输入租车数辆大于6辆的时候,会在要求输入第七辆车的时候出错,也就是数组越界操作了,应该在租车类型里面加一个租车数量,这样保证数组不会越界。自己的观点不知道对不对。

是租车的数量噢噢噢噢噢噢噢噢

查看全部评论