一、实验目的
1.掌握软件开发的流程
2.熟练应用开发软件
二、实验内容
设计一个包含登录界面的计算机软件,该软件可以实现第一次作业中的全部功能,也可以保存用户的历史计算记录
三、流程图

四、登录

五、代码部分
(一)实现流程图代码
|
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
|
import java.util.Scanner;import java.util.ArrayList;import java.util.List;public class Software { private static List<String> calculationHistory = new ArrayList<>(); public static void main(String[] args) { openSoftware(); } public static void openSoftware() { // 软件显示登录界面 showLoginScreen(); // 用户输入用户名和密码 String username = getUserInput("Enter username: "); String password = getUserInput("Enter password: "); // 软件验证用户名和密码 if (validateCredentials(username, password)) { // 如果验证成功,则进入软件主界面 showMainScreen(username); } else { // 如果验证失败,则显示错误信息并返回步骤3 System.out.println("Invalid username or password. Please try again."); openSoftware(); } } public static void showLoginScreen() { System.out.println("Displaying login screen..."); } public static String getUserInput(String prompt) { Scanner scanner = new Scanner(System.in); System.out.print(prompt); return scanner.nextLine(); } public static boolean validateCredentials(String username, String password) { // 在这里进行验证用户名和密码的逻辑 // 这里简单地返回一个固定的用户名和密码进行模拟 return username.equals("user") && password.equals("password"); } public static void showMainScreen(String username) { System.out.println("Welcome, " + username + "!"); String userAction; do { // 用户在主界面进行操作 userAction = getUserInput("Enter your action (or 'exit' to end): "); if (!userAction.equalsIgnoreCase("exit")) { // 软件执行相应操作 executeAction(userAction); } } while (!userAction.equalsIgnoreCase("exit")); // 结束 endProgram(); } public static void executeAction(String action) { System.out.println("Executing action: " + action); // 在这里执行相应的操作,这里简单地将操作记录添加到历史记录中 calculationHistory.add(action); } public static void endProgram() { System.out.println("Ending the program..."); // 打印历史记录 System.out.println("Calculation history:"); for (String calculation : calculationHistory) { System.out.println(calculation); } }} |
(二)实现登录界面代码部分
|
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
|
import java.util.Scanner;import java.util.ArrayList;public class CalculatorWithLoginAndHistory { private static ArrayList<String> history = new ArrayList<>(); public static void main(String[] args) { openSoftware(); } public static void openSoftware() { showLoginScreen(); String username = getUserInput("Enter username: "); String password = getUserInput("Enter password: "); if (validateCredentials(username, password)) { showMainScreen(username); } else { System.out.println("Invalid username or password. Please try again."); openSoftware(); } } public static void showLoginScreen() { System.out.println("====================================="); System.out.println("| CALCULATOR |"); System.out.println("| LOGIN PAGE |"); System.out.println("====================================="); } public static String getUserInput(String prompt) { Scanner scanner = new Scanner(System.in); System.out.print(prompt); return scanner.nextLine(); } public static boolean validateCredentials(String username, String password) { // 在这里进行验证用户名和密码的逻辑 // 这里简单地返回一个固定的用户名和密码进行模拟 return username.equals("user") && password.equals("password"); } public static void showMainScreen(String username) { System.out.println("====================================="); System.out.println("| CALCULATOR |"); System.out.println("| MAIN SCREEN |"); System.out.println("====================================="); System.out.println("| 1. Perform Calculation |"); System.out.println("| 2. View History |"); System.out.println("| 3. Exit |"); System.out.println("====================================="); String userAction; do { userAction = getUserInput("Enter your choice: "); switch (userAction) { case "1": performCalculation(username); break; case "2": viewHistory(username); break; case "3": System.out.println("Exiting the program..."); break; default: System.out.println("Invalid choice. Please try again."); break; } } while (!userAction.equals("3")); } public static void performCalculation(String username) { System.out.println("Enter the expression to calculate (e.g. 5 + 3): "); String expression = getUserInput(""); try { String[] parts = expression.split(" "); double num1 = Double.parseDouble(parts[0]); String operator = parts[1]; double num2 = Double.parseDouble(parts[2]); double result = 0; switch (operator) { case "+": result = num1 + num2; break; case "-": result = num1 - num2; break; case "*": result = num1 * num2; break; case "/": result = num1 / num2; break; default: System.out.println("Invalid operator"); return; } history.add(username + " calculated: " + expression + " = " + result); System.out.println("Result: " + result); } catch (Exception e) { System.out.println("Invalid expression"); } } public static void viewHistory(String username) { System.out.println("====================================="); System.out.println("| CALCULATOR |"); System.out.println("| HISTORY |"); System.out.println("====================================="); for (String entry : history) { if (entry.startsWith(username)) { System.out.println(entry); } } System.out.println("====================================="); }} |
|
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
|
import java.util.ArrayList;import java.util.Scanner;public class Calculator { private static ArrayList<String> history = new ArrayList<>(); public static void main(String[] args) { System.out.println("Welcome to the calculator!"); Scanner scanner = new Scanner(System.in); String userAction; do { System.out.println("Choose an action:"); System.out.println("1. Perform calculation"); System.out.println("2. View history"); System.out.println("3. Exit"); userAction = scanner.nextLine(); switch (userAction) { case "1": performCalculation(); break; case "2": viewHistory(); break; case "3": System.out.println("Exiting the program..."); break; default: System.out.println("Invalid choice. Please try again."); break; } } while (!userAction.equals("3")); } public static void performCalculation() { Scanner scanner = new Scanner(System.in); System.out.println("Enter the expression to calculate (e.g. 5 + 3): "); String expression = scanner.nextLine(); try { String[] parts = expression.split(" "); double num1 = Double.parseDouble(parts[0]); String operator = parts[1]; double num2 = Double.parseDouble(parts[2]); double result = 0; switch (operator) { case "+": result = num1 + num2; break; case "-": result = num1 - num2; break; case "*": result = num1 * num2; break; case "/": result = num1 / num2; break; default: System.out.println("Invalid operator"); return; } history.add(expression + " = " + result); System.out.println("Result: " + result); } catch (Exception e) { System.out.println("Invalid expression"); } } public static void viewHistory() { System.out.println("====================================="); System.out.println("| CALCULATOR |"); System.out.println("| HISTORY |"); System.out.println("====================================="); for (String entry : history) { System.out.println(entry); } System.out.println("====================================="); }}<br><br> |
(六)、计算器页面
