Backend Development 14 min read

Java Parking System with User and Admin Management

This article presents a complete Java implementation of a parking system, detailing user and admin login, random parking slot allocation, time‑based fee calculation, and management functions, accompanied by full source code for entities, services, and client interaction.

IT Xianyu
IT Xianyu
IT Xianyu
Java Parking System with User and Admin Management

The document describes a console‑based parking management application written in Java. It outlines functional requirements such as displaying available slots (up to 100), assigning a random free slot to a car, charging based on parking duration (free for the first 3 hours, then 2 CNY per hour up to a maximum of 20 CNY), and allowing users to view their slot, fee, and parking history. Administrators can view remaining slots, occupied slots, and historical payments.

Client class provides the entry point, presenting a menu for car owners and administrators, handling user input via Scanner , and delegating actions to service implementations.

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);
          break;
        default:
          break;
      }
    }
  }

  // ... (other menu methods omitted for brevity)
}

Admin entity stores administrator credentials and a list of associated data.

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
list = new ArrayList<>();

  @Override
  public String toString() {
    return username + "\t" + password;
  }

  public Admin(String username, String password) {
    this.username = username;
    this.password = password;
  }
}

CarParking entity models a parking slot, tracking its ID, state (occupied or free), start/end times, and fee calculations.

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 {
  private int id; // slot number
  private double money; // fee for this slot
  private String state; // "当前车位为空" or car plate
  private Date stime; // parking start time
  private Date etime; // parking end time
  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);
  }

  @Override
  public String toString() {
    return id + "\t" + money + "\t" + stime;
  }

  public CarParking(int id, String state) {
    this.id = id;
    this.state = state;
  }

  // ... other getters/setters omitted for brevity
}

Service interfaces define the contract for admin, user, and parking operations.

package com.gem.service;

import com.gem.entity.Admin;

public interface AdminService {
  Admin login(String username, String password);
  boolean register(Admin admin);
}
package com.gem.service;

import com.gem.entity.User;

public interface UserService {
  User login(User a);
}
package com.gem.service;

public interface CarParkingService {
  boolean parkcar(int id, String carid);
  void showlist();
  boolean leavecar(String carid);
}

Implementations use singleton (eager) patterns and in‑memory ArrayList collections to store data.

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() {}

  @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) {
    return adminList.add(admin);
  }
}
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;
  }

  @Override
  public User login(User a) {
    user.add(a);
    return a;
  }
}
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<>();

  { // initialize 10 slots as example (original description mentions up to 100)
    for (int i = 1; i <= 10; i++) {
      carpaking.add(new CarParking(i, "当前车位为空"));
    }
  }

  private static CarParkingServiceimpl carparkingservice = new CarParkingServiceimpl();

  public static CarParkingServiceimpl getCarparkingservice() {
    return carparkingservice;
  }

  @Override
  public boolean parkcar(int id, String carid) {
    for (CarParking car : carpaking) {
      if (id == car.getId() && car.getState().equals("当前车位为空")) {
        car.setState(carid);
        car.setStime();
        return true; // parked successfully
      }
    }
    return false; // slot occupied
  }

  @Override
  public void showlist() {
    for (CarParking car : carpaking) {
      System.out.println(car.getId() + "\t" + car.getState());
    }
  }

  @Override
  public boolean leavecar(String carid) {
    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);
        if (t <= 3) {
          car.setMoney(0);
        } else if (t <= 13) {
          car.setMoney(2 * (t - 3));
        } else {
          car.setMoney(20);
        }
        CarParking.setAllmoney(CarParking.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 showcartime(String carid) {
    for (CarParking car : carpaking) {
      if (car.getState().equals(carid)) {
        car.setEtime();
        System.out.println("车位为:" + car.getId() + "车牌为" + car.getState());
        System.out.println("停车时间为:" + car.getStime());
        System.out.println("当前时间为:" + car.getEtime());
        System.out.println("已经停车时间为:" + (car.getEtime().getTime() - car.getStime().getTime()) / (1000 * 60 * 60));
      }
    }
  }

  public boolean chack(String carid) {
    for (CarParking car : carpaking) {
      if (car.getState().equals(carid)) {
        System.out.println("车辆正在停使中");
        return true;
      }
    }
    return false;
  }
}

The overall architecture follows a simple layered design: entities represent data models, service interfaces define business operations, and concrete service implementations contain the logic. The Client class orchestrates user interaction, while administrators can manage slots and view historical revenue.

backendjavaCLIServiceEntityParking System
IT Xianyu
Written by

IT Xianyu

We share common IT technologies (Java, Web, SQL, etc.) and practical applications of emerging software development techniques. New articles are posted daily. Follow IT Xianyu to stay ahead in tech. The IT Xianyu series is being regularly updated.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.