Java模拟停车场管理

简易模拟停车场管理

并不是非常地精致,各位大佬有优化建议尽管提。

1.Car.java

import java.util.Objects;

public class Car {
    /**
     * number:车牌号
     * location:车位号
     * time_in:进场时间
     * time_out:出场时间
     */
    private String number;
    private String location;
    private String time_in;
    private String time_out;

    public Car(String number, String location, String time_in, String time_out) {
        this.number = number;
        this.location = location;
        this.time_in = time_in;
        this.time_out = time_out;
    }

    public Car() {
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getTime_in() {
        return time_in;
    }

    public void setTime_in(String time_in) {
        this.time_in = time_in;
    }

    public String getTime_out() {
        return time_out;
    }

    public void setTime_out(String time_out) {
        this.time_out = time_out;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Car car = (Car) o;
        return Objects.equals(number, car.number);
    }
}


2.Parking.java

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

public class Parking {
    CarDao carDao;

    public Parking(CarDao carDao) {
        this.carDao = carDao;
    }

    public void setCarDao(CarDao carDao){
        this.carDao = carDao;
    }

   //停车,并记录车辆信息
    public void ParkCar(){
        System.out.println("请输入车排号");
        Scanner input = new Scanner(System.in);
        String number = input.nextLine();
        if(carDao.getCar(number) == null){
            Date time_in = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:MM:ss");
            String nowtime = sdf.format(time_in);
            Car car = new Car();
            car.setNumber(number);
            car.setTime_in(nowtime);
            car.setTime_out("未知");
            carDao.addCar(car);
        }else{
            System.out.println("这辆车已经在停车场");
        }
    }

    //计算费用,不超过一天每小时3块钱,超过一天每天30块钱,一小时以内免费。
    public int Cost_hours(Car car){
        String time1 = car.getTime_in();
        String time2 = car.getTime_out();
        long dif = 0;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:MM:ss");
        try {
            Date date1 = format.parse(time1);
            Date date2 = format.parse(time2);
            dif = date2.getTime() - date2.getTime();
            long diff = TimeUnit.HOURS.convert(dif,TimeUnit.MILLISECONDS);
            long diff2 = TimeUnit.DAYS.convert(dif,TimeUnit.MILLISECONDS);
            if(diff <= 1){
                return 0;
            }else if(diff <= 24){
                return (int) (diff * 3);
            }else {
                //假设每天30块钱
                return (int) (diff2 * 30);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    //离开停车场
    public void Leave(){
        System.out.println("请输入车排号");
        Scanner input = new Scanner(System.in);
        String number = input.nextLine();
        Car car ;
        if((car = carDao.getCar(number))!= null){
            carDao.DelCar(car);
            Print(car);
        }else {
            System.out.println("停车场无此车!");
        }
    }

    //查询车辆是否存在
    public void isPresented(String number){
        Car car;
        if((car = carDao.getCar(number)) == null){
            System.out.println("此车不在场!");
        }else {
            System.out.println("此车在场!");
        }
    }

    //打印车辆信息
    public void Print(Car car){
        String number = car.getNumber();
        String location = car.getLocation();
        String time_in = car.getTime_in();
        String time_out = car.getTime_out();
        int cost = Cost_hours(car);
        System.out.println("车牌号\t车位号码\t\t\t入场时间\t\t\t\t\t出场时间\t\t\t\t\t费用\t");
        System.out.println(number+"\t"+location+"\t\t\t"+time_in+"\t\t\t"+time_out+"\t\t\t"+cost);
        System.out.println("一路顺风");
    }

}


3.CarDao.java

public interface CarDao {
    public static final int MAXSIZE = 100;

    public boolean addCar(Car car);

    public Car getCar(String number);

    public int Rest();

    public boolean DelCar(Car car);
}

4.CarDaoArray.java

import java.text.SimpleDateFormat;
import java.util.*;

public class CarDaoArray implements CarDao{
    private List<Car> cars = new ArrayList<>();
    private Map<String,Boolean> No = new HashMap<>();//存储车位号码和这个位置是否被占
    private int count = 0;
    public CarDaoArray() {
        //初始化车位号码
        for (int i = 0; i < MAXSIZE; i++){
            No.put("0" + i,false);
        }
    }

    @Override
    public boolean addCar(Car car) {
        //记录车牌和入场时间
        if(count < MAXSIZE){
            for(Map.Entry<String,Boolean> entry : No.entrySet()){
                if(!entry.getValue()){
                    car.setLocation(entry.getKey());
                    entry.setValue(true);
                    cars.add(car);
                    break;
                }
            }
            count++;
            System.out.println("剩余车位还有" + Rest() + "个");
            return true;
        }else{
            System.out.println("车位已满");
            return false;
        }

    }

    @Override
    public Car getCar(String number) {
        for(Car car : cars){
            if(number.equals(car.getNumber())){
                return car;
            }
        }
        return null;
    }

    //显示剩余车辆
    public int Rest(){
        return MAXSIZE - count;
    }

    @Override
    public boolean DelCar(Car car) {
        if(count > 0){
            for(Map.Entry<String,Boolean> entry : No.entrySet()){
                if(car.getLocation().equals(entry.getKey())){
                    entry.setValue(false);
                }
            }
            cars.remove(car);
            count --;
            Date time_out = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:MM:ss");
            String nowtime = sdf.format(time_out);
            car.setTime_out(nowtime);
            return true;
        }
        return false;
    }
}


之后找时间重构一下(我承认这没啥代码品味)。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄某人学不完了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值