第二次作业

一、实验目的

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

2.掌握软件设计和开发的基本工具。

3.理解集成软件开发环境在软件开发过程中的作用。

4.模拟计算器的功能,对数据进行加减乘除以及开方运算并可保存历史计算记录。

二、实验要求

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

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

3.自己设计登录页面并和简易计算器连接并将数据保存在数据库中。

三、实验环境

1.操作系统:Windows11

2.开发测试工具:IDEA编写登录界面以及计算器相关代码,连接MySQL数据库存储计算记录。

3.流程图:visio

四、基本功能描述

一、相关功能

简易计算器包括基本的四则运算(加、减、乘、除)及开方运算并可存储历史计算数据功能。

五、软件设计

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;


  class Deng{
  public JFrame jf = new JFrame("登录界面");
  public Container con = jf.getContentPane();
  public Toolkit toolkit = Toolkit.getDefaultToolkit();
  public Dimension sc = toolkit.getScreenSize();

  public JLabel name1 = new JLabel("用户名");
  public JLabel pass1 = new JLabel("密码");
  public JTextField textName = new JTextField();
  public JPasswordField textPs = new JPasswordField();
  public JButton button1 = new JButton("登陆");
  public JButton button2 = new JButton("注册");

  public Deng(){
  con.setLayout(null);
  jf.setSize(sc.width/3,sc.height*10/25);
  jf.setLocation(sc.width/3,sc.height/4);

  con.setLayout(null);.
  jf.setSize(sc.width/3,sc.height*10/25);
  jf.setLocation(sc.width/3,sc.height/4);
  jf.setResizable(false);

  name1.setLocation(80,30);
  name1.setSize(100, 100);
  name1.setForeground(Color.white);

  pass1.setLocation(80,90);
  pass1.setSize(100,100);
  pass1.setForeground(Color.white);

  textName.setSize(140, 20);
  textName.setLocation(170, 70);

  textPs.setSize(140, 20);
  textPs.setLocation(170, 130);
  textPs.setEchoChar('*');

  button1.setSize(90,25);
  button1.setLocation(80,180 );
  button2.setSize(90,25) ;
  button2.setLocation(220, 180);

  con.add(name1);
  con.add(pass1);
  con.add(textName);
  con.add(textPs);
  con.add(button1);
  con.add(button2);
  con.setBackground(Color.white);

  jf.setResizable(false);
  jf.setVisible(true);
  jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}
  public class Denglu {
  public static void main(String args[]) {
  new Deng();
  }
}

    

 

  //登录
    Login login = new Login();
    login.setAdmin(admin);

    if(login.JudgeAdmin()==0) {
  //弹出账号或密码错误的窗口
    JOptionPane.showMessageDialog(null, "账号或密码错误", "账号或密码错误",JOptionPane.WARNING_MESSAGE);
  //清除密码框中的信息
    password.setText("");
  //清除账号框中的信息
    userID.setText("");
  //System.out.println("登陆失败");
    } else {
  //弹出登录成功的窗口
    JOptionPane.showMessageDialog(null, "登陆成功", "登陆成功", JOptionPane.NO_OPTION);
  //点击确定后会跳转到主窗口
    frame.setVisible(false);
    Calulator Cal = new Calulator();
    }

  }
}); 

    

  //为注册按钮添加监听器
    buttonregister.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    //注册页面

    frame.setVisible(false);

    AdminRegister ar = new AdminRegister();
    }
  });
}

  //Admin用户的实体类
    package src;
    public class Admin {
    private String id;
    private String name;
    private String password;
    void setID(String id) {
    this.id=id;
  }
    void setName(String name) {
    this.name=name;
  }
    void setPassword(String password) {
    this.password=password;
  }
    String getID() {
    return this.id;
  }
    String getName() {
    return this.name;
  }
    String getPassword() {
    return this.password;
  }
}

    

  //判断登录状态
    public class Login {
    Admin admin;
    void setAdmin(Admin admin) {
    this.admin=admin;
  }
  /*
    //JudgeAdmin()方法
    //判断Admin的用户和密码是否正确,如果正确,显示登录成功
    //如果错误,弹出一个窗口,显示账号或密码错误
  */
    private String driver = "com.mysql.cj.jdbc.Driver";
    private String url = "jdbc:mysql://localhost:3306/Test?serverTimezone=UTC&characterEncoding=utf-8";
    private String user = "root";
    private String password = "123456";

    public boolean login(Admin admin) throws SQLException, ClassNotFoundException {
    String sql="select * from admin where id=? and password=?";

    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, user, password);
    PreparedStatement ps = conn.prepareStatement(sql);

    ps.setString(1, admin.getID());
    ps.setString(2, admin.getPassword());
    ResultSet rs = ps.executeQuery();
    int ans = 0;
    if(rs.next()) {
    ans = 1;
  }
    rs.close();
    ps.close();
    conn.close();
    if(ans == 1) {
    return true;
  }
    else return false;
  }
    int JudgeAdmin() {
      try {
        if(login(this.admin)) {
        System.out.println("登录成功");
          return 1;
      }else {
          return 0;
        }
      }catch(Exception e) {
     }
      return 0;
    }
}
  //判断注册的账号是否符合规则
    boolean JudgeRegister() throws SQLException, ClassNotFoundException {

    if(this.name.equals("")) {
    JOptionPane.showMessageDialog(null, "用户名不能为空!", "用户名", JOptionPane.ERROR_MESSAGE);
    return false;
  }

    if(this.ID.equals("")) {
    JOptionPane.showMessageDialog(null, "账号不能为空!", "账号为空", JOptionPane.ERROR_MESSAGE);
    return false;
  }

    if(this.password.equals("")) {
    JOptionPane.showMessageDialog(null, "密码不能为空!", "密码为空", JOptionPane.ERROR_MESSAGE);
    return false;
  }

    if(!this.password.equals(this.confirmpassword)) {
    JOptionPane.showMessageDialog(null, "两次输入的密码不一致!", "密码不一致", JOptionPane.ERROR_MESSAGE);
    return false;
  }

  //符合规则,弹出注册成功的窗口,并将账号添加数据库
    JOptionPane.showMessageDialog(null, "注册成功");
    addAdmin();
    return true;
    package src;

    

  

  //计算器实现

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Objects;

import javax.swing.*;    

    public class Calulator extends JFrame implements ActionListener {

    private final String[] KEYS = { "7", "8", "9", "AC", "4", "5", "6", "-", "1", "2", "3", "+", "0", ".", "√","%","*", "(", ")", "/", "=" };
    private String Result;
  // 构造方法
    public Calulator() {
    super("Caculator");
    resultText.setBounds(20, 18, 255, 115);
    resultText.setAlignmentX(RIGHT_ALIGNMENT);
    resultText.setEditable(false);
    

    History.setBounds(290, 40, 250,370);
    History.setAlignmentX(LEFT_ALIGNMENT);
    History.setEditable(false);
    label.setBounds(300, 15, 100, 20);
    jp2.setBounds(290,40,250,370);
    jp2.setLayout(new GridLayout());
    JPanel jp1 = new JPanel();
    

    jp1.setBounds(20,18,255,115);//设置面板窗口位置及大小
    jp1.setLayout(new GridLayout());
    

    resultText.setLineWrap(true);//
    resultText.setWrapStyleWord(true);
    resultText.setSelectedTextColor(Color.RED);
    

    History.setLineWrap(true);//自动换行
    History.setWrapStyleWord(true);
    History.setSelectedTextColor(Color.blue);
    gdt2.setViewportView(History);

    this.add(jp1);//将面板添加到总窗体中
    this.add(jp2);//将面板添加到总窗体中
    this.setLayout(null);

  // 放置按钮
    int x = 20, y = 150;
    for (int i = 0; i < KEYS.length; i++)
  {
    keys[i] = new JButton();
    keys[i].setText(KEYS[i]);
    keys[i].setBounds(x, y, 60, 40);
      if (x < 215) {
      x += 65;
    } else {
      x = 20;
      y += 45;
    }
    this.add(keys[i]);
  }
    for (int i = 0; i < KEYS.length; i++)// 每个按钮都注册事件监听器
  {
    keys[i].addActionListener(this);
  }
    this.setResizable(false);
    this.setBounds(500, 200, 567, 480);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setVisible(true);
  }
    private String[] houzhui(String str) {
    String s = "";
    char[]opStack = new char[100];
    String[] postQueue = new String[100];
    int top = -1, j = 0;
    for (int i = 0; i < str.length(); i++)

  {
    if ("0123456789.".indexOf(str.charAt(i)) >= 0)
  {
    s = "";
    for (; i < str.length() && "0123456789.".indexOf(str.charAt(i)) >= 0; i++) {
    s = s + str.charAt(i);
  }
    i--;
    postQueue[j] = s;
    j++;
  }
    else if ("(".indexOf(str.charAt(i)) >= 0) {
    top++;
    opStack[top] = str.charAt(i);
  }
    else if (")".indexOf(str.charAt(i)) >= 0) {
    for (;;)
  {
    if (opStack[top] != '(') {
      postQueue[j] = opStack[top] + "";
      j++;
      top--;
    } else {
      top--;
      break;
      }
    }
}
      else if ("*%/+-".indexOf(str.charAt(i)) >= 0)
    {
      if (top == -1)
    {
      top++;
      opStack[top] = str.charAt(i);
    }
      else if ("*%/".indexOf(opStack[top]) >= 0)
    {
      postQueue[j] = opStack[top] + "";
      j++;
      opStack[top] = str.charAt(i);
    }
      else
      {
        top++;
        opStack[top] = str.charAt(i);
        }
      }
  }
    while (top != -1) {
    postQueue[j] = opStack[top] + "";
    j++;
    top--;
  }
    return postQueue;
  }
    public String kfys(String str) {
    String result = "";
    double a = Double.parseDouble(str), b = 0;
    b = Math.sqrt(a);
    result = String.valueOf(b);
    return result;
  }
    public String Result(String[] str) {
    String[] Result = new String[100];
    int Top = -1;// 静态指针Top
    for (int i = 0; str[i] != null; i++) {
    if ("+-*%/".indexOf(str[i]) < 0) {
    Top++;
    Result[Top] = str[i];
  }
    if ("+-*%/".indexOf(str[i]) >= 0)
  {
    double x, y, n;
    x = Double.parseDouble(Result[Top]);
    Top--;
    y = Double.parseDouble(Result[Top]);
    Top--;
    if ("*".indexOf(str[i]) >= 0) {
    n = y * x;
    Top++;
    Result[Top] = String.valueOf(n);

  }
    if ("/".indexOf(str[i]) >= 0)
  {
    if (x == 0)
    {
      String s = "error!";
      return s;
      } else {
        n = y / x;
        Top++;
        Result[Top] = String.valueOf(n);
      }
}
    if ("%".indexOf(str[i]) >= 0)
  {
    if (x == 0)
    {
      String s = "error!";
      return s;
    } else {
      n = y % x;
      Top++;
      Result[Top] = String.valueOf(n);
    }
  }
    if ("-".indexOf(str[i]) >= 0) {
    n = y - x;
    Top++;
    Result[Top] = String.valueOf(n);
    }
      if ("+".indexOf(str[i]) >= 0) {
      n = y + x;
      Top++;
      Result[Top] = String.valueOf(n);
      }
    }
  }
    return Result[Top];
  }

    String record;
    String result;

    public void setCaled(String record) {
    this.record = record;
  }

    public void setAns(String result) {
    this.result = result ;
  }

    public String getrecord() {
    return record;
  }

    public String getresult() {
    return result;
  }

    

 

posted @   张文迪  阅读(17)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示