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

答答租车系统,练手,非原创

长是人千离
关注TA
已关注
手记 5
粉丝 5
获赞 21
package example;

public class Car {
    private String type;
    private double price;
    public void setType(String type) {
        this.type = type;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public String getType() {
        return type;
    }
    public double getPrice() {
        return price;
    }
    @Override
    public String toString() {
        return type + "\t" + price;
    }
}
package example;

public class CarBus extends Car implements IPassenger { 
    private int passCapacity;
    public CarBus(String type,int price,int passCapacity) {
        // TODO Auto-generated constructor stub
        this.setType(type);
        this.setPrice(price);
        this.setPassCapacity(passCapacity);
    }
    @Override
    public int getPassCapacity() {
        // TODO Auto-generated method stub
        return passCapacity;
    }

    @Override
    public void setPassCapacity(int passCapacity) {
        // TODO Auto-generated method stub
        this.passCapacity=passCapacity;
    }
    @Override
    public String toString() {
        return super.toString() + "\t" + passCapacity + "\t---";
    }

}
package example;

public class CarPickup extends Car implements IPassenger, ICargo {
    private int passCapacity;
    private int cargoCapacity;
    public CarPickup(String type,int price,int passCapacity,int cargoCapacity) {
        // TODO Auto-generated constructor stub
        this.setType(type);
        this.setPrice(price);
        this.setPassCapacity(passCapacity);
        this.setCargoCapacity(cargoCapacity);
    }
    @Override
    public int getCargoCapacity() {
        // TODO Auto-generated method stub
        return cargoCapacity;
    }
    @Override
    public void setCargoCapacity(int cargoCapacity) {
        // TODO Auto-generated method stub
        this.cargoCapacity=cargoCapacity;
    }
    @Override
    public int getPassCapacity() {
        // TODO Auto-generated method stub
        return passCapacity;
    }

    @Override
    public void setPassCapacity(int passCapacity) {
        // TODO Auto-generated method stub
        this.passCapacity=passCapacity;
    }
    @Override
    public String toString() {
        return super.toString() + "\t" + passCapacity + "\t" + cargoCapacity;
    }
}
package example;

public class CarTruck extends Car implements ICargo {
    private int cargoCapacity;
    public CarTruck(String type,int price,int cargoCapacity) {
        // TODO Auto-generated constructor stub
        this.setType(type);
        this.setPrice(price);
        this.setCargoCapacity(cargoCapacity);
    }
    @Override
    public int getCargoCapacity() {
        // TODO Auto-generated method stub
        return cargoCapacity;
    }
    @Override
    public void setCargoCapacity(int cargoCapacity) {
        // TODO Auto-generated method stub
        this.cargoCapacity=cargoCapacity;
    }
    @Override
    public String toString() {
        return super.toString() + "\t---\t" + cargoCapacity;
    }

}
package example;

public interface ICargo {
    public int getCargoCapacity();
    public void setCargoCapacity(int cargoCapacity);
}
package example;

public interface IPassenger {
    public int getPassCapacity();
    public void setPassCapacity(int passCapacity);
}
package example;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;

public class Demo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("欢迎来到答答租车系统!");
        List carList = initCarList();
        for (;;) {
            System.out.println("*********");
            System.out.println("1:租车");

            System.out.println("0:退出");
            System.out.println("*********");
            int choice;
            try {
                choice = Integer.parseInt(getInput());
            } catch (Exception e) {
                // TODO: handle exception
                System.out.println("错误");
                continue;
            }
            if (choice == 0)
                break;
            else if (choice == 1) {
                rentCars(carList);
                System.out.println();
            } else {
                System.out.println("错误");
                continue;
            }
        }
        System.out.println("感谢您使用答答租车系统!");
    }

    private static List initCarList() {
        List<Car> carList = new ArrayList<Car>();
        carList.add(new CarBus("小型客车", 500, 6));
        carList.add(new CarBus("大型客车", 1000, 20));
        carList.add(new CarTruck("小型货车", 400, 2));
        carList.add(new CarTruck("大型货车", 800, 10));
        carList.add(new CarPickup("大型皮卡", 900, 6, 8));
        return carList;
    }

    private static void printCarList(List carList) {
        // TODO Auto-generated method stub
        System.out.println("可用的车型:");
        System.out.println("序号\t车型\t租金\t载客量\t载货量");
        for (int i = 0; i < carList.size(); i++) {
            System.out.println((i + 1) + "\t" + (carList.get(i).toString()));
        }
    }

    private static List selectCars(List carList) {
        List<Integer> rentArray = new ArrayList<Integer>();
        List<Car> selectCars = new ArrayList<Car>();
        System.out.println("请输入租车序号,以回车分隔。输入0结束:");
        for (;;) {
            System.out.println("请输入车辆序号(1-" + carList.size() + "):");
            boolean repeated = false;
            int carNum;
            try {
                carNum = Integer.parseInt(getInput());
            } catch (Exception e) {
                // TODO: handle exception
                System.out.println("错误");
                continue;
            }
            if (carNum == 0)
                break;
            else if (carNum > 0 && carNum <= carList.size()) {
                for (int i = 0; i < rentArray.size(); i++) {
                    if (carNum == rentArray.get(i)) {
                        System.out.println("你已经输入过该车型!请重新输入序号!");
                        repeated = true;
                        break;
                    }
                }
                if (!repeated) {
                    rentArray.add(carNum);
                    selectCars.add((Car) carList.get(carNum - 1));
                }
            } else {
                System.out.println("输入不正确");
                continue;
            }
        }
        return selectCars;
    }

    private static int rentCount() {
        for (;;) {
            System.out.println("请输入租车数");
            int rentCount;
            try {
                rentCount = Integer.parseInt(getInput());
            } catch (Exception e) {
                // TODO: handle exception
                System.out.println("输入不正确");
                continue;
            }
            if (rentCount >= 1 && rentCount <= 50)
                return rentCount;
            else
                System.out.println("输入不准确");
        }
    }

    private static int[] rentNumArray(List carList) {
        int rentNumArray[] = new int[carList.size()];
        for (int i = 0; i < carList.size(); i++) {
            System.out.println("请输入选定列表里,第" + (i + 1) + "辆车的租车数:");
            rentNumArray[i] = rentCount();
        }
        return rentNumArray;
    }

    private static int rentDays() {
        // TODO Auto-generated method stub
        for (;;) {
            System.out.println("请输入租车天数(1-365)");
            int rentDays;
            try {
                rentDays = Integer.parseInt(getInput());
            } catch (Exception e) {
                // TODO: handle exception
                System.out.println("错误");
                continue;
            }
            if (rentDays >= 1 && rentDays <= 365) {
                return rentDays;
            } else {
                System.out.println("错误");
            }
        }
    }

    private static int[] rentDayArray(List carList) {
        int rentDayArray[] = new int[carList.size()];
        for (int i = 0; i < rentDayArray.length; i++) {
            System.out.println("请输入选定列表里,第" + (i + 1) + "辆车的租车天数:");
            rentDayArray[i] = rentDays();
        }
        return rentDayArray;
    }

    private static void printOrder(List carList, int rentNumArray[], int rentDayArray[]) {
        double[] rentSinglePriceTotal = new double[carList.size()];
        int[] rentSinglePassTotal = new int[carList.size()];
        int[] rentSingleCargoTotal = new int[carList.size()];
        System.out.println("以下是您的订单:");
        System.out.println("序号\t车型\t租金\t载客量\t载货量" + "\t租用数量\t租用天数\t消费数额");
        for (int i = 0; i < carList.size(); i++) {
            rentSinglePriceTotal[i] = ((Car) carList.get(i)).getPrice() * rentNumArray[i] * rentDayArray[i];
            System.out.println((i + 1) + "\t" + (carList.get(i)).toString() + "\t" + rentNumArray[i] + "\t" + rentDayArray[i] + "\t" + rentSinglePriceTotal[i]);
            try {
                rentSinglePassTotal[i] = ((CarBus) carList.get(i)).getPassCapacity() * rentNumArray[i];
            } catch (Exception e) {
                // TODO: handle exception
                try {
                    rentSinglePassTotal[i] = ((CarPickup) carList.get(i)).getPassCapacity() * rentNumArray[i];
                } catch (Exception e2) {
                    // TODO: handle exception
                    rentSinglePassTotal[i] = 0;
                }
            }
            try {
                rentSingleCargoTotal[i] = ((CarTruck) carList.get(i)).getCargoCapacity() * rentNumArray[i];
            } catch (Exception eCarTruck) {
                try {
                    rentSingleCargoTotal[i] = ((CarPickup) carList.get(i)).getCargoCapacity() * rentNumArray[i];
                } catch (Exception eCarPickup) {
                    rentSingleCargoTotal[i] = 0;
                }
            } // 双重try嵌套,如果车辆有载货功能,则存储一天的总载货量,否则存储0
        }
        double rentTotal = DoubleStream.of(rentSinglePriceTotal).sum();
        int passCapacityTotal = IntStream.of(rentSinglePassTotal).sum();
        int cargoCapacityTotal = IntStream.of(rentSingleCargoTotal).sum();
        System.out.println("总载客量(人/天):" + passCapacityTotal);
        System.out.println("总载货量(人/天):" + cargoCapacityTotal);
        System.out.println("订单总价(元):" + rentTotal); // 所有车的订单总价
    }

    private static String getInput() {
        Scanner input=new Scanner(System.in);
        try {
            return input.next();
        } catch (Exception e) {
            // TODO: handle exception
            return null;
        } finally {
            input.nextLine();
        }
    }

    private static void rentCars(List carList) {
        printCarList(carList);
        List selectCars=selectCars(carList);
        System.out.println("这是您选定的租车列表");
        printCarList(selectCars);
        int rentNumArray[]=rentNumArray(selectCars);
        int rentDayArray[]=rentDayArray(selectCars);
        printOrder(selectCars,rentNumArray,rentDayArray);
    }
}
打开App,阅读手记
2人推荐
发表评论
随时随地看视频慕课网APP

热门评论

这个比较符合  多态 封装 继承 接口都涉及

查看全部评论