Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

question on returning strings in a program.

$
0
0
Hello. I am making a program for school. It is for my computer programming class. Here are the instructions given. Create a program that will allow the user to create or add bank accounts. The user options will be:
1. Create an account
2. Deposit into Checking Account
3. Deposit into Savings Account
4. Withdrawal from Checking Account
5. Withdrawal from Savings Account
6. Print out Bank Statement
0. Quit

The program will start with no bank accounts. Each time a new bank account is created store it at an object. These objects will be an array of objects and initialize it with a size of 10. The objects will store the following data:

Name (user inputted when creating the account)
Checking Account (the bank starts by putting $50 in your checking account)
Savings Account (starts at 0)
Account Number (1000-9999, created in the constructor)

Whenever options 2-6 are used print out a list of everyone at the bank and their account number. Have them type in their account number to choose which person they want to deposit, withdrawal, or print. Do not allow them to take out more money then they have.


Enhance your program:
For all of you advanced students this is the program to work on. Here are different ways to enhance it.

- Start the size of the array at 0. Whenever option 1 is chosen have the object array increase in size by 1 before creating the new account
- Allow the user to enter either the full name, or ID number in the same prompt. The program will then determine what they typed in and then finds the account
- Do not allow the computer to accidentally create the same account number for two different account. Test it using 0-9 instead of 1000-9999.

Here is What I have accomplished but the final balances are not returning when I print a statement and it is confusing me to no end.
import java.util.*;
public class SeveralBankAccountsMain
{
    static BankSub[] list = new BankSub[10];
    static Scanner scan = new Scanner(System.in);
    static Scanner scanLine = new Scanner(System.in);
    static int x = 1, numAccounts = 0;
    static int s = 0;
    static int d = 1;
    static int f;
    static int number;
    static double balC = 50;
    static double balS = 0;
    public static void main(String[] args)
    {
        System.out.println("\f");
        printMenu();
        int choice = scan.nextInt();
        while (choice != 0)
        {
            dispatch(choice);
            printMenu();
            choice = scan.nextInt();
        }
    }
    
    public static void dispatch(int choice)
    {
       
        switch(choice)
        {
            case 0:
            System.out.println("Bye!");
            break;
            case 1:
            System.out.print("What is the name: ");
            String name = scanLine.nextLine();
            System.out.print("What is the account number: ");
            number = scan.nextInt();
            list[numAccounts] = new BankSub (name, number);
            numAccounts++;
            x++;
            s++;
            
            break;
            case 2:
            System.out.print("What account do you want to deposit into? ");
            int accountAns = scan.nextInt();
            for( int z = 0;z<numAccounts;z++)
            {
                if (list[z].returnNum() == accountAns)
                    {
                        System.out.print("How much do you want to deposit into Checking account " + list[z].returnNum() + ": ");
                        double Cdeposits = scan.nextDouble();
                        list[z].depositchecking(list[z].returnNum(), Cdeposits, balC);
                    }
            }
            break;
            case 3:
            System.out.print("What account do you want to deposit into? ");
            int accountAn = scan.nextInt();
            for( int e = 0;e<numAccounts;e++)
            {
                if (list[e].returnNum() == accountAn)
                    {
                        System.out.print("How much do you want to deposit into Savings account " + list[e].returnNum() + ": ");
                        double Sdeposits = scan.nextDouble();
                        list[e].depositsavings(list[e].returnNum(), Sdeposits, balS);
                    }
            }
           
            break;
            case 4:
            System.out.print("What account do you want to withdrawl from? ");
            int accountA = scan.nextInt();
            for( int j = 0;j<numAccounts;j++)
            {
                if (list[j].returnNum() == accountA)
                    {
                  System.out.print("How much do you want to withdrawal from checking : ");
                  double Cwithdrawal = scan.nextDouble();
                  list[j].withdrawchecking(list[j].returnNum(),Cwithdrawal,balC);
                }
            }
            break;
            case 5:
            System.out.print("What account do you want to withdrawl from? ");
            int accountAnsw = scan.nextInt();
            for( int d = 0;d<numAccounts;d++)
            {
                if (list[d].returnNum() == accountAnsw)
                    {
                  System.out.print("How much do you want to withdrawal from savings: ");
                  double Swithdrawal = scan.nextDouble();
                  list[d].withdrawchecking(list[d].returnNum(),Swithdrawal,balS);
                }
            }
          
            break;
            case 6:
            System.out.print("What account do you want to get the statement for? ");
            int accountAnswer = scan.nextInt();
              for( int b = 0;b<numAccounts;b++)
              {
                 if (list[b].returnNum() == accountAnswer)
                    {
                        
                        System.out.println(list[b].returnChecking());
                    }
                }
           
           
            
            break;
            default:
            System.out.println("Sorry, invalid choice");
           }
    }
 
    public static void printMenu()
    {
        System.out.println("\n   Menu   ");
        System.out.println("   ====");
        System.out.println("0: Quit");
        System.out.println("1: Create an account");
        System.out.println("2: Deposit into Checking Account");
        System.out.println("3: Deposit into Savings Account");
        System.out.println("4: Withdrawal from Checking Account");
        System.out.println("5: Withdrawal from Savings Account");
        System.out.println("6: Print out Bank Statement");
        System.out.print("\nEnter your choice: ");
    }
  }



public class BankSub
{
   private int d, acctNum;
   private int[] nums = new int[d];
   private double balanceS = 0;
   private double balanceC = 0;
   private int x = 1;
   private int c = 0;
   private int account[] = new int[x];
   private String[] AccountFinal = new String [d]; 
   private String AccountPrint, name;
   private int finalBalC;
   private int finalBalS;
   public BankSub(String n, int acctNumber)
   {
      name = n;
      acctNum = acctNumber;
   }
  public void depositchecking(int accounts, double amount, double balC)
 
  {
    for (int g = 0; g<d; g++)
     {
         if (accounts == account[g])
         {
             amount += balC;            
         }
     }
  }
  
  public void depositsavings(int accounts, double amount, double BalS)
 
  {
 
    for (int g = 0; g<d; g++)
     {
         if (accounts == account[g])
         {
             amount += BalS;
         }
     }
 
  }
  
  
  public void withdrawchecking(int accounts, double amount, double balC)
 
  {
     for (int g = 0; g<d; g++)
     {
         if (accounts == account[g])
            {
               
                if (balanceC >= amount)
                {
                    balC -= amount;       
       
                }
                else
 
                System.out.println("Insufficient funds");
            }
        }
  }
  
   public void withdrawsavings(int accounts, double amount, double balS)
 
  {
 
    for (int g = 0; g<d; g++)
     {
         if (accounts == account[g])
            {
               
                if (balanceS >= amount)
                {
                    balS -= amount;       
       
                }
                else
 
                System.out.println("Insufficient funds");
            }
        }
    
  }
  
  public String returnChecking()
 
  {
     AccountPrint = "Statement for " + name + "\nChecking Balance: " + balanceC + "\nSavings Balance: " + balanceS;
     return AccountPrint;
  }
  public int returnNum()
  {
      return acctNum;
   }
  
}



Thanks in advance!

Crap wrong code
public class BankSub
{
   private int d, acctNum;
   private int[] nums = new int[d];
   private double balanceS = 0;
   private double balanceC = 0;
   private int x = 1;
   private int c = 0;
   private int account[] = new int[x];
   private String[] AccountFinal = new String [d]; 
   private String AccountPrint, name;
   public BankSub(String n, int acctNumber)
   {
      name = n;
      acctNum = acctNumber;
      balanceC = 50;
      balanceS = 0;
   }
    
   
  public void depositchecking(int accounts, double amount)
 
  {
    for (int g = 0; g<d; g++)
     {
         if (accounts == account[g])
         {
             amount += balanceC;
         }
     }
  }
  
  public void depositsavings(int accounts, double amount)
 
  {
 
    for (int g = 0; g<d; g++)
     {
         if (accounts == account[g])
         {
             amount += balanceS;
         }
     }
 
  }
  
  
  public void withdrawchecking(int accounts, double amount)
 
  {
     for (int g = 0; g<d; g++)
     {
         if (accounts == account[g])
            {
               
                if (balanceC >= amount)
                {
                    balanceC -= amount;       
       
                }
                else
 
                System.out.println("Insufficient funds");
            }
        }
  }
  
   public void withdrawsavings(int accounts, double amount)
 
  {
 
    for (int g = 0; g<d; g++)
     {
         if (accounts == account[g])
            {
               
                if (balanceS >= amount)
                {
                    balanceS -= amount;       
       
                }
                else
 
                System.out.println("Insufficient funds");
            }
        }
    
  }
  
  public String toString()
 
  {
     AccountPrint = "Statement for " + name+ "\nChecking Balance: " + balanceC + "\nSavings Balance: " + balanceS;
     return AccountPrint;
  }
  public int returnNum()
  {
      return acctNum;
    }
  
}


import java.util.*;
public class SeveralBankAccountsMain
{
    static BankSub[] list = new BankSub[10];
    static Scanner scan = new Scanner(System.in);
    static Scanner scanLine = new Scanner(System.in);
    static int x = 1, numAccounts = 0;
    static int s = 0;
    static int d = 1;
    static int f;
    static int number;
    public static void main(String[] args)
    {
        System.out.println("\f");
        printMenu();
        int choice = scan.nextInt();
        while (choice != 0)
        {
            dispatch(choice);
            printMenu();
            choice = scan.nextInt();
        }
    }
    
    public static void dispatch(int choice)
    {
       
        switch(choice)
        {
            case 0:
            System.out.println("Bye!");
            break;
            case 1:
            System.out.print("What is the name: ");
            String name = scanLine.nextLine();
            System.out.print("What is the account number: ");
            number = scan.nextInt();
            list[numAccounts] = new BankSub (name, number);
            numAccounts++;
            x++;
            s++;
            
            break;
            case 2:
            System.out.print("What account do you want to deposit into? ");
            int accountAns = scan.nextInt();
            for( int z = 0;z<numAccounts;z++)
            {
                if (list[z].returnNum() == accountAns)
                    {
                        System.out.print("How much do you want to deposit into Checking account " + list[z].returnNum() + ": ");
                        double Cdeposits = scan.nextDouble();
                        list[z].depositchecking(list[z].returnNum(), Cdeposits);
                    }
            }
            break;
            case 3:
            System.out.print("What account do you want to deposit into? ");
            int accountAn = scan.nextInt();
            for( int e = 0;e<numAccounts;e++)
            {
                if (list[e].returnNum() == accountAn)
                    {
                        System.out.print("How much do you want to deposit into Savings account " + list[e].returnNum() + ": ");
                        double Sdeposits = scan.nextDouble();
                        list[e].depositsavings(list[e].returnNum(), Sdeposits);
                    }
            }
           
            break;
            case 4:
            System.out.print("What account do you want to withdrawl from? ");
            int accountA = scan.nextInt();
            for( int j = 0;j<numAccounts;j++)
            {
                if (list[j].returnNum() == accountA)
                    {
                  System.out.print("How much do you want to withdrawal from checking : ");
                  double Cwithdrawal = scan.nextDouble();
                  list[j].withdrawchecking(list[j].returnNum(),Cwithdrawal);
                }
            }
            break;
            case 5:
            System.out.print("What account do you want to withdrawl from? ");
            int accountAnsw = scan.nextInt();
            for( int d = 0;d<numAccounts;d++)
            {
                if (list[d].returnNum() == accountAnsw)
                    {
                  System.out.print("How much do you want to withdrawal from savings: ");
                  double Cwithdrawal = scan.nextDouble();
                  list[d].withdrawchecking(list[d].returnNum(),Cwithdrawal);
                }
            }
          
            break;
            case 6:
            System.out.print("What account do you want to get the statement for? ");
            int accountAnswer = scan.nextInt();
              for( int b = 0;b<numAccounts;b++)
            {
                 if (list[b].returnNum() == accountAnswer)
                    {
                        
                        System.out.println(list[b].toString());
                    }
                }
           
           
            
            break;
            default:
            System.out.println("Sorry, invalid choice");
           }
    }
 
    public static void printMenu()
    {
        System.out.println("\n   Menu   ");
        System.out.println("   ====");
        System.out.println("0: Quit");
        System.out.println("1: Create an account");
        System.out.println("2: Deposit into Checking Account");
        System.out.println("3: Deposit into Savings Account");
        System.out.println("4: Withdrawal from Checking Account");
        System.out.println("5: Withdrawal from Savings Account");
        System.out.println("6: Print out Bank Statement");
        System.out.print("\nEnter your choice: ");
    }
  }


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>