-1

这是我第一次学习java,也是第一次上网站。这是我们的 3 个最终项目之一,我碰壁了,我有两个星期的时间来做这个,我无法显示总数。

任务是使用 GUI 的表示类和具有所有计算的帐户类创建一个银行帐户。在演示类中,我们创建了 3 个面板:第一个面板包含 3 个单选按钮,一个用于创建帐户,一个用于进行交易(存款或取款),第三个按钮显示所有帐户

在第二个面板中,客户首先必须创建一个帐户,输入他们的名字姓氏和密码。然后,textArea 将显示初始余额为 100 美元的输入信息。我对此没有意见。![面板][1]

在第三个面板中,客户可以选择是否要存款或取款,然后输入金额。我们必须创建一个可以计算至少 10 个帐户的数组。如果他们想要进行交易,则必须检查数组是否有重复的 pin,如果没有重复,则 TextArea 将显示 pin、名称、交易类型、交易金额和余额。

这就是我的问题所在,我不确定我是否正确检查了重复的引脚,当我尝试显示时,我得到一个空指针异常。任何关于我做错了什么的方向或建议都可能真的很有帮助。先感谢您。

演示类:

我的问题出在方法 findPin2Duplicate() 和显示 transactions() 中。不确定是不是因为 find2Duplicate() 而没有显示,还是因为它也有问题。

import javax.swing.*;       //needed for swing components

import java.awt.*;          //needed for ItemListener and ActionListener interfaces
import java.awt.event.*;    //needed for Font class
import java.text.DecimalFormat;



public class Presentation extends JFrame 
    implements ActionListener
{

        //Declare components
        JLabel titleLabel = new JLabel("The Friendly NeighborHood Bank");
        //intro panel
        JRadioButton createAccountRadioButton = new JRadioButton("Create New Account");
        JRadioButton transactionRadioButton = new JRadioButton("Deposit or Withdrawal");
        JRadioButton displayRadioButton = new JRadioButton("Display all accounts");
        JRadioButton default1RadioButton = new JRadioButton();
        ButtonGroup introButtonGroup = new ButtonGroup();
        //new account panel
        JTextField firstNameTextField = new JTextField (20);
        JTextField lastNameTextField = new JTextField(20);
        JTextField pin1TextField = new JTextField(20);
        JTextArea output1TextArea = new JTextArea(18,45);
        JButton process1Button = new JButton ("Process");
        JButton back1Button = new JButton ("Back");
        //deposit or withdrawal panel
        JRadioButton depositRadioButton = new JRadioButton("Deposit");
        JRadioButton withdrawalRadioButton = new JRadioButton("Withdrawal");
        JRadioButton default2RadioButton = new JRadioButton();
        ButtonGroup transactionButtonGroup = new ButtonGroup();
        JTextField pin2TextField = new JTextField(20);
        JTextField amountTextField = new JTextField(20);
        JTextArea output2TextArea = new JTextArea(20,30);
        JButton process2Button = new JButton ("Process");
        JButton back2Button = new JButton ("Back");



        JScrollPane outputScrollPane1 = new JScrollPane(output1TextArea);
        JScrollPane outputScrollPane2 = new JScrollPane(output2TextArea);

        Font plainFont = new Font("Times New Roman", Font.PLAIN, 14);
        Font taFont = new Font("Courier", Font.PLAIN, 12);

        JPanel introPanel = new JPanel();
        JPanel newAccountPanel = new JPanel();
        JPanel transactionPanel = new JPanel();




        //declare instance variables
        Account myAccountArray[] = new Account[10];
        int lastAccountInteger = -1;
        int pinIndexInteger = 0;


        public static void main(String[] args)
        {
            // This method creates sets the default close operation
            Presentation myProgram = new Presentation();
            myProgram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }//end of main method

        //This is the constructor and will call the 3 panels to add the components to the GUI, 
        //register the listeners, and set the properties of the JFrame
        public Presentation()
        {

            super("Account");
            //Constructor - calls methods to add components to panel, set up 
            //the listeners, and set properties for the JFrame
            introPanel();
            newAccountPanel();
            transactionPanel();
            //Call method to add controls and set listeners
            addListeners();


            //add the intro panel to the frame and set the size and the visibility of the frame

            add(introPanel);
            setSize(200,300);
            setVisible(true);

        }


        // This method will add components to the first panel shown
        public void introPanel()
        {

            introButtonGroup.add(createAccountRadioButton);
            introButtonGroup.add(transactionRadioButton);
            introButtonGroup.add(displayRadioButton);
            introButtonGroup.add(default1RadioButton);

            createAccountRadioButton.setSelected(false);
            transactionRadioButton.setSelected(false);
            displayRadioButton.setSelected(false);
            default1RadioButton.setSelected(true);

            introPanel.add(titleLabel);
            introPanel.add(createAccountRadioButton);
            introPanel.add(transactionRadioButton);
            introPanel.add(displayRadioButton);
        }
        //This method will add components to a second panel
        public void newAccountPanel()
        {

            newAccountPanel.add(titleLabel);

            newAccountPanel.add(new JLabel("First Name:"));
            newAccountPanel.add(firstNameTextField);
            newAccountPanel.add(new JLabel("Last Name:"));
            newAccountPanel.add(lastNameTextField);
            newAccountPanel.add(new JLabel("PIN:"));
            newAccountPanel.add(pin1TextField);

            output1TextArea.setFont(taFont);
            String formattedTitleString = String.format("%-10s%13s%15s%n", "Name", 
                    "PIN",  "Balance");
            output1TextArea.append(formattedTitleString);
            newAccountPanel.add(output1TextArea);
            newAccountPanel.add(new JLabel(" Programmed by: Maria Garcia "));

            newAccountPanel.add(process1Button);
            newAccountPanel.add(back1Button);
        }

        //This method will add components to a third panel
        public void transactionPanel()
        {

            transactionButtonGroup.add(depositRadioButton);
            transactionButtonGroup.add(withdrawalRadioButton);
            transactionButtonGroup.add(default2RadioButton);

            depositRadioButton.setSelected(false);
            withdrawalRadioButton.setSelected(false);
            default2RadioButton.setSelected(true);

            transactionPanel.add(titleLabel);
            transactionPanel.add(depositRadioButton);
            transactionPanel.add(withdrawalRadioButton);
            transactionPanel.add( new JLabel ("PIN:"));
            transactionPanel.add(pin2TextField);
            transactionPanel.add(new JLabel ("Amount:"));
            transactionPanel.add(amountTextField);
            output2TextArea.setFont(taFont);
            String formattedTitleString = String.format("%-8s%11s%13s%11s%13s%n", "PIN", 
                    "Name",  "Transaction Type ",   "Transaction Amount",   "Balance");
            output2TextArea.setText(formattedTitleString);
            transactionPanel.add(output2TextArea);
            transactionPanel.add(new JLabel(" Programmed by: Maria Garcia "));
            transactionPanel.add(process2Button);
            transactionPanel.add(back2Button);
        }
        public void addListeners()
        {
            //This method sets up the listeners
            //radio buttons set up to respond to the ItemListener

            process1Button.addActionListener(this);
            back1Button.addActionListener(this);
            process2Button.addActionListener(this);
            back2Button.addActionListener(this);
            amountTextField.addActionListener(this);

            createAccountRadioButton.addActionListener(this);
            transactionRadioButton.addActionListener(this);
            displayRadioButton.addActionListener(this);
            depositRadioButton.addActionListener(this);
            withdrawalRadioButton.addActionListener(this);
        }//end of addListeners method




           public void actionPerformed(ActionEvent evt)
                {

                Object sourceObject = evt.getSource();  


                    if(sourceObject == createAccountRadioButton)
            {
                if(createAccountRadioButton.isSelected())
                {
                    remove(introPanel);
                    default1RadioButton.setSelected(true);
                    remove(transactionPanel);
                    add(newAccountPanel);
                    setSize(420,475);
                    setVisible(true);
                }
            } //to display the admin panel
            else if(sourceObject == transactionRadioButton )
            {
                if(transactionRadioButton.isSelected())
                {

                    remove(introPanel);
                    default2RadioButton.setSelected(true);
                    remove(newAccountPanel);
                    add(transactionPanel);
                    setSize(420,500);
                    setVisible(true);
                }
            }
                    if(sourceObject == process1Button)
                    {

                        if(newAccountValidation() == true) // validation method will be called and then the processRental
                        {



                                displayNewAccount();




                        }    
                    }
                    else if (sourceObject ==process2Button )
                    {
                        if(transactionsValidation() == true)
                        {
                            if(findPin2Duplicate()== false)
                            {

                                displayTransactions();
                                //reset();
                            }
                        }
                    }

                    else if ( sourceObject == back1Button) //If the summary button is pressed then the summary method will be called
                    {
                        remove(newAccountPanel);
                        add(introPanel);
                        setSize(200,300);
                        setVisible(true);

                    }
                    else if (sourceObject == back2Button)
                    {
                        remove(transactionPanel);
                        add(introPanel);
                        setSize(200,300);
                        setVisible(true);
                    }
                    else if (displayRadioButton.isSelected())
                    {
                        summary();
                    }

                }
                    public boolean newAccountValidation()
                    {
                        boolean newAccountValidationBoolean;
                        if(!(firstNameTextField.getText()).equals(""))
                        {
                            if(!(lastNameTextField.getText()).equals(""))
                            {
                                if (!(pin1TextField.getText()).equals(""))
                                {
                                    newAccountValidationBoolean = true;
                                }
                                else
                                {
                                    JOptionPane.showMessageDialog(null, "Please enter your pin");
                                    pin1TextField.requestFocus();
                                    newAccountValidationBoolean = false;
                                }
                            }
                            else
                            {
                                JOptionPane.showMessageDialog(null, "Please enter your last name");
                                lastNameTextField.requestFocus();
                                newAccountValidationBoolean = false;
                            }
                        }
                        else
                        {
                            JOptionPane.showMessageDialog(null, "Please enter your first name");
                            firstNameTextField.requestFocus();
                            newAccountValidationBoolean = false;

                        }
                        return newAccountValidationBoolean;
                    }

                    public boolean transactionsValidation()
                    {
                        boolean transactionsValidationBoolean;
                        if(!(pin2TextField.getText()).equals(""))
                        {
                            if(!(amountTextField.getText()).equals(""))
                            {
                                transactionsValidationBoolean = true;
                            }
                            else
                            {
                                JOptionPane.showMessageDialog(null, "Please enter an amount");
                                amountTextField.requestFocus();
                                transactionsValidationBoolean = false;
                            }
                        }
                        else
                        {
                            JOptionPane.showMessageDialog(null, "Please enter your pin");
                            pin2TextField.requestFocus();
                            transactionsValidationBoolean = false;

                        }
                        return transactionsValidationBoolean;
                    }







                   public void displayNewAccount()
                   {
                       String firstNameString = ("");
                       String lastNameString = ("");
                       String pinString = ("");

                       DecimalFormat valueDecimalFormat = new DecimalFormat("$#0.00");

                       if (lastAccountInteger >= 0) //don't want to do this for the first account
                       {
                           if(findPin1Duplicate() == false)
                           {


                               lastAccountInteger ++;
                               myAccountArray[lastAccountInteger]= new Account(firstNameString, lastNameString, pinString);
                               String nameString = lastNameTextField.getText() + ", " + firstNameTextField.getText();
                               double Amount = (myAccountArray[lastAccountInteger].getBalance());
                               String tempString1 = Double.toString(Amount);


                               output1TextArea.append(nameString + "\t    " + pin1TextField.getText() + "\t " + "$" + tempString1 + "\n");
                       //String formattedOutputString = String.format("%6s%9s%13s%n", nameString, myAccountArray[lastAccountInteger].getTransactionAmount(transactionAmountDouble)); 
                       //myAccountArray[lastAccountInteger].getPin();
                        //output1TextArea.append(formattedOutputString);
                           }
                           else
                           {
                               String displayString = "Enter a valid Pin";
                               JOptionPane.showMessageDialog(null, displayString);
                           }
                               //JOptionPane message
                       }
                       else
                       {
                           lastAccountInteger ++;
                           myAccountArray[lastAccountInteger]= new Account(firstNameString, lastNameString, pinString);
                           String nameString = lastNameTextField.getText() + ", " + firstNameTextField.getText();
                           double Amount = (myAccountArray[lastAccountInteger].getBalance());
                           String tempString1 = Double.toString(Amount);


                           output1TextArea.append(nameString + "\t    " + pin1TextField.getText() + "\t " + "$" + tempString1 + "\n");


                       }
                   }

                   public void displayTransactions()
                   {
                       String firstNameString = ("");
                       String lastNameString = ("");
                       String pinString = ("");
                       char typeOfTransactionChar = (' ');
                       String transactionString = ("");
                       double transactionAmountDouble = 0.0;
                       double amountDouble = 0.0;



                      //for (int i = 0; i < lastAccountInteger; i++)
                       lastAccountInteger++;
                       myAccountArray[lastAccountInteger]= new Account(firstNameString, lastNameString, pinString);
                           if (lastAccountInteger >= 0)
                           {

                       if (depositRadioButton.isSelected())
                       {
                          amountDouble =myAccountArray[lastAccountInteger].getDeposit();
                          //typeOfTransactionChar ='D';
                           transactionString = "Deposit";
                           //transactionAmountDouble = depositAmountDouble ;
                       }

                       else if( withdrawalRadioButton.isSelected())
                       {
                        amountDouble = myAccountArray[lastAccountInteger].getWithdrawal();
                           //typeOfTransactionChar= 'W';
                          transactionString = "Withdrawal";
                           //transactionAmountDouble = withdrawalAmountDouble;
                       }


                       }
                           String nameString = lastNameTextField.getText() + ", " + firstNameTextField.getText();

                           String tempString = Double.toString(amountDouble);
                    output2TextArea.append(pinString + nameString + transactionString + amountDouble); 
                    //String formattedTransactionString = String.format("%-10s%6s%9s%9s%13s%11s%n", transactionString); //6d=decimal integer



                    // output2TextArea.append(formattedTransactionString);
                   }
                    public boolean findPin1Duplicate()
                    {
                        boolean itemFoundBoolean = false;
                        pinIndexInteger = 0;
                        String listItemString;
                        lastAccountInteger++;
                        String pinString = myAccountArray[lastAccountInteger].getPin();
                        String inputPin1String = pin1TextField.getText();
                        int arrayPin1Sum = 0;



                        while (!itemFoundBoolean && pinIndexInteger < lastAccountInteger)
                        {

                            //listItemString = String.valueOf(arrayPin1Sum);

                            if(inputPin1String.equalsIgnoreCase(pinString))
                            {
                                itemFoundBoolean = true;
                            }
                            else
                                pinIndexInteger++;
                        }

                        return itemFoundBoolean;
                    }
                    public boolean findPin2Duplicate()
                    {
                        boolean itemFoundBoolean = false;
                        pinIndexInteger = 0;
                        lastAccountInteger ++;
                        String pin2String = myAccountArray[lastAccountInteger].getPin();
                        String inputPin1String = pin2TextField.getText();
                        int arrayPin2Sum = 0;

                    //  for (int i = 0; i < myAccountArray.length; i++)
                        //{
                            //pin2String += myAccountArray[i].getPin();
                        //}

                        while (!itemFoundBoolean && pinIndexInteger < lastAccountInteger)
                        {

                            //listItemString = String.valueOf(arrayPin2Sum);

                            if(inputPin1String.equalsIgnoreCase(pin2String))
                            {

                                itemFoundBoolean = true;
                            }
                            else
                            {
                            pinIndexInteger++;
                            }
                        }

                        return itemFoundBoolean;
                    }



                    //method to display summary

                        public void summary()
                        {
                            //Format the values to currency format
                            DecimalFormat valueDecimalFormat = new DecimalFormat("$#0.00");



                        }




                //This method resets the textfields for re-entry and sends the cursor to the nameTextField      
                    public void reset()
                            {




                            } //end of reset method

}


Account Class

   public class Account
{


//declare instance variables



    //constants
    private final double DEPOSIT_AMOUNT_DOUBLE = 100.0;


    //class variables
    private double withdrawalAmountDouble;
    private static double currentBalanceDouble;
    private double totalAmountDouble;

    private static double totalCount;
    private static double totalDepositDouble;
    private double depositAmountDouble;
    private static String firstNameString;
    private static String lastNameString;
    private static String fullNameString;
    private static String pinString;
    private static double addedAmountDouble;
    private static String transactionTypeString;
    private static boolean balanceBoolean;
    private static char typeOfTransactionChar;
    private static double transactionAmountDouble;
    private static double totalWithdrawalAmountDouble;
    private static double totalDepositAmountDouble;
    private static double grossTransactionDouble;
    private static double balanceDouble;
    private static boolean isValidBoolean;




    //Constructors
    public Account()
    {
        setFirstName("");
        setLastName("");
        setPin("");





        totalCount++;
    }

    public Account(String newFirstNameString, String newLastNameString, String newPinString) 
    {
        setFirstName(newFirstNameString);
        setLastName(newLastNameString);
        setPin(newPinString);
        setBalance(DEPOSIT_AMOUNT_DOUBLE);

    }



    public void setFirstName(String newFirstNameString)
        {
            firstNameString = newFirstNameString;
        }

    public void setLastName(String newLastNameString)
        {
            lastNameString = newLastNameString;
        }

    public void setPin(String newPinString)
        {
            pinString = newPinString;
        }


        //this method will set the type of transaction for the transaction panel
    public void setBalance(double newBalanceDouble)
    {

        balanceDouble = newBalanceDouble;
    }   


        public String getTypeOfTransaction()
        {

            return transactionTypeString;
        }
        public double getBalance()
        {
            return balanceDouble;
        }
        public double getTransactionAmountDouble()
        {
            return transactionAmountDouble;
        }


        public double getTransactionAmount(double newTransactionAmountDouble)
        {

            currentBalanceDouble = DEPOSIT_AMOUNT_DOUBLE;

            transactionAmountDouble = currentBalanceDouble;
            return transactionAmountDouble;
        }


        public String getFirstNameString()
        {
            return firstNameString;
        }
        public String getLastNameString()
        {
            return lastNameString;
        }
        public void setName()
        {

            fullNameString = (firstNameString + " , " + lastNameString);

        }
        public String getName()
        {
            return fullNameString;
        }


        public void setIsValid(double newBalanceDouble)
        {
            if (balanceDouble > currentBalanceDouble)
            {
                isValidBoolean = false;
            }



        }

        public void setDeposit(double newDepositDouble)
        {
            balanceDouble += newDepositDouble;


        }
        public void setWithdrawal(double newWithdrawalDouble)
        {


                    balanceDouble -= (newWithdrawalDouble);




        }

        public double getDeposit()
        {
            return balanceDouble;
        }
        public double getWithdrawal()
        {
            return balanceDouble;
        }
        public boolean getIsValid()
        {
            return isValidBoolean;
        }



        public String getPin()
        {
            return pinString;
        }
        }

再次感谢任何可以引导我走向正确方向或指出我的错误的地方。先感谢您

4

1 回答 1

0

我不确定问题到底是什么,所以我将首先从 NullPointerException 开始。如果没有完整的堆栈跟踪,我们将无能为力。包括堆栈跟踪以指示哪一行遇到错误。

如果您想询问如何在数组中查找重复项,那么我可以提供帮助。但是我仍然不清楚您要做什么。您是否只想知道数组是否包含重复项?您需要对阵列进行重复数据删除吗?或者确定哪些值是重复的?

防止重复

如果重复出现问题,数组就不是正确的数据结构。考虑一组。由于这是 Java,因此您可以使用 TreeSet 和 HashSet。

识别重复项的存在

我会尝试的两件事:

  1. 将每个元素复制到 HashSet 中并比较大小。如果 HashSet 的元素比数组少,那么肯定有重复的值。
  2. 对数组进行排序,然后编写一个循环,将每个值与之前的值进行比较。排序将确保重复值是相邻的,因此将每个值与之前的值进行比较将很容易发现重复值。

识别重复的值

  • 创建一个名为“dups”的空数组。
  • 对原始数组进行排序。
  • 遍历原始数组,将每个值与之前的值进行比较。
  • 如果找到匹配项,则将该值添加到“dups”数组中。
  • 在循环结束时,“dups”将包含在原始数组中多次出现的值。
于 2013-06-01T21:43:40.273 回答