Link to home
Start Free TrialLog in
Avatar of Cyart
CyartFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Code problem

Hello I have three classes customer, gui and testingGui. This is my first attempt with JTextField controls. I have converted this program from Messageboxes so the program did work. From the first screen i selected the first option create account(at the moment I have disbaled the account functionas the customer function is not working) after creating the first account I create a subsequent account after which I select option "List details of all accounts" however the program outputs the same customer name for all accounts, could you have a llok at the code below and tell me whats going wrong.

Cheers


///////////////////////////////////////////////////Class gui

import javax.swing.* ;
import java.awt.* ;
import java.awt.event.* ;
import java.io.IOException ;

class SetGui extends JFrame
{
      Container container = getContentPane ();
      
      int selectedOption = 0 ;
      static int noOfAccounts = 0 ;
      private JButton proceedButton, cancelButton ;
      private JLabel menuLabel ;
      private JTextField customerField,  telephoneField ;
      private JRadioButton createAcc, depositWithdraw , displayOne,  listAll, interestCalcs ;
      bank[] newBank = new bank [100] ;
      customer[] newCustomer = new customer[100] ;
      
      public SetGui()
      {
            createMenu () ;
      }
      
      public void createMenu ()
      {
            container.setLayout (null)  ;
            
            menuLabel = new JLabel ("Menu Options:") ;
            menuLabel.setBounds(30,30,300,20) ;
            container.add(menuLabel) ;
            
            createAcc = new JRadioButton ("Create an Account") ;
            createAcc.setBounds (30,50,200,60) ;
            createAcc.addActionListener(new ActionListener()
            {
                  public void actionPerformed (ActionEvent e)
                  {
                        selectedOption = 1 ;
                        
                  }
            }) ;
            container.add(createAcc) ;
            
            depositWithdraw = new JRadioButton ("Deposit/Withdraw Money") ;
            depositWithdraw.setBounds (30,90,200,60) ;
            depositWithdraw.addActionListener(new ActionListener()
            {
                  public void actionPerformed (ActionEvent e)
                  {
                        selectedOption = 2 ;
                        
                  }
            }) ;
            container.add(depositWithdraw) ;
            
            displayOne = new JRadioButton ("Display Account Details") ;
            displayOne.setBounds (30,130,200,60) ;
            displayOne.addActionListener(new ActionListener()
            {
                  public void actionPerformed (ActionEvent e)
                  {
                        selectedOption = 3 ;
                        
                  }
            }) ;
            container.add(displayOne) ;
            
            listAll = new JRadioButton ("List details of all Accounts") ;
            listAll.setBounds (30,170,200,60) ;
            listAll.addActionListener(new ActionListener()
            {
                  public void actionPerformed (ActionEvent e)
                  {
                        selectedOption = 4 ;
                        
                  }
            }) ;
            container.add(listAll) ;
            
            interestCalcs = new JRadioButton ("Interest Calculations") ;
            interestCalcs.setBounds (30,210,200,60) ;
            interestCalcs.addActionListener(new ActionListener()
            {
                  public void actionPerformed (ActionEvent e)
                  {
                        selectedOption = 5 ;
                        
                  }
            }) ;
            container.add(interestCalcs) ;
            
            proceedButton = new JButton ("OK") ;
            proceedButton.setBounds(30,270,100,60) ;
            proceedButton.addActionListener(new ActionListener()
            {
                  public void actionPerformed (ActionEvent e)
                  {
                              
                        if (selectedOption != 0)
                        {
                        
                              switch (selectedOption)
                              {
                                    case 1 :
                                                noOfAccounts++ ;
                                                //newBank[noOfAccounts] = new bank () ;
                                                //newBank[noOfAccounts].setNewAccount () ;
                                                newCustomer[noOfAccounts] = new customer () ;
                                                //newCustomer[noOfAccounts].setNewAccount () ;
                                                //setVisible(false);
                                                
                                                
                                                
                                                break ;
                                    case 2 :
                                    case 3 :
                                    case 4 :
                                                if(noOfAccounts >= 1)
                                                {
                                                      if((selectedOption == 2) || (selectedOption == 4))
                                                      {
                                                            newCustomer[noOfAccounts].displayAccountDetails(newCustomer, noOfAccounts) ;
                                                            //newBank[noOfAccounts].displayAccountDetails(newBank, noOfAccounts) ;
                                                      }
                                                      if(selectedOption == 2)
                                                      {      try
                                                            {
                                                            
                                                                  int tempAccount = Integer.parseInt(JOptionPane.showInputDialog("Please enter customer account number to use")) ;
                                                                  newBank[tempAccount].setBalance() ;
                                                            }
                                                            catch(NullPointerException s)
                                                            {
                                                                  JOptionPane.showMessageDialog(null,"You have selected an invalid account number!!","error", JOptionPane.INFORMATION_MESSAGE) ;
                                                            }
                                                            break ;
                                                            
                                                      }else if (selectedOption == 3)
                                                      {
                                                            newCustomer[noOfAccounts].displayAccountDetails () ;
                                                            //newBank[noOfAccounts].displayAccountDetails () ;
                                                            break ;
                                                      }
                                                      else
                                                      {
                                                            break ;
                                                      }
                                    
                                                }
                                                else
                                                {
                                                      JOptionPane.showMessageDialog(null,"No Accounts Present!!","error", JOptionPane.INFORMATION_MESSAGE) ;
                                                      break ;
                                                }
                                    
                                    case 5 :
                                                                  
                                                newBank[noOfAccounts].calculateInterest(newBank, noOfAccounts) ;
                                                break ;
                                                
                                                
                                                
                                    default :
                                                JOptionPane.showMessageDialog(null,"Exiting program","Terminating program", JOptionPane.INFORMATION_MESSAGE) ;
                              }
                  
                        }
                        }
            }) ;
            
            container.add(proceedButton) ;
            
            ButtonGroup bg = new ButtonGroup () ;
            
            bg.add(createAcc) ;
            bg.add(depositWithdraw) ;
            bg.add(displayOne) ;
            bg.add(listAll) ;
            bg.add(interestCalcs) ;
            
            setBounds(400,400,400,400) ;
            setVisible (true) ;
            
            
            
            
      }
}




////////////////////////////////////////////////////////////////////////class testingGui






import javax.swing.* ;
import java.awt.* ;

class testthisBank
{
      
      
      public static void main (String[] args)
      {
            
            
            
            SetGui thisGui = new SetGui () ;
            thisGui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
            
            
            
            
      }
}






////////////////////////////////////////////////////////////////////////////////////////class customer







import javax.swing.* ;
import java.awt.* ;
import java.awt.event.* ;
import java.io.IOException ;

class customer extends JFrame
{
      
      String customerName ;      
      String customerAddress ;
      String customerTelephone ;
      boolean noInput = true ;
      
      private JButton okButton, cancelButton ;
      private JLabel customerLabel, addressLabel, telephoneLabel ;
      private JTextField customerField,  telephoneField ;
      private JTextField addressField[] = new JTextField [5] ;
      JTextArea outputArea = new JTextArea();            //Creation of object outputArea of type JTextArea
      Container container = getContentPane ();
      //TextFieldHandler textHandler = new TextFieldHandler () ;
      
      public customer ()
      {
            String customerName = "";      
            String customerAddress = "";
            String customerTelephone = "";
            gui () ;
            
      }
      
      
      public void gui ()
      {
            
            
            
            container.setLayout (null) ;
            
            setName () ;
            setAddress () ;
            setTelephone () ;
            
            okButton = new JButton ("OK") ;
            cancelButton = new JButton ("Cancel") ;
            cancelButton.addActionListener(new ActionListener()
            {
                  public void actionPerformed (ActionEvent e)
                  {
                        setVisible(false);
                        
                  }
            }) ;
            okButton.setBounds(30,250,100,60) ;
            cancelButton.setBounds(270,250,100,60) ;
            
            okButton.addActionListener(new ActionListener()
            {
                  public void actionPerformed (ActionEvent e)
                        {
                              String      checkName ;
                              String checkAddress ;
                              checkName = customerField.getText() ;
                                    //System.out.println (test);
                              if (checkName.length() <= 0)
                              {
                                    noInput = false ;
                              }
                              else
                              {
                                    noInput = true ;
                                    
                              }
                                     
                                     for  (int x = 0; x < addressField.length ; x ++)
                                     {
                                           checkAddress = addressField[x].getText() ;
                                           if(checkAddress.length() < 1)
                                           {
                                                 noInput = false ;
                                                 //JOptionPane.showMessageDialog(null,"Please ensure each field has an input","Input Error", JOptionPane.WARNING_MESSAGE) ;
                                                 break ;
                                           }
                                           else
                                           {
                                                 noInput = true ;
                                                 
                                           }
                                     }
                                    
                              
                              
                              if(noInput == false)
                              {
                                    JOptionPane.showMessageDialog(null,"Please ensure each field has an input","Input Error", JOptionPane.WARNING_MESSAGE) ;
                              }
                              else
                              {
                                    customerName = customerField.getText() ;
                                     customerAddress = addressField[0].getText() + ", " ;
                                     customerAddress += addressField[1].getText() + ", ";
                                     customerAddress += addressField[2].getText() + ", ";
                                     customerAddress += addressField[3].getText() + ", ";
                                     customerAddress += addressField[4].getText() ;
                                     customerTelephone = telephoneField.getText () ;
                                     //JOptionPane.showMessageDialog(null,customerName + customerAddress + customerTelephone,"Input Error", JOptionPane.WARNING_MESSAGE) ;
                                     //setVisible(false);
                              }
                        
                        }
                        
            }) ;
            
            
            container.add(okButton) ;
            container.add(cancelButton) ;
            
            setBounds(400,400,400,400) ;
            setVisible (true) ;
      }
      
      public void setName()
      {
            customerLabel = new JLabel ("Customer Name") ;
            customerLabel.setBounds(30,30,100,20) ;
            container.add(customerLabel) ;
            customerField = new JTextField () ;
            customerField.setBounds(30,50,200,20) ;
            container.add(customerField) ;
      
      }
      
      public String getName()
      {
            return customerName ;
      }
      
      
      public void setAddress()
      {
            addressLabel = new JLabel ("Address") ;
            addressLabel.setBounds(30,70,200,20) ;
            container.add(addressLabel) ;
            addressField[0] = new JTextField () ;
            addressField[0].setBounds(30,90,200,20) ;
            container.add(addressField[0]) ;
            addressField[1] = new JTextField () ;
            addressField[1].setBounds(30,110,200,20) ;
            container.add(addressField[1]) ;
            addressField[2] = new JTextField () ;
            addressField[2].setBounds(30,130,200,20) ;
            container.add(addressField[2]) ;
            addressField[3] = new JTextField () ;
            addressField[3].setBounds(30,150,200,20) ;
            container.add(addressField[3]) ;
            addressField[4] = new JTextField () ;
            addressField[4].setBounds(30,170,200,20) ;
            container.add(addressField[4]) ;
            //customerAddress = Address ;
      }
      
      public String getAddress()
      {
            return customerAddress ;
      }
      
      public void setTelephone()
      {
            telephoneLabel = new JLabel ("Telephone") ;
            telephoneLabel.setBounds(30,190,100,20) ;
            container.add(telephoneLabel) ;
            telephoneField = new JTextField () ;
            telephoneField.setBounds(30,210,200,20) ;
            container.add(telephoneField) ;
            //customerTelephone = Telephone ;
      }
      
      public String getTelephone()
      {
            return customerTelephone ;
      }
      
      public void  displayAccountDetails ()
      {
            
            JOptionPane.showMessageDialog(null, getName () + "\nAddress: " + getAddress () + "\nTelephone No: " +      getTelephone () ,  "Account Details", JOptionPane.INFORMATION_MESSAGE) ;
      }
      
      public void displayAccountDetails (customer newCustomer[], int noOfAccounts)
      {
            String accountInfo = "" ;
            accountInfo += "Listing of All Accounts\nAccount No:\t\tName:\t\tAddress\t\tTelephone Number\n" ;
            for(int i = 1 ; i<= noOfAccounts ; i++)
            {
                  
                  //Pass the value within the grid variable into the setText function of the outputArea object
                  accountInfo += i + "\t\t" + newCustomer[noOfAccounts].getName () + "\t\t" + newCustomer[noOfAccounts].getAddress () + "\t\t" + newCustomer[noOfAccounts].getTelephone () + "\n" ;
                  //System.out.println(newBank[i].getCustomer ()) ;
            }
            
            outputArea.setText(accountInfo);
            JOptionPane.showMessageDialog(null,outputArea,"Names", JOptionPane.INFORMATION_MESSAGE) ;
            
            
      }

}
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Cyart

ASKER

I have been looking at this code for ages, went away came back and still no joy cant believe I missed it.

Cheers
8-)