共计 10451 个字符,预计需要花费 27 分钟才能阅读完成。
现开发一个停车零碎, 用户依据车位停车
1)车主查看当初有无车位 (最多 100 个车位), 给车主随机调配一个车位, 如果该地位上有车, 不能停
2) 按停车工夫免费,3 小时以内不免费,3 小时以外, 每小时 2 元, 最大 20 元
3) 车主能够查看本人停车的车位和免费, 以及停车的时长
4) 查看历史停车记录
管理员:
查看现有的残余车位
查看已停车的车位
查看停在车位上的车以及历史缴费
Client
package com.gem.client;
import java.util.Scanner;
import com.gem.entity.Admin;
import com.gem.entity.User;
import com.gem.service.impl.AdminServiceImpl;
import com.gem.service.impl.CarParkingServiceimpl;
import com.gem.service.impl.UserServiceimpl;
public class Client {public static void main(String[] args) {CarParkingServiceimpl carparkingservice = CarParkingServiceimpl.getCarparkingservice();
UserServiceimpl userserviceimpl = UserServiceimpl.getUserserviceimpl();
Scanner scanner = new Scanner(System.in);
Menu1(carparkingservice, userserviceimpl, scanner);
}
private static void Menu1(CarParkingServiceimpl carparkingservice, UserServiceimpl userserviceimpl,
Scanner scanner) {while (true) {System.out.println("**** 欢送进入停车零碎 ****");
System.out.println("***1. 请输入您的车牌 ***");
System.out.println("***2. 管理员");
System.out.println("***3. 退出 ****");
int choice = Integer.parseInt(scanner.nextLine().trim());
switch (choice) {
case 1:
System.out.println("请输入您的车牌");
String chepai = scanner.nextLine().trim();
User a = userserviceimpl.login(new User(chepai));
if (a != null) {System.out.println("车牌注销胜利");
Menu2(carparkingservice, userserviceimpl, scanner, a);
} else {System.out.println("车牌注销失败");
}
break;
case 2:
Menu_Manger(carparkingservice, userserviceimpl);
default:
break;
}
}
}
// 管理员的登录注册
private static void Menu_Manger(CarParkingServiceimpl carparkingservice, UserServiceimpl userserviceimpl) {AdminServiceImpl adminserviceimpl = AdminServiceImpl.getAdminSeriver();
while (true) {System.out.println("1. 管理员登录");
System.out.println("2. 管理员注册");
System.out.println("返回上一级");
Scanner scanner = new Scanner(System.in);
int chose = Integer.parseInt(scanner.nextLine().trim());
switch (chose) {
case 1:
System.out.println("请输出管理员账号");
String name = scanner.nextLine().trim();
System.out.println("请输出管理员明码");
String password = scanner.nextLine().trim();
if (adminserviceimpl.login(name, password) != null) {System.out.println("登录胜利");
Menu_Manger2(carparkingservice, userserviceimpl, adminserviceimpl);// 进行管理员操作
} else {System.out.println("登录失败");
return;
}
break;
case 2:
System.out.println("请输出注册账号");
name = scanner.nextLine().trim();
System.out.println("请输出注册明码");
password = scanner.nextLine().trim();
adminserviceimpl.register(new Admin(name, password));
break;
case 3:
return;
default:
return;
}
}
}
// 管理员操作
private static void Menu_Manger2(CarParkingServiceimpl carparkingservice, UserServiceimpl userserviceimpl,
AdminServiceImpl adminserviceimpl) {while (true) {System.out.println("*** 欢送进入管理员操作 ***");
System.out.println("1. 查看现有停车位");
System.out.println("2. 查看停车位上的车以及缴费");
System.out.println("3. 返回上一级");
Scanner scanner = new Scanner(System.in);
int chose = Integer.parseInt(scanner.nextLine().trim());
switch (chose) {
case 1:
carparkingservice.showlist();// 显示目前所有的空的车位
break;
case 2:
carparkingservice.show();
break;
case 3:
return;
default:
System.out.println("输出谬误");
break;
}
}
}
private static void Menu2(CarParkingServiceimpl carparkingservice, UserServiceimpl userserviceimpl, Scanner scanner,
User a) {while (true) {System.out.println("欢送车牌为:" + a.getCarid() + "车主" + "请抉择您要的服务");
System.out.println("1. 停车");
System.out.println("2. 来到并缴费");
System.out.println("3. 查看停车车位");
System.out.println("4. 停车时长");
System.out.println("0. 返回上一级");
int chose = Integer.parseInt(scanner.nextLine().trim());
switch (chose) {
case 1:
if (carparkingservice.chack(a.getCarid())) {break;}
while (true) {int random = (int) (Math.random() * 10); // 随机一个车位
if (carparkingservice.parkcar(random, a.getCarid())) {System.out.println("停车胜利, 停车地位为:" + random);
break;
} else {System.out.println("以后车位有车");
}
}
break;
case 2:
if (carparkingservice.leavecar(a.getCarid())) {System.out.println("请缴费");
} else {System.out.println("该车未停");
}
break;
case 3:
carparkingservice.showlist();// 显示目前所有的空的车位
break;
case 4:
carparkingservice.showcartime(a.getCarid());// 显示停车时长
break;
case 0:
return;
default:
System.out.println("输出谬误");
break;
}
}
}
}
Admin
package com.gem.entity;
import java.util.ArrayList;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class Admin {
private String username;
private String password;
private double money;
private ArrayList<String> list = new ArrayList<String>();
@Override
public String toString() {return username + "t" + password;}
public Admin(String username, String password) {super();
this.username = username;
this.password = password;
}
}
CarParking
package com.gem.entity;
import java.text.SimpleDateFormat;
import java.util.Date;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
@Getter
public class CarParking { // 车位
public static String getLishi() {return lishi;}
public static void setLishi(String lishi) {CarParking.lishi = lishi;}
private int id; // 停车的编号
private double money;// 钱的计算
private String state; // 以后停车状态
private Date stime;// 停车工夫
private Date etime;// 停车完结
private static double allmoney;
private static String lishi = "";
public void setEtime() {// 停车完结工夫
etime = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String time = dateFormat.format(this.etime);
}
public void setStime() {// 停车开始工夫
stime = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String time1 = dateFormat.format(this.stime);
}
public String toString() {return id + "t" + money + "t" + stime;}
public CarParking(int id, String state) {
this.id = id;
this.state = state;
}
public void CarPark(int id, String carid) {
// TODO Auto-generated method stub
this.id = id;
this.state = carid;
}
public int getId() {return id;}
public void setId(int id) {this.id = id;}
public double getMoney() {return money;}
public void setMoney(double money) {this.money = money;}
public String getState() {return state;}
public void setState(String state) {this.state = state;}
public static double getAllmoney() {return allmoney;}
public static void setAllmoney(double allmoney) {CarParking.allmoney = allmoney;}
}
User
package com.gem.entity;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@AllArgsConstructor
@Setter
@NoArgsConstructor
@Getter
public class User {
private String carid; // 车牌登录
@Override
public String toString() {return "车牌为" + carid;}
}
AdminService
package com.gem.service;
/*
* 管理员业务接口
*
* */
import com.gem.entity.Admin;
public interface AdminService {
// 登录
Admin login(String username, String password);
// 注册
boolean register(Admin admin);
}
AdminServiceImpl
package com.gem.service.impl;
import java.util.ArrayList;
import com.gem.entity.Admin;
import com.gem.service.AdminService;
/*
* 管理员类实现类
* */
public class AdminServiceImpl implements AdminService {private ArrayList<Admin> adminList = new ArrayList<Admin>();
// 单例饿汉模式
private static final AdminServiceImpl adminSeriver = new AdminServiceImpl();
public static AdminServiceImpl getAdminSeriver() {return adminSeriver;}
private AdminServiceImpl() {}
@Override // 管理员登录
public Admin login(String username, String password) {// 登录
for (Admin admin : adminList) {if (admin.getUsername().equals(username) && admin.getPassword().equals(password)) {return admin;}
}
return null;
}
@Override // 增加管理员
public boolean register(Admin admin) {boolean flag = adminList.add(admin);// 增加管理员
return flag;
}
}
CarParkingService
package com.gem.service;
public interface CarParkingService {boolean parkcar(int id, String carid); // 停车
void showlist();// 显示所有列表
boolean leavecar(String carid); // 来到
}
CarParkingServiceimpl
package com.gem.service.impl;
import java.util.ArrayList;
import java.util.Date;
import com.gem.entity.CarParking;
import com.gem.service.CarParkingService;
public class CarParkingServiceimpl implements CarParkingService {private ArrayList<CarParking> carpaking = new ArrayList<CarParking>();
{for (int i = 1; i <= 10; i++) {carpaking.add(new CarParking(i, "以后车位为空")); // 为 100 个车位赋予编号 和状态
}
}
private static CarParkingServiceimpl carparkingservice = new CarParkingServiceimpl();
public static CarParkingServiceimpl getCarparkingservice() {return carparkingservice;}
public CarParkingServiceimpl() {}
// 停车过去的信息, 车牌, 还有车位, 还有更改状态, 利用车位编号来扭转内容
@Override
public boolean parkcar(int id, String carid) {
// TODO Auto-generated method stub
for (CarParking car : carpaking) {if (id == car.getId() && car.getState().equals("以后车位为空")) {car.setState(carid);
car.setStime();
return true; // 停车胜利
}
}
return false;// 以后车位有车
}
@Override // 显示所有停车零碎的信息
public void showlist() {
// TODO Auto-generated method stub
for (CarParking car : carpaking) {System.out.println(car.getId() + "t" + car.getState());
}
}
public void showcartime(String carid) {
// TODO Auto-generated method stub
for (CarParking car : carpaking) {if (car.getState().equals(carid)) {car.setEtime();
Date time1 = car.getEtime();// 失去以后的查问工夫
Date time2 = car.getStime();// 失去开始工夫
System.out.println("车位为:" + car.getId() + "车牌为" + car.getState());
System.out.println("停车工夫为:" + car.getStime());
System.out.println("以后工夫为:" + car.getEtime());
System.out.println("曾经停车工夫为:" + (time1.getTime() - time2.getTime()) / (1000 * 60 * 60));
}
}
}
public boolean leavecar(String carid) {
// TODO Auto-generated method stub
for (CarParking car : carpaking) {if (car.getState().equals(carid)) {car.setEtime();
Date time1 = car.getEtime();// 失去以后的查问工夫
Date time2 = car.getStime();// 失去开始工夫
long t = (time1.getTime() - time2.getTime()) / (1000 * 60 * 60);
System.out.println("车位为:" + car.getId() + "车牌为" + car.getState());
System.out.println("停车工夫为:" + car.getStime());
System.out.println("以后工夫为:" + car.getEtime());
System.out.println("曾经停车工夫为:" + t);
if (t <= 3) {car.setMoney(0);
car.setAllmoney(car.getAllmoney() + car.getMoney()); // 钱累加
} else if (t > 3 && t <= 13) {car.setMoney(2 * (t - 3));
car.setAllmoney(car.getAllmoney() + car.getMoney()); // 钱累加
} else if (t > 13) {car.setMoney(20);
car.setAllmoney(car.getAllmoney() + car.getMoney()); // 钱累加
}
car.setLishi(car.getLishi() + "车位:" + car.getId() + "支出金额为:" + car.getMoney() + "n");
System.out.println("停车费为:" + car.getMoney());
car.setState("以后车位为空");
return true;
}
}
return false;
}
public void show() {
// TODO Auto-generated method stub
for (CarParking car : carpaking) {System.out.println(car.getLishi());
return;
}
}
public boolean chack(String carid) {for (CarParking car : carpaking) {if (car.getState().equals(carid)) {System.out.println("车辆正在停使中");
return true;
}
}
return false;
}
}
UserService
package com.gem.service;
import com.gem.entity.User;
public interface UserService {
// 用车牌登录
User login(User a);
}
UserServiceimpl
package com.gem.service.impl;
import java.util.ArrayList;
import com.gem.entity.User;
import com.gem.service.UserService;
public class UserServiceimpl implements UserService {private ArrayList<User> user = new ArrayList<User>();
private static UserServiceimpl userserviceimpl = new UserServiceimpl(); // 单例
private UserServiceimpl() {}
public static UserServiceimpl getUserserviceimpl() {return userserviceimpl;}
@Override
public User login(User a) { // 把车牌录入
user.add(a);
return a;
}
}
起源:blog.csdn.net/weixin_43951778
PS:如果感觉我的分享不错,欢送大家顺手点赞、在看。
往期举荐
很快呀,马保国语录“耗子尾汁”被抢注商标
IntelliJ IDEA 超实用技巧分享,大大提高效率
64 岁 Python 之父自称退休太无聊?正式退出微软搞开源!
因为“扫一扫”性能,支付宝被起诉了!
2020 年 10 月 GitHub 上最热门的开源我的项目
11 月全国程序员平均工资出炉,网友:我又丢了同行的脸~
图解计算机网络(1):带你搞懂 5 个基础知识
别乱提交代码了,你必须晓得的 Git 分支开发标准!
5 千字帮你搞定 JAVA 汇合类 / 枚举
Java 罕用类库,用好大大提高生产力
正文完