这个是我们学校的实训的作业,本人有些不太理解, 求高手帮忙看一下,小弟不胜感激

ManaStuDent 2013-06-14 01:58:24

/*这个是我们学校的实训的作业,本人有些不太理解, 求高手帮忙看一下,小弟不胜感激。
这个项目是一个ATM取款机的窗口,用的是JDBC和MySpl;
我创建了4个包 bean,dao,frame,util
功能要求是(1)账户登录,这个已经解决(2)取款,存款,查询余额,和修改密码。
取款有问题,望高手解决。
查询余额和修改密码,本人不是很理解,请大家补充完整。
修改密码要在新的界面下修改。

以下是代码:*/
package com.wxcsxy.bean;
public class Account {
private int id;
private String userName;
private String userPwd;
private String accountNumber;
private double balance;
private String type;
//这里省略构造函数和Getter Setter方法

}

package com.wxcsxy.dao;
public class AccountDAO {
public Account validateLogin(String userName,String userPwd)
{
Account account = null;
//获得数据库的连接
Connection con = DBConnection.getConnection();
//定义SQL语句
String sql = "select * from account where userName=? and userPwd

=?";
//定义语句对象
PreparedStatement stmt = null;
//定义结果集对象
ResultSet rs = null;
try {
stmt = con.prepareStatement(sql);
//为语句对象的参数进行赋值
stmt.setString(1, userName);
stmt.setString(2, userPwd);
//执行语句对象
rs = stmt.executeQuery();
//如果account不为空,将结果集中的数据封装到account中
if(rs.next())
{
account = new Account();
account.setId(rs.getInt(1));
account.setUserName(rs.getString(2));
account.setUserPwd(rs.getString(3));
account.setAccountNumber(rs.getString(4));
account.setBalance(rs.getDouble(5));
account.setType(rs.getString(6));
}


} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return account;
}

//存款操作
public boolean desposit(Account account,double amt)
{
//定义状态变量result
boolean result = true;
int n = 0;
//获取当前帐户原有的余额
double balance = account.getBalance();
//获得数据库的连接
Connection con = DBConnection.getConnection();
//定义SQL语句
String sql = "update account set balance = ? where id=?";
//定义语句对象
PreparedStatement stmt = null;
try {
stmt = con.prepareStatement(sql);
stmt.setDouble(1, balance+amt);
stmt.setInt(2, account.getId());

n = stmt.executeUpdate();
if(n==0)
result = false;
else
{
account.setBalance(balance+amt);
}

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return result;
}
//取款操作
public boolean withDraw(Account account,double amt)
{
boolean result = true;
int n = 0;
//获取当前帐户原有的余额
double balance = account.getBalance();
//获得数据库的连接
Connection con = DBConnection.getConnection();
//定义SQL语句
String sql = "update account set balance = ? where id=?";
//定义语句对象
PreparedStatement stmt = null;
try {
stmt = con.prepareStatement(sql);
stmt.setDouble(1, balance-amt);
stmt.setInt(2, account.getId());

n = stmt.executeUpdate();
if(n==0){
result = false;
}
else
account.setBalance(balance-amt);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return result;
}
//查询 怎么做??
//修改密码
public boolean updatePwd(Account account,String oldPwd,String newPwd)
{
boolean result = true;

return result;
}

}

package com.wxcsxy.frame;
public class LoginFrame extends JFrame {
private JLabel lblTitle;
private JTextField txtUserName;
private JPasswordField txtUserPwd;
private JButton btnLogin,btnExit;
//定义静态变量保存登录用户信息
private static Account account;
public static Account getAccount() {
return account;
}
public static void setAccount(Account account) {
LoginFrame.account = account;
}
public LoginFrame()
{
this.setTitle("ATM系统登录窗口");
this.setResizable(false);
this.setLayout(null);
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
this.setBounds(screenWidth/2-100, screenHeight/2-100,300 , 300);

lblTitle = new JLabel("用户名:");
lblTitle.setBounds(30, 100, 60, 25);
this.add(lblTitle);

txtUserName = new JTextField(20);
txtUserName.setBounds(100, 100, 160, 25);
this.add(txtUserName);

lblTitle = new JLabel("密 码:");
lblTitle.setBounds(30, 140, 60, 25);
this.add(lblTitle);

txtUserPwd = new JPasswordField(20);
txtUserPwd.setBounds(100, 140, 160, 25);
txtUserPwd.addKeyListener(new KeyAdapter() {
public void keyPressed(final KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER)
btnLogin.doClick();
}
});

this.add(txtUserPwd);

btnLogin = new JButton("登录");
btnLogin.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
//获取界面中用户名和密码
String userName = txtUserName.getText().trim();
String userPwd = new String(txtUserPwd.getPassword

());
//验证用户是否合法
AccountDAO accountDAO = new AccountDAO();
account = accountDAO.validateLogin(userName,

userPwd);
//判断下一步的操作:如果account不为空,进入下一个界

面;否则,给出错误的提示
if(account!=null)
{
LoginFrame.this.setVisible(false);
new MainFrame();
}
else
{
JOptionPane.showMessageDialog(null, "该用户

名不存在或者密码有误!");
}
}
});
btnLogin.setBounds(60, 200, 70, 25);
this.add(btnLogin);
btnExit = new JButton("退出");
btnExit.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
btnExit.setBounds(180, 200, 70, 25);
this.add(btnExit);



this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new LoginFrame();
}
}





package com.wxcsxy.frame;
public class MainFrame extends JFrame {
private JButton btnWithDraw,btnDeposit,btnQuery,btnExit,btnUpdatePwd;
public MainFrame()
{
this.setTitle("ATM系统主界面");
this.setResizable(false);
this.setLayout(null);
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
this.setBounds(screenWidth/4, screenHeight/4, screenWidth/2-200,

screenHeight/2);

btnWithDraw = new JButton("取款");
btnWithDraw.setBounds(200, 50, 70, 25);

btnWithDraw.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
String inputMoney = JOptionPane.showInputDialog("请

输入取款的数额:");
AccountDAO accountDAO = new AccountDAO();
double amt = Double.parseDouble(inputMoney);
if(amt>0)
{
boolean result = accountDAO.withDraw

(LoginFrame.getAccount(), amt);
if(result)
{

JOptionPane.showMessageDialog(null,

"您的取款数额是:"+amt+",取款成功!");
}
else
{
JOptionPane.showMessageDialog(null,

"存款失败,请重新操作!");
}
}
else
{
JOptionPane.showMessageDialog(null, "您输入

的存款数额格式错误,请重新再输!");
}

}

}
);
this.add(btnWithDraw);

btnDeposit = new JButton("存款");
btnDeposit.setBounds(200, 100, 70, 25);
btnDeposit.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
// 告知存款的数额
//完成存款的业务逻辑,修改数据库
//告知用户存款的结果
String inputMoney = JOptionPane.showInputDialog("请

输入存款的数额:");
AccountDAO accountDAO = new AccountDAO();
double amt = Double.parseDouble(inputMoney);
if(amt>0)
{
boolean result = accountDAO.desposit

(LoginFrame.getAccount(), amt);
if(result)
{
JOptionPane.showMessageDialog(null,

"您的存款数额是:"+amt+",存款成功!");
}
else
{
JOptionPane.showMessageDialog(null,

"存款失败,请重新操作!");
}
}
else
{
JOptionPane.showMessageDialog(null, "您输入

的存款数额格式错误,请重新再输!");
}

}

}
);
this.add(btnDeposit);

btnQuery = new JButton("查询");
btnQuery.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
}
}
);
btnQuery.setBounds(200, 150, 70, 25);
this.add(btnQuery);

btnUpdatePwd = new JButton("修改密码");
btnUpdatePwd.setBounds(200, 200, 70, 25);
this.add(btnUpdatePwd);

btnExit = new JButton("退出");
btnExit.setBounds(200, 250, 70, 25);
this.add(btnExit);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);

}

public static void main(String[] args) {
new MainFrame();
}
}

package com.wxcsxy.frame;
public class upDatePwdFrame extends JFrame {
private JPanel panel;
private JLabel lalUserOldPwd,lalUserNewPwd1,lalUserNewPwd2;
private JPasswordField userPwd1,userPwd2,userPwd3;
private JButton btnAffirm,btnReset;
public upDatePwdFrame(){
this.setTitle("修改密码");
this.setBounds(100, 100, 300, 200);
this.setLayout(new GridLayout(4,1));

panel = new JPanel();
lalUserOldPwd = new JLabel("原始密码");
userPwd1 = new JPasswordField(12);
panel.add(lalUserOldPwd);
panel.add(userPwd1);
this.add(panel);

panel = new JPanel();
lalUserNewPwd1 = new JLabel("新始密码");
userPwd2 = new JPasswordField(12);
panel.add(lalUserNewPwd1);
panel.add(userPwd2);
this.add(panel);

panel = new JPanel();
lalUserNewPwd2 = new JLabel("确认密码");
userPwd3 = new JPasswordField(12);
panel.add(lalUserNewPwd2);
panel.add(userPwd3);
this.add(panel);

panel = new JPanel();
btnAffirm = new JButton("确认");
btnReset = new JButton("重置");
panel.add(btnAffirm);
panel.add(btnReset);
this.add(panel);

this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new upDatePwdFrame();
}
}

package com.wxcsxy.util;
public class DBConnection {
private static Connection con = null;

public static Connection getConnection()
{
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection

("jdbc:mysql://localhost:3306/atmdb", "root", "wxcsxy");
System.out.println("数据库联接成功");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return con;
}
}

...全文
157 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
darknb 2013-08-28
  • 打赏
  • 举报
回复
mark

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧