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

答答租車系統,早上实现

python玄
关注TA
已关注
手记 3
粉丝 4
获赞 26

業務:計算價格、計算載人量、計算載貨量;
代碼實現:Calc接口、Car類、各種車型類繼承Car類
接口Calc:

package com.dadataxi;

public interface Calc {
    public double calcPrice(int days,int num);
    public int calcPersons(int num);
    public double calcGoods(int num);
}

Car類,实现接口calc;

package com.dadataxi;

public class Car implements Calc{
    public String name; //车名
    public double price;//租车价格
    public int persons;//载客量
    public double goods;//载货量
    public boolean isPerson;//是否可载人
    public boolean isGood;//是否可载货
    /**
     * @param days 租车天数
     * @param num  租车数量
     * @return double类型 某种类型车租车总价
     */
    @Override
    public double calcPrice(int days,int num) {
        // TODO Auto-generated method stub
        return price*days*num;
    }
    /**
     * @param num  租车数量
     * @return int类型 某种类型车可乘坐人数
     */
    @Override
    public int calcPersons(int num) {
        // TODO Auto-generated method stub
        return persons*num;
    }
    /**
     * @param num  租车数量
     * @return int类型 某种类型车可载货量
     */
    @Override
    public double calcGoods(int num) {
        // TODO Auto-generated method stub
        return goods*num;
    }
}

载人车辆:

package com.dadataxi;

public class PersonCar extends Car{
    //使用构造函数初始化车辆属性
    public PersonCar(String name,double price,int persons) {
        this.name = name;
        this.price = price;
        this.persons = persons;
        this.isPerson = true;
    }
    //使用toString方法输出
    public String toString() {
        return name+"\t"+price+"\\天"+"\t"+"载人:"+persons+"人";
    }
}

皮卡车型:

package com.dadataxi;

public class PiKa extends Car {
    //使用构造函数初始化车辆属性
    public PiKa(String name,double price,int persons,double goods) {
        this.name = name;
        this.price = price;
        this.persons = persons;
        this.isPerson = true;
        this.isGood = true;
        this.goods = goods;
    }
    //使用toString方法输出
    public String toString() {
        return name+"\t"+price+"\\天"+"\t"+"载人:"+persons+"人"+"载货:"+goods+"吨";
    }
}

货车类

package com.dadataxi;

public class HuoChe extends Car{
    //使用构造函数初始化车辆属性
    public HuoChe(String name,double price,double goods) {
        this.name = name;
        this.price = price;
        this.goods = goods;
        this.isGood = true;
    }
    //使用toString方法输出
    public String toString() {
        return name+"\t"+price+"\\天"+"\t"+"载货:"+goods+"吨";
    }
}

主函数

package com.dadataxi;

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class UserInter {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        double totalPrice = 0;//订单总价格
        int totalPersons = 0;//订单总可坐人数
        double totalGoods = 0;//订单总可载货量
        Set<String> carPersons = new HashSet<String>();//存储载人车辆
        Set<String> carGoods = new HashSet<String>();//存储载货车辆
        Car cars[] = { new PersonCar("奥迪A4", 500.0, 4), new PiKa("皮卡", 200, 2, 1.2),
                new HuoChe("东风大货", 1000, 20) };//引用Car类型数组变量存储车辆
        //用户交互代码
        //获取用户输入
        Scanner in = new Scanner(System.in);
        System.out.println("--------欢迎使用答答租车系统--------");
        System.out.println("您是否要租车:1 是,2 否");
        String result = in.next();
        if (result.equals("1")) {
            System.out.println("序号" + "\t" + "汽车名称" + "\t" + "租金" + "\t" + "容量" + "\t");
            for (int i = 0; i < cars.length; i++) {
                System.out.println((i + 1) + "." + "\t" + cars[i]);
            }
            int flag = 1;
            String res1 = "1";
            do {
                if (res1.equals("1")) {
                    System.out.println("请输入要租车的车型");
                    int xuHao = in.nextInt();
                    System.out.println("请输入要租车的天数");
                    int days = in.nextInt();
                    System.out.println("请输入要租车的数量");
                    int num = in.nextInt();
                    totalPrice += cars[xuHao - 1].calcPrice(days, num);
                    if (cars[xuHao - 1].isGood) {
                        carGoods.add(cars[xuHao - 1].name);
                        totalGoods += cars[xuHao - 1].calcGoods(num);
                    }
                    if (cars[xuHao - 1].isPerson) {
                        carPersons.add(cars[xuHao - 1].name);
                        totalPersons += cars[xuHao - 1].calcPersons(num);
                    }
                    System.out.println("您是否继续租车:1 是 0 否");
                    res1 = in.next();
                } else if (res1.equals("0")) {
                    System.out.println("退出答答租车系统");
                    flag = 0;
                } else {
                    System.out.println("请输入正确的指令");
                }
            } while (flag == 1);
            System.out.println("--------您的账单:--------");
            if (carPersons.size() > 0) {
                System.out.println("---可载人车辆有:---");
                for (String name : carPersons) {
                    System.out.print(name + "\t");
                }
                System.out.print(totalPersons + "人" + "\n");
            }
            if (carGoods.size() > 0) {
                System.out.println("---可载货车辆有:---");
                for (String name : carGoods) {
                    System.out.print(name + "\t");
                }
                System.out.print(totalGoods + "吨" + "\n");              
            }
            System.out.println("---您的租车总价格:" + totalPrice + "元");
        } else if (result.equals("2")) {
            System.out.println("退出答答租车系统");
            System.exit(0);
        } else {
            System.out.println("请输入正确的指令");
        }
    }
}

有问题请多多指教

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