基于JavaSwing ATM取款機系統的設計和實現
前言:
本項目是使用Java swing開發,可實現ATM系統/銀行系統的基本登陸、轉賬、查詢余額、存取款業務。界面設計比較簡介、適合作為Java課設設計以及學習技術使用。
需求分析:
隨著生活水平的提高,消費量的增大,開銷也越來越大,自然離不開的就是錢。人們有的要取錢,有的要存錢,可是只能去銀行,而銀行的遍布并不是很廣,它可能在人流密集度比較大的地方會設立,或者稍大范圍內設立一個,但是對于比較偏遠地區的人們,無疑造成了非常大的困難。那么,如何來解決這個問題那?研發ATM柜員機即為廣大用戶提供了便捷,改善了生活。您無需再到銀行排隊辦理一些簡單的業務, ATM柜員機為您提供取款,存款,余額查詢,修改密碼等功能操作。而且ATM的遍及范圍遠遠大于銀行,主要是ATM的自身功能容易實現日容易布局,不需要耗費大量的空間,人力及物力,可以在很多點來設立。也正是在這種情況下, ATM柜員機得到了人們的喜愛并得到了大量的普及,可以說對銀行和人們都非常有益的。本系統通過設計與開發Windows系統,主要完成了余額查詢功能,取款功能,存款功能,轉賬功能,退出系統功能,目的在于通過 ATM自動存取款?機實現一些簡單的動能。
主要模塊:
用戶登錄、注冊、重置、存款、查詢余額、取款、轉賬、更改密碼、退卡等具體功能
功能截圖:
登錄注冊:
運行程序啟動mian方法進入登錄頁面
首頁
存款
存入輸入的金額點擊確認完成存款、存款的時候輸入的必須是整數
查詢余額
查詢自己的余額以及操作記錄信息
取款
取款金額不能大于賬戶余額
轉賬
轉賬的時候必須正確輸入用戶id信息、否則轉款失敗
更改密碼
輸入原密碼進行校驗后、輸入2次相同的新密碼完成修改密碼功能
數據庫設計:
這個ATM暫時沒用數據庫、是以文本txt的形式進行存儲數據、更方便快捷簡單話
部分關鍵代碼:
主啟動:
public static void main(String[] args)throws Exception {
usersList = new ArrayList
//System.out.println(usersList);
/**********************用戶文本**********************/
File users = new File("users.txt");
if (!users.exists()) {
try {
users.createNewFile();
Writer fw = new FileWriter("users.txt");
fw.write("123456 123456 10000");
fw.flush();
fw.close();
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "創建用戶文檔失敗");
}
}
usersFile = users;//創建用戶文檔,存儲用戶賬戶,密碼,余額信息;
usersListRead();
usersListUpdate();
/*****************************Login****************************/
LoginGui loginGui = new LoginGui();
}
賬號相關:
package atm;
//import com.sun.deploy.util.SyncFileAccess;
//import com.sun.org.apache.regexp.internal.RE;
import javax.swing.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
public class Account {
int money;
String id;//賬號名
String password;
Date now=new Date();
Date currentTime;
SimpleDateFormat formatter;
Reader fr;
;
public Account(String id, String password, String money) {//構造方法
this.id = id;
this.password = password;
this.money=Integer.parseInt(money);
}
public void outMoney (int money)throws Exception {//拋出異常,由相關的界面類彈窗處理異常,下面幾個方法同理
//如在取錢界面取錢,則會調用此函數,進行try/catch處理,獲得這個函數的異常,彈窗說明異常
if (money > this.money) {
throw new Exception("余額不足");
}
if(money<0)
{
throw new Exception("不能取出負數");
}
formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss");//時間格式
currentTime = new Date();//當前時間
String dateString = formatter.format(currentTime);//處理當前時間格式
Writer fw = new FileWriter(Test.file);
fw.write(Test.recordString.append(dateString + "\t" + Test.currentAccount.id + "\t取出" + money + "元\r\n").toString());//將這次的取錢行為添加到記錄文件中
fw.flush();//寫進文件
fw.close();
this.money -= money;
Test.usersListUpdate();//更新用戶文檔(信息)
}
public void inMoney(int money)throws Exception
{
try {
Writer fw = new FileWriter(Test.file);
// System.out.println(Test.file);
formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
currentTime=new Date();
String dateString=formatter.format(currentTime);
fw.write(Test.recordString.append(dateString+"\t"+Test.currentAccount.id+"\t存入" + money + "元\r\n").toString());
fw.flush();//寫進文件
fw.close();
this.money+=money;
Test.usersListUpdate();//更新當前用戶信息
}
catch (Exception e1)
{
throw new Exception("寫入記錄失敗");
}
}
public void transfer(int money,String id)throws Exception//轉賬
{
if(id.equals(Test.currentAccount.id))
{
throw new Exception("不能轉給自己");
}
if(money>this.money)
{
throw new Exception("余額不足");
}
if(money<0) {
throw new Exception("不能轉入負數");
}
for(int i=0;i { if(Test.usersList.get(i).id.equals(id))//找到要轉帳的用戶 { Test.usersList.get(i).money+=money;//轉入 this.money-=money;//扣錢 FileWriter fw=new FileWriter(Test.file); formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss");//聲明時間格式 currentTime=new Date();//獲取當前時間 String dateString=formatter.format(currentTime);//轉換時間格式 fw.write(Test.recordString.append(dateString+"\t向"+id+"\t轉出"+money+"元\r\n").toString());//Test類中的靜態字符串拼接上這個字符串覆蓋寫入當前用戶文檔 fw.close(); /********************向轉入目標寫入轉賬信息*************************/ try { fr = new FileReader(id+".txt");//字符流 } catch (Exception e) { System.out.println("字符流創建失敗"); } BufferedReader bfr = new BufferedReader(fr); String temp=""; String temp1; while ((temp1 = bfr.readLine()) != null) { temp+=temp1; } temp=temp.replace("元","元\n\r")+dateString+"\t由"+Test.currentAccount.id+"\t轉進"+money+"元\r\n"; System.out.println(temp); fw=new FileWriter(id+".txt"); fw.write(temp); fw.close(); JOptionPane.showMessageDialog(null,"轉賬成功"); Test.usersListUpdate();//更新用戶文檔 return; } } throw new Exception("目標用戶不存在"); } public void ChangePassword(String newPassword)throws Exception { if(newPassword.equals(this.password)) { throw new Exception("原密碼和新密碼不能一樣"); } else { if(newPassword.equals("")) { throw new Exception("密碼不能為空"); } } password=newPassword; Test.usersListUpdate(); } } 修改密碼: package atm; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class ChangePassword implements ActionListener{ public JPasswordField oldPassword,newPassword,checkPassword; public JFrame iframe; public JPanel ip0,ip1,ip2,ip3,ip4; public JButton confirm,cancel,exit; public ChangePassword() { iframe=new JFrame("更改密碼"); iframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); ip2=new JPanel(); ip2.add(new JLabel("原密碼:")); oldPassword=new JPasswordField(20); ip2.add(new JLabel(" ip2.add(oldPassword); ip0=new JPanel(); ip0.add(new JLabel("新密碼:")); newPassword=new JPasswordField(20); ip0.add(new JLabel(" ip0.add(newPassword); ip4=new JPanel(); ip4.add(new JLabel("再次輸入新密碼:")); checkPassword=new JPasswordField(20); ip4.add(new JLabel(" ip4.add(checkPassword); ip3=new JPanel(); confirm=new JButton("確定"); ip3.add(confirm); cancel=new JButton("返回"); ip3.add(cancel); iframe.add(ip2); iframe.add(ip0); iframe.add(ip4); iframe.add(ip3); iframe.add(confirm); iframe.add(cancel); iframe.setLayout(new FlowLayout()); iframe.setVisible(true); iframe.setTitle("密碼更改");//窗體標簽 iframe.setSize(400, 200);//窗體大小 iframe.setLocationRelativeTo(null);//在屏幕中間顯示(居中顯示) confirm.addActionListener(this); cancel.addActionListener(this); } public void pw_clean() { newPassword.setText(""); oldPassword.setText(""); checkPassword.setText(""); } @Override public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("確定")) { if (Test.currentAccount.password.equals(oldPassword.getText())) { try { if(newPassword.getText().equals(checkPassword.getText())) { if(newPassword.getText().length()>=6) { Test.currentAccount.ChangePassword(newPassword.getText()); JOptionPane.showMessageDialog(null, "更改密碼成功"); iframe.setVisible(false); Test.menu.mframe.setVisible(false);//關閉菜單界面 new LoginGui(); }else { JOptionPane.showMessageDialog(null,"密碼不能少于6位!\n請重新輸入","提示消息",JOptionPane.ERROR_MESSAGE); pw_clean(); } } else { JOptionPane.showMessageDialog(null, "兩次輸入的密碼不一致"); pw_clean(); } } catch (Exception e1) {//捕獲賬戶類中更改密碼函數的異常并彈窗顯示 JOptionPane.showMessageDialog(null, e1.getMessage()); pw_clean(); } } else { JOptionPane.showMessageDialog(null, "原密碼錯誤"); pw_clean(); } } else//如果點擊cancel { iframe.setVisible(false); } } } 文檔結構圖: 總結: 在本次課程設計中我主要負責登陸界面部分和界面優化。通過這次課程設計。我學到了許多令我受益匪淺的知識。感覺java的界面設計和 mfc差不多。只是java沒有可視化的界面做起來太累了。其他主要是類和對象的問題。實現起來還是挺簡單的。 完整源碼- 從今天開始打卡更新Java相關項目 1/100天 大家可以、、關注、評論我啦 Java
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
"));//換行
"));//換行
"));//換行