發愁沒項目?用Java開發一個停車場系統【附源碼】

現開發一個停車系統,用戶根據車位停車java


1)車主查看如今有無車位(最多100個車位),給車主隨機分配一個車位,若是該位置上有車,不能停
2)按停車時間收費,3小時之內不收費,3小時之外,每小時2元,最大20元
3)車主能夠查看本身停車的車位和收費,以及停車的時長
4)查看歷史停車記錄程序員

管理員:
查看現有的剩餘車位
查看已停車的車位
查看停在車位上的車以及歷史繳費網絡








Clientdom

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;
 }
 }
 }
}

Adminide

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;
 }
}

CarParkingthis

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;
 }
}

Userspa

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.net

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;
 }
}

CarParkingService3d

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 經常使用類庫,用好大大提升生產力

相關文章
相關標籤/搜索