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

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

标签:
Java 测试

一、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+"元");
    }
}

测试结果图

点击查看更多内容
20人点赞

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消