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

菜鸟写的答答租车系统

慕囚
关注TA
已关注
手记 2
粉丝 0
获赞 5

package com.imooc;
//创建汽车父类**
public abstract class Car {
private int id;//汽车编号
private String name;//汽车名字
private double price;//租车价格

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public abstract void showInfo();
    public int getPersonNum() {
        // TODO Auto-generated method stub
        return 0;
    }
    public double getGoodsNum() {
        // TODO Auto-generated method stub
        return 0;
    }

}


//创建客车类****
package com.imooc;

public class PassengerCar extends Car{
private int personNum;//客车载人量

public int getPersonNum() {
    return personNum;
}

public void setPersonNum(int personNum) {
    this.personNum = personNum;
}

public PassengerCar(int id, String name, double price, int personNum){
    super.setId(id);
    super.setName(name);
    super.setPrice(price);
    this.personNum = personNum;
}

public void showInfo(){
    System.out.println(getId() + ". \t" + getName() + "\t" + getPrice() + "/天" + "\t\t" + "载人:" + getPersonNum());
}

}


//创建货车类**
package com.imooc;

public class Truck extends Car{
private double goodsNum;//货车载货量

public double getGoodsNum() {
    return goodsNum;
}

public void setGoodsNum(double goodsNum) {
    this.goodsNum = goodsNum;
}

public Truck(int id, String name, double price, double goodsNum){
    super.setId(id);
    super.setName(name);
    super.setPrice(price);
    this.goodsNum = goodsNum;
}

public void showInfo(){
    System.out.println(getId() + ". \t" + getName() + "\t" + getPrice() + "/天" + "\t\t" + "载货:" + getGoodsNum());
}

}


//创建皮卡类**
package com.imooc;

public class Pickup extends Car {
private int personNum;
private double goodsNum;

public int getPersonNum() {
    return personNum;
}
public void setPersonNum(int personNum) {
    this.personNum = personNum;
}
public double getGoodsNum() {
    return goodsNum;
}
public void setGoodsNum(double goodsNum) {
    this.goodsNum = goodsNum;
}
public Pickup(int id, String name, double price, int personNum, double goodsNum) {
    super.setId(id);
    super.setName(name);
    super.setPrice(price);
    this.personNum = personNum;
    this.goodsNum = goodsNum;
}

public void showInfo(){
    System.out.println(getId() + ". \t" + getName() + "\t" + getPrice() + "/天  " + "\t" + "载人:" + getPersonNum()
                + "载货:" + getGoodsNum());
}

}


//主类***
package com.imooc;
import java.util.Scanner;

public class DaDaRentCar {

//创建Scanner对象
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
    // TODO Auto-generated method stub
    new DaDaRentCar().rentCar();
}
private static int inputCommand(){
    while(true){
            String rules = input.nextLine();
            if(rules.matches("^\\d{1,2}$")){
                return Integer.parseInt(rules);
            }
            else{
                System.out.println("请输入0-99的数字");
            }
        }
    }

public void rentCar(){

    //创建车辆信息
     Car[] cars={new PassengerCar(1, "奥迪A4", 500, 4), new PassengerCar(2, "马自达6", 400, 4),
             new Pickup(3, "皮卡雪", 450, 4, 2), new PassengerCar(4, "金龙", 800, 20),
             new Truck(5, "松花江", 400, 4), new Truck(6, "依维柯", 1000, 20)};

    System.out.println("欢迎使用答答租车系统:\n您是否要租车:1是 0否");
    while (true){
            String rules = input.nextLine();
            if(rules.matches("^1$")){
                System.out.println("您可租车的类型及价目表:");
                System.out.println("序号 \t 汽车名称 \t 租金 \t\t 容量");
                //显示车辆信息
                for(int i = 0; i < cars.length; i++){
                    cars[i].showInfo();
                }
                break;
            }
            else if(rules.matches("^0$")){
                System.out.println("欢迎下次光临!");
                return;
            }
            else{
                System.out.println("请输入0或1");
            }
        }   

     System.out.println("请输入租车数量:");
     //读取用户租车数量
    int carNum = inputCommand();

    //客户选择的汽车编号
    int carId = 0;
    //保存用户选择的汽车
    Car[] selectCars = new Car[carNum];
    //依次让用户输入租车编号
    for(int i=0; i<carNum; i++){
        System.out.println("请输入第"+(i+1)+"辆车的序号:");

        while(true){
            String rules = input.nextLine();
            if(rules.matches("^[1-6]$")){
                 carId = Integer.parseInt(rules);
                 selectCars[i] = cars[carId-1];
                 break;
            }
            else{
                System.out.println("请输入1-6的数字");
            }
        }
    }
    System.out.println("请输入租车的天数:");
    //用户租车天数
    int day = inputCommand();
    System.out.println("***您的账单***");
    System.out.println("可载人的车有:");
    //共载人数量
    int personCount = 0;
    //输出可载人的车
    for(int i=0; i<selectCars.length; i++){
        if(selectCars[i] instanceof PassengerCar || selectCars[i] instanceof Pickup){
            System.out.print(selectCars[i].getName() + " ");
            personCount += selectCars[i].getPersonNum();
        }
    }
    System.out.println("共载人:" + personCount);
    //共载货量
    double goodsCount = 0;
    //输出可载货的车
    for(int i=0; i<selectCars.length; i++){
        if(selectCars[i] instanceof Truck || selectCars[i] instanceof Pickup){
            System.out.print(selectCars[i].getName() + " ");
            goodsCount += selectCars[i].getGoodsNum();
        }
    }
    System.out.println("共载货:" + goodsCount);
    //租车总价格
    double priceCount = 0;
    for(int i=0; i<selectCars.length; i++){
        priceCount += selectCars[i].getPrice();
    }
    //输出租车总价格
    System.out.println("租车总价格:"+priceCount * day);
    input.close();
}

}


//运行结果**

欢迎使用答答租车系统:
您是否要租车:1是 0否

1
您可租车的类型及价目表:
序号 汽车名称 租金 容量

  1. 奥迪A4 500.0/天 载人:4
  2. 马自达6 400.0/天 载人:4
  3. 皮卡雪 450.0/天 载人:4载货:2.0
  4. 金龙 800.0/天 载人:20
  5. 松花江 400.0/天 载货:4.0
  6. 依维柯 1000.0/天 载货:20.0
    请输入租车数量:
    5
    请输入第1辆车的序号:
    2
    请输入第2辆车的序号:
    3
    请输入第3辆车的序号:
    5
    请输入第4辆车的序号:
    4
    请输入第5辆车的序号:
    6
    请输入租车的天数:
    5
    您的账单
    可载人的车有:
    马自达6 皮卡雪 金龙 共载人:28
    皮卡雪 松花江 依维柯 共载货:26.0
    租车总价格:3050.0
打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP