案例需求
- 某商场地下停车场需要一套停车计费系统,停车每小时5元钱,
- 如果不足1小时按1小时算,如果只是临时停车在半小时以内是免费的。
要求:用我们现在所学的知识点,来完成这套系统的开发.
需求分析
1.当车辆进停车场时,需要采集车辆信息,并有系统自动生成车辆编号和入场时间
采集信息用键盘录入代替:车牌号、颜色、品牌
2.汽车进场时,把车辆信息存储到car.txt文件中,格式付下(毫秒值表示入场时间)
颚A61QT2,哈佛,白色,1610450263186
颚A86U22,比亚迪,红色,1610450963186
...
3.当车辆出停车场时,由系统生成出停车场时间,并根据车牌号从car.txt文件中查找入场时间
4.计算该车辆停留的时间
停留时间 = 出场时间 - 入场时间
5.根据停留时间计算停车费用
1)如果30分钟以内,免费
2)如果超过30分钟,5元/小时
3)如果当天超过8小时,40元封顶
功能菜单
public static void main(String[] args) throws IOException {
// 输出功能界面
while (true) {
System.out.println("-----停车管理系统-----");
System.out.println("1.进去停车场 " + "2.驶出停车场 " + "3.退出系统" + "\n请输入您的选择:");
Scanner sc = new Scanner(System.in);
switch (sc.nextInt()) {
case 1:
into();
break;
case 2:
out();
break;
case 3:
System.exit(0);
default:
System.out.println("输入有误");
}
}
}
Car汽车类
public class Car {
private String id;
private String brand;
private String color;
private long intoTime;
public Car() {
}
public Car(String id, String brand, String color, long intoTime) {
this.id = id;
this.brand = brand;
this.color = color;
this.intoTime = intoTime;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public long getIntoTime() {
return intoTime;
}
public void setIntoTime(long intoTime) {
this.intoTime = intoTime;
}
}
读取文件到集合
public static ArrayList<Car> readFileToList() throws IOException {
// 创建一个空集合
ArrayList<Car> list = new ArrayList<>();
// 创建br对象
BufferedReader br = new BufferedReader(new FileReader("ParkingManageSystem/src/com/CarManageSystem/car.txt"));
//创建一个字符串对象,接收read返回的值
String s;
while ((s = br.readLine()) != null) {
// 如果读出来的内容不为空,用spilt方法切割
String[] array = s.split(",");
// 切割的返回值为字符串数组,但是intoTime的类型为long。先转换
long intoTime = Long.parseLong(array[3]);
// 将切割出来的数据封装成car对象
Car car = new Car(array[0], array[1], array[2], intoTime);
// 把对象添加到集合当中
list.add(car);
}
// 关流
br.close();
return list;
}
将集合写入文件
public static void writeListToFile(ArrayList<Car> list) throws IOException {
// 创建一个bw对象
BufferedWriter bw = new BufferedWriter(new FileWriter("ParkingManageSystem/src/com/CarManageSystem/car.txt"));
// 遍历集合,将数据写入文件中
for (Car car : list) {
bw.write(car.getId() + "," + car.getBrand() + "," + car.getColor() + "," + car.getIntoTime());
bw.newLine();
}
bw.close();
}
判断车牌是否存在
public static int getIndex(ArrayList<Car> list, String id) {
// 定义index默认为-1
int index = -1;
for (int i = 0; i < list.size(); i++) {
// 遍历集合,判断id是否相同
if (id.equals(list.get(i).getId())) {
index = i;
}
}
return index;
}
进入停车场
public static void into() throws IOException {
// 读取文件中数据然后返回一个list
ArrayList<Car> list = readFileToList();
while (true) {
System.out.println("请输入车牌:");
Scanner sc = new Scanner(System.in);
String id = sc.nextLine();
// 判断车牌号是否存在
int index = getIndex(list, id);
if (index == -1) {
System.out.println("请输入厂商:");
String brand = sc.nextLine();
System.out.println("请输入颜色:");
String color = sc.nextLine();
long intoTime = System.currentTimeMillis();
// 将录入的数据封装成Car类
Car car = new Car(id, brand, color, intoTime);
// 将对象添加到list中
list.add(car);
// 将集合写入文件
writeListToFile(list);
break;
} else {
System.out.println("车牌已存在");
}
}
}
驶出停车场
***重点在于出来的时候判断怎么收费
public static void out() throws IOException {
// 读取文件中的数据
ArrayList<Car> list = readFileToList();
long money;
System.out.println("请输入车牌:");
Scanner sc = new Scanner(System.in);
String id = sc.nextLine();
int index = getIndex(list, id);
if (index == -1) {
System.out.println("车辆不存在");
// 直接退出方法
return;
} else {
// 获取当前车辆的进去时间
long intoTime = list.get(index).getIntoTime();
// 获取驶出时间
long outTime = System.currentTimeMillis();
// 总共停车时常
long stopTime = outTime - intoTime;
// 获取天数,小时,分钟
long day = stopTime / 1000 / 3600 / 24;
long hour = stopTime / 1000 / 3600 % 24;
long min = stopTime / 1000 / 60 % 60;
System.out.println("停车时长为"+day+"天"+hour+"小时"+min+"分钟");
// 停车不足半个小时
if (day == 0 && hour == 0 && min < 30) {
money = 0;
} else {
// 开始收费后,不足一小时按照一小时选,第一种情况,min>0,所以hour要+1
if (min > 0) {
// 先判断是否达到了8小时
if ((hour + 1) >= 8) {
//达到当天封顶金额
money = day * 40 + 40;
} else {
// 没达到8小时就按照每小时5块钱算(因为这种情况是在min>0的时候,所以hour要+1)
money = day * 40 + (hour + 1) * 5;
}
} else {
// 开始收费后,不足一小时按照一小时选,第二种情况,min=0,所以hour不用+1
// 先判断是否达到了8小时
if ((hour + 1) >= 8) {
//达到当天封顶金额
money = day * 40 + 40;
} else {
// 没达到8小时就按照每小时5块钱算(因为这种情况是在min=0的时候,所以hour不要+1)
money = day * 40 + hour * 5;
}
}
}
}
System.out.println("停车费为"+money+"元");
// 删除驶出车辆
list.remove(index);
// 将集合写入文件中
writeListToFile(list);
}