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

嗒嗒租车手记(分类租车,同时租各种类型)

攻城头狮
关注TA
已关注
手记 1
粉丝 0
获赞 7

父类Car

public class Car {
    public String CarName;
    public double CarContent;
    public double CarPrice;
    public int PeopleCount;

    public String getCarName() {
        return CarName;
    }
    public void setCarName(String carName) {
        CarName = carName;
    }
    public double getCarContent() {
        return CarContent;
    }
    public void setCarContent(double carContent) {
        CarContent = carContent;
    }
    public double getCarPrice() {
        return CarPrice;
    }
    public void setCarPrice(double carPrice) {
        CarPrice = carPrice;
    }
    public int getPeopleCount() {
        return PeopleCount;
    }
    public void setPeopleCount(int peopleCount) {
        PeopleCount = peopleCount;
    }

    public Car(String carName, double carContent, double carPrice, int peopleCount) {
        CarName = carName;
        CarContent = carContent;
        CarPrice = carPrice;
        PeopleCount = peopleCount;
    }

    public Car(String carName, double carContent, double carPrice) {
        CarName = carName;
        CarContent = carContent;
        CarPrice = carPrice;
    }

    public Car(String carName,  double carPrice, int peopleCount) {
        CarName = carName;
        CarPrice = carPrice;
        PeopleCount = peopleCount;
    }
    public Car() {
        // TODO Auto-generated constructor stub
    }

}

子类PickUp

public class PickUp extends Car {
    private String CarName;
    private double CarContent;
    private double CarPrice;
    private int PeopleCount;

    public String getCarName() {
        return CarName;
    }
    public void setCarName(String carName) {
        CarName = carName;
    }
    public double getCarContent() {
        return CarContent;
    }
    public void setCarContent(double carContent) {
        CarContent = carContent;
    }
    public double getCarPrice() {
        return CarPrice;
    }
    public void setCarPrice(double carPrice) {
        CarPrice = carPrice;
    }
    public int getPeopleCount() {
        return PeopleCount;
    }
    public void setPeopleCount(int peopleCount) {
        PeopleCount = peopleCount;
    }

    public PickUp(String carName, double carContent, int peopleCount, double carPrice) {
        // TODO Auto-generated constructor stub
        this.CarName = carName;
        this.CarContent = carContent;
        this.CarPrice = carPrice;
        this.PeopleCount = peopleCount;
    }

}

子类Trunk


public class Trunk extends Car{
    private String TrunkName;
    private double TrunkContent;
    private double TrunkPrice;

    public String getCarName() {
        return TrunkName;
    }
    public void setCarName(String trunkName) {
        TrunkName = trunkName;
    }
    public double getCarContent() {
        return TrunkContent;
    }
    public void setCarContent(double trunkContent) {
        TrunkContent = trunkContent;
    }
    public double getCarPrice() {
        return TrunkPrice;
    }
    public void setCarPrice(double trunkPrice) {
        TrunkPrice = trunkPrice;
    }

    public Trunk(String trunkName, double trunkContent, double trunkPrice) {
        // TODO Auto-generated constructor stub
        this.TrunkName = trunkName;
        this.TrunkContent = trunkContent;
        this.TrunkPrice = trunkPrice;
    }

}

子类Bus

public class Bus extends Car{
    public String CarName;
    public double CarPrice;
    public int PeopleCount;

    public String getCarName() {
        return CarName;
    }
    public void setCarName(String carName) {
        CarName = carName;
    }
    public double getCarPrice() {
        return CarPrice;
    }
    public void setCarPrice(double carPrice) {
        CarPrice = carPrice;
    }
    public int getPeopleCount() {
        return PeopleCount;
    }
    public void setPeopleCount(int peopleCount) {
        PeopleCount = peopleCount;
    }

    public Bus() {
        // TODO Auto-generated constructor stub
    }

    public Bus(String carName, int peopleCount, double carPrice) {
        // TODO Auto-generated constructor stub
        this.CarName = carName;
        this.CarPrice = carPrice;
        this.PeopleCount = peopleCount;
    }

}

租车类RentCar

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class RentCar {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Car[] cars = { new PickUp("皮卡1", 1, 4, 200), new Trunk("货车1", 3, 400),
                new Bus("客车1", 33, 300), new PickUp("皮卡2", 1.5, 3, 400),
                new Trunk("货车2", 4.5, 550), new Bus("客车2", 37, 400),
                new PickUp("皮卡3", 2, 1, 500), new Trunk("货车3", 6, 700),
                new Bus("客车3", 44, 500) };
        int num;
        int reState;
        List<String> carNum = new ArrayList<String>();
        List<Car> pickup = new ArrayList<Car>();
        List<Car> trunk = new ArrayList<Car>();
        List<Car> bus = new ArrayList<Car>();

        System.out.println("------------------欢迎使用XX租车系统------------------");
        do {
        System.out.println("请选择租车类型");
        System.out.println("1:皮卡,2:货车,3:客车");
        Scanner sc = new Scanner(System.in);
        int rentType = sc.nextInt();

        System.out.println("您可租车的类型极其价目表:");
        System.out.println("序号\t" + "汽车名称\t" + "日租金\t" + "载货量\t" + "载客量\t");

        num = 1;
        for (Car curcar : cars) {
            if (rentType == 1) {
                if (curcar instanceof PickUp) {
                    if(!pickup.contains(curcar)) pickup.add(curcar);
                    System.out.println("No." + num + "\t" + curcar.getCarName()
                            + "\t" + curcar.getCarPrice() + "\t"
                            + curcar.getCarContent() + "\t"
                            + curcar.getPeopleCount() + "\t");
                    num++;
                }
            }
            if (rentType == 2) {
                if (curcar instanceof Trunk) {
                    if(!pickup.contains(curcar)) trunk.add(curcar);
                    System.out.println("No." + num + "\t" + curcar.getCarName()
                            + "\t" + curcar.getCarPrice() + "\t"
                            + curcar.getCarContent() + "\t" + "--" + "\t");
                    num++;
                }
            }
            if (rentType == 3) {
                if (curcar instanceof Bus) {
                    if(!pickup.contains(curcar)) bus.add(curcar);
                    System.out.println("No." + num + "\t" + curcar.getCarName()
                            + "\t" + curcar.getCarPrice() + "\t" + "--" + "\t"
                            + curcar.getPeopleCount() + "\t");
                    num++;
                }
            }
        }

        System.out.println("请输入要租车的序号");

        Scanner sc2 = new Scanner(System.in);
        reState = sc2.nextInt();
        do {
            carNum.add(rentType+"-"+reState);
            System.out.println("继续租车请直接输入序号,结算输入0,继续租其他类型车辆输入9999");
            sc2 = new Scanner(System.in);
            reState = sc2.nextInt();
        } while (reState != 0 && reState != 9999);
        if (reState == 0) {
            // 计算费用
            double priceCount = 0, goodsWeight = 0; int peopleCount = 0;
            Iterator<String> it = carNum.iterator();
            while (it.hasNext()) {
                String[] choseCar = it.next().toString().split("-");
                if ( Integer.parseInt(choseCar[0])==1) {
                    priceCount+=pickup.get(Integer.parseInt(choseCar[1])-1).getCarPrice();
                    peopleCount+=pickup.get(Integer.parseInt(choseCar[1])-1).getPeopleCount();
                    goodsWeight+=pickup.get(Integer.parseInt(choseCar[1])-1).getCarContent();
                } else if ( Integer.parseInt(choseCar[0])==2) {
                    priceCount+=trunk.get(Integer.parseInt(choseCar[1])-1).getCarPrice();
                    goodsWeight+=trunk.get(Integer.parseInt(choseCar[1])-1).getCarContent();
                } else if ( Integer.parseInt(choseCar[0])==3) {
                    priceCount+=bus.get(Integer.parseInt(choseCar[1])-1).getCarPrice();
                    peopleCount+=pickup.get(Integer.parseInt(choseCar[1])-1).getPeopleCount();
                }
            }
            System.out.println("总金额:"+ priceCount);
            System.out.println("总载货量:"+ goodsWeight);
            System.out.println("总载客数:"+ peopleCount);
            System.out.println("总租车数:"+ carNum.size());
            System.out.println("------------------欢迎下次再来------------------");
        } else  if(rentType == 1 && (reState > pickup.size() || reState < 0) && reState != 9999){
            System.out.println("------------------请输入有效数字------------------");
        } else  if(rentType == 2 && (reState > trunk.size() || reState < 0) && reState != 9999){
            System.out.println("------------------请输入有效数字------------------");
        } else  if(rentType == 3 && (reState > bus.size() || reState < 0) && reState != 9999){
            System.out.println("------------------请输入有效数字------------------");
        }
    } while (reState == 9999);

    }
}

这段代码里没有计算天数....想弄的可以试一下!

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