简易模拟停车场管理
并不是非常地精致,各位大佬有优化建议尽管提。
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;
}
}
之后找时间重构一下(我承认这没啥代码品味)。