lzufe-cc

博客园 首页 新随笔 联系 订阅 管理

 第二次作业

一、实验目的

1.掌握软件开发的基本流程

2.掌握常用的软件开发方式和工具。

二、实验内容

1.设计一个包含登录界面的计算器软件,该软件可以实现第一次作业中的全部功能,同时可以保存用户的历史计算记录(保存数据最好使用数据库)。

三、实验要求

1.完成软件的UI设计、使用Visio设计软件中所涉及的所有流程图。

2.选择合适的集成开发环境和工具完成计算器软件的开发

3.将开发好软件进行测试并截图

4.将本次实验过程写成实验报告提交在本次作业的链接中

5.关键代码部分以代码块格式粘贴在实验报告正文中

6.软件架构以及开发技术不限

四、实验步骤

1.登陆界面流程图:

 

计算器的前端登录界面

 

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <meta name="viewport" content="width=, initial-scale=1.0">
 6     <title>计算器登录界面</title>
 7     <style>
 8         body{
 9             background-image: url("b.jpg");
10             background-size: 500px 280px;
11             background-repeat: no-repeat;
12         }
13         .reg{
14             text-align:center ;
15             color:greenyellow;
16         }
17         div{
18             width:500px;
19             height:280px;
20         }
21         #reg0{
22             width:200px;
23             height:30px;
24             border: 1px solid green;
25             border-radius:20px;
26             margin:10px 150px;
27         }
28         #reg1{
29             width:200px;
30             height:30px;
31             border: 1px solid green;
32             border-radius:20px;
33             margin:10px 150px;
34         }
35 
36         #reg2{
37             width:80px;
38             height:25px;
39             border: 1px solid green;
40             border-radius:20px;
41             margin:5px 0px 5px 150px;
42             background-color:skyblue;
43             color:white;
44         }
45         #reg3{
46             width:80px;
47             height:25px;
48             border: 1px solid grey;
49             border-radius:20px;
50             margin:5px 150px 5px 0px;
51             background-color:skyblue;
52             color:white;
53         }
54     </style>
55 </head>
56 <body>
57     <div>
58         <form action="/计算器" method="post">
59             <br>
60             <input type="text" id="reg0" name="id" id="userId" placeholder="请输入账号" > <br>
61             <input type="password" id="reg1" name="pwr" id="password" placeholder="请输入密码" > <br>
62             <input type="button" id="reg2" value="登录" >
63             <input type="reset" id="reg3" value="重置" >
64         </form>
65     </div>
66 </body>
67     <script type="text/javascript" src="jquery-3.7.1.js"></script>
68     <script type="text/javascript">
69         $("#reg2").click(function () {
70             var userId = $("#userId").val();
71             var password = $("#password").val();
72             //判断用户id是否为空
73             if(isEmpty(userId)){
74                 alert("用户账号不可为空");
75             }
76             if(isEmpty(password)){
77                 alert("用户密码不可为空");
78             }
79         });
80         //判断字符串是否为空
81         function isEmpty(str){
82             if(str == null || str.trim() == ""){
83                 return true;
84             }
85             return false;
86         }
87         $("#login").submit();
88        $("#reg3").click(function () {
89            alert("重置成功了");
90        })
91        
92     </script>
93 </html>

连接数据库并实现对数据库的增删改查

 1 import com.mysql.cj.jdbc.Driver;
 2 
 3 import java.sql.*;
 4 import java.util.Map;
 5 import java.util.Properties;
 6 import java.util.concurrent.Executor;
 7 
 8 public class Jdbc {
 9     public static void main(String[] args) throws SQLException {
10         Driver driver = new Driver();
11         String url = "jdbc:mysql://localhost:3306/计算器";
12         Properties properties = new Properties();
13         properties.setProperty("user", "root");
14         properties.setProperty("password", "Ksg59368.");
15         Connection connect = driver.connect(url, properties);
16         System.out.println(connect);
17         //添加数据
18         String sql = "insert into user values(5,'李明','34252a',15)";
19         //删除数据
20         String sql1 = "delete from user where userId='5'";
21 
22         Statement statement = connect.createStatement();
23 
24         try (ResultSet rs = statement.executeQuery("SELECT userId, userName, userPwd, age FROM user ")) {
25             while (rs.next()) {
26                 long id = rs.getLong(1);
27                 long grade = rs.getLong(2);
28                 String name = rs.getString(3);
29                 int gender = rs.getInt(4);
30             }
31             rs.close();
32             int rows = statement.executeUpdate(sql1);
33             System.out.println(rows > 0 ? "成功" : "失败");
34         }
35     }
36 }

 前后端的连接

  1 // User.java
  2 public class User {
  3     private Long id;
  4     private String name;
  5 
  6     // Getters and setters
  7 }
  8 
  9 // UserMapper.java
 10 import org.apache.ibatis.annotations.*;
 11 
 12 import java.util.List;
 13 
 14 @Mapper
 15 public interface UserMapper {
 16     @Select("SELECT * FROM users")
 17     List<User> getAllUsers();
 18 
 19     @Select("SELECT * FROM users WHERE id = #{id}")
 20     User getUserById(Long id);
 21 
 22     @Insert("INSERT INTO users(name) VALUES(#{name})")
 23     @Options(useGeneratedKeys = true, keyProperty = "id")
 24     void addUser(User user);
 25 
 26     @Update("UPDATE users SET name = #{name} WHERE id = #{id}")
 27     void updateUser(User user);
 28 
 29     @Delete("DELETE FROM users WHERE id = #{id}")
 30     void deleteUser(Long id);
 31 }
 32 
 33 // UserService.java
 34 import org.springframework.beans.factory.annotation.Autowired;
 35 import org.springframework.stereotype.Service;
 36 
 37 import java.util.List;
 38 
 39 @Service
 40 public class UserService {
 41 
 42     @Autowired
 43     private UserMapper userMapper;
 44 
 45     public List<User> getAllUsers() {
 46         return userMapper.getAllUsers();
 47     }
 48 
 49     public User getUserById(Long id) {
 50         return userMapper.getUserById(id);
 51     }
 52 
 53     public void addUser(User user) {
 54         userMapper.addUser(user);
 55     }
 56 
 57     public void updateUser(User user) {
 58         userMapper.updateUser(user);
 59     }
 60 
 61     public void deleteUser(Long id) {
 62         userMapper.deleteUser(id);
 63     }
 64 }
 65 
 66 // UserController.java
 67 import org.springframework.web.bind.annotation.*;
 68 
 69 import java.util.List;
 70 
 71 @RestController
 72 @RequestMapping("/users")
 73 public class UserController {
 74 
 75     private final UserService userService;
 76 
 77     public UserController(UserService userService) {
 78         this.userService = userService;
 79     }
 80 
 81     @GetMapping
 82     public List<User> getAllUsers() {
 83         return userService.getAllUsers();
 84     }
 85 
 86     @GetMapping("/{id}")
 87     public User getUserById(@PathVariable Long id) {
 88         return userService.getUserById(id);
 89     }
 90 
 91     @PostMapping
 92     public void addUser(@RequestBody User user) {
 93         userService.addUser(user);
 94     }
 95 
 96     @PutMapping("/{id}")
 97     public void updateUser(@PathVariable Long id, @RequestBody User user) {
 98         user.setId(id);
 99         userService.updateUser(user);
100     }
101 
102     @DeleteMapping("/{id}")
103     public void deleteUser(@PathVariable Long id) {
104         userService.deleteUser(id);
105     }
106 }
107 
108 // MybatisApplication.java
109 import org.springframework.boot.SpringApplication;
110 import org.springframework.boot.autoconfigure.SpringBootApplication;
111 
112 @SpringBootApplication
113 public class MybatisApplication {
114     public static void main(String[] args) {
115         SpringApplication.run(MybatisApplication.class, args);
116     }
117 }

 

计算器功能的实现

 

  1 package Calculate;
  2 
  3 import javax.swing.*;
  4 import java.awt.*;
  5 import java.awt.event.ActionEvent;
  6 import java.awt.event.ActionListener;
  7 
  8 public class CalculateUl extends JFrame {
  9     private JTextField numField1, numField2, resultField;
 10     private JButton addButton, subtractButton, multiplyButton, divideButton;
 11 
 12     public CalculateUl() {
 13         setTitle("Simple Calculator");
 14         setSize(500, 300);
 15         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 16 
 17         setLayout(new GridLayout(5, 2));
 18 
 19         numField1 = new JTextField();
 20         numField2 = new JTextField();
 21         resultField = new JTextField();
 22         resultField.setEditable(false);
 23 
 24         addButton = new JButton("+");
 25         subtractButton = new JButton("-");
 26         multiplyButton = new JButton("*");
 27         divideButton = new JButton("/");
 28 
 29         add(new JLabel("Number 1:"));
 30         add(numField1);
 31         add(new JLabel("Number 2:"));
 32         add(numField2);
 33         add(new JLabel("Result:"));
 34         add(resultField);
 35         add(addButton);
 36         add(subtractButton);
 37         add(multiplyButton);
 38         add(divideButton);
 39 
 40         addButton.addActionListener(new ActionListener() {
 41             @Override
 42             public void actionPerformed(ActionEvent e) {
 43                 performOperation('+');
 44             }
 45         });
 46 
 47         subtractButton.addActionListener(new ActionListener() {
 48             @Override
 49             public void actionPerformed(ActionEvent e) {
 50                 performOperation('-');
 51             }
 52         });
 53 
 54         multiplyButton.addActionListener(new ActionListener() {
 55             @Override
 56             public void actionPerformed(ActionEvent e) {
 57                 performOperation('*');
 58             }
 59         });
 60 
 61         divideButton.addActionListener(new ActionListener() {
 62             @Override
 63             public void actionPerformed(ActionEvent e) {
 64                 performOperation('/');
 65             }
 66         });
 67     }
 68 
 69     private void performOperation(char operator) {
 70         try {
 71             double num1 = Double.parseDouble(numField1.getText());
 72             double num2 = Double.parseDouble(numField2.getText());
 73             double result = 0;
 74 
 75             switch (operator) {
 76                 case '+':
 77                     result = num1 + num2;
 78                     break;
 79                 case '-':
 80                     result = num1 - num2;
 81                     break;
 82                 case '*':
 83                     result = num1 * num2;
 84                     break;
 85                 case '/':
 86                     if (num2 != 0) {
 87                         result = num1 / num2;
 88                     } else {
 89                         JOptionPane.showMessageDialog(this, "Division by zero is not allowed");
 90                     }
 91                     break;
 92             }
 93 
 94             resultField.setText(String.valueOf(result));
 95         } catch (NumberFormatException ex) {
 96             JOptionPane.showMessageDialog(this, "Invalid input");
 97         }
 98     }
 99 
100     public static void main(String[] args) {
101         SwingUtilities.invokeLater(() -> {
102             CalculateUl calculatorUI = new CalculateUl();
103             calculatorUI.setVisible(true);
104         });
105     }
106 }

 

posted on 2023-12-05 13:26  fleeting1  阅读(14)  评论(0编辑  收藏  举报