第二次作业

一、实验目的:

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

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

二、实验内容:

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

三、程序流程图

登陆流程图

 

 

 

 

 注册流程图

 

 

 

 

四、UI设计

用户打开网页

 

打开浏览器,在地址栏中输入http://localhost:8080/10,并按下Enter键。

 

 

 用户成功登陆

输入账户用户名mr

密码123456

用户注册

用户名123456

密码123456

确认密码123456

邮箱111122233@163.com

 

计算器实现计算

加法

 

减法

 

乘法

 

除法

 

 

登陆注册代码

public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) {

DynaActionForm userLoginForm = (DynaActionForm) form;

UserLogin userLogin = userLoginDao.findbyNameAndPwd(userLoginForm

.getString("loginName"), userLoginForm.getString("pwd"));

if (userLogin == null) {

request.setAttribute("error", "用户登录失败,用户名或密码不正确!");

return mapping.findForward("index");

} else {

request.getSession().setAttribute("loginName",

userLogin.getLoginName());

request.getSession().setAttribute("id", userLogin.getId());

if (userLogin.getType().equals("1")) {

System.out.println("管理员登录");

return mapping.findForward("manager");

} else {

// 判断学生有没有填写过基本信息

StuUser stuUser = stuUserDao.findById(userLogin.getId());

if (stuUser == null) {

System.out.println("没添写过基础信息");

List<Specialty> list = specialtyDao.findStuByAll();

request.setAttribute("list", list);

return mapping.findForward("addStuInfo");

} else {

Specialty specialty = specialtyDao.findById(stuUser

.getSpecialtyId());

request.setAttribute("specialty", specialty);

request.setAttribute("stuUser", stuUser);

System.out.println("添写过基础信息");

return mapping.findForward("welcome");

}

}

}

}

}

package com.jwy.dto;

 

public class UserLogin implements java.io.Serializable {

private Integer id;

private String loginName;

private String pwd;

private String type;

private String mail;

 

// Constructors

 

/** default constructor */

public UserLogin() {

}

 

/** full constructor */

public UserLogin(String loginName, String pwd, String type, String mail) {

this.loginName = loginName;

this.pwd = pwd;

this.type = type;

this.mail = mail;

}

 

// Property accessors

 

public Integer getId() {

return this.id;

}

 

public void setId(Integer id) {

this.id = id;

}

 

public String getLoginName() {

return this.loginName;

}

 

public void setLoginName(String loginName) {

this.loginName = loginName;

}

 

public String getPwd() {

return this.pwd;

}

 

public void setPwd(String pwd) {

this.pwd = pwd;

}

 

public String getType() {

return this.type;

}

 

public void setType(String type) {

this.type = type;

}

 

public String getMail() {

return this.mail;

}

 

public void setMail(String mail) {

this.mail = mail;

}

 

}

计算器代码

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Calculator extends JFrame implements ActionListener {
    private static final long serialVersionUID = 1L;

    private JPanel panel1, panel2, panel3;
    private JTextField tfDisplay;
    private JButton btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btnAdd, btnSubtract, btnMultiply, btnDivide, btnClear, btnEqual;

    public Calculator() {
        super("计算器");

        // 创建面板
        panel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        panel2 = new JPanel(new GridLayout(4, 4));
        panel3 = new JPanel(new FlowLayout(FlowLayout.RIGHT));

        // 创建按钮和文本框
        tfDisplay = new JTextField(20);
        btn0 = new JButton("0");
        btn1 = new JButton("1");
        btn2 = new JButton("2");
        btn3 = new JButton("3");
        btn4 = new JButton("4");
        btn5 = new JButton("5");
        btn6 = new JButton("6");
        btn7 = new JButton("7");
        btn8 = new JButton("8");
        btn9 = new JButton("9");
        btnAdd = new JButton("+");
        btnSubtract = new JButton("-");
        btnMultiply = new JButton("*");
        btnDivide = new JButton("/");
        btnClear = new JButton("C");
        btnEqual = new JButton "=");

        // 添加按钮和文本框到面板
        panel1.add(tfDisplay);
        panel2.add(btn0);
        panel2.add(btn1);
        panel2.add(btn2);
        panel2.add(btn3);
        panel2.add(btn4);
        panel2.add(btn5);
        panel2.add(btn6);
        panel2.add(btn7);
        panel2.add(btn8);
        panel2.add(btn9);
        panel2.add(btnAdd);
        panel2.add(btnSubtract);
        panel2.add(btnMultiply);
        panel2.add(btnDivide);
        panel3.add(btnClear);
        panel3.add(btnEqual);

        // 设置按钮的监听器
        btn0.addActionListener(this);
        btn1.addActionListener(this);
        btn2.addActionListener(this);
        btn3.addActionListener(this);
        btn4.addActionListener(this);
        btn5.addActionListener(this);
        btn6.addActionListener(this);
        btn7.addActionListener(this);
        btn8.addActionListener(this);
        btn9.addActionListener(this);
        btnAdd.addActionListener(this);
        btnSubtract.addActionListener(this);
        btnMultiply.addActionListener(this);
        btnDivide.addActionListener(this);
        btnClear.addActionListener(this);
        btnEqual.addActionListener(this);

        // 设置布局
        setLayout(new BorderLayout());
        add(panel1, BorderLayout.NORTH);
        add(panel2, BorderLayout.CENTER);
        add(panel3, BorderLayout.SOUTH);

        // 设置默认文本框内容为 0
        tfDisplay.setText("0");
    }
    public void actionPerformed(ActionEvent e) {
        String btnText = e.getActionCommand();

        if (btnText.equals("0")) {
            tfDisplay.setText(tfDisplay.getText() + btnText);
        } else if (btnText.equals("1")) {
            tfDisplay.setText(tfDisplay.getText() + btnText);
        } else if (btnText.equals("2")) {
            tfDisplay.setText(tfDisplay.getText() + btnText);
        } else if (btnText.equals("3")) {
            tfDisplay.setText(tfDisplay.getText() + btnText);
        } else if (btnText.equals("4")) {
            tfDisplay.setText(tfDisplay.getText() + btnText);
        } else if (btnText.equals("5")) {
            tfDisplay.setText(tfDisplay.getText() + btnText);
        } else if (btnText.equals("6")) {
            tfDisplay.setText(tfDisplay.getText() + btnText);
        } else if (btnText.equals("7")) {
            tfDisplay.setText(tfDisplay.getText() + btnText);
        } else if (btnText.equals("8")) {
            tfDisplay.setText(tfDisplay.getText() + btnText);
        } else if (btnText.equals("9")) {
            tfDisplay.setText(tfDisplay.getText() + btnText);
        } else if (btnText.equals("+")) {
            tfDisplay.setText(tfDisplay.getText() + btnText);
        } else if (btnText.equals("-")) {
            tfDisplay.setText(tfDisplay.getText() + btnText);
        } else if (btnText.equals("*")) {
            tfDisplay.setText(tfDisplay.getText() + btnText);
        } else if (btnText.equals("/")) {
            tfDisplay.setText(tfDisplay.getText() + btnText);
        } else if (btnText.equals("C")) {
            tfDisplay.setText("0");
        } else if (btnText.equals("=")) {
            try {
                double num = Double.parseDouble(tfDisplay.getText());
                double result = 0;

                if (btnText.equals("+")) {
                    result = num + Double.parseDouble(tfDisplay.getText());
                } else if (btnText.equals("-")) {
                    result = num - Double.parseDouble(tfDisplay.getText());
                } else if (btnText.equals("*")) {
                    result = num * Double.parseDouble(tfDisplay.getText());
                } else if (btnText.equals("/")) {
                    if (Double.parseDouble(tfDisplay.getText()) != 0) {
                        result = num / Double.parseDouble(tfDisplay.getText());
                    } else {
                        throw new IllegalArgumentException("错误:除数不能为 0");
                    }
                }

posted @ 2023-12-04 22:10  llkkjjj  阅读(46)  评论(0)    收藏  举报