首页 > 解决方案 > 无法将我的文本文件读取到我的数组中,并且我无法让我的平衡按钮工作。这里的初学者程序员......可以使用一些方向

问题描述

我无法将我的文本文件读取到我的数组并让“平衡按钮”正常工作。我可以从数组中输入密码,但我不能对帐户做任何事情(即查看余额、提款等)。

下面是我的 account.data 文本文件:

7623, S, Jones, Phyllis, 2000.00, 0 7621, C, Jones, Phyllis, 1200.00, 2 8729, S, Smith, Cletus, 1000.00, 1 8728, C, Smith, Cletus, 1700.00, 0 7321, S, Booth, Betty, 4500.00, 2 3242, C, Seybright, Sam, 4612.00, 0 3241, S, Seybright, Sam, 100.00, 3

编码:

package cmis_242_project2_hamilton;

import javax.swing.JOptionPane;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JButton;

import javax.swing.JLabel;

import javax.swing.JTextField;

import javax.swing.JScrollPane;

import java.awt.Dimension;

import java.awt.Color;

import java.awt.Font;

import java.awt.FlowLayout;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.text.NumberFormat;

import java.util.ArrayList;

import java.io.BufferedReader;

import java.io.FileReader;

/**
 *
 *
 *
 * @author Hamil
 */
    public class CMIS_242_PROJECT2_HAMILTON {

    public static class ATM {

    // these must be declared here to be seen in the Action classes
        JFrame welcomeFrame;

        JFrame mainFrame;

        JFrame inputFrame;

        JTextField numberField;

        JTextField pinField;

        JLabel verifyLabel;

        JLabel outLabel;

        JButton depositButton;

        JButton withdrawButton;

        JButton makeDepositButton;

        JButton makeWithdrawalButton;

        BankAccount theAccount;

        NumberFormat currency;

        double theAmount;

        int width;

        int height;

        ArrayList accounts;

        public ATM() {

            int xLocation = 200;

            int yLocation = 200;

            // define frames
            mainFrame = new JFrame("ATM");

            welcomeFrame = new JFrame("Welcome to the ATM");

            inputFrame = new JFrame("Enter Amount");

            mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            welcomeFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            inputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            welcomeFrame.setLocation(xLocation, yLocation);

            mainFrame.setLocation(xLocation, yLocation);

            inputFrame.setLocation(xLocation, yLocation);

            JPanel welcomePanel = new JPanel();

            welcomePanel.setLayout(new FlowLayout(FlowLayout.CENTER, 200, 20));

            welcomePanel.setPreferredSize(new Dimension(400, 200));

            JLabel pinLabel = new JLabel("Enter pin and Press Next");

            pinField = new JTextField(10);

            pinField.addActionListener(new PinFieldAction());

            welcomePanel.add(pinLabel);

            welcomePanel.add(pinField);

            JButton nextButton = new JButton("Next");

            nextButton.addActionListener(new NextButtonAction());

           welcomePanel.add(nextButton);
            // the main frame panel
            JPanel thePanel = new JPanel();

            width = 500;

            height = 400;

            thePanel.setPreferredSize(new Dimension(width, height));

            thePanel.setBackground(Color.LIGHT_GRAY);

            thePanel.setLayout(new FlowLayout(FlowLayout.CENTER, 500, 20));

            JPanel inputPanel = new JPanel();

            JLabel numberLabel = new JLabel("Type Amount and Press Enter");

            numberField = new JTextField(10);

            numberField.addActionListener(new NumberFieldAction());

            inputPanel.add(numberLabel);

            inputPanel.add(numberField);

            JPanel verifyPanel = new JPanel();

            verifyLabel = new JLabel("No Amount Entered");

            verifyPanel.add(verifyLabel);

            JPanel choosePanel = new JPanel();

            JLabel chooseLabel = new JLabel("Choose Transaction");

            choosePanel.add(chooseLabel);

            JPanel buttonPanel = new JPanel();

            JPanel makeButtonPanel = new JPanel();

            JButton balanceButton = new JButton("Balance");

            balanceButton.addActionListener(new BalanceButtonAction());

            buttonPanel.add(balanceButton);

            depositButton = new JButton("Deposit");

            depositButton.addActionListener(new DepositButtonAction());

            buttonPanel.add(depositButton);

            withdrawButton = new JButton("Withdraw");

            withdrawButton.addActionListener(new WithdrawButtonAction());

            buttonPanel.add(withdrawButton);

            makeDepositButton = new JButton("Make Deposit");

            makeDepositButton.addActionListener(new MakeDepositButtonAction());

            makeButtonPanel.add(makeDepositButton);

            makeWithdrawalButton = new JButton("Make Withdrawal");

            makeWithdrawalButton.addActionListener(new 
            MakeWithdrawalButtonAction());

            makeButtonPanel.add(makeWithdrawalButton);

            JPanel finishButtonPanel = new JPanel();

            JButton finishButton = new JButton("Finish");

            finishButton.addActionListener(new FinishButtonAction());

            finishButtonPanel.add(finishButton);

            JPanel outPanel = new JPanel();

            outPanel.setBackground(Color.LIGHT_GRAY);

            outLabel = new JLabel("");

            outLabel.setForeground(Color.RED);

            outLabel.setFont(new Font("Courier", Font.BOLD, 20));

            outPanel.add(outLabel);

            thePanel.add(choosePanel);

            thePanel.add(verifyPanel);

            thePanel.add(buttonPanel);

            thePanel.add(makeButtonPanel);

            thePanel.add(finishButtonPanel);

            thePanel.add(outPanel);

            welcomeFrame.getContentPane().add(welcomePanel);

            welcomeFrame.pack();

            welcomeFrame.setVisible(true);

            mainFrame.getContentPane().add(thePanel);

            mainFrame.pack();

            inputFrame.getContentPane().add(inputPanel);

            inputFrame.pack();

            currency = NumberFormat.getCurrencyInstance();

             // Load the data base
            accounts = getAccountDataFromFile("accounts.data");

        } // end constructor

        ArrayList getAccountDataFromFile(String fileName) {

            try {

                ArrayList accounts = new ArrayList();
                double S = 0;
                double C = 0;
                 // All numbers in array are account numbers
                double[][] array = {{7623},
                {7621},
                {8729},
                {8728},
                {7321},
                {3242},
                {3241}

                };

                return accounts;

            } catch (Exception e) {

                JOptionPane.showMessageDialog(null, "File Error. Program 
                Aborted");

                System.exit(0);

                return accounts;

            } // end try/catch

        } // end getAccountDataFromFile

        class PinFieldAction implements ActionListener {

            public void actionPerformed(ActionEvent e) {

          // Get the pin number, verify that it is legal
          // and then get the account from the ArrayList accounts
           // and remove the following statement
                theAccount = new BankAccount(1000000);

                welcomeFrame.setVisible(false);

                mainFrame.setVisible(true);

                pinField.setText("");

            } // end actionPerformed

        } // end PinFieldAction

        class NumberFieldAction implements ActionListener {

            public void actionPerformed(ActionEvent e) {

                String input = numberField.getText();

                if (!isInputOK(input)) {

                    JOptionPane.showMessageDialog(null, "Invalid input. Try 
                 again.");

                    numberField.requestFocus();

                    return;

                } // end if

                theAmount = Double.parseDouble(input);

                welcomeFrame.setVisible(false);

                inputFrame.setVisible(false);

                mainFrame.setVisible(true);

                verifyLabel.setText("Amount Entered was " + 
               currency.format(theAmount));

            } // end actionPerformed

        } // end NumberButtonAction

        class BalanceButtonAction implements ActionListener {

            public void actionPerformed(ActionEvent e) {

                numberField.requestFocus();

                drawBalance(outLabel, theAccount);

            } // end actionPerformed

        } // end BalanceButtonAction

        class DepositButtonAction implements ActionListener {

            public void actionPerformed(ActionEvent e) {

                inputFrame.setVisible(true);

                numberField.setText("");

                numberField.requestFocus();

                withdrawButton.setEnabled(false);

                makeWithdrawalButton.setEnabled(false);

            } // end actionPerformed

        } // end DepositButtonAction

        class WithdrawButtonAction implements ActionListener {

            public void actionPerformed(ActionEvent e) {

                inputFrame.setVisible(true);

                numberField.setText("");

                numberField.requestFocus();

                depositButton.setEnabled(false);

                makeDepositButton.setEnabled(false);

            } // end actionPerformed

        } // end WithdrawButtonAction

        class MakeDepositButtonAction implements ActionListener {

            public void actionPerformed(ActionEvent e) {

                makeDepositButton.setEnabled(true);
                withdrawButton.setEnabled(true);

                makeWithdrawalButton.setEnabled(true);

            } // end actionPerformed

        } // end MakeDepositButtonAction

        class MakeWithdrawalButtonAction implements ActionListener {

            public void actionPerformed(ActionEvent e) {

                makeWithdrawalButton.setEnabled(true);
                depositButton.setEnabled(true);

                makeDepositButton.setEnabled(true);

            } // end actionPerformed

        } // end MakeWithDrawalButtonAction

        class NextButtonAction implements ActionListener {

            public void actionPerformed(ActionEvent e) {

                pinField.setText("");

                pinField.requestFocus(true);

                welcomeFrame.setVisible(false);

                mainFrame.setVisible(true);

            } // end actionPerformed

        } // end NextButtonAction

        class FinishButtonAction implements ActionListener {

            public void actionPerformed(ActionEvent e) {

                outLabel.setText("");

                mainFrame.setVisible(false);

                inputFrame.setVisible(false);

                welcomeFrame.setVisible(true);

                pinField.requestFocus();

                depositButton.setEnabled(true);

                withdrawButton.setEnabled(true);

            } // end actionPerformed

        } // end FinishButtonAction

        boolean isInputOK(String input) {

            input = input.trim();

            if (input.equals("")) {

                return false;

            } // end if

            int decimalIndex = input.indexOf(".");

            if (decimalIndex == -1) {

                return isAllDigits(input);

            } else {

                String first = input.substring(0, decimalIndex);

                String last = input.substring(decimalIndex + 1);

                boolean firstAndLastAllDigits = isAllDigits(first) && 
                isAllDigits(last);

                boolean lastlengthOK = last.length() == 2;

                return firstAndLastAllDigits && lastlengthOK;

            } // end if

        } // end is InputOK

        boolean isAllDigits(String input) {

            int index = 0;

            while (index < input.length()) {

                if (!Character.isDigit(input.charAt(index))) {

                    return false;

                } // end if

                index++;

            } // end while

            return true;

        } // end isAllDigits

        boolean isAmountOK(double amount) {

            if (amount <= 0.0) {

                JOptionPane.showMessageDialog(null, "Enter an Positive Amount 
               First");

                return false;

            } // end if

            return true;

        } // isAmountOK

        void drawBalance(JLabel outLabel, BankAccount account) {

            outLabel.setText("The balance is " + 
         currency.format(theAccount.getBalance()) + ".");

        } // end drawBalance

        public static void main(String[] args) {

            new ATM();

        } // end main

    } // end ATM
}

标签: java

解决方案


如果要读取文件,可以使用Scanner类。

Scanner sc = new Scanner(new FileReader("file.txt"));

例如,如果您想将文件编号放入列表中:

ArrayList list = new ArrayList();
while(sc.hasNext()) {
    list.add(sc.nextInt());
 }
sc.close();

推荐阅读