某航空公司在其航班到达的不同的国家的不同地方设有不同的办事处,这个项目要求开发一个自动化软件系 统,该系统将提供给这些办事处的管理者(role=1)和普通用户(role=0)用于管理航班信息。根据以上描述,要 求实现系统的用户模块和办事处模块,包含以下功能(注:系统存在一个默认管理员admin/admin123):
下面代码完全是按题目要求来做的
public class AccountNumber implements Serializable{
private int id;
private String username;//账号
private String passord;//密码
private int age;//年龄
private String role;//角色
private String email;//邮箱
private int officeID;//办事处id
private String status;//账户状态
public AccountNumber() {
}
public AccountNumber(int id, String username, String passord, int age, String role, String email, int officeID,
String status) {
super();
this.id = id;
this.username = username;
this.passord = passord;
this.age = age;
this.role = role;
this.email = email;
this.officeID = officeID;
this.status = status;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassord() {
return passord;
}
public void setPassord(String passord) {
this.passord = passord;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getOfficeID() {
return officeID;
}
public void setOfficeID(int officeID) {
this.officeID = officeID;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
@Override
public String toString() {
return "AccountNumber [id=" + id + ", username=" + username + ", passord=" + passord + ", age=" + age
+ ", role=" + role + ", email=" + email + ", officeID=" + officeID + ", status=" + status + "]";
}
}
public class Office implements Serializable {
private int id;//办事处ID
private String officeName;//办事处名字
public Office() {
}
public Office(int id, String officeName) {
super();
this.id = id;
this.officeName = officeName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getOfficeName() {
return officeName;
}
public void setOfficeName(String officeName) {
this.officeName = officeName;
}
@Override
public String toString() {
return "Office [id=" + id + ", officeName=" + officeName + "]";
}
}
public class AccountNumberFunction {
Scanner scanner = new Scanner(System.in);
private ArrayList<AccountNumber> accountNumber = new ArrayList<AccountNumber>();//存储用户账号的集合
private ArrayList<Office> office = new ArrayList<Office>();//存储办公的集合
private AccountNumber account;
private int userid;
private int officeid;
public AccountNumberFunction() {
// TODO Auto-generated constructor stub
readerUser();
readerUserID();
readerOfficeID();
readerOffice();
}
public void register() {
int k = 0;
String s = 1+"";
System.out.println("登录");
System.out.print("账号:");
String name = scanner.nextLine();
System.out.print("密码:");
String passord = scanner.nextLine();
for (int i = 0; i < accountNumber.size(); i++) {
if(accountNumber.get(i).getUsername().equals(name) && accountNumber.get(i).getPassord().equals(passord)) {
if(accountNumber.get(i).getRole().equals("管理员")) {
System.out.println("管理员");
if(accountNumber.get(i).getStatus().equals(s)) {
k++;
catalogue();
return;
}else {
System.out.println("你的账号状态处于被禁用。请跟管理员联系并取消禁用状态");
}
}else if(accountNumber.get(i).getRole().equals("用户")) {
if(accountNumber.get(i).getStatus().equals(s)) {
userCatalogue();
return;
}else {
System.out.println("你的账号状态处于被禁用。请跟管理员联系并取消禁用状态");
}
}
}
}
System.out.println("(账号或者密码)错误请重新输入!!!!");
register();
}
public String menu() {
System.out.println("用户添加:(1)");
System.out.println("用户查看:(2)");
System.out.println("修改密码:(3)");
System.out.println("账号状态修改(禁用0、启用1):(4)");
System.out.println("角色状态修改(管理员、用户):(5)");
System.out.println("用户列表:(6)");
System.out.println("删除用户:(7)");
System.out.println("办事处添加:(8)");
System.out.println("办事处列表:(9)");
System.out.println("查询指定办事处的所有员工:(10)");
System.out.println("退出(11):");
return scanner.nextLine();
}
//用户菜单
public String userMenu() {
System.out.println("修改密码:(1)");
System.out.println("用户查看:(2)");
System.out.println("退出:(3)");
return scanner.nextLine();
}
//用户界面
public void userCatalogue() {
String n = userMenu();
switch (n) {
case "1":
//修改密码
passordAlter();
userCatalogue();
break;
case "2":
//修改密码
selectUser();
userCatalogue();
break;
case "3":
break;
}
}
//管理员界面
public void catalogue() {
String n = menu();
System.out.println(n);
switch (n) {
case "1":
//用户添加
userAdd();
catalogue();
break;
case "2":
//用户查看
selectUser();
catalogue();
break;
case "3":
//修改密码
passordAlter();
catalogue();
break;
case "4":
//账号状态修改
stateAlter();
catalogue();
break;
case "5":
//角色状态修改
roleAlter();
catalogue();
break;
case "6":
//用户列表
userList();
catalogue();
break;
case "7":
//删除用户
deleteuser();
catalogue();
break;
case "8":
//办公添加
officeAdd();
catalogue();
break;
case "9":
//办公添加
officeSelect();
catalogue();
break;
case "10":
//办公添加
selectOffice();
catalogue();
break;
case "11":
break;
}
}
//写入用户
public void writeUser() {
FileOutputStream fileO = null;
ObjectOutputStream objectO = null;
try {
fileO = new FileOutputStream("C:\\Users\\admin\\Desktop\\新建文件夹 (2)\\航空公司_办事处\\用户.txt");
objectO = new ObjectOutputStream(fileO);
objectO.writeObject(accountNumber);
objectO.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(objectO != null) {
objectO.close();
}
if(fileO != null) {
fileO.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//读入本地
public void readerUser() {
FileInputStream fileI = null;
ObjectInputStream objectI = null;
try {
fileI = new FileInputStream("C:\\Users\\admin\\Desktop\\新建文件夹 (2)\\航空公司_办事处\\用户.txt");
objectI = new ObjectInputStream(fileI);
Object object = objectI.readObject();
if(object instanceof ArrayList) {
this.accountNumber = (ArrayList<AccountNumber>) object;
}
}catch(EOFException e) {
String s = 1+"";
account = new AccountNumber();
account.setUsername("admin");
account.setPassord("admin123");
account.setRole("管理员");
account.setStatus(s);
this.accountNumber.add(account);
writeUser();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void writeUserID() {
FileWriter fileW = null;
try {
fileW = new FileWriter("C:\\Users\\admin\\Desktop\\新建文件夹 (2)\\航空公司_办事处\\用户ID.txt");
fileW.write(this.userid+"");
fileW.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//读入本地
public void readerUserID() {
FileReader fileR = null;
BufferedReader buff = null;
try {
fileR = new FileReader("C:\\\\Users\\\\admin\\\\Desktop\\\\新建文件夹 (2)\\\\航空公司_办事处\\\\用户ID.txt");
buff = new BufferedReader(fileR);
this.userid = Integer.parseInt(buff.readLine());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void userAdd() {
AccountNumber accou = new AccountNumber();
accou.setId(userid+1);
System.out.println("添加账号:");
accou.setUsername(scanner.nextLine());
System.out.println("添加密码:");
accou.setPassord(scanner.nextLine());
System.out.println("添加年龄:");
accou.setAge(Integer.parseInt(scanner.nextLine()));
System.out.println("添加角色:");
accou.setRole(scanner.nextLine());
System.out.println("添加邮箱:");
accou.setEmail(scanner.nextLine());
System.out.println("添加办事处ID:");
accou.setOfficeID(Integer.parseInt(scanner.nextLine()));
System.out.println("添加账号状态(禁用0、启用1):");
accou.setStatus(scanner.nextLine());
accountNumber.add(accou);
//写入
writeUser();
userid++;
writeUserID();
}
//密码修改
public void passordAlter() {
System.out.println("请输入原密码");
String passord = scanner.nextLine();
System.out.println("请输入新密码");
String passordNew = scanner.nextLine();
System.out.println("请确认新密码");
String passordNew1 = scanner.nextLine();
for (int i = 0; i < accountNumber.size(); i++) {
if(accountNumber.get(i).getPassord().equals(passord)) {
if(passordNew.equals(passordNew1)) {
accountNumber.get(i).setPassord(passordNew1);
writeUser();
return;
}
}
}
System.out.println("原密码输入错误!!!");
return;
}
//账号状态修改
public void stateAlter() {
System.out.println("请输入你要修改账号状态的账号和ID:");
System.out.println("请输入ID:");
int ID = Integer.parseInt(scanner.nextLine());
System.out.println("请输入账号:");
String state = scanner.nextLine();
System.out.print("(禁用0、启用1)");
String s = scanner.nextLine();
for (int i = 0; i < accountNumber.size(); i++) {
if(accountNumber.get(i).getUsername().equals(state)&&accountNumber.get(i).getId() == ID) {
accountNumber.get(i).setStatus(s);
writeUser();
return;
}
}
System.out.println("ID或者账号输入错误。请重新输入!!!");
}
//管理员状态修改
public void roleAlter() {
System.out.println("请输入你要修改账号为管理员的账号和ID:");
System.out.println("请输入ID:");
int ID = Integer.parseInt(scanner.nextLine());
System.out.println("请输入账号:");
String state = scanner.nextLine();
System.out.print("(管理员或用户)");
String s = scanner.nextLine();
for (int i = 0; i < accountNumber.size(); i++) {
if(accountNumber.get(i).getUsername().equals(state)&& accountNumber.get(i).getId() == ID) {
accountNumber.get(i).setRole(s);
writeUser();
return;
}
}
System.out.println("ID或者账号输入错误。请重新输入!!!");
}
//查看用户
public void selectUser(){
for (AccountNumber accountNumber2 : accountNumber) {
System.out.println(accountNumber2);
}
}
//用户列表
public void userList() {
for (int i = 0; i < accountNumber.size(); i++) {
System.out.println("账号:"+accountNumber.get(i).getUsername());
}
}
public void deleteuser() {
System.out.println("请输入你要删除的账号:");
String delete = scanner.nextLine();
for (int i = 0; i < accountNumber.size(); i++) {
if(accountNumber.get(i).getUsername().equals(delete)) {
accountNumber.remove(i);
writeUser();
}
}
}
//添加办公
public void officeAdd() {
Office off = new Office();
off.setId(officeid+1);
System.out.println("请输入办公室名:");
off.setOfficeName(scanner.nextLine());
office.add(off);
writeOffice();
officeid++;
writeOfficeID();
}
public void officeSelect() {
for (int i = 0; i < office.size(); i++) {
System.out.println(office.get(i));
}
}
public void writeOfficeID() {
FileWriter fileW = null;
try {
fileW = new FileWriter("C:\\Users\\admin\\Desktop\\新建文件夹 (2)\\航空公司_办事处\\办公ID.txt");
fileW.write(this.officeid+"");
fileW.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//读入本地
public void readerOfficeID() {
FileReader fileR = null;
BufferedReader buff = null;
try {
fileR = new FileReader("C:\\\\Users\\\\admin\\\\Desktop\\\\新建文件夹 (2)\\\\航空公司_办事处\\\\办公ID.txt");
buff = new BufferedReader(fileR);
this.officeid = Integer.parseInt(buff.readLine());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void writeOffice() {
FileOutputStream fileO = null;
ObjectOutputStream objectO = null;
try {
fileO = new FileOutputStream("C:\\Users\\admin\\Desktop\\新建文件夹 (2)\\航空公司_办事处\\办公.txt");
objectO = new ObjectOutputStream(fileO);
objectO.writeObject(office);
objectO.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(objectO != null) {
objectO.close();
}
if(fileO != null) {
fileO.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//读入本地
public void readerOffice() {
FileInputStream fileI = null;
ObjectInputStream objectI = null;
try {
fileI = new FileInputStream("C:\\Users\\admin\\Desktop\\新建文件夹 (2)\\航空公司_办事处\\办公.txt");
objectI = new ObjectInputStream(fileI);
Object object = objectI.readObject();
if(object instanceof ArrayList) {
this.office = (ArrayList<Office>) object;
}
}catch(EOFException e) {
System.out.println("办公为空!!!");
this.office = new ArrayList<Office>();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void selectOffice() {
System.out.println("请输入你要查询的办公名称:");
String select = scanner.nextLine();
for (int i = 0; i < office.size(); i++) {
if(office.get(i).getOfficeName().equals(select)) {
int n = office.get(i).getId();
for (int j = 0; j < accountNumber.size(); j++) {
if(accountNumber.get(j).getOfficeID() == n) {
System.out.println(accountNumber.get(j).getUsername());
}
}
}
}
}
}
public class Text {
public static void main(String[] args) {
// TODO Auto-generated method stub
AccountNumberFunction accountN = new AccountNumberFunction();
accountN.register();
}
}
程序运行截图
管理员界面:
用户界面:
存放在本地的数据:
我这个小程序写法和思路跟我之前写的学生管理系统一样,一个思路。
这个是利用IO流来把数据转化为二进制保存在本地文件。
本人是个学习java基础的菜鸟。还请各位欢迎指点和评论。这样会知道我有那些欠缺。