基于MVC开发模式的ATM
期末课程设计附全代码。功能需求分析:
用户登录
查询余额
用户取款
用户存款
修改密码
首先还是分析将分为3部分,第一部分为
model数据层(javabean)用来封装传送数据,第二为control层,用于连接数据库操作
第三为view界面层用于展示。
数据库设计
创建表数据库acount 表card
向表中填入初始测试数据
model层设计
设计简单点就是账户名和密码
代码
package ATM;
public class Model {
String AccountNumber="123456";
String password="123456";
String surplus="0";
public Model(String AccountNumber,String password) {
this.AccountNumber=AccountNumber;
this.password=password;
}
public String getSurplus() {
return surplus;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void setSurplus(String surplus) {
this.surplus = surplus;
}
}
control层设计
还是使用JDBC连接数据库
package ATM;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class control {
static String AccountNumber;
java.sql.Statement sta;
Connection con;
ResultSet st;
boolean flage=false;
ArrayList<Model> AL=new ArrayList<>();
public boolean LoginAuthentication(String user,String password) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/acount","root","yp18783819559");
sta=con.createStatement();
String selectsql="Select * from card";
st= sta.executeQuery(selectsql);
while (st.next()) {
String user1=st.getString(1);
String password1=st.getString(2);
Model M=new Model(user1, password1);
AL.add(M);
}
for (Model model : AL) {
if (model.AccountNumber.equals(user) & model.password.equals(password)) {
flage=true;
AccountNumber=model.AccountNumber;
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flage;
}
public String Quey() {
String accountNumber = null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/acount","root","yp18783819559");
sta=con.createStatement();
String selectsql="Select surplus from card where user="+AccountNumber;
st= sta.executeQuery(selectsql);
while (st.next()) {
accountNumber=st.getString("surplus");
}
} catch (SQLException e) {
e.printStackTrace();
}
return accountNumber;
}
public void teller(int num) {
int num2=Integer.parseInt( Quey()) ;
String num3=Integer.toString( num2-num);
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/acount","root","yp18783819559");
sta=con.createStatement();
String selectsql="UPDATE card SET surplus="+num3+" WHERE USER="+AccountNumber;
sta.executeUpdate(selectsql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public int deposite(int num)
{
int x = 0;
int num2=Integer.parseInt( Quey()) ;
String num3=Integer.toString( num2+num);
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/acount","root","yp18783819559");
sta=con.createStatement();
String selectsql="UPDATE card SET surplus="+num3+" WHERE USER="+AccountNumber;
x=sta.executeUpdate(selectsql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return x;
}
public int ChangeCode(String newpassword)
{
int x=0;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/acount","root","yp18783819559");
sta=con.createStatement();
String selectsql="UPDATE card SET password="+newpassword+" WHERE USER="+AccountNumber;
x= sta.executeUpdate(selectsql);
} catch (SQLException e) {
e.printStackTrace();
}
return x;
}
}
view层设计
view层功能有几个,我们就创建几个Jpanel,经过需求分析我们发现有五个功能,故需要五个界面来完成,我们取名为,p1,p2,p3,p4,view.这部分代码太多,这里就只展示view界面的,需要的可以到作者的github上下载末尾附链接
package ATM;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import java.awt.Toolkit;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.SystemColor;
public class view extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextPane textPane;
private JTextPane textPane_1;
private JButton btnNewButton;
private JTextPane txtpnAtm;
private ATM atm;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
view frame = new view();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public view() {
setResizable(false);
setTitle("\u767B\u5F55");
setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\Administrator\\Desktop\\\u767B\u5F55 \u7528\u6237.png"));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 362, 272);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setContentPane(contentPane);
textField = new JTextField();
textField.setBounds(166, 70, 66, 21);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JPasswordField();
//textField_1.SetsecureTextEntry=YES;
textField_1.setBounds(166, 123, 66, 21);
contentPane.add(textField_1);
textField_1.setColumns(10);
textPane = new JTextPane();
textPane.setBackground(SystemColor.menu);
textPane.setEditable(false);
textPane.setText("\u8D26\u53F7:");
textPane.setBounds(59, 70, 48, 21);
contentPane.add(textPane);
textPane_1 = new JTextPane();
textPane_1.setBackground(SystemColor.menu);
textPane_1.setEditable(false);
textPane_1.setText("\u5BC6\u7801:");
textPane_1.setBounds(59, 123, 48, 21);
contentPane.add(textPane_1);
btnNewButton = new JButton("\u767B\u5F55");
btnNewButton.setFocusable(false);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnNewButtonActionPerformed(e);
}
});
btnNewButton.setBounds(156, 195, 93, 23);
contentPane.add(btnNewButton);
txtpnAtm = new JTextPane();
txtpnAtm.setBackground(SystemColor.menu);
txtpnAtm.setEditable(false);
txtpnAtm.setForeground(Color.RED);
txtpnAtm.setFont(new Font("华文琥珀", Font.PLAIN, 30));
txtpnAtm.setText("ATM");
txtpnAtm.setBounds(139, 10, 79, 50);
contentPane.add(txtpnAtm);
}
protected void btnNewButtonActionPerformed(ActionEvent e) {
String AccountNumber=textField.getText();
String password=textField_1.getText();
control con=new control();
boolean x= con.LoginAuthentication(AccountNumber,password);
if (x==true) {
JOptionPane.showMessageDialog(textField_1, "登录成功!");
atm=new ATM();
atm.setVisible(true);
this.setVisible(false);
}else {
JOptionPane.showMessageDialog(textField_1, "登录失败,密码或账号错误!");
}
}
}
运行效果
登录
登录失败
登录成功
查询
取款
余额不足
限制取款
存款
存款不为整钞时
存款为100元整钞
查询
修改密码
修改密码失败
修改成功
总结
总的来说还是非常简单的,在做得过程需要注意的是怎么才能将登录成功获取的信息拿到后面的页面中,通过这个练习希望能让你进一步巩固javaSE的知识。如果觉得好的话,希望关注作者,作者会不定期更新相关文章。全代码链接 JavaSE/JavaSE课程设计--ATM at master · yp1919597583/JavaSE · GitHubhttps://github.com/yp1919597583/JavaSE/tree/master/JavaSE%E8%AF%BE%E7%A8%8B%E8%AE%BE%E8%AE%A1--ATM