Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Creating a bank, I need to check the balance of a specific account in the array

ID: 3674192 • Letter: C

Question

Creating a bank, I need to check the balance of a specific account in the array list using the balance() method. First I validate the account number, then show them their balance. But I cant get it to work and im not sure why?? Im getting errors about the getNum and getBal methods.

import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;

public class Bank
{
   String acctName;
   int acctNum, acctBal;
   private ArrayList<Account> bankAccounts = new ArrayList<Account>();
  
   //READ from file
   public void read()
   {
      bankAccounts.clear();
      
       try
      {
      FileReader fr = new FileReader("bank.txt");
       BufferedReader br = new BufferedReader(fr);
      String stringRead = br.readLine();

      while(stringRead != null)
      {
          StringTokenizer st = new StringTokenizer(stringRead, ",");
         String acctName = st.nextToken();
          String sAcctNum = st.nextToken();
          String sAcctBal = st.nextToken();
              
               //Parse strings to ints
               acctNum = Integer.parseInt(sAcctNum);
               acctBal = Integer.parseInt(sAcctBal);
              
               //Add accounts to arraylist
               Account account = new Account(acctName, acctNum, acctBal);
               bankAccounts.add(account);

           stringRead = br.readLine();
      }
      br.close();
      }
      catch(FileNotFoundException fnfe)
      {
      JOptionPane.showMessageDialog(null, "Unable to find bank.txt", "ERROR", JOptionPane.ERROR_MESSAGE);
      }
      catch(IOException ioe)
      {
      ioe.printStackTrace();
      }
   }
  
   //WRITE to file
   public void write()
   {  
       try
       {
           FileWriter fw = new FileWriter("bank.txt");
           BufferedWriter bw = new BufferedWriter(fw);
           int size = bankAccounts.size();
          
           for(int i=0; i < size; i++)
           {
               bw.write(bankAccounts.get(i).getName() + ","
               + bankAccounts.get(i).getNum() + ","
               + bankAccounts.get(i).getBal() + " ");
           }
           bw.close();
       }
       catch(IOException ioe)
       {
           ioe.printStackTrace();
       }
   }
  
   //CHECK account balance
   public void balance()
   {
       acctNum = Integer.parseInt(JOptionPane.showInputDialog("Please validate your account #:"));
      
       for(Account acct : bankAccounts)
       {
           if(bankAccounts.getNum().equals(acctNum))
           {
               JOptionPane.showMessageDialog(null, "Your account balance is: " + bankAccounts.getBal(), "Current Balance", JOptionPane.PLAIN_MESSAGE);
           }
           else
           {
               JOptionPane.showMessageDialog(null, "We couldn't find you account, please try again.", "Error", JOptionPane.ERROR_MESSAGE);
           }
       }
   }
  
   //DEPOSIT money
   public void deposit()
   {
       acctNum = Integer.parseInt(JOptionPane.showInputDialog("Please validate your account #:"));
      
      
   }
  
   //WITHDRAW money
   public void withdraw()
   {
       acctNum = Integer.parseInt(JOptionPane.showInputDialog("Please validate your account #:"));
      
      
   }
  
   //ADD a new account
   public void add()
   {
       acctName = JOptionPane.showInputDialog("Please enter the name for the account:");
       acctNum = Integer.parseInt(JOptionPane.showInputDialog("Please enter the number for the account:"));
       acctBal = Integer.parseInt(JOptionPane.showInputDialog("Please enter the initial deposit for the account:"));
      
       Account account = new Account(acctName, acctNum, acctBal);
       bankAccounts.add(account);
      
       JOptionPane.showMessageDialog(null, "Account has been added.", "Success!", JOptionPane.PLAIN_MESSAGE);
   }
  
   //DELETE account
   public void delete()
   {
       //Gather account# to be deleted
       acctNum = Integer.parseInt(JOptionPane.showInputDialog("Please enter your account#:"));

   }
  
   //OUTPUT to console
   public void output()
   {
       String output = "";

       for(Account list : bankAccounts)
       {
          output += list.toString();
   }
   System.out.println(output);
    }

   public static void main (String[] args)
   {
       boolean var = false;
       String choice;
      
       Richard1155318 account = new Richard1155318();
       account.read();
  
       do
       {
           //Choose transaction type
           choice = JOptionPane.showInputDialog("Please choose an option: 1. Check Balance 2. Deposit 3. Withdraw 4. Add Account 5. Delete Account #. Quit");
          
           if(choice.equals("1"))
           {
               account.balance();
           }
          
           else if(choice.equals("2"))
           {
               account.deposit();
           }
          
           else if(choice.equals("3"))
           {
               account.withdraw();
           }
          
           else if(choice.equals("4"))
           {
               account.add();
           }
          
           else if(choice.equals("5"))
           {
               account.delete();
           }
          
           else if(choice.equals("#"))
           {
               var = true;
               JOptionPane.showMessageDialog(null, "Have a nice day!", "Thank You", JOptionPane.PLAIN_MESSAGE);
               account.write();
               //account.output();
           }
          
           else
           {
               JOptionPane.showMessageDialog(null, "There was an error, please try again.", "Error", JOptionPane.ERROR_MESSAGE);
           }
       } while(var == false);
   }
}

Explanation / Answer

I am written correct for you check this below code and excute :

import java.util.*;

import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;

class bankInternal {

    int ac;

    float bal, amt;

    Scanner get = new Scanner(System.in);

    void getdetails()

    {

        System.out.print("Enter Account Number:");

        ac = get.nextInt();

        System.out.println();

        System.out.print("Enter Initial Balance:");

        bal = get.nextFloat();

    }

    void deposit()

    {

        System.out.println();

        System.out.print("Enter Amount:");

        amt = get.nextInt();

        bal = bal+amt;

        System.out.println("Success! Amount deposited. Balance Available:"+bal);

    }

    void withdraw()

    {

        System.out.println();

        System.out.print("Enter Amount:");

        amt = get.nextInt();

        if(amt>bal)

        {

            System.out.println("Insufficient Funds!!");

        }

        else

        {

            bal = bal-amt;

            System.out.println("Amount Withdrawn! Available Balance:"+bal);

        }  

    }

     

    void balchk()

    {

        System.out.println("Balance available in Account Number "+ac+" is: "+bal);

    }

}

class bankArray {

    public static void main(String args[])

    {

        boolean a=true;

        int acno, flag;

        Scanner in = new Scanner(System.in);

        bankInternal obj[] = new bankInternal[100];

        for(int i=0;i<3;i++)

        {

            obj[i] = new bankInternal();

        }

        int ch;

        do

        {

            flag=1; //Reset flag value.

            System.out.println("1.Add Account"+" "+"2.Deposit"+" "+"3.Withdraw"+" "+"4.Check Balance"+" "+"5.Exit");

            System.out.println();

            System.out.print("Enter your Choice:");

            ch = in.nextInt();

            switch(ch)

            {

                case 1:for(int i=0;i<3;i++)

                        {

                            obj[i].getdetails();

                        }

                        break;

                case 2: System.out.println("Enter Account Number:");

                        acno = in.nextInt();

                        for(int i=0;i<3;i++)

                        {

                            if(acno == obj[i].ac)

                            {

                                obj[i].deposit();

                                flag=0;

                            }

                        }

                            if(flag==1)

                            {

                                System.out.println("Account number not found!!"+" ");

                            }

                        break;

                case 3: System.out.println("Enter Account Number:");

                        acno = in.nextInt();

                        for(int i=0;i<3;i++)

                        {

                            if(acno == obj[i].ac)

                            {

                                obj[i].withdraw();

                                flag=0;

                            }

                        }  

                            if(flag==1)

                            {

                                System.out.println("Account number not found!!"+" ");

                            }

                        break;

                case 4: System.out.println("Enter Account Number:");

                        acno = in.nextInt();

                        for(int i=0;i<3;i++)

                        {

                            if(acno == obj[i].ac)

                            {

                                obj[i].balchk();

                                flag=0;

                            }

                        }

                        if(flag==1)

                        {

                            System.out.println("Account number not found!!"+" ");

                        }

                        break;

                case 5: System.exit(0);

                default: System.out.println("Enter a valid Option!"+" ");

            }

        }while(a);

    }

}          

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote