用javaS简单实现ATM项目:
package Frame;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import savaCD.IOUtils;
import Main.Main;
import User.User;
/**
* 查看插卡登录页面
*
*/
public class LoginFrame extends JFrame implements ActionListener {
// private static final long serialVersionUID = 1L;
private JTextField jtName;
private JPasswordField jtPwd;
private JButton btnLogin;
/**
* 构造器中初始化页面信息
*
* @param login
*/
public LoginFrame() {
this.setTitle("请输入卡号和密码");
this.setBounds(200, 200, 300, 260);
// this.setResizable(false);
this.add(initView());
this.setVisible(true);
}
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {
User user = new User();
user.setCardNo(jtName.getText());
user.setPwd(jtPwd.getText());
// 调用登录的方法,方法返回值为boolean型,true=登录成功
if (login(user)) {
dispose(); // 如果登录成功,关闭登录页面
}
}
/**
* 初始化控件
*
* @return
*/
private JPanel initView() {
JPanel jpMain = new JPanel();
jpMain.setLayout(new GridLayout(3, 1));
JPanel jpUName = new JPanel();
jpUName.setLayout(new FlowLayout());
JLabel jlName = new JLabel("卡号:");
jtName = new JTextField(10);
jpUName.add(jlName);
jpUName.add(jtName);
JPanel jpPwd = new JPanel();
jpPwd.setLayout(new FlowLayout());
JLabel jlPwd = new JLabel("密码:");
jtPwd = new JPasswordField(10);
jpPwd.add(jlPwd);
jpPwd.add(jtPwd);
btnLogin = new JButton("确认");
btnLogin.addActionListener(this);
jpMain.add(jpUName);
jpMain.add(jpPwd);
jpMain.add(btnLogin);
return jpMain;
}
/**
* 校验插卡登录的方法
*
* @param user
* @return true=登录成功;false=登录失败
*/
private static boolean login(User user) {
boolean isOk = false;
try {
File file = new File(Main.getFilePath() + user.getCardNo() + ".dat");
if (file.exists()) { // 校验卡号有效性,如果磁盘中存在卡号命名的文件,则说明卡号是有效的
User local = IOUtils.loadUser(user.getCardNo()); // 调用读取磁盘用户信息的方法获取用户信息
if (user.getPwd().equals(local.getPwd())) { // 校验密码
isOk = true;
new TipFrame("账户信息读取成功!");
Main.setUser(user); // 保存当前用户
} else {
new TipFrame("密码错误,请重新输入");
}
} else {
new TipFrame("卡号不正确,请重新输入");
}
} catch (Exception e) {
e.printStackTrace();
new TipFrame("数据读取错误,请重试");
}
return isOk;
}
}
package atm;
import atm.entity.User;
import atm.frame.AccountFrame;
public class Main {
// 金库路径
private static String moneyPath = "e:/study/work/FirstATM/ATM";
// 保存当前登录的用户信息
private static User user = null;
// 程序入口
public static void main(String[] args) {
new AccountFrame(); // 实现欢迎用户界面
}
// 金钱路径
public static String getFilePath() {
return moneyPath;
}
// 用户信息
public static User getUser() {
return user;
}
// 保存账户信息
public static void setUser(User user) {
Main.user = user;
}
}