java实现停车场系统
程序员文章站
2022-04-29 17:31:07
本文实例为大家分享了java实现停车场系统的具体代码,供大家参考,具体内容如下现开发一个停车系统,用户根据车位停车1)车主查看现在有无车位(最多100个车位),给车主随机分配一个车位,如果该位置上有车...
本文实例为大家分享了java实现停车场系统的具体代码,供大家参考,具体内容如下
现开发一个停车系统,用户根据车位停车
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; } }
更多学习资料请关注专题《管理系统开发》。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。