Java开发停车场系统,非常适合刚学者!
关注我们,设为星标,每天7:30不见不散,架构路上与您共享 回复"架构师"获取资源
现开发一个停车系统,用户根据车位停车
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;
public class Admin {
private String username;
private String password;
private double money;
private ArrayList<String> list = new ArrayList<String>();
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;
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;
public class User {
private String carid; //车牌登录
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
adminList = new ArrayList (); //单例饿汉模式
private static final AdminServiceImpl adminSeriver = new AdminServiceImpl();
public static AdminServiceImpl getAdminSeriver() {
return adminSeriver;
}
private AdminServiceImpl() {
}
//管理员登录
public Admin login(String username, String password) {//登录
for (Admin admin : adminList) {
if (admin.getUsername().equals(username) && admin.getPassword().equals(password)) {
return admin;
}
}
return null;
}
//添加管理员
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
carpaking = new ArrayList ();
{
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 = new ArrayList ();
private static UserServiceimpl userserviceimpl = new UserServiceimpl(); //单例
private UserServiceimpl() {
}
public static UserServiceimpl getUserserviceimpl() {
return userserviceimpl;
}
public User login(User a) { //把车牌录入
user.add(a);
return a;
}
}
这些年小编给你分享过的干货
《不花钱的IDEA 2020.3 最新激活教程,有效期到2100年!》
转发在看就是最大的支持❤️
评论