[課程設計]Java實現圖形化窗口界面可存儲ATM機(自助取款機)

[很久以前寫的了,沒想到這麼多瀏覽了,現在看着不是一般的亂..當時剛學java,望見諒,不過現在也沒有閒工夫重寫和心情修改了,湊活湊活吧]

這是一個使用io流和swing庫製作的可存儲的窗口化ATM機程序;臭不要臉的發上來敲打

實現的功能有:1.登錄和註冊用戶(雖然現實中的ATM中沒有註冊功能敲打

2.存款

3.取款

4.查詢記錄,包括存款和取款和轉賬的記錄

5.更改密碼

6.退卡


類的構成:1.Test類,實現讀取用戶文檔並更新用戶文檔的功能;

2.LoginGui類,登錄界面,實現登錄和註冊等功能;

3.Menu類,菜單界面

4.InMoney,OutMoney,Inqury,Transfer類,ChangePassword,分別爲存款界面,取款界面,查詢界面,轉賬界面,更改密碼界面

5.Account,賬戶類,實現賬戶的各種功能,包括存款,取款,轉賬,更改密碼等


項目思路:1.該項目中通過文本來保存用戶信息和操作記錄,用戶信息存儲在users.txt中,用戶的記錄存儲在相應用戶的用戶名.txt中,在程序開始時,調用Test類中的

usesrListRead方法讀取users文檔,將信息讀入程序,得到用戶信息,創建用戶的List(Tset類中的usersList),並創建用戶;


2.首先在登陸界面進行登陸或註冊,進行合法性驗證,若能成功註冊,創建相應的Account類,並創建相應的記錄文本,然後要求登錄。如果能成功登陸,將Tes

t中的靜態變量currentAccount(當前登錄的用戶)設置爲該用戶,之後的操作對這個用戶進行操作。本程序中

有一個默認用戶,id爲admin,密碼爲123456,可以直接登錄。記錄文本用來存儲用戶的操作,在後面的每一步操

作中都會通過io流寫入記錄文本;


3.登陸後彈出菜單,點擊按鈕彈出相應界面,使用功能後將記錄根據文件名存儲在用戶相應的記錄文件中,然後每一次用戶狀態改變時(使用功能後)都會調

用Test類中的usersListUpdate對用戶文檔進行更新。這樣關閉程序後下次再打開程序時再讀取文檔就能恢復信息。也就是完成了保存功能。

4.各個界面均是使用java自帶的swing庫實現的。


注意:1.Test類中使用了很多靜態變量來進行全局傳值。Test.xxx什麼都表示是Test  類中的靜態變量。

     2.不能同時用read和write對同一個文件進行操作,否則會清空文件 ,應注意

流的關閉


源碼下載地址:鏈接:點擊打開鏈接 點擊打開鏈接 密碼: 3fy5


實現的功能截圖:

登錄:


菜單及總功能界面:




具體代碼:

Test類(測試類)

package mybank;

import javafx.scene.layout.Pane;

import javax.swing.*;

import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Test {
    public static List<Account> usersList;
    public static Account currentAccount;//登錄的用戶
    public static File file;//當前用戶的記錄文件
    public static StringBuilder recordString=new StringBuilder();//登錄後讀取文本中的記錄,然後和recordString拼接
    public static Menu menu;//靜態的菜單界面,用於在更換密碼後關閉菜單界面
    public static File usersFile;
    public static StringBuilder usersString=new StringBuilder();


     static Reader fw;

    public static void main(String[] args)throws Exception {

        usersList = new ArrayList<Account>();

        //System.out.println(usersList);
        /**********************用戶文本**********************/
        File users = new File("users.txt");

        if (!users.exists()) {
            try {
                users.createNewFile();
                Writer fw = new FileWriter("users.txt");
                fw.write("admin  12345   88888");
                fw.flush();
                fw.close();
            } catch (Exception e1) {
                JOptionPane.showMessageDialog(null, "創建用戶文檔失敗");
            }

        }
        usersFile = users;//創建用戶文檔,存儲用戶賬戶,密碼,餘額信息;
        usersListRead();
        usersListUpdate();
        /*****************************Login****************************/

        LoginGui loginGui = new LoginGui();
    }
    public static void usersListRead()
    {
        /**********************按照用戶文檔讀取用戶列表並創建所有用戶**********************/
        /**********************並寫入list**********************/
        try {
            fw = new FileReader("users.txt");//字符流
        } catch (Exception e) {
            System.out.println("字符流創建失敗");
        }

        BufferedReader bfr = new BufferedReader(fw);

        String temp = "";
        try {

            System.out.println("開始寫入list");
            while ((temp = bfr.readLine()) != null) {//不知爲何讀取出的字符串中最前面會出現Null
                String[] tmpstr = new String[5];
                tmpstr = temp.split("\\s+");//分割空格

                System.out.println("餘額:" + tmpstr[2]);

                Account a = new Account(tmpstr[0], tmpstr[1], tmpstr[2]);
                usersList.add(a);
                System.out.println("讀取到"+a.id+",實例化用戶" + a.id);

            }
            bfr.close();
            fw.close();
            System.out.println("用戶list:"+usersList);
        } catch (Exception e) {
            System.out.println("讀取用戶文檔失敗");
        }
    }





    public static void usersListUpdate()
    {



        /**********************按照list內容寫入文本用戶信息**********************/
        try {
        Writer fw = new FileWriter("users.txt");

        StringBuilder tmpstr = new StringBuilder();
        for (int i = 0; i < usersList.size(); i++) {
           // System.out.println(Test.currentAccount.id);
            tmpstr.append(usersList.get(i).id + "    " + usersList.get(i).password + "    " + usersList.get(i).money + "\r\n");

            //fw.write(Test.currentAccount.id + "    " + Test.currentAccount.password + "    " + Test.currentAccount.money+"\r\n");
        }
        fw.write(tmpstr.toString());
        fw.flush();
        fw.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
        System.out.println("更新用戶失敗");
    }

    }
}

Account類(賬戶類)

方法主要寫在這裏面。

package mybank;
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<Test.usersList.size();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();


    }



}


LoginGui類(登錄界面)

package mybank;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;


public class LoginGui implements ActionListener{//實現監聽器的接口
    private JFrame frame;
    private JPanel p0,p1,p2,p3,p4;//p4包括確認密碼時的輸入框;點擊register按鈕出現

    private JTextField userName;
    private JTextField passWord,passwordCheck;
    private JButton login;
    private JButton register;
    private Reader fw;
    Boolean regirsterable=true;//控制是否能註冊的變量


    public LoginGui() {
        frame=new JFrame("登錄ATM");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//設置窗口的點擊右上角的x的處理方式,這裏設置的是退出程序
        p0=new JPanel();

        p0.add(new JLabel("中國郵政儲蓄銀行ATM"));
        frame.add(p0);

        p1=new JPanel();
        p1.add(new JLabel("\tUserName:"));
        userName=new JTextField(20);
        p1.add(userName);

        p2=new JPanel();
        p2.add(new JLabel("\tPassword:"));
        passWord=new JTextField(20);
        p2.add(passWord);


        p3=new JPanel();

        login=new JButton("     Login    ");
        register=new JButton("   Register   ");
        p3.add(login);
        p3.add(register);

        p4=new JPanel();
        p4.add(new JLabel("PasswordCheck:"));
        passwordCheck=new JTextField(20);
        p4.add(passwordCheck);


        frame.add(p1);
        frame.add(p2);
        frame.add(p4);//確認密碼框
        frame.add(p3);


        frame.pack();
        frame.setVisible(true);
        p4.setVisible(false);
        show();
        /*****************************Login****************************/
    }



    public void show(){

        login.addActionListener(this);//添加監視器
        register.addActionListener(this);
        frame.setBounds(500,500,350,250);//設置大小
        frame.setLayout(new FlowLayout());//設置流式佈局
    }




    @Override
    public void actionPerformed(ActionEvent e) {

        if(e.getActionCommand().equals("   Register   ")) {//註冊,如果監聽器獲得的按鈕文本是這個,也就是點擊的按鈕文本是這個的話,執行下面的語句
            if(p4.isVisible()==false)
            {
                p4.setVisible(true);//檢查密碼輸入欄
                login.setText("     Cancel    ");//將login文本改爲cancel,同時也能觸發作爲Cancel的監聽器
                regirsterable=true;
                return;
            }
            if(regirsterable==true) {
                if (userName.getText().equals(""))//如果userName的文本是空
                {
                    JOptionPane.showMessageDialog(frame, "用戶名不能爲空");//彈窗
                    return;
                }


                for (int i = 0; i < Test.usersList.size(); i++) {

                    if (Test.usersList.get(i).id.equals(userName.getText())) {
                        JOptionPane.showMessageDialog(frame, "該用戶已被註冊");
                        userName.setText("");//清空輸入框
                        passWord.setText("");
                        passwordCheck.setText("");
                        return;//如果同名,結束方法,不在運行下面的語句
                    }

                }
                //如果執行到這裏說明找到用戶名
                if (passWord.getText().equals("")) {
                    JOptionPane.showMessageDialog(frame, "密碼不能爲空,請重新輸入");
                    return;

                } else {
                    if (passwordCheck.getText().equals(passWord.getText())) {
                        Account registeraccount = new Account(userName.getText(), passWord.getText(), "0");//實例化此賬號
                        JOptionPane.showMessageDialog(frame, "註冊成功,請登錄");
                        Test.usersList.add(registeraccount);//加入Test類的靜態用戶list
                        Test.usersListUpdate();//更新用戶文檔

                        return;
                    } else {
                        JOptionPane.showMessageDialog(frame, "兩次輸入的密碼不一致,請重新輸入");
                        return;
                    }


                }
            }


            }
        if(e.getActionCommand().equals("     Login    ")){
            for (int i = 0; i < Test.usersList.size(); i++) {

                if (Test.usersList.get(i).id.equals(userName.getText())) {

                    if(passWord.getText().equals(Test.usersList.get(i).password))
                    {
                        JOptionPane.showMessageDialog(frame, "登錄成功");
                        Test.currentAccount=Test.usersList.get(i);//將list中符合登陸輸入的賬戶密碼的類設爲當前Test類中的靜態的“當前類”,以便後面各種操作;
                        Test.file=new File(Test.currentAccount+".txt");
                        Test.recordString=new StringBuilder();//清空,避免將上一個用戶的記錄寫進新登錄的用戶中
                        //Test.recordString.append("");//避免recordString空指針
                        Menu menu=new Menu();//實例化菜單窗口

                        Test.menu=menu;
                        frame.setVisible(false);//隱藏登陸窗口

                        /************************創建記錄文件**********************/
                        File records = new File(Test.currentAccount.id+".txt");//以賬戶id命名
                        if(!records.exists())
                        {
                            try {
                                records.createNewFile();
                            }
                            catch (Exception e1)
                            {
                                JOptionPane.showMessageDialog(null, "創建該用戶的記錄失敗");
                            }
                        }
                        Test.file=records;
                        /*****************讀取記錄文件************/
                        try {
                             fw = new FileReader(Test.file);//字符流
                        }
                        catch (Exception e1)
                        {
                            JOptionPane.showMessageDialog(null, "讀取記錄失敗");
                        }
                        BufferedReader bfr=new BufferedReader(fw);

                        String temp="";
                        try {


                            while ((temp = bfr.readLine()) != null) {//不知爲何讀取出的字符串中最前面會出現Null,但不影響使用
                                Test.recordString .append(temp);//讀取原先該賬戶的記錄的每一行並拼接到Test.recordString中,在inqury類中輸出作爲查詢記錄的結果
                            }
                            //將記錄讀取並合併爲一個字符串



                            fw.close();
                        }
                        catch (Exception e1)
                        {
                            System.out.println("讀取記錄過程中出現錯誤");
                        }


                        return;
                    }
                    else
                    {
                        JOptionPane.showMessageDialog(frame, "密碼錯誤");
                        passwordCheck.setText("");
                        return;
                    }

                }
            }
            JOptionPane.showMessageDialog(frame, "該用戶不存在");


        }
        if(e.getActionCommand().equals("     Cancel    "))
        {
            p4.setVisible(false);
            login.setText("     Login    ");
            regirsterable=false;//不可註冊
        }


    }
}


Menu類(菜單界面)

package mybank;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Menu implements ActionListener{
    public JFrame mframe;
    private JPanel mp0,mp1,mp2,mp3,mp4;//p4是確認密碼;點擊register按鈕石出現


    private JTextField passWord,passwordCheck;
    private JButton inqury;
    private JButton outmoney;
    private JButton transfer;
    private JButton inmoney;
    private JButton changepassword;


    public Menu()
    {
        mframe=new JFrame();
        mframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        JButton inqury=new JButton("查詢");
        JButton outmoney=new JButton("取款");
        JButton transfer=new JButton("轉賬");
        JButton inmoney=new JButton("存款");
        JButton changepassword=new JButton("更改密碼");
        JButton exit=new JButton("退卡");
        mp0=new JPanel();
        mp0.add(new JLabel("選擇項目"));
        mframe.add(mp0);
        mp1=new JPanel();

        mp1.add(inmoney);
        mp1.add(inqury);
        mp1.add(outmoney);
        mp1.add(transfer);
        mp1.add(changepassword);
        mp1.add(exit);

        mp1.setLayout(new GridLayout(3,2,20,20));
        mframe.add(mp1);
        mframe.pack();
        mframe.setVisible(true);
        mframe.setLayout(new FlowLayout());
        mframe.setBounds(500,500,450,300);
        inqury.addActionListener(this);//綁定監聽器
        inmoney.addActionListener(this);
        outmoney.addActionListener(this);
        transfer.addActionListener(this);
        changepassword.addActionListener(this);
        exit.addActionListener(this);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String cmd=e.getActionCommand();//cmd賦值爲點擊的按鈕的值
        if(cmd.equals("查詢"))
        {
        Inqury inquryGui=new Inqury();
        }
        else if(cmd.equals("取款"))
        {
        OutMoney outMoneyGui=new OutMoney();
        }
        else if(cmd.equals("存款"))
        {
        InMoney inMoney=new InMoney();
        }else if(cmd.equals("轉賬"))
        {
        Transfer transfer=new Transfer();
        }else if(cmd.equals("更改密碼"))
        {
        ChangePassword changePassword=new ChangePassword();
        }
        else if(cmd.equals("退卡")){

            mframe.setVisible(false);//隱藏
            LoginGui loginGui=new LoginGui();
            JOptionPane.showMessageDialog(null,"請記得取走你的銀行卡");
        }
       
    }
}


InMoney類(存款界面)

package mybank;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;

public class InMoney implements ActionListener{
    public JTextField money;
    public JFrame iframe;
    public JPanel ip0,ip1,ip2,ip3;
    public JButton confirm,cancel,exit;
    public JLabel yue;
    public InMoney() {
        iframe=new JFrame("存款");

        ip0=new JPanel();
        ip0.add(new JLabel("賬戶id:"+Test.currentAccount.id));

        ip1=new JPanel();
        yue=new JLabel("賬戶餘額:"+Test.currentAccount.money);
        ip1.add(yue);

        ip2=new JPanel();
        ip2.add(new JLabel("存款金額:"));
        money=new JTextField(20);
        ip2.add(money);

        ip3=new JPanel();
        confirm=new JButton("確定");
        ip3.add(confirm);
        cancel=new JButton("返回");
        ip3.add(cancel);

        iframe.add(ip0);
        iframe.add(ip1);
        iframe.add(ip2);
        iframe.add(confirm);
        iframe.add(cancel);
        iframe.setLayout(new FlowLayout());
        iframe.setVisible(true);

        iframe.setBounds(500,500,350,300);
        confirm.addActionListener(this);//綁定監聽器

        cancel.addActionListener(this);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("確定"))//按下確定按鈕
        {
            try {

                Test.currentAccount.inMoney(Integer.parseInt(money.getText()));//調用當前登陸賬戶的存錢函數

                JOptionPane.showMessageDialog(null, "存款成功");//彈窗
                yue.setText("賬戶餘額:"+Test.currentAccount.money);
            }
            catch (ClassCastException e1)//捕獲當前登錄賬戶中inmoney函數中的異常。類型轉換異常
            {

                JOptionPane.showMessageDialog(null, "輸入數據類型錯誤,請輸入整數");

            }
            catch (Exception e1)//
            {
                JOptionPane.showMessageDialog(null, e1.getMessage());
            }
        }
        else
        {
        iframe.setVisible(false);//隱藏

        }
    }
}


Inqury類(查詢類)

package mybank;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Inqury implements ActionListener{
    public JFrame iframe;
    public JPanel ip0,ip1,ip2;
    public JTextArea inquryresult;
    public JButton confirm,cancel,exit;
    public JLabel yue;
    public Inqury() {
        iframe=new JFrame("查詢");

        ip0=new JPanel();
        ip0.add(new JLabel("賬戶id:"+Test.currentAccount.id));
        ip1=new JPanel();
        yue=new JLabel("賬戶餘額:"+Test.currentAccount.money);
        ip1.add(yue);
        ip2=new JPanel();
        inquryresult=new JTextArea(10,30);
        ip2.add(inquryresult);
        confirm=new JButton("查詢記錄");
        confirm.addActionListener(this);
        iframe.add(ip0);
        iframe.add(ip1);
        iframe.add(ip2);
        iframe.add(confirm);
        iframe.setLayout(new FlowLayout());
        iframe.setVisible(true);

        iframe.setBounds(500,500,350,300);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("查詢記錄"));
        {
            //Test.recordString是從賬戶文檔中度去處的字符串
            //在寫入文本時/r/n纔是換行,但在java中\r\n則是兩個換行,而且Test.recordString是一行一行讀取出來的拼接上的,所以並沒有換行符,所以這裏替換成一個\n
            inquryresult.setText(Test.recordString.toString().replace("元","元\n").replace("null",""));//去除掉結果字符串中的null,並將元替換爲元\r\n來換行換行
            yue.setText("賬戶餘額:"+Test.currentAccount.money);//更新顯示餘額
        }
    }
}

OutMoney類(取款)


package mybank;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;

public class OutMoney implements ActionListener{
    public JTextField money;
    public JFrame iframe;
    public JPanel ip0,ip1,ip2,ip3;
    public JButton confirm,cancel,exit;
    public JLabel yue;//餘額
    public OutMoney() {
        iframe=new JFrame("取款");

        ip0=new JPanel();
        ip0.add(new JLabel("賬戶id:"+Test.currentAccount.id));

        ip1=new JPanel();
        yue=new JLabel("賬戶餘額:"+Test.currentAccount.money);
        ip1.add(yue);

        ip2=new JPanel();
        ip2.add(new JLabel("取款金額:"));
        money=new JTextField(20);
        ip2.add(money);

        ip3=new JPanel();
        confirm=new JButton("確定");
        ip3.add(confirm);
        cancel=new JButton("返回");
        ip3.add(cancel);

        iframe.add(ip0);
        iframe.add(ip1);
        iframe.add(ip2);
        iframe.add(confirm);
        iframe.add(cancel);
        iframe.setLayout(new FlowLayout());
        iframe.setVisible(true);

        iframe.setBounds(500,500,350,300);
        confirm.addActionListener(this);

        cancel.addActionListener(this);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("確定"))//點擊確定按鈕
        {
            try {

                Test.currentAccount.outMoney(Integer.parseInt(money.getText()));

                JOptionPane.showMessageDialog(null, "取款成功");//彈窗
                yue.setText("賬戶餘額:"+Test.currentAccount.money);//更新餘額顯示
            }
            catch (ClassCastException e1)
            {

                JOptionPane.showMessageDialog(null, "輸入數據類型錯誤,請輸入整數");//捕獲Test類中outmoney方法的異常

            }
            catch (Exception e1)
            {
                JOptionPane.showMessageDialog(null, e1.getMessage());
            }
        }
        else
        {
            iframe.setVisible(false);//隱藏

        }
    }
}

Transfer類(轉賬)


package mybank;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Transfer implements ActionListener{
    public JTextField money,other;
    public JFrame iframe;
    public JPanel ip0,ip1,ip2,ip3,ip4;
    public JButton confirm,cancel,exit;
    public JLabel yue;
    public Transfer() {
        iframe=new JFrame("轉賬");

        ip0=new JPanel();
        ip0.add(new JLabel("賬戶id:"+Test.currentAccount.id));

        ip1=new JPanel();
        yue=new JLabel("賬戶餘額:"+Test.currentAccount.money);
        ip1.add(yue);

        ip2=new JPanel();
        ip2.add(new JLabel("轉賬賬戶id:"));
        other=new JTextField(10);
        ip2.add(other);

        ip4=new JPanel();
        ip4.add(new JLabel("轉賬金額:"));
        money=new JTextField(10);
        ip4.add(new JLabel("<html><br/><html>"));//換行
        ip4.add(money);

        ip3=new JPanel();
        confirm=new JButton("確定");
        ip3.add(confirm);
        cancel=new JButton("返回");
        ip3.add(cancel);

        iframe.add(ip0);
        iframe.add(ip1);
        iframe.add(ip2);
        iframe.add(ip4);
        iframe.add(ip3);
        iframe.setLayout(new FlowLayout());
        iframe.setVisible(true);

        iframe.setBounds(500,500,350,300);
        confirm.addActionListener(this);

        cancel.addActionListener(this);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("確定"))
        {
            try {

                Test.currentAccount.transfer(Integer.parseInt(money.getText()),other.getText());

                yue.setText("賬戶餘額:"+Test.currentAccount.money);//更新面板上的餘額
            }

            catch (Exception e1)
            {
                JOptionPane.showMessageDialog(null, e1.getMessage());
            }
        }
        else
        {
            iframe.setVisible(false);

        }
    }
}

ChangePassword類(更改密碼)

package mybank;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ChangePassword implements ActionListener{
    public JTextField 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 JTextField(20);
        ip2.add(new JLabel("<html><br/><html>"));//換行
        ip2.add(oldPassword);

        ip0=new JPanel();
        ip0.add(new JLabel("新密碼:"));
        newPassword=new JTextField(20);
        ip0.add(new JLabel("<html><br/><html>"));//換行
        ip0.add(newPassword);

        ip4=new JPanel();
        ip4.add(new JLabel("再次輸入新密碼:"));
        checkPassword=new JTextField(20);
        ip4.add(new JLabel("<html><br/><html>"));//換行
        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.setBounds(500,500,350,300);
        confirm.addActionListener(this);

        cancel.addActionListener(this);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("確定")) {
            if (Test.currentAccount.password.equals(oldPassword.getText())) {
                try {
                    if(newPassword.getText().equals(checkPassword.getText())) {

                        Test.currentAccount.ChangePassword(newPassword.getText());
                        JOptionPane.showMessageDialog(null, "更改密碼成功");
                        iframe.setVisible(false);
                        Test.menu.mframe.setVisible(false);//關閉菜單界面
                        new LoginGui();
                    }
                    else
                    {
                        JOptionPane.showMessageDialog(null, "兩次輸入的密碼不一致");
                    }
                }
             catch (Exception e1) {//捕獲賬戶類中更改密碼函數的異常並彈窗顯示
                    JOptionPane.showMessageDialog(null, e1.getMessage());
                }
            } else {

                JOptionPane.showMessageDialog(null, "原密碼錯誤");
            }
        }
        else//如果點擊cancel
        {
            iframe.setVisible(false);
        }
    }
}




本人是一名大二的學生,目前只學了一學期的java,因此有很多不規範的地方,請見諒。

這個程序技術含量並不高,在計劃做的時候覺得很簡單,但實際上寫的時候,發現有一些細節邏輯比較難以處理和混亂,花費了我一週的時間,其中修復邏輯上的bug花了60%的時間,所以想發出來紀念一下。

我第一次寫這麼大的java程序,所以可能會有些混亂和有些多餘的代碼,但我詳細的寫了註釋。希望能幫助到

需要幫助的朋友。若有問題,歡迎交流。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章