手记

java学习第二季哒哒租车系统

根据自己的理解写的,还不完善,错误的地方还请不吝赐教,谢谢!
cars父类:

public abstract class Car {

    public double getPrice() {
        return price;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public double getGoods() {
        return goods;
    }

    public void setGoods(double goods) {
        this.goods = goods;
    }

    public int getPeople() {
        return people;
    }

    public void setPeople(int people) {
        this.people = people;
    }

    public double price;
    public String title;
    public int people;
    public double goods;

    @Override
    public String toString() {
        return  title + " \t " + price + "/天\t 载人:" + people + "/人";
    }
}

货车,客车,皮卡车子类:

public class Trunck extends Car {
    /**
     * Override construct function.
     */
    public Trunck(String title, double price, int goods){
        this.price = price;
        this.title = title;
        this.goods = goods;
    }
    /**
     * Override toString function.
     */
    @Override
    public String toString() {
        return  title + "\t " + price + "/天\t 载货:" + goods + "吨";
    }
}
public class Bus extends Car{
    /**
     * Override construct function.
     */
    public Bus(String title, double price, int people){
        this.price = price;
        this.title = title;
        this.people = people;
    }
}
public class Pickup extends Car{
    /**
     * Override construct function.
     */
    public Pickup(String title, double price, int people, double goods){

        this.price = price;
        this.title = title;
        this.people = people;
        this.goods = goods;
    }
    /**
     * Override toString function.
     */
    @Override
    public String toString() {
        return  title + "\t " + price + "/天\t 载人:" + people + "/人\t 载货:" + goods + "吨";
    }
}

用户选项类:

public class SelectInformation {

    public double getDays() {
        return days;
    }

    public void setDays(double days) {
        this.days = days;
    }

    public double getSumPrice() {
        return sumPrice;
    }

    public void setSumPrice(double sumPrice) {
        this.sumPrice += sumPrice;
    }

    public int getSumPeople() {
        return sumPeople;
    }

    public void setSumPeople(int sumPeople) {
        this.sumPeople += sumPeople;
    }

    public double getSumTrunk() {
        return sumTrunk;
    }

    public void setSumTrunk(double sumTrunk) {
        this.sumTrunk += sumTrunk;
    }

    public double getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(double days, double price) {
        this.totalPrice = days * price;
    }

    public int getCarRental() {
        return carRental;
    }

    public void setCarRental(int carRental) {
        this.carRental = carRental;
    }

    public ArrayList<String> getPcarsList() {
        return pcarsList;
    }

    public void setPcarsList(ArrayList<String> pcarsList) {
        this.pcarsList = pcarsList;
    }

    public ArrayList<String> getTcarsList() {
        return tcarsList;
    }

    public void setTcarsList(ArrayList<String> tcarsList) {
        this.tcarsList = tcarsList;
    }

    public double days;
    public int carRental;
    public double sumPrice;
    public int sumPeople;
    public double sumTrunk;
    public double totalPrice;
    public ArrayList<String> pcarsList;
    public ArrayList<String> tcarsList;
}

main测试类:

public class Main {
    /**
     * Print car rental information.
     */
    public static void printInfo(Car[] cars) {
        System.out.println("您可租车的类型及其价目表:");
        System.out.println("序号  汽车名称  租金     容量");
        for (int i = 0; i < cars.length; i++) {
            System.out.println(i + 1 + ".  " + cars[i].toString());
        }
    }

    /**
     * Get user input information.
     */
    public static void scanInput(Car[] cars, int num, Scanner input, SelectInformation selInfo) {
        System.out.println("请输入您要租用的汽车数量:");
        num = input.nextInt();
        if (num < 1 || num > cars.length) {
            System.out.println("对不起,您输入的数量有误,请重新输入:");
            System.out.println("请输入您要租用的汽车数量:");
            num = input.nextInt();
        }
        selInfo.setCarRental(num);
        if (0 < selInfo.getCarRental() && selInfo.getCarRental() <= cars.length) {
            ArrayList<String> tmpPlist = new ArrayList<>();
            ArrayList<String> tmpTlist = new ArrayList<>();
            for (int i = 0; i < selInfo.getCarRental(); i++) {
                System.out.println("请输入" + (i + 1) + "车辆的序号:");
                num = input.nextInt();
                if (cars[num - 1].getPeople() != 0) {
                    tmpPlist.add(cars[num - 1].getTitle());
                    selInfo.setSumPeople(cars[num - 1].people);
                }
                if ((cars[num - 1].getPeople() != 0 && cars[num - 1].getGoods() != 0.0) || cars[num - 1].getGoods() != 0.0) {
                    tmpTlist.add(cars[num - 1].getTitle());
                    selInfo.setSumTrunk(cars[num - 1].goods);
                }
                selInfo.setSumPrice(cars[num - 1].getPrice());
            }
            selInfo.setPcarsList(tmpPlist);
            selInfo.setTcarsList(tmpTlist);
        }
        System.out.println("请输入租车天数:");
        num = input.nextInt();
        selInfo.setDays(num);
        selInfo.setTotalPrice(num, selInfo.sumPrice);

    }
    /**
     * Print user billlist.
     */
    public static void billList(SelectInformation selInfo) {
        System.out.println("您的账单:");
        System.out.println("****可载人的车有:");
        for(int i=0; i < selInfo.getPcarsList().size(); i++){
            System.out.print(selInfo.getPcarsList().get(i) + "\t");
        }
        System.out.println("\t共载人:" + selInfo.getSumPeople() + "人");
        System.out.println("****载货的车有:");
        for(int i=0; i < selInfo.getTcarsList().size(); i++){
            System.out.print(selInfo.getTcarsList().get(i) + "\t");
        }
        System.out.println("\t共载货:" + selInfo.getSumTrunk() + "吨");
        System.out.print("****租车总天数:" + selInfo.getDays() + "天");
        System.out.println("****租车总价格:" + selInfo.getTotalPrice() + "元");
    }

    public static void main(String[] args){
        Car cars[] = {new Bus("奥迪A4", 500, 4),new Bus("马自达6", 400, 4), new Pickup("皮卡雪6", 450,4,2),
            new Bus("金龙", 800,20), new Trunck("松花江", 400, 4), new Trunck("依维柯", 1000, 20)};
        Scanner input = new Scanner(System.in);
        System.out.println("欢迎使用哒哒租车系统:");
        System.out.println("您是否要租车:1是 0否");
        try {
            Integer num = input.nextInt();
            SelectInformation selInfo = new SelectInformation();
            if (num == 1) {
                printInfo(cars);
                scanInput(cars, num, input, selInfo);
                billList(selInfo);
            }
        }catch (Exception e) {
            System.out.println("输入错误,系统不识别!");
        }
        System.out.println("谢谢光临,下次再来!");
        return;
    }
}
1人推荐
随时随地看视频
慕课网APP