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

Java练习答答租车系统

qq_好男人_3
关注TA
已关注
手记 1
粉丝 3
获赞 7
package rentsystem;

    // 抽象类父类,子类包括bus,pickup,truck
public abstract class Car {

    // 私有变量,起保护作用
    private int number;  // 序号
    private String name;  // 车名
    private int day;  // 天数
    private double price; // 每车每天价格
    private int busLoad; // 载客数量
    private double bagLoad; // 载货数量

    // 客户获取所租车清单,供子类继承
    public abstract void list();

    // 获得Car类封装的属性及初始化属性
    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getName() {
        return name;
    }

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

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getBusLoad() {
        return busLoad;
    }

    public void setBusLoad(int busLoad) {
        this.busLoad = busLoad;
    }

    public double getBagLoad() {
        return bagLoad;
    }

    public void setBagLoad(double bagLoad) {
        this.bagLoad = bagLoad;
    }

}
package rentsystem;

public class Bus extends Car {
    public Bus(int number, String name, int busLoad, double price){
        this.setNumber(number)  ;
        this.setName(name);
        this.setBusLoad(busLoad);

        this.setPrice( price);
    }
    @Override
    public void list() {
        // TODO Auto-generated method stub
        System.out.println (getNumber() + "\t" + getName() + "\t" + getBusLoad() + "\t" + getPrice());
    }

}
package rentsystem;

public class Truck extends Car {
    public Truck(int number, String name, double bagLoad, double price){
        this.setNumber(number)  ;
        this.setName(name);
        this.setBagLoad(bagLoad);

        this.setPrice( price);
    }
    @Override
    public void list() {
        // TODO Auto-generated method stub
        System.out.println (getNumber() + "\t" + getName() + "\t" + getBagLoad() + "\t" + getPrice());
    }

}
package rentsystem;

public class Pickup extends Car {
    public Pickup(int number, String name, int busLoad, double bagLoad, double price){
        this.setNumber(number)  ;
        this.setName(name);
        this.setBusLoad(busLoad);
        this.setBagLoad(bagLoad);
        this.setPrice( price);
    }
    @Override
    public void list() {
        // TODO Auto-generated method stub
        System.out.println (getNumber() + "\t" + getName() + "\t" + getBusLoad() + "\t" + getBagLoad() + "\t" + getPrice());
    }

}
package rentsystem;

import java.util.Scanner;

public class Initail {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Car[] cars ={ new Bus(1, "虎跃", 30, 300),
                      new Truck (2, "东风", 10, 500),
                      new Pickup (3, "长城", 5, 8.5, 400)
        };

        System.out.println("欢迎来到答答租车!\n您是否要租车? 1 是  0 否");
        Scanner input = new Scanner (System.in);

        if (input.nextInt() != 1 && input.nextInt() != 0 ){
            System.out.println("您的输入有误,请重新输入");
        }else if (input.nextInt() == 0){
            System.out.println("欢迎下次光临!");
        }else if (input.nextInt() == 1){
            System.out.println("您所能租车列表");
            System.out.println ("序号" + "\t" + "车名" + "\t" + "载客量" + "\t" + "载货量" + "\t" + "价格");
            for (int i=0; i<cars.length; i++){
                cars[i].list();
            }
        }

        System.out.println ("请输入租车数量");
        int rentNum = input.nextInt();
        double total = 0; 
        for (int i=0; i<rentNum; i++){
            System.out.println ("请输入第" +(i+1)+ "辆车的序号");
            int num = input.nextInt();
            cars[num].list();
            int days = input.nextInt();
            total += cars[num].getPrice() * days;
        }
        System.out.println ("您的总价格为:" + total);

        input.close();
    }

}

图片描述

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