交通罚单管理系统
主要描述:
管理员: (1)车辆信息(车牌号、行驶证号、车主信息等)、交警信息(交警代号、交警姓名、所属分局等)的录入、修改、删除,查询 (2)罚单查询(分别按车牌号、驾驶证号、交警代码等)
交警:(1)罚单信息(车牌号、驾驶证号、交警代号、违章时间、违章地点、罚款金额、缴费状态默认为未缴费等)的录入、 对自己开出的罚单进行修改、删除、查询
驾驶员:(1)罚单查询(按车牌号)、罚单缴费(修改缴费状态)
代码
public class Admin {
private String id
;
private String password
;
public Admin() {
}
public Admin(String id
, String password
) {
this.id
= id
;
this.password
= password
;
}
static Scanner sc
= new Scanner(System
.in
);
public void login() {
while (true) {
System
.out
.print("账号:");
String id
= sc
.next();
System
.out
.print("密码:");
String pwd
= sc
.next();
try {
if (id
.equals(DataBase
.ADMIN_MAP
.get(id
).getId()) && pwd
.equals(DataBase
.ADMIN_MAP
.get(id
).getPassword())) {
System
.out
.println("登录成功");
adminWork();
break;
}
} catch (Exception e
) {
System
.out
.println("账号密码错误");
}
}
}
public void adminWork() {
while (true) {
System
.out
.println("选择工作内容:1.车辆 2.交警 3.罚单查询 0.回到主页面");
int choice
= sc
.nextInt();
switch (choice
) {
case 1:
Car car
= new Car();
car
.carWork();
break;
case 2:
Police police
= new Police();
police
.policeWork();
break;
case 3:
findTicket();
break;
case 0:
return;
default:
System
.err
.println("重新输入");
break;
}
}
}
public void findTicket() {
Ticket t
= new Ticket();
while (true) {
try {
System
.out
.println("选择查询方法:1.车牌号 2.行驶证号 3.交警代码");
int choice
= sc
.nextInt();
switch (choice
) {
case 1:
System
.out
.print("输入车牌号:");
String license
= sc
.next();
t
.findTicketByLicenseId(license
);
return;
case 2:
System
.out
.print("输入行驶证号:");
String driveId
= sc
.next();
t
.findTicketByDriveId(driveId
);
return;
case 3:
System
.out
.print("输入警察代号:");
String policeId
= sc
.next();
t
.findTicketByPoliceId(policeId
);
return;
default:
System
.err
.println("重新输入");
break;
}
} catch (Exception e
) {
System
.err
.println("重新输入");
break;
}
}
}
}
public class Police {
private String id
;
private String name
;
private String station
;
public Police() {
}
public Police(String id
, String name
, String station
) {
this.id
= id
;
this.name
= name
;
this.station
= station
;
}
@Override
public String
toString() {
return "交警信息:" + "\t" +
"警察代号:" + id
+ '\t' +
"警察姓名" + name
+ '\t' +
"所属分局" + station
;
}
static Scanner sc
= new Scanner(System
.in
);
public void login() {
while (true) {
System
.out
.print("交警代号:");
String id
= sc
.next();
if (id
.equals(DataBase
.POLICE_MAP
.get(id
).getId())) {
System
.out
.println("登录成功");
Ticket t
= new Ticket();
t
.ticketWork(id
);
break;
} else {
System
.out
.println("没有这个交警");
}
}
}
public void policeWork() {
int choice
;
System
.out
.println("选择工作内容:1.新增交警 2.其他 0.回到上一页面");
choice
= sc
.nextInt();
if (choice
== 1) {
addPolice();
} else if (choice
== 2) {
System
.out
.println("输入交警代号:");
String policeId
= sc
.next();
while (true) {
if (DataBase
.POLICE_MAP
.containsKey(policeId
)) {
System
.out
.println("其他工作内容:1.查询交警 2.删除交警 3.修改交警 0.回到上一页面");
choice
= sc
.nextInt();
switch (choice
) {
case 1:
searchPolice(policeId
);
break;
case 2:
deletePolice(policeId
);
return;
case 3:
updatePolice(policeId
);
return;
case 0:
return;
default:
System
.err
.println("重新输入");
break;
}
} else {
System
.err
.println("没有这个人");
break;
}
}
} else if (choice
== 0) {
return;
} else {
System
.err
.println("重新输入");
return;
}
}
public void searchPolice(String policeId
) {
Police police
= DataBase
.POLICE_MAP
.get(policeId
);
System
.out
.println(police
);
}
public void addPolice() {
System
.out
.print("新交警代号:");
String newPoliceId
= sc
.next();
if (!DataBase
.POLICE_MAP
.containsKey(newPoliceId
)) {
System
.out
.print("新交警姓名:");
String newPoliceName
= sc
.next();
System
.out
.print("所属分局:");
String newStation
= sc
.next();
try {
DataBase
.POLICE_MAP
.put(newPoliceId
, new Police(newPoliceId
, newPoliceName
, newStation
));
System
.out
.println("添加成功");
searchPolice(newPoliceId
);
} catch (Exception e
) {
System
.err
.println("添加失败");
}
} else {
System
.err
.println("该警察已存在");
}
}
public void updatePolice(String policeId
) {
System
.out
.println("修改前:");
Police police
= DataBase
.POLICE_MAP
.get(policeId
);
System
.out
.println(police
);
System
.out
.print("新交警代号:");
String newPoliceId
= sc
.next();
if (!DataBase
.POLICE_MAP
.containsKey(newPoliceId
)) {
System
.out
.print("新交警姓名:");
String newPoliceName
= sc
.next();
System
.out
.print("所属分局:");
String newStation
= sc
.next();
Police newPolice
= new Police(newPoliceId
, newPoliceName
, newStation
);
DataBase
.POLICE_MAP
.put(newPolice
.getId(), newPolice
);
System
.out
.println("修改成功");
searchPolice(newPoliceId
);
DataBase
.POLICE_MAP
.remove(policeId
);
} else {
System
.err
.println("该警察已存在");
}
}
public void deletePolice(String policeId
) {
try {
DataBase
.CAR_MAP
.remove(policeId
);
System
.out
.println("删除成功");
} catch (Exception e
) {
System
.err
.println("删除失败");
}
}
}
public class Car {
private String licenseId
;
private String driveId
;
private String driverName
;
public Car() {
}
public Car(String licenseId
, String driveId
) {
this.licenseId
= licenseId
;
this.driveId
= driveId
;
}
public Car(String licenseId
, String driveId
, String driverName
) {
this.licenseId
= licenseId
;
this.driveId
= driveId
;
this.driverName
= driverName
;
}
@Override
public String
toString() {
return "车辆信息:" + "\t" +
"车牌号:" + licenseId
+ '\t' +
"驾驶证号:" + driveId
+ '\t' +
"驾驶员姓名:" + driverName
;
}
static Scanner sc
= new Scanner(System
.in
);
public void carWork() {
int choice
;
System
.out
.println("选择工作内容:1.新增车辆 2.其他 0.回到上一页面");
choice
= sc
.nextInt();
if (choice
== 1) {
addCar();
} else if (choice
== 2) {
System
.out
.println("输入车牌号:");
String licenseId
= sc
.next();
while (true) {
if (DataBase
.CAR_MAP
.containsKey(licenseId
)) {
System
.out
.println("其他工作内容:1.查询车辆 2.删除车辆 3.修改车辆 0.回到上一页面");
choice
= sc
.nextInt();
switch (choice
) {
case 1:
searchCar(licenseId
);
break;
case 2:
deleteCar(licenseId
);
return;
case 3:
updateCar(licenseId
);
return;
case 0:
return;
default:
System
.err
.println("重新输入");
break;
}
} else {
System
.err
.println("没有这辆车");
break;
}
}
} else if (choice
== 0) {
return;
} else {
System
.err
.println("重新输入");
return;
}
}
public void searchCar(String licenseId
) {
Car car
= DataBase
.CAR_MAP
.get(licenseId
);
System
.out
.println(car
);
}
public void addCar() {
System
.out
.print("新车牌号:");
String newLicenseId
= sc
.next();
if (!DataBase
.CAR_MAP
.containsKey(newLicenseId
)) {
System
.out
.print("新行驶证号:");
String newDriveId
= sc
.next();
System
.out
.print("新车主:");
String newDriver
= sc
.next();
try {
DataBase
.CAR_MAP
.put(newLicenseId
, new Car(newLicenseId
, newDriveId
, newDriver
));
System
.out
.println("添加成功");
searchCar(newLicenseId
);
} catch (Exception e
) {
System
.err
.println("添加失败");
}
} else {
System
.err
.println("车牌号已存在");
}
}
public void updateCar(String licenseId
) {
System
.out
.println("修改前:");
Car car
= DataBase
.CAR_MAP
.get(licenseId
);
System
.out
.println(car
);
System
.out
.print("新车牌号:");
String newLicenseId
= sc
.next();
System
.out
.print("新行驶证号:");
String newDriveId
= sc
.next();
System
.out
.print("新车主:");
String newDriver
= sc
.next();
Car newCar
= new Car(newLicenseId
, newDriveId
, newDriver
);
DataBase
.CAR_MAP
.put(newCar
.getLicenseId(), newCar
);
System
.out
.println("修改成功");
searchCar(newLicenseId
);
DataBase
.CAR_MAP
.remove(licenseId
);
}
public void deleteCar(String licenseId
) {
try {
DataBase
.CAR_MAP
.remove(licenseId
);
System
.out
.println("删除成功");
} catch (Exception e
) {
System
.err
.println("删除失败");
}
}
}
public class Ticket {
private String illegalTime
;
private String illegalPlace
;
private String illegalReason
;
private double fine
;
private boolean payment
;
public Ticket() {
}
public Ticket(String illegalTime
, String illegalPlace
, String illegalReason
, double fine
) {
this.illegalTime
= illegalTime
;
this.illegalPlace
= illegalPlace
;
this.illegalReason
= illegalReason
;
this.fine
= fine
;
}
@Override
public String
toString() {
String pay
;
if (payment
) {
pay
= "已缴费";
} else {
pay
= "未缴费";
}
return
"违章时间:" + illegalTime
+ '\t' +
"违章地点:" + illegalPlace
+ '\t' +
"违章原因:" + illegalReason
+ '\t' +
"罚款:" + fine
+ '\t' +
"缴费状态:" + pay
;
}
static Scanner sc
= new Scanner(System
.in
);
public void findTicketByLicenseId(String license
) {
if (DataBase
.CAR_MAP
.containsKey(license
)) {
Ticket ticket
= DataBase
.LICENSE_TICKET_MAP
.get(license
);
if (ticket
!= null
) {
System
.out
.println(ticket
);
} else {
System
.out
.println("没有罚单");
}
} else {
System
.out
.println("没有这辆车");
}
}
public void findTicketByDriveId(String driveId
) {
if (DataBase
.DRIVEID_TICKET_MAP
.containsKey(driveId
)) {
Ticket ticket
= DataBase
.DRIVEID_TICKET_MAP
.get(driveId
);
if (ticket
!= null
) {
System
.out
.println(ticket
);
} else {
System
.out
.println("没有罚单");
}
} else {
System
.err
.println("没有这个行驶证");
}
}
public void findTicketByPoliceId(String policeId
) {
if (DataBase
.POLICE_MAP
.containsKey(policeId
)) {
Ticket ticket
= DataBase
.POLICEID_TICKET_MAP
.get(policeId
);
if (ticket
!= null
) {
System
.out
.println(ticket
);
} else {
System
.out
.println("该交警没有开过罚单");
}
} else {
System
.err
.println("没有这个交警");
}
}
public void ticketWork(String policeId
) {
int choice
;
while (true) {
System
.out
.println("选择工作内容:1.开罚单 2.查罚单 3.修改罚单 4.删除罚单 0.回到上一页面");
System
.out
.print("请选择:");
choice
= sc
.nextInt();
String licenseId
;
String driveId
;
if (choice
!= 0) {
System
.out
.println("输入车辆信息:");
System
.out
.print("车牌号:");
licenseId
= sc
.next();
System
.out
.print("行驶证号:");
driveId
= sc
.next();
switch (choice
) {
case 1:
try {
addTicket(policeId
, licenseId
, driveId
);
System
.out
.println("添加成功");
} catch (Exception e
) {
System
.out
.println("添加失败");
}
break;
case 2:
System
.out
.println(policeId
+ "号交警开的罚单有:");
findTicketByPoliceId(policeId
);
break;
case 3:
updateTicket(policeId
, licenseId
, driveId
);
return;
case 4:
try {
deleteTicket(policeId
, licenseId
, driveId
);
System
.out
.println("删除成功");
} catch (Exception e
) {
System
.err
.println("删除失败");
}
return;
default:
System
.err
.println("输入错误");
}
} else {
return;
}
}
}
public void addTicket(String policeId
, String licenseId
, String driveId
) {
if (DataBase
.CAR_MAP
.containsKey(licenseId
)) {
System
.out
.print("违章时间:");
String time
= sc
.next();
System
.out
.print("违章原因:");
String reason
= sc
.next();
System
.out
.print("违章地点:");
String place
= sc
.next();
System
.out
.print("罚款金额:");
double fine
= sc
.nextDouble();
Ticket ticket
= new Ticket(time
, reason
, place
, fine
);
DataBase
.LICENSE_TICKET_MAP
.put(licenseId
, ticket
);
DataBase
.DRIVEID_TICKET_MAP
.put(driveId
, ticket
);
DataBase
.POLICEID_TICKET_MAP
.put(policeId
, ticket
);
DataBase
.CAR_MAP
.put(policeId
, new Car(licenseId
, driveId
, DataBase
.CAR_MAP
.get(licenseId
).getDriverName()));
} else {
System
.err
.println("车辆不存在");
}
}
public void updateTicket(String policeId
, String licenseId
, String driveId
) {
System
.out
.println("修改前:");
findTicketByPoliceId(policeId
);
System
.out
.println("输入新的车辆信息:");
System
.out
.print("车牌号:");
String newLicenseId
= sc
.next();
System
.out
.print("行驶证号:");
String newDriveId
= sc
.next();
addTicket(policeId
, newLicenseId
, newDriveId
);
deleteTicket(policeId
, licenseId
, driveId
);
}
public void deleteTicket(String policeId
, String licenseId
, String driveId
) {
DataBase
.LICENSE_TICKET_MAP
.remove(licenseId
);
DataBase
.DRIVEID_TICKET_MAP
.remove(driveId
);
DataBase
.POLICEID_TICKET_MAP
.remove(policeId
);
}
}
public class Driver {
static Scanner sc
= new Scanner(System
.in
);
public void login() {
while (true) {
System
.out
.print("输入车牌号:");
String id
= sc
.next();
if (id
.equals(DataBase
.CAR_MAP
.get(id
).getLicenseId())) {
System
.out
.println("登录成功");
Car car
= new Car();
car
.searchCar(id
);
driverWork(id
);
break;
} else {
System
.out
.println("没有这个交警");
}
}
}
public void driverWork(String licenseId
) {
Ticket ticket
= new Ticket();
int choice
;
while (true) {
System
.out
.println("选择业务:1.罚单查询 2.罚单缴费");
choice
= sc
.nextInt();
switch (choice
) {
case 1:
ticket
.findTicketByLicenseId(licenseId
);
break;
case 2:
payTicket(licenseId
);
break;
default:
System
.err
.println("重新输入");
break;
}
}
}
public void payTicket(String licenseId
) {
try {
Ticket ticket
= DataBase
.LICENSE_TICKET_MAP
.get(licenseId
);
ticket
.setPayment(true);
DataBase
.LICENSE_TICKET_MAP
.put(licenseId
, ticket
);
System
.out
.print("行驶证号:");
String driveId
= sc
.next();
DataBase
.DRIVEID_TICKET_MAP
.put(driveId
, ticket
);
System
.out
.print("开罚单交警的代号:");
String policeId
= sc
.next();
DataBase
.POLICEID_TICKET_MAP
.put(policeId
, ticket
);
System
.out
.println("违章缴费成功");
} catch (Exception e
) {
System
.err
.println("违章缴费失败");
}
}
}
public class DataBase {
public static final Map
<String, Admin> ADMIN_MAP
= new HashMap<>();
public static final Map
<String, Police> POLICE_MAP
= new HashMap<>();
public static final Map
<String, Car> CAR_MAP
= new HashMap<>();
public static final Map
<String, Ticket> LICENSE_TICKET_MAP
= new HashMap<>();
public static final Map
<String, Ticket> DRIVEID_TICKET_MAP
= new HashMap<>();
public static final Map
<String, Ticket> POLICEID_TICKET_MAP
= new HashMap<>();
static {
Admin admin
= new Admin("admin", "123456");
ADMIN_MAP
.put(admin
.getId(), admin
);
Police police1
= new Police("p001", "张三", "南京路分局");
Police police2
= new Police("p002", "李四", "上海路分局");
Police police3
= new Police("p003", "老大", "水产路分局");
POLICE_MAP
.put(police1
.getId(), police1
);
POLICE_MAP
.put(police2
.getId(), police2
);
POLICE_MAP
.put(police3
.getId(), police3
);
Car car1
= new Car("jc908", "z001", "王五");
Car car2
= new Car("ha186", "z002", "赵六");
Car car3
= new Car("xm361", "z003", "冯七");
CAR_MAP
.put(car1
.getLicenseId(), car1
);
CAR_MAP
.put(car2
.getLicenseId(), car2
);
CAR_MAP
.put(car3
.getLicenseId(), car3
);
Ticket ticket1
= new Ticket("2020-06-21", "南京路", "闯红灯", 200.0);
Ticket ticket2
= new Ticket("2020-07-02", "同济支路", "违章停车", 100.0);
Ticket ticket3
= new Ticket("2020-05-15", "上海路", "未让行人", 50.0);
LICENSE_TICKET_MAP
.put(car1
.getLicenseId(), ticket1
);
DRIVEID_TICKET_MAP
.put(car1
.getDriveId(), ticket1
);
POLICEID_TICKET_MAP
.put(police1
.getId(), ticket1
);
LICENSE_TICKET_MAP
.put(car2
.getLicenseId(), ticket2
);
DRIVEID_TICKET_MAP
.put(car2
.getDriveId(), ticket2
);
POLICEID_TICKET_MAP
.put(police2
.getId(), ticket2
);
LICENSE_TICKET_MAP
.put(car1
.getLicenseId(), ticket3
);
DRIVEID_TICKET_MAP
.put(car1
.getDriveId(), ticket3
);
POLICEID_TICKET_MAP
.put(police1
.getId(), ticket3
);
CAR_MAP
.put(police1
.getId(), car1
);
CAR_MAP
.put(police2
.getId(), car2
);
CAR_MAP
.put(police1
.getId(), car2
);
}
}
public class Test {
public static void main(String
[] args
) throws Exception
{
Scanner sc
= new Scanner(System
.in
);
Police police
= new Police();
Ticket t
= new Ticket();
Admin admin
= new Admin();
Driver driver
= new Driver();
int choice
;
while (true) {
System
.out
.println("1.管理员登录 2.交警登录 3.驾驶员登录 0.退出系统");
choice
= sc
.nextInt();
switch (choice
) {
case 1:
admin
.login();
break;
case 2:
police
.login();
break;
case 3:
driver
.login();
break;
case 0:
return;
default:
System
.err
.println("重新输入");
break;
}
}
}
}
测试
管理员登录 新增车辆 查询车辆 修改车辆信息 删除车辆 新增交警 查询交警 管理员根据车牌号查罚单 管理员根据行驶证查罚单 管理员根据交警代号查罚单 交警开罚单 交警查罚单 .驾驶员登录 驾驶员查罚单 驾驶员为罚单缴费