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

答答租车系统(Java课后作业)

大呀大话梅
关注TA
已关注
手记 3
粉丝 11
获赞 44

一、Car.java(父类)

package com.rentcar;

public class Car {
    private String name;
    private int price;
    private int mancapacity;
    private double goodscapacity;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void getList(){

    }

    public double getCargo() {
        return goodscapacity;
    }

    public int getPeople() {
        return mancapacity;
    }

    public int getRent() {
        return price;
    }

}

二、 PassengerCar.java(载客子类)

package com.rentcar;

public class PassengeCar extends Car {
    String name;
    int price;
    int mancapacity;

    public PassengeCar(String name,int price,int mancapacity){
        this.name = name;
        this.price = price;
        this.mancapacity = mancapacity;
    }

    public String getName(){
        return name;
    }

    public int getPeople(){
        return mancapacity;
    }

    public int getRent(){
        return price;
    }

    public void getList(){
        System.out.println(name+"\t"+price+"元/天"+"\t"+"载人:"+mancapacity+"人");
    }
}

三、Cargo.java(货车子类)

package com.rentcar;

public class Cargo extends Car {
    String name;
    int price;
    double goodscapacity;

    public Cargo(String name,int price,double goodscapacity){
        this.name = name;
        this.price = price;
        this.goodscapacity = goodscapacity;
    }

    public String getName(){
        return name;
    }

    public double getCargo(){
        return goodscapacity;
    }

    public int getRent(){
        return price;
    }

    public void getList(){
        System.out.println(name+"\t"+price+"元/天"+"\t"+"载货:"+goodscapacity+"吨");
    }
}

四、Pickup.java(皮卡子类)

package com.rentcar;

public class Pickup extends Car {
    String name;
    int price;
    int mancapacity;
    int goodscapacity;

    public Pickup(String name,int price,int mancapacity,int goodscapacity){
        this.name = name;
        this.price = price;
        this.mancapacity = mancapacity;
        this.goodscapacity = goodscapacity;
    }

    public String getName(){
        return name;
    }

    public int getPeople(){
        return mancapacity;
    }

    public double getCargo(){
        return goodscapacity;
    }

    public int getRent(){
        return price;
    }

    public void getList(){
        System.out.println(name+"\t"+price+"元/天"+"\t"+"载人:"+mancapacity+"人"+"\t"+"载货"+goodscapacity+"吨");
    }
}

五、Test.java(测试类)

package com.rentcar;
import java.util.Scanner;

public class Test {

    /**
     * @param args
     */
    static Car[] cars = {new PassengeCar("奥迪", 500, 4),
          new PassengeCar("马自达6", 400, 4),
          new Pickup("皮卡雪6", 450, 4, 2),
          new PassengeCar("金龙", 800, 20),
          new Cargo("松花江", 400, 4),
          new Cargo("依维柯", 1000, 20)
};
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.out.println("欢迎使用答答租车系统");
        Isrentcar();//判断并打印租车列表
        int carcount = rentcarCount();//获取租车数量
        int[] carnums = getCarnums(carcount);//获取租车序号列表
        int days = rentcarDay();
        getBill(days,carcount,carnums);
    }

    public static void Isrentcar(){
        System.out.println("您是否要租车:1 是   0 否");
        Scanner input = new Scanner(System.in);
        int isno = input.nextInt();
        if(isno == 1){
            System.out.println("您可租车的类型及价目表:");
            System.out.println("序号"+"\t"+"汽车名称"+"\t"+"租金"+"\t"+"容量");
            for(int i=0;i<cars.length;i++){
                System.out.print("NO."+(i+1)+"\t");
                cars[i].getList();
            }
        }
        else{
            System.out.println("感谢使用,期待您的下次光临");
            System.exit(0);
        }
    }

    public static int rentcarCount(){
        System.out.println("请输入您要租车的数量:");
        Scanner input2 = new Scanner(System.in);
        int count = input2.nextInt();
        return count;
        }

    public static int[] getCarnums(int carcount){
        int[] rentArray = new int[carcount];
        int y = 0;
        for(int i=0;i<carcount;i++){
            System.out.println("请输入第"+(i+1)+"辆车的序号");
            Scanner input3 = new Scanner(System.in);
            rentArray[y] = input3.nextInt();
            y++;
        }
        return rentArray;
    }

    public static int rentcarDay(){
        System.out.println("请输入租车天数:");
        Scanner input4 = new Scanner(System.in);
        int day = input4.nextInt();
        return day;
    }

    public static void getBill(int days,int carcount,int[] carnums){
        int people = 0;
        double cargo = 0;
        int rent = 0;
        System.out.println("您的账单:");
        System.out.println("***可载人的车有:");
        for(int i=0;i<carnums.length;i++){
            if(cars[carnums[i]-1] instanceof PassengeCar || cars[carnums[i]-1] instanceof Pickup){
            System.out.print(cars[carnums[i]-1].getName()+"  ");
            people += cars[carnums[i]-1].getPeople();
            }
        }
        System.out.println("共载人"+people+"人");
        System.out.println("***可载货的车有:");
        for(int i=0;i<carnums.length;i++){
            if(cars[carnums[i]-1] instanceof Cargo || cars[carnums[i]-1] instanceof Pickup){
            System.out.print(cars[carnums[i]-1].getName()+"  ");
            cargo += cars[carnums[i]-1].getCargo();
            }
        }
        System.out.println("共载货"+cargo+"吨");
        for(int i=0;i<carnums.length;i++){
            rent += cars[carnums[i]-1].getRent();
        }
        rent = rent * days;
        System.out.println("***租车总价格:"+rent+"元");
    }
}

测试结果图

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

热门评论

请教:private属性不是不能继承吗?那父类中写那些属性做什么?

怎么input哪里提示感叹号啊

查看全部评论