java实现航空用户管理系统

时间:2022-08-30 11:47:35

本文实例为大家分享了java实现航空用户管理系统的具体代码,供大家参考,具体内容如下

题目内容:

某航空公司在其航班到达的不同的国家的不同地方设有不同的办事处,这个项目要求开发一个自动化软件系统,该系统将提供给这些办事处的管理者(role=1)和普通用户(role=0)用于管理航班信息。根据以上描述,要求实现系统的用户模块和办事处模块,包含以下功能(注:系统存在一个默认管理员admin/admin123):

用户模块:

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类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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接口:

?
1
2
3
4
5
6
package com.softeem.j2106.work;
 
public interface Inter {
 
    public void ShowAll();
}

usermange:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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类:(实现)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
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实现航空用户管理系统

管理员登录:

java实现航空用户管理系统

用户登录:

java实现航空用户管理系统

写的不是很好,代码重复较多,希望各位朋友可以指点

基本实现练习要求,有兴趣的朋友可以自行尝试一下

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_44873394/article/details/118887672