Results 1 to 3 of 3
  1. #1
    Ortlep is offline Member
    Join Date
    Dec 2011
    Posts
    2
    Rep Power
    0

    Default Stuck on a banking program

    I've got most of the code finished, but my problem is with the actual output of data. When I select option 5 (to list all accounts) it only lists the most recent Checking and Savings accounts (Say I create 2 checking accounts, only the newest one will get saved and outputted on screen). How would I go about making my program list ALL accounts? I have 4 files to my code so here we go

    Account
    Java Code:
    public abstract class Account
    {
        int acctNum;
        double acctBal;
    
    public Account(int num, double bal)
    {
        acctNum = num;
        acctBal = bal;
    }
    public void deposit(double amount)
    {
        acctBal += amount;
    System.out.println("$" + amount + " deposited into your account");
    System.out.println("Your account bal is $" + acctBal);
    }
    public void withdraw(double amount)
    {
        if(amount < acctBal)
            {
            acctBal -= amount;
            System.out.println("You have drawn an amount of $" + amount);
            System.out.println("Your account bal is $" + acctBal);
            }
        else
            {
            System.out.println("Your account doesn't have sufficient balance");
            }
    }
    public void show()
    {
        System.out.println("Account " + acctNum + " has balance " + acctBal);   
    }
    abstract void calculateDailyInterest();
    }
    Checking
    Java Code:
    public class Checking extends Account
    {
    public Checking(int acctNum,double balance)
    {
        super(acctNum,balance);
    }
    void calculateDailyInterest()
    {
        acctBal += acctBal + (acctBal - 500) * 0.02;
    }
    }
    Savings
    Java Code:
     public class Savings extends Account
    {
    public Savings(int acctNum, double balance)
    {
        super(acctNum, balance);
    }
    void calculateDailyInterest()
    {
        acctBal += acctBal + acctBal * 0.03;
    }
    }
    Account Test
    Java Code:
    import java.util.Scanner;
    
     public class AccountTest
    {
    public static void main(String args[])
    {
        Savings save = new Savings(0,0.0);
        Checking check = new Checking(0,0.0);
        int choice;
        int acctNum;
        double balance;
        Scanner scan = new Scanner(System.in);
        System.out.println("1. create new checking account");
        System.out.println("2. create new savings");
        System.out.println("3. make deposit");
        System.out.println("4. make withdraw");
        System.out.println("5. display all accounts");
        System.out.println("6. calculate daily interest");
        System.out.println("7 exit");
        choice = scan.nextInt();
        do
            {       
            switch(choice)
                    {
                case 1: System.out.print("A new checking account with account number: ");
                        acctNum = scan.nextInt();
                        System.out.print("Enter the initial balance: ");
                        balance = scan.nextDouble();
                        check = new Checking(acctNum, balance);
                        choice = scan.nextInt();
                        break;
               
                case 2: System.out.print("A new savings account with account number: ");
                        acctNum = scan.nextInt();
                        System.out.print("Enter the initial balance:");
                        balance = scan.nextDouble();
                        save = new Savings(acctNum, balance);
                        choice = scan.nextInt();
                        break;
                       
                case 3: System.out.print("Which account to deposit? ");
                		acctNum = scan.nextInt();
                		System.out.print("Enter the amount to deposit: ");
                        balance = scan.nextDouble();
                        check.deposit(balance);
                        save.deposit(balance);
                        choice = scan.nextInt();
                        break;
                case 4: System.out.print("Enter amount to withdraw: ");
                        balance = scan.nextDouble();
                        check.withdraw(balance);
                        save.withdraw(balance);
                        choice = scan.nextInt();
                        break;
                case 5: System.out.println("*********************************");
                		check.show();
                		save.show();
                		System.out.println("*********************************");
                		choice = scan.nextInt();
                    break;
                case 6: check.calculateDailyInterest();
                    save.calculateDailyInterest();
                    choice = scan.nextInt();
                    break;
                case 7:System.exit(0);
                       }
            }while(choice != 7);
        }
    }

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Stuck on a banking program

    I believe you have three problems here;

    1. your Account.show() method only prints one line i.e. it is not set to print a list of all Accounts made
    Java Code:
    public void show()
    {
        System.out.println("Account " + acctNum + " has balance " + acctBal);
    }
    2. you have not stored a list of all accounts anywhere in the program and
    3. you do infact write-over the last account made (stored in the variable 'check') with this code
    Java Code:
            switch (choice)
                    {
                case 1: System.out.print("A new checking account with account number: "); 
                        ...
                        check = new Checking(acctNum, balance);
                        ...
                        break;
    Last edited by ozzyman; 12-14-2011 at 12:17 AM.

  3. #3
    Ortlep is offline Member
    Join Date
    Dec 2011
    Posts
    2
    Rep Power
    0

    Default Re: Stuck on a banking program

    So I am assuming I need an arraylist to store the accounts? Eh, I hate arrays, never been with them lol.

Similar Threads

  1. Web Crawler for Online Banking
    By foulkes in forum Advanced Java
    Replies: 0
    Last Post: 09-05-2011, 09:22 PM
  2. Stuck - simple program
    By dirtycash in forum New To Java
    Replies: 4
    Last Post: 11-24-2008, 07:44 PM
  3. Really Stuck on how to Start this program
    By random0munky in forum New To Java
    Replies: 4
    Last Post: 10-29-2008, 08:57 AM
  4. Replies: 8
    Last Post: 08-26-2008, 04:50 AM
  5. FireSimulation Program, got stuck with a bug...
    By riz618 in forum New To Java
    Replies: 5
    Last Post: 01-28-2008, 03:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •