用文件流实现一个停车场系统
//主方法函数
package app;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import dao.ICarDao;
import entity.Car;
import util.DaoFactory;
public class ParkingSystem {
private static boolean flag = false;
private static Car car = null;
public static void main(String[] args) throws Exception {
ICarDao carDao = DaoFactory.getDaoFactory();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) {
menu();
String ss = br.readLine();
switch (ss) {
case "1":
car = inputCarInfo(br);
if (car != null) {
while (true) {
Date date1 = inputDate(br);
car.setInDate(date1);
if (carDao.add(car, date1)) {
System.out.println("停车成功");
break;
} else
System.out.println("停车失败");
}
}
break;
case "2":
while (true) {
Car car2 = findCar(br);
if (car2 != null) {
Date date2 = inputDate(br);
System.out.println(car2);
if (date2 != null && car2.isBigger(date2)) {
if (date2 !=null&& carDao.delete(car2, date2)) {
System.out.println(date2);
System.out.println("您的停车时间为:" + car2.showTime() + "分钟");
System.out.println("您一共消费了" + car2.charge() + "元");
System.out.println("取车成功");
break;
}
else
System.out.println("输入有误");
} else {
System.out.println("取车时间有误,请重新输入");
}
}else
System.out.println("查无此车");
}
break;
case "3":
Object[] obj = carDao.show().getArray();
for (Object object : obj) {
if (object != null)
System.out.println(object);
}
break;
case "0":
System.out.println("欢迎下次使用");
System.exit(0);
break;
default:
break;
}
}
}
public static void menu() {
System.out.println("===============欢迎使用================");
System.out.println("1------停车 2------取车");
System.out.println("3------查看停车情况 0------退出系统");
System.out.println("=====================================");
}
// 根据车牌号找车
private static Car findCar(BufferedReader br) {
Car car = null;
ICarDao carDao = DaoFactory.getDaoFactory();
String str = inputString(br, "车牌号", 7, 8);
Object[] array = carDao.show().getArray();
if (array != null && array instanceof Car[]) {
Car[] tmp = (Car[]) array;
for (int i = 0; i < tmp.length; i++) {
if (tmp[i] != null && tmp[i].getPlate().equals(str)) {
car = tmp[i];
break;
}
}
}
return car;
}
// 输入车的基本信息
public static Car inputCarInfo(BufferedReader br) {
Car car1 = new Car();
String brand = inputString(br, "品牌", 1, 10);
String color = inputString(br, "颜色", 2, 10);
String plate = inputString(br, "车牌号", 7, 8);
if (!carIsExists(plate)) {
car1.setColor(color);
car1.setBrand(brand);
car1.setPlate(plate);
} else {
car1 = null;
System.out.println("我真的会谢,此车牌号已存在");
}
return car1;
}
// 查询车牌号是否唯一
private static boolean carIsExists(String str) {
ICarDao carDao = DaoFactory.getDaoFactory();
boolean res = false;
Object[] array = carDao.show().getArray();
if (array != null && array instanceof Car[]) {
Car[] tmp = (Car[]) array;
for (int i = 0; i < tmp.length; i++) {
if (tmp[i] != null && str.equals(tmp[i].getPlate())) {
res = true;
break;
}
}
}
return res;
}
public static String inputString(BufferedReader br, String name, int... para) {
String ss = null;
System.out.println("请输入" + name + ":");
while (true) {
try {
ss = br.readLine();
if (ss != null && ss.trim().length() > 0) {
if (para != null && para.length > 0) {
flag = ss.trim().length() >= para[0];
if (flag)
flag = ss.trim().length() < para[1];
} else {
flag = true;
}
}
} catch (Exception e) {
e.printStackTrace();
break;
}
if (flag)
break;
System.out.println("输入有误,请重新输入");
}
return ss;
}
// 输入日期
public static Date inputDate(BufferedReader br) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
System.out.println("请输入格式为(yyyy/MM/dd HH:mm)的日期");
Date date = null;
try {
String str = br.readLine();
date = sdf.parse(str);
} catch (Exception e) {
System.out.println("输入有误,请重新输入");
}
return date;
}
}
//定义文件位置
package dao;
class Constants{
public static final String FILE_NAME = "src/data/car.txt";
public static final String TEMP_NAME = "src/data/car_tmp.txt";
}
//定义接口实现CURD
package dao;
import java.util.Date;
import entity.Car;
import util.ArrayList;
public interface ICarDao {
boolean delete(Car car1,Date date1);
ArrayList show();
void update();
boolean add(Car car1,Date date1);
}
//接口的实现类
package dao;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
import entity.Car;
import util.ArrayList;
public class CarDaoImplements implements ICarDao {
private static boolean flag = false;
@Override
public boolean add(Car car1, Date date1) {
File oldFile = new File(Constants.FILE_NAME);
File tmpFile = new File(Constants.TEMP_NAME);
if (date1 != null) {
car1.setInDate(date1);
if (oldFile.exists()) {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(oldFile));
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(tmpFile))) {
while (true) {
try {
Object obj = ois.readObject();
if (obj != null && obj instanceof Car) {
Car tmp = (Car) obj;
oos.writeObject(tmp);
}
} catch (EOFException e) {
break;
}
}
oos.writeObject(car1);
flag = true;
} catch (Exception e) {
e.printStackTrace();
}
} else {
try (ObjectOutputStream oos2 = new ObjectOutputStream(new FileOutputStream(oldFile))) {
oos2.writeObject(car1);
flag = true;
} catch (Exception e) {
e.printStackTrace();
}
}
}
if (oldFile.exists() && tmpFile.exists()) {
oldFile.delete();
tmpFile.renameTo(oldFile);
}
return flag;
}
@Override
public boolean delete(Car car1, Date date1) {
File oldFile = new File(Constants.FILE_NAME);
File tmpFile = new File(Constants.TEMP_NAME);
if (date1 != null)
car1.setOutDate(date1);
if (oldFile.exists()) {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(oldFile));
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(tmpFile))) {
while (true) {
try {
Object obj = ois.readObject();
if (obj != null && obj instanceof Car) {
Car tmp = (Car) obj;
if (!tmp.equals(car1))
oos.writeObject(tmp);
else {
flag = true;
}
}
} catch (EOFException e) {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
if (oldFile.exists() && tmpFile.exists()) {
oldFile.delete();
tmpFile.renameTo(oldFile);
}
return flag;
}
@Override
public ArrayList show() {
ArrayList list = new ArrayList();
File oldFile = new File(Constants.FILE_NAME);
if (oldFile.exists()) {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(oldFile))) {
while (true) {
Object obj = ois.readObject();
if (obj != null && obj instanceof Car) {
Car tmp = (Car) obj;
list.add(tmp);
}
}
} catch (EOFException e) {
System.out.println("文件读取完成");
} catch (Exception e) {
e.printStackTrace();
}
}
return list;
}
@Override
public void update() {
// TODO Auto-generated method stub
}
}
//再创建一个data包存文件
//定义出汽车类
package entity;
import java.io.Serializable;
import java.util.Date;
import java.util.Objects;
public class Car implements Serializable {
private static final long serialVersionUID = 1L;
private String brand;// 品牌
private String color;// 颜色
private String plate;// 车牌号
private Date inDate;// 进场时间
private Date outDate;// 出场时间
// 30分钟内免费,过后每小时3元,超过1小时后再超过15分钟按一小时计算,15分钟以内不额外计费
private int parkingCharge;// 停车费
// 收费
public long charge() {
long time = showTime();
int time1 = (int)(time % 60);
int time2 = (int)(time/60);
if (showTime() > 0) {
if (time <= 30) {
this.parkingCharge = 0;
} else if(time<=60||time1==0){
this.parkingCharge = 3;
}else if(time1 <= 15) {
this.parkingCharge = 3 * time2;
}else {
this.parkingCharge = 3 * time2 + 3;
}
}
// else
// throw new IllegalArgumentException();
return this.parkingCharge;
}
// 显示停车时间(分钟数)
public long showTime() {
long timeDifference = -1;
long time = this.getOutDate().getTime() - this.getInDate().getTime();
if (time > 0) {
timeDifference = this.getOutDate().getTime() - this.getInDate().getTime();
}
return timeDifference / 1000 / 60;
}
public boolean isBigger(Date date1) {
this.setOutDate(date1);
return this.getOutDate().getTime()>this.getInDate().getTime();
}
// ===========================================
public Car() {
}
// public int getParkingCharge() {
// return parkingCharge;
// }
//
// public void setParkingCharge(int parkingCharge) {
// this.parkingCharge = parkingCharge;
// }
public String getBrand() {
return brand;
}
public Car(String brand, String color, String plate) {
super();
this.brand = brand;
this.color = color;
this.plate = plate;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getPlate() {
return plate;
}
public void setPlate(String plate) {
this.plate = plate;
}
public Date getInDate() {
return inDate;
}
public void setInDate(Date inDate) {
this.inDate = inDate;
}
public Date getOutDate() {
return outDate;
}
public void setOutDate(Date outDate) {
this.outDate = outDate;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", color=" + color + ", plate=" + plate + ", inDate=" + inDate + "]";
}
@Override
public int hashCode() {
return Objects.hash(plate);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Car other = (Car) obj;
return Objects.equals(plate, other.plate);
}
}
//根据设计模式使用单例模式
package util;
import dao.CarDaoImplements;
import dao.ICarDao;
public class DaoFactory {
private DaoFactory() {
}
private static ICarDao carDao = new CarDaoImplements();
public static ICarDao getDaoFactory() {
return carDao;
}
}
//工具类
package util;
import java.util.Arrays;
import entity.Car;
public class ArrayList {
private Car[] array;
private int count;
public void add(Car car1) {
array[count++] = car1;
if(count>=array.length)
extendArray();
}
private void extendArray() {
Car[] res = new Car[array.length * 3 /2];
// for (int i = 0; i < count; i++)
// res[i] = array[i];
System.arraycopy(array, 0, res, 0, count);
this.array = res;
}
//==============================================================
@Override
public String toString() {
return Arrays.toString(array);
}
public Car[] getArray() {
return array;
}
public void setArray(Car[] array) {
this.array = array;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public ArrayList(int len) {
super();
array = new Car[len];
}
public ArrayList() {
this(5);
}
}