您好,登錄后才能下訂單哦!
這篇文章給大家介紹使用java編寫一個停車場管理系統(tǒng),內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
現(xiàn)開發(fā)一個停車系統(tǒng),用戶根據(jù)車位停車
1)車主查看現(xiàn)在有無車位(最多100個車位),給車主隨機分配一個車位,如果該位置上有車,不能停
2)按停車時間收費,3小時以內(nèi)不收費,3小時以外,每小時2元,最大20元
3)車主可以查看自己停車的車位和收費,以及停車的時長
4)查看歷史停車記錄
管理員:
查看現(xiàn)有的剩余車位
查看已停車的車位
查看停在車位上的車以及歷史繳費
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("****歡迎進入停車系統(tǒng)****"); 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.查看現(xiàn)有停車位"); 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() + "車主" + "請選擇您要的服務(wù)"); 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; //當前停車狀態(tài) private Date stime;//停車時間 private Date etime;//停車結(jié)束 private static double allmoney; private static String lishi = ""; public void setEtime() {//停車結(jié)束時間 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; /* * 管理員業(yè)務(wù)接口 * * */ 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; /* * 管理員類實現(xiàn)類 * */ 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個車位賦予編號 和狀態(tài) } } private static CarParkingServiceimpl carparkingservice = new CarParkingServiceimpl(); public static CarParkingServiceimpl getCarparkingservice() { return carparkingservice; } public CarParkingServiceimpl() { } //停車過來的信息,車牌,還有車位,還有更改狀態(tài),利用車位編號來改變內(nèi)容 @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 //顯示所有停車系統(tǒng)的信息 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("已經(jīng)停車時間為:" + (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("已經(jīng)停車時間為:" + 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; } }
關(guān)于使用java編寫一個停車場管理系統(tǒng)就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。