使用JavaFX设计银行系统

版本三:基于JavaFX设计可视化银行操作系统

1. 题目说明

请参照银行的 ATM 机界面,在 Account2 类的基础上开发一个 GUI 界面的 ATM 系统。 (Account2位于版本二中)

要求界面应模拟小键盘,并且账户信息读、写于文件 accounts.dat。

2. 分析过程

需要做一个ATM系统,根据题目需求,我们需要有登录界面、菜单界面、取款界面、存款界面、修改密码界面。

因此当运行程序的时候会出现一个用户登录界面,如果用户有账户,可以直接登入,界面中出现UserName、Password及对应的文本框,当用户输入完id和姓名的时候,点击Login按钮,如果密码和用户输入正确,则提示用户登录成功,如果密码个用户输入错误,则提示用户输入错误;如果选择注册,则在界面显示UserName、Password、CheckPassword以及对应的文本框,提示用户输入成功,然后再进行登录。

当用户登录成功后,进入菜单界面,菜单界面包括存款、取款、修改密码、查询、退卡等按钮,用户可以点击其中的按钮,进行相应的操作,每一个按钮都有一个事件处理方法,处理方法的内容是创建相应的界面。

(1) 当用户点击存款的时候,弹出存款面板,在面板的上面会显示中账户和账户余额,还有一个文本框供用户输入需要存款的金额,在程序中,我使用getText()方法获取文本域的内容,然后将其中的账户的金额加上输入的要存款的金额,以此来修改账户的余额,内设一个Quit按钮,当按下这个按钮时,会返回菜单界面。

(2) 当用户点击取款的时候,弹出取款面板,在面板的上面会显示中账户和账户余额,还有一个文本框供用户输入需要取款的金额,在程序中,我使用getText()方法获取文本域的内容,然后将其中的账户的金额减去输入的要取款的金额,以此来修改账户的余额,如果账户的余额小于用户要取款的金额,会报出余额不足的错误,内设一个Quit按钮,当按下这个按钮时,会返回菜单界面。

(3) 当用户点击修改密码的时候,弹出修改密码的面板,面板里原密码、新密码、再次输入新密码以及相应的文本域,这里使用一个多重判断,当原始密码与用户的密码相同,然后输入新密码,再一次输入密码,当两次密码一致,则更改密码成功,否则报两次密码不一致的错误,当原密码和用户密码不一致,则报出原密码错误。

(4) 当用户点击查询的时候,弹出查询记录界面,界面上面会显示账户id和账户余额,采用HBoxfang放置Label、文本域、按钮,采用HBox放置按钮,并将HBox放置在VBox的最下方,当用户点击查询记录,在文本域中会出现对应的时间、存款和取款记录,当用户点击Quit按钮,会返回菜单界面。

(5) 当用户点击退卡的时候,返回登录界面,并提醒用户“请取走你的银行卡”,程序中创建一个LoginGui对象,并调用start方法显示登录界面。

(6) Test类中除了有start方法,还有usersListRead()和usersListUpdate()方法

UsersListRead():从文件中读取用户的信息

UsersListUpdate():更新用户的信息

3.系统测试

用户登录界面

使用JavaFX设计银行系统_第1张图片

菜单界面

使用JavaFX设计银行系统_第2张图片

存款界面

使用JavaFX设计银行系统_第3张图片

取款界面

使用JavaFX设计银行系统_第4张图片

修改密码界面

使用JavaFX设计银行系统_第5张图片

查询界面

使用JavaFX设计银行系统_第6张图片

退卡界面

使用JavaFX设计银行系统_第7张图片

4.代码展示

4.1Account类

package version3;

import java.io.FileWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Account {
    int money;
    String id;//账号名

    String password;
    Date now=new Date();
    Date currentTime;
    SimpleDateFormat formatter;

    public Account(String id, String password, String money) {//构造方法
        this.id = id;
        this.password = password;
        this.money= Integer.parseInt(money);
    }

    public void withDraw (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 dePosit(int money)throws Exception
    {
        try {
            Writer 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"+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 ChangePassword(String newPassword)throws Exception
    {
     if(newPassword.equals(this.password))
     {
         throw new Exception("原密码和新密码不能一样");
     }

     else
     {
         if(newPassword.equals(""))
         {
             throw new Exception("密码不能为空");
         }

     }
     password=newPassword;
        Test.usersListUpdate();
    }
}

4.2LoginGui类

package version3;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;

public class LoginGui extends Application{//实现监听器的接口
    String currentNum = "";
    Boolean isRegister = true;
    private Reader fw;
    public void start(Stage primaryStage) {
        GridPane gridPane = new GridPane();
        gridPane.setAlignment(Pos.CENTER);
        gridPane.setPadding(new Insets(20, 20, 20, 20));
        gridPane.setHgap(30);
        gridPane.setVgap(30);

        Label label1 = new Label("UserName");
        TextField userName = new TextField();
        Label label2 = new Label("Password");
        TextField password = new TextField();
        Label label3 = new Label("CheckPassword");
        TextField passwordCheck = new TextField();

        gridPane.add(label1, 0, 0);
        gridPane.add(userName, 1, 0);
        gridPane.add(label2, 0, 1);
        gridPane.add(password, 1, 1);
        gridPane.add(label3, 0, 2);
        gridPane.add(passwordCheck, 1, 2);
        label3.setVisible(false);
        passwordCheck.setVisible(false);

        Button btLogin = new Button("Login");
        Button btRegister = new Button("Register");
        gridPane.add(btLogin, 0, 3);
        gridPane.add(btRegister, 1, 3);
        GridPane.setHalignment(btRegister, HPos.RIGHT);

        VBox pane = new VBox();
        Scene scene = new Scene(pane, 800, 500);

        Text text = new Text("中国邮政储蓄银行");
        text.setFont(Font.font("Courier", FontWeight.BOLD, FontPosture.ITALIC, 20));

        GridPane layout = new GridPane();
        layout.setAlignment(Pos.CENTER);

        //Add styling properties to the stage
        layout.setPadding(new Insets(10));
        layout.setHgap(5);
        layout.setVgap(5);

        //Set width for each column in the grid
        layout.getColumnConstraints().add(new ColumnConstraints(150));
        layout.getColumnConstraints().add(new ColumnConstraints(150));
        layout.getColumnConstraints().add(new ColumnConstraints(150));
        layout.getColumnConstraints().add(new ColumnConstraints(150));

        Button btnClear = new Button("Clear");
        Button btnEnter = new Button("Enter");
        Button btn0 = new Button("0");
        Button btn1 = new Button("1");
        Button btn2 = new Button("2");
        Button btn3 = new Button("3");
        Button btn4 = new Button("4");
        Button btn5 = new Button("5");
        Button btn6 = new Button("6");
        Button btn7 = new Button("7");
        Button btn8 = new Button("8");
        Button btn9 = new Button("9");

        //Set styling properties for the buttons
        btnClear.setMaxWidth(Double.MAX_VALUE);
        btnEnter.setMaxWidth(Double.MAX_VALUE);
        btn0.setMaxWidth(Double.MAX_VALUE);
        btn1.setMaxWidth(Double.MAX_VALUE);
        btn2.setMaxWidth(Double.MAX_VALUE);
        btn3.setMaxWidth(Double.MAX_VALUE);
        btn4.setMaxWidth(Double.MAX_VALUE);
        btn5.setMaxWidth(Double.MAX_VALUE);
        btn6.setMaxWidth(Double.MAX_VALUE);
        btn7.setMaxWidth(Double.MAX_VALUE);
        btn8.setMaxWidth(Double.MAX_VALUE);
        btn9.setMaxWidth(Double.MAX_VALUE);

        //Add the UI elements to the layout
        layout.add(btn1, 1, 2);
        layout.add(btn2, 2, 2);
        layout.add(btn3, 3, 2);
        layout.add(btn4, 1, 3);
        layout.add(btn5, 2, 3);
        layout.add(btn6, 3, 3);
        layout.add(btn7, 1, 4);
        layout.add(btn8, 2, 4);
        layout.add(btn9, 3, 4);
        layout.add(btn0, 1, 5);
        layout.add(btnClear, 2, 5);
        layout.add(btnEnter, 3, 5);

        pane.getChildren().addAll(text, gridPane, layout);
        pane.setAlignment(Pos.CENTER);

        primaryStage.setTitle("ATM");
        primaryStage.setScene(scene);
        primaryStage.show();

        userName.setText(currentNum);
        password.setText(currentNum);
        passwordCheck.setText(currentNum);

        btRegister.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                if (label3.isVisible() == false && passwordCheck.isVisible() == false) {
                    label3.setVisible(true);
                    passwordCheck.setVisible(true);
                    btLogin.setText("Cancel");
                    isRegister = true;
                    return;
                }
                if (isRegister == true) {
                    if (userName.getText().equals("")) {
                        Alert alert = new Alert(Alert.AlertType.INFORMATION);
                        alert.setTitle("Information Dialog");
                        alert.setHeaderText("用户名不能为空");
                        alert.setContentText("");

                        alert.showAndWait();
                        return;
                    }

                    for (int i = 0; i < Test.usersList.size(); i++) {
                        if (Test.usersList.get(i).id.equals(userName.getText())) {
                            Alert alert = new Alert(Alert.AlertType.INFORMATION);
                            alert.setTitle("Information Dialog");
                            alert.setHeaderText("Look, an Information Dialog");
                            alert.setContentText("该用户已被注册");

                            alert.showAndWait();
                            return;
                        }
                    }

                    if (password.getText().equals("")) {
                        Alert alert = new Alert(Alert.AlertType.INFORMATION);
                        alert.setTitle("Information Dialog");
                        alert.setHeaderText("Look, an Information Dialog");
                        alert.setContentText("密码不能为空,请重新输入");

                        alert.showAndWait();
                    } else {
                        if (passwordCheck.getText().equals(password.getText())) {
                            Account registerAccount = new Account(userName.getText(), password.getText(), "0");
                            Alert alert = new Alert(Alert.AlertType.INFORMATION);
                            alert.setTitle("Information Dialog");
                            alert.setHeaderText("Look, an Information Dialog");
                            alert.setContentText("注册成功,请登录!");

                            alert.showAndWait();

                            Test.usersList.add(registerAccount);//加入Test类的静态用户list
                            Test.usersListUpdate();//更新用户文档
                            return;
                        } else {
                            Alert alert = new Alert(Alert.AlertType.INFORMATION);
                            alert.setTitle("Information Dialog");
                            alert.setHeaderText("Look, an Information Dialog");
                            alert.setContentText("两次输入的密码不一致,请重新输入");

                            alert.showAndWait();
                        }
                    }
                }
            }
        });

        btLogin.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                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)) {
                            Alert alert = new Alert(Alert.AlertType.INFORMATION);
                            alert.setTitle("Information Dialog");
                            alert.setHeaderText("Look, an Information Dialog");
                            alert.setContentText("登录成功");

                            alert.showAndWait();

                            Test.currentAccount = Test.usersList.get(i);//将list中符合登陆输入的账户密码的类设为当前Test类中的静态的“当前类”,以便后面各种操作;
                            Test.file = new File(Test.currentAccount + ".txt");
                            Test.recordString = new StringBuilder();//清空,避免将上一个用户的记录写进新登录的用户中
                            Menu menu = new Menu();//实例化菜单窗口

                            Test.menu = menu;

                            menu.start(primaryStage);
                            pane.setVisible(false);//隐藏登陆窗口

                            File records = new File(Test.currentAccount.id + ".txt");//以账户id命名
                            if (!records.exists()) {
                                try {
                                    records.createNewFile();
                                } catch (Exception e1) {
                                    Alert alert1 = new Alert(Alert.AlertType.INFORMATION);
                                    alert1.setTitle("Information Dialog");
                                    alert1.setHeaderText("Look, an Information Dialog");
                                    alert1.setContentText("创建该用户的记录失败");

                                    alert1.showAndWait();
                                }
                            }
                            Test.file = records;
                            /*****************读取记录文件************/
                            try {
                                fw = new FileReader(Test.file);//字符流
                            } catch (Exception e1) {
                                Alert alert1 = new Alert(Alert.AlertType.INFORMATION);
                                alert1.setTitle("Information Dialog");
                                alert1.setHeaderText("Look, an Information Dialog");
                                alert1.setContentText("读取记录失败");

                                alert1.showAndWait();
                            }
                            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 {
                            Alert alert1 = new Alert(Alert.AlertType.INFORMATION);
                            alert1.setTitle("Information Dialog");
                            alert1.setHeaderText("Look, an Information Dialog");
                            alert1.setContentText("密码错误");

                            alert1.showAndWait();

                            passwordCheck.setText("");
                            return;
                        }

                    }
                }
                if (btLogin.getText().equals("Cancel")) {
                    label3.setVisible(false);
                    passwordCheck.setVisible(false);
                    btLogin.setText("     Login    ");
                    isRegister = false;//不可注册
                }
                Alert alert1 = new Alert(Alert.AlertType.INFORMATION);
                alert1.setTitle("Information Dialog");
                alert1.setHeaderText("Look, an Information Dialog");
                alert1.setContentText("用户不存在");

                alert1.showAndWait();
            }
        });
        btn0.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                currentNum += "0";
                password.setText(currentNum);
            }
        });

        //Handle the events related to the 1 button
        btn1.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                currentNum += "1";
                password.setText(currentNum);
            }
        });

        //Handle the events related to the 2 button
        btn2.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                currentNum += "2";
                password.setText(currentNum);
            }
        });

        //Handle the events related to the 3 button
        btn3.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                currentNum += "3";
                password.setText(currentNum);
            }
        });

        //Handle the events related to the 4 button
        btn4.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                currentNum += "4";
                password.setText(currentNum);
            }
        });

        //Handle the events related to the 5 button
        btn5.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                currentNum += "5";
                password.setText(currentNum);
            }
        });

        //Handle the events related to the 6 button
        btn6.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                currentNum += "6";
                password.setText(currentNum);
            }
        });

        //Handle the events related to the 7 button
        btn7.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                currentNum += "7";
                password.setText(currentNum);
            }
        });


        //Handle the events related to the 8 button
        btn8.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                currentNum += "8";
                password.setText(currentNum);
            }
        });


        //Handle the events related to the 9 button
        btn9.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                currentNum += "9";
                password.setText(currentNum);
            }
        });


        //Handle the events related to the Clear button
        btnClear.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                password.setText("");
                currentNum = "";
            }
        });
    }
}

4.3Menu类

package version3;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Menu extends Application{
    public void start(Stage primaryStage){
        Text text = new Text("选择项目");
        text.setFont(Font.font("Courier", FontWeight.BOLD, FontPosture.ITALIC, 20));

        GridPane gridPane = new GridPane();
        gridPane.setAlignment(Pos.CENTER);
        gridPane.setHgap(20);
        gridPane.setVgap(20);

        //Set width for each column in the grid
        gridPane.getColumnConstraints().add(new ColumnConstraints(150));
        gridPane.getColumnConstraints().add(new ColumnConstraints(150));
        gridPane.getColumnConstraints().add(new ColumnConstraints(150));
        gridPane.getColumnConstraints().add(new ColumnConstraints(150));

        Button btDeposit = new Button("存款");
        Button btWithDraw = new Button("取款");
        Button btCheck = new Button("查询");
        Button btChangePassword = new Button(" 修改密码 ");
        Button btRefund = new Button("退卡");

        btDeposit.setMaxWidth(Double.MAX_VALUE);
        btWithDraw.setMaxWidth(Double.MAX_VALUE);
        btCheck.setMaxWidth(Double.MAX_VALUE);
        btChangePassword.setMaxWidth(Double.MAX_VALUE);
        btRefund.setMaxWidth(Double.MAX_VALUE);

        gridPane.add(text, 0, 1);
        gridPane.add(btDeposit, 1, 0);
        gridPane.add(btWithDraw, 1, 1);
        gridPane.add(btChangePassword, 1,2);
        gridPane.add(btCheck, 2,0);
        gridPane.add(btRefund, 2,1);

        StackPane pane = new StackPane(gridPane);

        Scene scene = new Scene(pane, 800, 300);
        primaryStage.setTitle("Menu");
        primaryStage.setScene(scene);
        primaryStage.show();

        btDeposit.setOnAction((e) ->{
            Deposit deposit = new Deposit();
            deposit.start(primaryStage);
        });

        btWithDraw.setOnAction((e)->{
            WithDraw withDraw = new WithDraw();
            withDraw.start(primaryStage);
        });

        btCheck.setOnAction((e)->{
            Check check = new Check();
            check.start(primaryStage);
        });

        btChangePassword.setOnAction((e) ->{
            ChangePassword changePassword = new ChangePassword();
            changePassword.start(primaryStage);
        });

        btRefund.setOnAction((e)->{
            pane.setVisible(false);
            LoginGui loginGui = new LoginGui();
            loginGui.start(primaryStage);
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("Information Dialog");
            alert.setHeaderText("Look, an Information Dialog");
            alert.setContentText("请取走你的银行卡");

            alert.showAndWait();
        });
    }
}

4.4Withdraw类

package version3;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.*;
import javafx.stage.Stage;

public class WithDraw extends Application{
    String currentNum = "";
    public void start(Stage primaryStage){
        String text1 = "账户id:" + Test.currentAccount.id;
        Label label1 = new Label(text1);


        String text2 = "账户余额:" + Test.currentAccount.money;
        Label label2 = new Label(text2);

        String text3 = "取款金额";
        Label label3 = new Label(text3);
        TextField textField = new TextField();


        GridPane gridPane = new GridPane();
        gridPane.setAlignment(Pos.CENTER);
        gridPane.setPadding(new Insets(20, 20, 20, 20));
        gridPane.setHgap(5);
        gridPane.setVgap(40);
        gridPane.add(label1, 0, 0);
        gridPane.add(label2, 1,0);
        gridPane.add(label3, 0, 1);
        gridPane.add(textField, 1,1);

        GridPane layout = new GridPane();
        layout.setAlignment(Pos.CENTER);

        //Add styling properties to the stage
        layout.setPadding(new Insets(10));
        layout.setHgap(5);
        layout.setVgap(5);

        //Set width for each column in the grid
        layout.getColumnConstraints().add(new ColumnConstraints(150));
        layout.getColumnConstraints().add(new ColumnConstraints(150));
        layout.getColumnConstraints().add(new ColumnConstraints(150));
        layout.getColumnConstraints().add(new ColumnConstraints(150));

        Button btnClear = new Button("Clear");
        Button btnEnter = new Button("Enter");
        Button btn0 = new Button("0");
        Button btn1 = new Button("1");
        Button btn2 = new Button("2");
        Button btn3 = new Button("3");
        Button btn4 = new Button("4");
        Button btn5 = new Button("5");
        Button btn6 = new Button("6");
        Button btn7 = new Button("7");
        Button btn8 = new Button("8");
        Button btn9 = new Button("9");
        Button btnQuit = new Button("Quit");

        //Set styling properties for the buttons
        btnClear.setMaxWidth(Double.MAX_VALUE);
        btnEnter.setMaxWidth(Double.MAX_VALUE);
        btn0.setMaxWidth(Double.MAX_VALUE);
        btn1.setMaxWidth(Double.MAX_VALUE);
        btn2.setMaxWidth(Double.MAX_VALUE);
        btn3.setMaxWidth(Double.MAX_VALUE);
        btn4.setMaxWidth(Double.MAX_VALUE);
        btn5.setMaxWidth(Double.MAX_VALUE);
        btn6.setMaxWidth(Double.MAX_VALUE);
        btn7.setMaxWidth(Double.MAX_VALUE);
        btn8.setMaxWidth(Double.MAX_VALUE);
        btn9.setMaxWidth(Double.MAX_VALUE);
        btnQuit.setMaxWidth(Double.MAX_VALUE);

        //Add the UI elements to the layout
        layout.add(btnQuit, 0, 2);
        layout.add(btn1, 1, 2);
        layout.add(btn2, 2, 2);
        layout.add(btn3, 3, 2);
        layout.add(btn4, 1, 3);
        layout.add(btn5, 2, 3);
        layout.add(btn6, 3, 3);
        layout.add(btn7, 1, 4);
        layout.add(btn8, 2, 4);
        layout.add(btn9, 3, 4);
        layout.add(btn0, 1, 5);
        layout.add(btnClear, 2, 5);
        layout.add(btnEnter, 3, 5);


        VBox pane = new VBox();
        pane.getChildren().add(gridPane);
        pane.getChildren().add(layout);

        Scene scene = new Scene(pane, 800, 300);
        primaryStage.setTitle("OutMoney");
        primaryStage.setScene(scene);
        primaryStage.show();
        btn0.setOnAction((e)->{
                currentNum += "0";
                textField.setText(currentNum);
        });

        //Handle the events related to the 1 button
        btn1.setOnAction((e)->{
                currentNum += "1";
                textField.setText(currentNum);
        });

        //Handle the events related to the 2 button
        btn2.setOnAction((e)->{
                currentNum += "2";
                textField.setText(currentNum);
        });

        //Handle the events related to the 3 button
        btn3.setOnAction((e)->{
                currentNum += "3";
                textField.setText(currentNum);
        });

        //Handle the events related to the 4 button
        btn4.setOnAction((e)->{
                currentNum += "4";
                textField.setText(currentNum);
        });

        //Handle the events related to the 5 button
        btn5.setOnAction((e)->{
                currentNum += "5";
                textField.setText(currentNum);
        });

        //Handle the events related to the 6 button
        btn6.setOnAction((e)->{
                currentNum += "6";
                textField.setText(currentNum);
        });

        //Handle the events related to the 7 button
        btn7.setOnAction((e)->{
                currentNum += "7";
                textField.setText(currentNum);
        });


        //Handle the events related to the 8 button
        btn8.setOnAction((e)->{
                currentNum += "8";
                textField.setText(currentNum);
        });


        //Handle the events related to the 9 button
        btn9.setOnAction((e)->{
                currentNum += "9";
                textField.setText(currentNum);
        });


        //Handle the events related to the Clear button
        btnClear.setOnAction((e)->{
                textField.setText("");
                currentNum = "";
        });

        btnEnter.setOnAction((e)->{
            currentNum += "";
            try {
                Test.currentAccount.withDraw(Integer.parseInt(textField.getText()));
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setTitle("Information Dialog");
                alert.setHeaderText("Look, an Information Dialog");
                alert.setContentText("取款成功");

                alert.showAndWait();
                label2.setText("账户余额:" + Test.currentAccount.money);
            } catch (Exception ex) {
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setTitle("Information Dialog");
                alert.setHeaderText("Look, an Information Dialog");
                alert.setContentText("余额不足!");
                alert.showAndWait();
            }
        });

        btnQuit.setOnAction((e)->{
            pane.setVisible(false);
            new Menu().start(primaryStage);
        });
    }
}

4.5Deposit类

package version3;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Deposit extends Application {
    String currentNum = "";

    //这是一个main方法,是程序的入口
    public static void main(String[] args) {
        Application.launch(args);
    }
    @Override
    public void start(Stage primaryStage){

        String text1 = "账户id:" + Test.currentAccount.id;
        Label label1 = new Label(text1);


        String text2 = "账户余额:" + Test.currentAccount.money;
        Label label2 = new Label(text2);

        String text3 = "存款金额";
        Label label3 = new Label(text3);
        TextField textField = new TextField();


        GridPane gridPane = new GridPane();
        gridPane.setPadding(new Insets(20, 20, 20, 20));
        gridPane.setAlignment(Pos.CENTER);
        gridPane.setHgap(5);
        gridPane.setVgap(40);
        gridPane.add(label1, 0, 0);
        gridPane.add(label2, 1,0);
        gridPane.add(label3, 0, 1);
        gridPane.add(textField, 1,1);

        GridPane layout = new GridPane();
        layout.setAlignment(Pos.CENTER);

        //Add styling properties to the stage
        layout.setPadding(new Insets(10));
        layout.setHgap(5);
        layout.setVgap(5);

        //Set width for each column in the grid
        layout.getColumnConstraints().add(new ColumnConstraints(150));
        layout.getColumnConstraints().add(new ColumnConstraints(150));
        layout.getColumnConstraints().add(new ColumnConstraints(150));
        layout.getColumnConstraints().add(new ColumnConstraints(150));

        Button btnClear = new Button("Clear");
        Button btnEnter = new Button("Enter");
        Button btn0 = new Button("0");
        Button btn1 = new Button("1");
        Button btn2 = new Button("2");
        Button btn3 = new Button("3");
        Button btn4 = new Button("4");
        Button btn5 = new Button("5");
        Button btn6 = new Button("6");
        Button btn7 = new Button("7");
        Button btn8 = new Button("8");
        Button btn9 = new Button("9");
        Button btnQuit = new Button("Quit");

        //Set styling properties for the buttons
        btnClear.setMaxWidth(Double.MAX_VALUE);
        btnEnter.setMaxWidth(Double.MAX_VALUE);
        btn0.setMaxWidth(Double.MAX_VALUE);
        btn1.setMaxWidth(Double.MAX_VALUE);
        btn2.setMaxWidth(Double.MAX_VALUE);
        btn3.setMaxWidth(Double.MAX_VALUE);
        btn4.setMaxWidth(Double.MAX_VALUE);
        btn5.setMaxWidth(Double.MAX_VALUE);
        btn6.setMaxWidth(Double.MAX_VALUE);
        btn7.setMaxWidth(Double.MAX_VALUE);
        btn8.setMaxWidth(Double.MAX_VALUE);
        btn9.setMaxWidth(Double.MAX_VALUE);
        btnQuit.setMaxWidth(Double.MAX_VALUE);

        //Add the UI elements to the layout
        layout.add(btnQuit, 0, 2);
        layout.add(btn1, 1, 2);
        layout.add(btn2, 2, 2);
        layout.add(btn3, 3, 2);
        layout.add(btn4, 1, 3);
        layout.add(btn5, 2, 3);
        layout.add(btn6, 3, 3);
        layout.add(btn7, 1, 4);
        layout.add(btn8, 2, 4);
        layout.add(btn9, 3, 4);
        layout.add(btn0, 1, 5);
        layout.add(btnClear, 2, 5);
        layout.add(btnEnter, 3, 5);

        VBox pane = new VBox();
        pane.getChildren().addAll(gridPane, layout);

        Scene scene = new Scene(pane, 800, 400);
        primaryStage.setTitle("OutMoney");
        primaryStage.setScene(scene);
        primaryStage.show();

        btn0.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                currentNum += "0";
                textField.setText(currentNum);
            }
        });

        //Handle the events related to the 1 button
        btn1.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                currentNum += "1";
                textField.setText(currentNum);
            }
        });

        //Handle the events related to the 2 button
        btn2.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                currentNum += "2";
                textField.setText(currentNum);
            }
        });

        //Handle the events related to the 3 button
        btn3.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                currentNum += "3";
                textField.setText(currentNum);
            }
        });

        //Handle the events related to the 4 button
        btn4.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                currentNum += "4";
                textField.setText(currentNum);
            }
        });

        //Handle the events related to the 5 button
        btn5.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                currentNum += "5";
                textField.setText(currentNum);
            }
        });

        //Handle the events related to the 6 button
        btn6.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                currentNum += "6";
                textField.setText(currentNum);
            }
        });

        //Handle the events related to the 7 button
        btn7.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                currentNum += "7";
                textField.setText(currentNum);
            }
        });


        //Handle the events related to the 8 button
        btn8.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                currentNum += "8";
                textField.setText(currentNum);
            }
        });


        //Handle the events related to the 9 button
        btn9.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                currentNum += "9";
                textField.setText(currentNum);
            }
        });


        //Handle the events related to the Clear button
        btnClear.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                textField.setText("");
                currentNum = "";
            }
        });

        btnEnter.setOnAction((e)->{
            currentNum += "";
            try {
                Test.currentAccount.dePosit(Integer.parseInt(textField.getText()));
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setTitle("Information Dialog");
                alert.setHeaderText("Look, an Information Dialog");
                alert.setContentText("存款成功");

                alert.showAndWait();
                //label2.setText("账户余额:" + Test.currentAccount.money);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        });

        btnQuit.setOnAction((e)->{
            pane.setVisible(false);
            new Menu().start(primaryStage);
        });
    }
}

4.6ChangePaword类

package version3;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class ChangePassword extends Application{
    String currentNum = "";
    public void start(Stage primaryStage){
        Label label1 = new Label("原密码");
        TextField textField1 = new TextField();

        Label label2 = new Label("新密码");
        TextField textField2 = new TextField();

        Label label3 = new Label("再次输入新密码");
        TextField textField3 = new TextField();

        GridPane gridPane = new GridPane();
        gridPane.setAlignment(Pos.CENTER);
        gridPane.setHgap(5);
        gridPane.setVgap(5);
        gridPane.add(label1, 0, 0);
        gridPane.add(label2, 0, 1);
        gridPane.add(label3, 0, 2);
        gridPane.add(textField1, 1, 0);
        gridPane.add(textField2, 1,1);
        gridPane.add(textField3, 1,2);

        GridPane layout = new GridPane();
        layout.setAlignment(Pos.CENTER);

        //Add styling properties to the stage
        layout.setPadding(new Insets(10));
        layout.setHgap(5);
        layout.setVgap(5);

        //Set width for each column in the grid
        layout.getColumnConstraints().add(new ColumnConstraints(150));
        layout.getColumnConstraints().add(new ColumnConstraints(150));
        layout.getColumnConstraints().add(new ColumnConstraints(150));
        layout.getColumnConstraints().add(new ColumnConstraints(150));

        Button btnClear = new Button("Clear");
        Button btnEnter = new Button("Enter");
        Button btn0 = new Button("0");
        Button btn1 = new Button("1");
        Button btn2 = new Button("2");
        Button btn3 = new Button("3");
        Button btn4 = new Button("4");
        Button btn5 = new Button("5");
        Button btn6 = new Button("6");
        Button btn7 = new Button("7");
        Button btn8 = new Button("8");
        Button btn9 = new Button("9");

        //Set styling properties for the buttons
        btnClear.setMaxWidth(Double.MAX_VALUE);
        btnEnter.setMaxWidth(Double.MAX_VALUE);
        btn0.setMaxWidth(Double.MAX_VALUE);
        btn1.setMaxWidth(Double.MAX_VALUE);
        btn2.setMaxWidth(Double.MAX_VALUE);
        btn3.setMaxWidth(Double.MAX_VALUE);
        btn4.setMaxWidth(Double.MAX_VALUE);
        btn5.setMaxWidth(Double.MAX_VALUE);
        btn6.setMaxWidth(Double.MAX_VALUE);
        btn7.setMaxWidth(Double.MAX_VALUE);
        btn8.setMaxWidth(Double.MAX_VALUE);
        btn9.setMaxWidth(Double.MAX_VALUE);

        //Add the UI elements to the layout
        layout.add(btn1, 1, 2);
        layout.add(btn2, 2, 2);
        layout.add(btn3, 3, 2);
        layout.add(btn4, 1, 3);
        layout.add(btn5, 2, 3);
        layout.add(btn6, 3, 3);
        layout.add(btn7, 1, 4);
        layout.add(btn8, 2, 4);
        layout.add(btn9, 3, 4);
        layout.add(btn0, 1, 5);
        layout.add(btnClear, 2, 5);
        layout.add(btnEnter, 3, 5);

        VBox vBox = new VBox();
        vBox.getChildren().addAll(gridPane, layout);

        Scene scene = new Scene(vBox, 800, 300);
        primaryStage.setTitle("ChangePassword");
        primaryStage.setScene(scene);
        primaryStage.show();

        btnEnter.setOnAction((e)->{
            if(Test.currentAccount.password.equals(textField1.getText())){
                try{
                    if(textField2.getText().equals(textField3.getText())){
                        Test.currentAccount.ChangePassword(textField2.getText());
                        Alert alert = new Alert(Alert.AlertType.INFORMATION);
                        alert.setTitle("Information Dialog");
                        alert.setHeaderText("Look, an Information Dialog");
                        alert.setContentText("更改密码成功!");

                        alert.showAndWait();
                        vBox.setVisible(false);
                        new LoginGui().start(primaryStage);
                    }
                } catch (Exception ex) {
                    Alert alert = new Alert(Alert.AlertType.INFORMATION);
                    alert.setTitle("Information Dialog");
                    alert.setHeaderText("Look, an Information Dialog");
                    alert.setContentText("两次密码不一致!");

                    alert.showAndWait();
                }
            }else{
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setTitle("Information Dialog");
                alert.setHeaderText("Look, an Information Dialog");
                alert.setContentText("原密码错误!");

                alert.showAndWait();
            }
        });
        btn0.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                currentNum += "0";
                textField1.setText(currentNum);
            }
        });

        //Handle the events related to the 1 button
        btn1.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                currentNum += "1";
                textField1.setText(currentNum);
            }
        });

        //Handle the events related to the 2 button
        btn2.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                currentNum += "2";
                textField1.setText(currentNum);
            }
        });

        //Handle the events related to the 3 button
        btn3.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                currentNum += "3";
                textField1.setText(currentNum);
            }
        });

        //Handle the events related to the 4 button
        btn4.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                currentNum += "4";
                textField1.setText(currentNum);
            }
        });

        //Handle the events related to the 5 button
        btn5.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                currentNum += "5";
                textField1.setText(currentNum);
            }
        });

        //Handle the events related to the 6 button
        btn6.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                currentNum += "6";
                textField1.setText(currentNum);
            }
        });

        //Handle the events related to the 7 button
        btn7.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                currentNum += "7";
                textField1.setText(currentNum);
            }
        });


        //Handle the events related to the 8 button
        btn8.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                currentNum += "8";
                textField1.setText(currentNum);
            }
        });


        //Handle the events related to the 9 button
        btn9.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                currentNum += "9";
                textField1.setText(currentNum);
            }
        });

        //Handle the events related to the Clear button
        btnClear.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent event){
                currentNum = "";
                textField1.setText(currentNum);
            }
        });

    }
}

4.7Check类

package version3;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * @Auther: paradise
 * @Date: 2021/6/24 - 06 - 24 - 16:42
 */
public class Check extends Application {
    public void start(Stage primaryStage){
        String text1 = "账户id:" + Test.currentAccount.id;
        Label label1 = new Label(text1);

        String text2 = "账户余额:" + Test.currentAccount.money;
        Label label2 = new Label(text2);

        TextArea textArea = new TextArea();

        Button btCheck = new Button("查询记录");
        Button btQuit = new Button("Quit");

        HBox hBox = new HBox(40);
        hBox.setAlignment(Pos.CENTER);
        hBox.getChildren().addAll(btCheck, btQuit);

        VBox pane = new VBox();
        pane.getChildren().addAll(label1, label2, textArea, hBox);
        pane.setAlignment(Pos.CENTER);

        Scene scene = new Scene(pane, 800, 300);
        primaryStage.setTitle("Check");
        primaryStage.setScene(scene);
        primaryStage.show();

        btCheck.setOnAction((e)->{
            textArea.setText(Test.recordString.toString().replace("元","元\n").replace("null",""));//去除掉结果字符串中的null,并将元替换为元\r\n来换行换行
            label2.setText("账户余额:" + Test.currentAccount.money);
        });

        btQuit.setOnAction((e) ->{
            pane.setVisible(false);
            new Menu().start(primaryStage);
        });
    }
}

4.8Test类

package version3;

import javafx.application.Application;
import javafx.stage.Stage;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Test extends Application{
    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;
    static Reader fw;

    public void start(Stage primaryStage){
        LoginGui loginGui = new LoginGui();
        loginGui.start(primaryStage);
    }

    public static void main(String[] args)throws Exception {
        usersList = new ArrayList<Account>();

        /**********************用户文本**********************/
        File users = new File("users.txt");

        if (!users.exists()) {
            try {
                users.createNewFile();
                Writer fw = new FileWriter("users.txt");
                fw.write("admin  12345   1000");
                fw.flush();
                fw.close();
            } catch (Exception e1) {
                e1.getMessage();
            }
        }
        usersFile = users;//创建用户文档,存储用户账户,密码,余额信息;
        usersListRead();
        usersListUpdate();
        Application.launch(args);
        /*****************************Login****************************/
    }
    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++) {
            tmpstr.append(usersList.get(i).id + "    " + usersList.get(i).password + "    " + usersList.get(i).money + "\r\n");
        }
        fw.write(tmpstr.toString());
        fw.flush();
        fw.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
        System.out.println("更新用户失败");
    }
    }
}
  } 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++) {
            tmpstr.append(usersList.get(i).id + "    " + usersList.get(i).password + "    " + usersList.get(i).money + "\r\n");
        }
        fw.write(tmpstr.toString());
        fw.flush();
        fw.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
        System.out.println("更新用户失败");
    }
    }
}

你可能感兴趣的:(课程设计,银行系统,java,javafx,可视化)