这期内容当中小编将会给大家带来有关java中怎么实现一个航空用户管理系统,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
用户模块:
1. 用户添加
2. 密码修改
3. 个人信息查看
4. 账号状态修改(禁用0、启用1)
5. 用户登录(被禁用账号无法登录并提示友好的消息)
6. 修改用户角色(设置取消管理员)
7. 用户列表
8. 查询指定办事处的员工
9. 删除用户
办事处模块:
1. 办事处添加
2. 办事处列表
注意:管理员具备以上所有功能,普通用户只有密码修改和个人信息查看功能
参考类信息如下:
用户类(User):
id,账号(username),密码(passord),年龄(age),角色(role),邮箱(email),办事处id(officeID),账户状态(status)
办事处类(Office):
id,办公室名(officeName)
要求使用技术参数如下:
1.分支与循环
2.数组或ArrayList
3.方法
4.构造器
5.setter/getter
6.抽象类或接口
7.多态
8.Scanner类
分析:
1.题目中管理员与用户的功能实现不同,普通用户只有登录系统、密码修改与个人信息查看功能,而管理员实现的功能更多,包含了普通用户的所有功能,那么我可以在登录时就进行分辨,不同用户所获得的功能菜单界面不同。
2.管理员可以设置状态,如果状态为禁用则无法登录。(实现方法可以放在用户登录中)
3.默认管理员admin/admin123(可以设置一个初始化块,初始化块又称游离块,是一个没有名称的代码块,执行时间一般在创建对象执行构造器前先执行,并且执行次数取决于对象的创建次数,作用于将多个构造器中的重复代码提取到一起统一执行. )
4.个人信息查看只能查看个人的信息,没法看到其他用户的信息。(设置一个静态变量,登陆时的用户名存储在里面,个人信息查看通过该静态变量在信息存储中查找)
5.接口(这次的接口没有写好,可以将UserMange中的方法都放进接口中,在UserMange类中实现,OfficeMange类中也一样)
内容实现:
User类:
package com.softeem.j2106.work;
/**
* @author admin
* 2021/7/17
*/
public class User {
private int id;
private String username;
private String password;
private int age;
private boolean role;
private String email;
private int officeID;
private boolean status;
public User() {
}
public User(int id, String username, String password, int age, boolean role, String email, int officeID, boolean status) {
this.id = id;
this.username = username;
this.password = password;
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 getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
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 boolean isRole() {
return role;
}
public void setRole(boolean role) {
this.role = role;
}
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", age=" + age +
", role=" + role +
", email='" + email + '\'' +
", officeID=" + officeID +
", status=" + status +
'}';
}
}
office类:
package com.softeem.j2106.work;
/**
* @author admin
* 2021/7/17
*/
public class Office {
private int officeID;
private String officeName;
public Office() {
}
public Office(int officeID, String officeName) {
this.officeID = officeID;
this.officeName = officeName;
}
public int getOfficeID() {
return officeID;
}
public void setOfficeID(int officeID) {
this.officeID = officeID;
}
public String getOfficeName() {
return officeName;
}
public void setOfficeName(String officeName) {
this.officeName = officeName;
}
@Override
public String toString() {
return "Office{" +
"officeID=" + officeID +
", officeName='" + officeName + '\'' +
'}';
}
}
Inter接口:
package com.softeem.j2106.work;
public interface Inter {
public void ShowAll();
}
usermange:
package com.softeem.j2106.work;
import java.util.Objects;
/**
* @author admin
* 2021/7/17
*/
public class UserManage implements Inter{
private User[] users = new User[10];
private int index=1;
{
users[0] = new User(0,"admin","admin123",20,true,"@163.com",0,true);
}
/**
* 用户登录
*/
public int sign(String username, String password) {
for (int i = 0; i < users.length; i++) {
User s = users[i];
if ((Objects.nonNull(s))&& s.getUsername().equals(username) && s.getPassword().equals(password)) {
if ((s.isRole())&& s.isStatus()) {
return 1;
} else if ((!s.isRole()) && s.isStatus()) {
return 0;
} else if (!s.isStatus()){
return -2;
}
}
}
return -1;
}
/**
* 用户添加
*/
public boolean add(User u) {
if (index >= users.length) {
return false;
}
users[index++] = u;
return true;
}
/**
* 密码修改
*/
public boolean updatePassword(String password) {
for (int i = 0; i < users.length; i++) {
User user = this.users[i];
if ((Objects.nonNull(user))&&user.getPassword() != null) {
users[i].setPassword(password);
return true;
}
}
return false;
}
/**
* 个人信息查看
*/
public User SearchByID(String username) {
User user = new User();
for (User user1 : users) {
if ((Objects.nonNull(user))&&user1.getUsername().equals(username)) {
user = user1;
break;
}
}
return user;
}
/**
* 账号状态修改
*/
public boolean changeStatus(String username, boolean status) {
User user = SearchByID(username);
if (user != null) {
user.setStatus(status);
return true;
}
return false;
}
/**
* 修改用户角色
*/
public boolean changeAdmin(String username, boolean role) {
User user = SearchByID(username);
if (user != null) {
user.setRole(role);
return true;
}
return false;
}
/**
* 查询指定办事处的员工
*/
public boolean searchofficeID(int officeId) {
for (User user : users) {
if ((Objects.nonNull(user))&&officeId == user.getOfficeID()) {
System.out.println(user);
return true;
}
}
return false;
}
/**
* 删除用户
*/
public boolean delete(int id) {
for (int i = 0; i < users.length; i++) {
User s = users[i];
if (Objects.nonNull(s) && Objects.equals(s.getId(), id)) {
//将当前元素置为空
// stus[i] = null;
//后续的元素前移 覆盖空白位置(避免碎片化)
System.arraycopy(users, i + 1, users, i, users.length - index - 1);
index--;
return true;
}
}
return false;
}
/**
* 用户列表
*/
@Override
public void ShowAll() {
for (User user : users) {
if (user != null) {
System.out.println(user);
}
}
}
}
officeMange类:
package com.softeem.j2106.work;
/**
* @author admin
* 2021/7/17
*/
public class OfficeMange implements Inter {
private static Office[] off = new Office[10];
private int index;
/**
* 办事处添加
*/
public boolean officeAdd(Office o) {
if (index >= off.length) {
return false;
}
off[index++] = o;
return true;
}
/**办事处列表*/
@Override
public void ShowAll() {
for (Office office : off) {
if (office != null) {
System.out.println(office);
}
}
}
}
tset类:(实现)
package com.softeem.j2106.work;
import java.util.Scanner;
/**
* @author admin
* 2021/7/17
*/
public class Test {
static String loginname;
static UserManage a = new UserManage();
static OfficeMange b = new OfficeMange();
static Scanner sc = new Scanner(System.in);
public static void start() {
msg("==============SOFTEEM用户登录==============");
msg("=========================================");
msg("请输入账号:");
String username = sc.next();
loginname = username;
msg("请输入密码:");
String password = sc.next();
if (a.sign(username, password) == 1) {
sign1();
} else if (a.sign(username, password) == 0) {
sign2();
} else if (a.sign(username, password) == -1) {
msg("登录失败!");
start();
} else if (a.sign(username, password) == -2) {
msg("账号被禁用!");
start();
}
}
public static void sign1() {
msg("=========SOFTEEM管理员管理系统=========");
msg("[1] 用户添加");
msg("[2] 密码修改");
msg("[3] 个人信息查看");
msg("[4] 账号状态修改");
msg("[5] 修改用户角色");
msg("[6] 用户列表");
msg("[7] 查询指定办事处的员工");
msg("[8] 删除员工");
msg("[9] 用户登录");
msg("[10] 办事处添加");
msg("[11] 办事处列表");
msg("[0] 退出系统");
msg("====================================");
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
switch (i) {
case 1:
addUser();
break;
case 2:
pwd();
sign1();
break;
case 3:
selectbyid();
sign1();
break;
case 4:
updateStatus();
break;
case 5:
updateRole();
break;
case 6:
listUser();
break;
case 7:
Search();
break;
case 8:
delUser();
break;
case 9:
start();
break;
case 10:
addOffice();
break;
case 11:
listOffice();
break;
case 0:
msg("谢谢使用,再见!");
//系统退出(关闭JVM)
System.exit(0);
break;
default:
msg("指令错误,请重新输入");
sign1();
break;
}
}
public static void sign2() {
msg("==========SOFTEEM用户管理系统==========");
msg("[1] 个人查看");
msg("[2] 密码修改");
msg("[0] 退出系统");
msg("====================================");
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
switch (i) {
case 1:
selectbyid();
sign2();
break;
case 2:
pwd();
sign2();
break;
case 0:
msg("谢谢使用,再见!");
//系统退出(关闭JVM)
System.exit(0);
break;
default:
msg("指令错误,请重新输入");
start();
break;
}
}
public static void selectbyid() {
User u = a.SearchByID(loginname);
if (u == null) {
msg("您输入的用户id不存在");
}
System.out.println(u);
}
public static void pwd() {
msg("请输入新密码:");
String password = sc.next();
if (a.updatePassword(password)) {
msg("修改成功");
} else {
msg("修改失败");
}
}
private static void addUser() {
msg("请输入ID:");
int id = sc.nextInt();
msg("请输入用户名:");
String name = sc.next();
msg("请输入密码:");
String password = sc.next();
msg("请输入年龄:");
int age = sc.nextInt();
msg("设置为管理员【1】是: 【0】否");
int i = sc.nextInt();
boolean role = true;
if (i == 1){
role = true;
}else if (i == 0){
role = false;
}else {
msg("请输入正确的指令");
}
msg("请输入邮箱:");
String emial = sc.next();
msg("请输入办事处ID:");
int officeid = sc.nextInt();
msg("设置状态【1】启用: 【0】禁用");
int j = sc.nextInt();
boolean status = true;
if (j == 1){
status = true;
}else if (j == 0){
status = false;
}else {
msg("请输入正确的指令");
}
User u = new User(id, name, password, age, role, emial, officeid, status);
if (a.add(u)) {
msg("添加成功!!");
} else {
msg("容量不足!!");
}
//返回主菜单
sign1();
}
/**办事处添加*/
public static void addOffice(){
msg("请输入officeID:");
int id = sc.nextInt();
msg("请输入办事处名:");
String name = sc.next();
Office o = new Office(id,name);
if (b.officeAdd(o)){
msg("添加成功!!");
} else {
msg("容量不足!!");
}
sign1();
}
public static void updateStatus() {
msg("请输入修改用户名:");
String username = sc.next();
msg("请修改用户的账户状态(禁用0/启用1):");
int j = sc.nextInt();
boolean status = true;
if (j == 1){
status = true;
}else if (j == 0){
status = false;
}else {
msg("请输入正确的指令");
}
if (a.changeStatus(username, status)) {
msg("修改成功");
} else {
msg("修改失败");
}
sign1();
}
/**修改用户的角色信息*/
public static void updateRole() {
msg("请输入修改用户名:");
String username = sc.next();
msg("请修改用户的角色信息(禁用0/启用1):");
int i = sc.nextInt();
boolean role = true;
if (i == 1){
role = true;
}else if (i == 0){
role = false;
}else {
msg("请输入正确的指令");
}
if (a.changeAdmin(username, role)) {
msg("修改成功");
} else {
msg("修改失败");
}
sign1();
}
/**用户删除*/
public static void delUser() {
msg("请输入ID:");
int Id = sc.nextInt();
if (a.delete(Id)) {
msg("删除成功!!");
} else {
msg("用户不存在!!");
}
//返回上一级
sign1();
}
/**用户列表*/
public static void listUser() {
a.ShowAll();
//返回上一级
sign1();
}
/**办事处列表*/
public static void listOffice(){
b.ShowAll();
sign1();
}
private static void Search() {
msg("请输入ID:");
int ID = sc.nextInt();
if (a.searchofficeID(ID)){
}else {
msg("未知查询");
}
sign1();
}
public static void msg(String msg) {
System.out.println(msg);
}
public static void main(String[] args) {
start();
}
}
用户登录:
管理员登录:
用户登录:
上述就是小编为大家分享的java中怎么实现一个航空用户管理系统了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注天达云行业资讯频道。