Counting Transactions

Cannot get my for loop to work properly

Page 1 of 1

0 Replies - 1529 Views - Last Post: 29 March 2010 - 10:57 AM Rate Topic: -----

#1 justabeginner   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 17-February 10

Counting Transactions

Posted 29 March 2010 - 10:57 AM

I have completed all of the code for this assignment except for the following task:

Imagine that the while loop contains the transactions for a single day. Embed it in a loop that allows the transactions to be recorded and counted for many days. At the beginning of each day print the summary for each account, then have the user enter the transactions for the day. When all of the transactions have been entered, print the total numbers and amounts, then reset these values to 0 and repeat for the next day. Should the reset variable methods be static or instance methods?

My methods class:
import java.util.Random;
public class Account
{
  private double balance;
  private String name;
  private long acctNum;
  private static int numAccounts = 0;
  
  private static int numDeposits = 0;
  private static int numWithdrawals = 0;
  private static double totDeposits = 0;
  private static double totWithdrawals = 0;

  //Constructor -- initializes balance, owner, and account number
  //----------------------------------------------
  public Account(double initBal, String owner, long number)
  {
    balance = initBal;
    name = owner;
    acctNum = number;
  }
  
  //----------------------------------------------
  //Constructor -- initializes balance, owner, and account number 
  //---------------------------------------------- 
  public Account (double initBal, String owner) 
  { 
    balance = initBal; 
	 name = owner; 
	 Random generator = new Random();
	 acctNum = Math.abs(generator.nextLong());
	 numAccounts++;
  } 
  
  //----------------------------------------------
  //Returns number of deposits
  //----------------------------------------------
  public static int getNumDeposits()
  {
    return numDeposits;
  }
  
  //----------------------------------------------
  //Returns number of withdrawals
  //----------------------------------------------
  public static int getNumWithdrawals()
  {
    return numWithdrawals;
  }
  
  //----------------------------------------------
  //Returns total value of deposits
  //----------------------------------------------
  public static double getTotDeposits()
  {
    return totDeposits;
  }

  //----------------------------------------------
  //Returns total value of withdrawals
  //----------------------------------------------
  public static double getTotWithdrawals()
  {
    return totWithdrawals;
  }
 
  //----------------------------------------------
  //Resets number of deposits
  //----------------------------------------------
  public static int resetNumDeposits()
  {
    numDeposits = 0;  
	 return numDeposits;
  }
  
  //----------------------------------------------
  //Resets number of withdrawals
  //----------------------------------------------
  public static int resetNumWithdrawals()
  {
    numWithdrawals = 0;  
	 return numWithdrawals;
  }
  
  //----------------------------------------------
  //Resets total value of deposits
  //----------------------------------------------
  public static double resetTotDeposits()
  {
    totDeposits = 0;  
	 return totDeposits;
  }

  //----------------------------------------------
  //Resets total value of withdrawals
  //----------------------------------------------
  public static double resetTotWithdrawals()
  {
    totWithdrawals = 0;  
	 return totWithdrawals;
  }

  //----------------------------------------------
  //Returns number of accounts created
  //----------------------------------------------
  public static int getNumAccounts()
  {
    return numAccounts;
  }
  
  //----------------------------------------------
  //Returns account number
  //----------------------------------------------
  public long getAcctNumber()
  {
    return acctNum;
  }
  
  //----------------------------------------------
  //Prints the summary
  //----------------------------------------------
  public String printSummary()
  {
	return "Name: " + name + 
        "\nAccount Number: " + acctNum +
        "\nBalance: $" + balance; 
  }
  
  //----------------------------------------------
  // Returns name on the account
  //----------------------------------------------
  public String getName()
  {
    return name;
  }
  
  //----------------------------------------------
  //Closes current account
  //----------------------------------------------
  public void close()
  {
    name = name.concat("CLOSED");
	 balance = 0;
	 numAccounts--;
  }
  
  //----------------------------------------------
  // Consolidates two accounts into one account.
  //----------------------------------------------
  public static Account consolidate(Account acct1, Account acct2)
  {
    if((acct1.name).equalsIgnoreCase(acct2.name))
	 {
	   if((acct1.acctNum) != (acct2.acctNum))
		{
	     Account consAcct = new Account(acct1.getBalance() + acct2.getBalance(), acct1.getName());
	     acct1.close();
	     acct2.close();
	     return consAcct;
      }
	   else
	     System.out.println("Two accounts with the same number cannot be consolidated.");
		  return null;	
	 }
	 else
	 System.out.println("Accounts to be consolidated must have the same owner name.");
    return null;
  }
  
  //----------------------------------------------
  // Checks to see if balance is sufficient for withdrawal.
  // If so, decrements balance by amount; if not, prints message.
  //----------------------------------------------
  public void withdraw(double amount)
  {
    if (balance >= amount)
      {
		 balance -= amount;
		 numWithdrawals++;
		 totWithdrawals -= amount;
		}
    else
       System.out.println("Insufficient funds for this transaction.");
  }
   public void withdraw(double amount, double fee)
  {
    if (balance >= (amount+fee))
       {  
		  balance -= (amount+fee);
		  numWithdrawals++;
		  totWithdrawals -= amount;
		 }
    else
       System.out.println("Insufficient funds for this transaction.");
  }

  //----------------------------------------------
  // Adds deposit amount to balance.
  //----------------------------------------------
  public void deposit(double amount)
  {
    balance += amount;
	 numDeposits++;
	 totDeposits += amount;
  }

  //----------------------------------------------
  // Returns balance.
  //----------------------------------------------
  public double getBalance()
  {
    return balance;
  }
  //----------------------------------------------
  // Returns a string containing the name, account number, and balance.
  //----------------------------------------------
  public String toString()
  {
	return "Name: " + name + 
        "\nAccount Number: " + acctNum +
        "\nBalance: $" + balance; 
  }
}


The main program:
//*******************************************************
// ProcessTransactions.java
//
// A class to process deposits and withdrawals for two bank
// accounts for a single day.
//*******************************************************
import java.util.Scanner;

public class ProcessTransactions
{
    public static void main(String[] args){

	Account acct1, acct2;           //two test accounts
	String keepGoing = "y";         //more transactions?
	String action;                  //deposit or withdraw
	double amount;                  //how much to deposit or withdraw
	long acctNumber;                //which account to access
	int count=1;

	Scanner scan = new Scanner(System.in);

	//Create two accounts
	acct1 = new Account(1000, "Sue", 123);
	acct2 = new Account(1000, "Joe", 456);

	System.out.println("The following accounts are available:\n");
	System.out.println(acct1.printSummary());

	System.out.println();
	System.out.println(acct2.printSummary());

	for(count=1; keepGoing.equals("y") || keepGoing.equals("Y"); count++)
	{
	while (keepGoing.equals("y") || keepGoing.equals("Y"))
	    {
		//get account number, what to do, and amount
		System.out.print("\nEnter the number of the account you would like to access: ");
		acctNumber = scan.nextLong();
		System.out.print("Would you like to make a deposit (D) or withdrawal (W)? ");
		action = scan.next();
		System.out.print("Enter the amount: ");
		amount = scan.nextDouble();

		if (amount > 0)
		    if (acctNumber == acct1.getAcctNumber())
			if (action.equals("w") || action.equals("W"))
			    acct1.withdraw(amount);
			else if (action.equals("d") || action.equals("D"))
			    acct1.deposit(amount);
			else 
			    System.out.println("Sorry, invalid action.");
		    else if (acctNumber == acct2.getAcctNumber())
			if (action.equals("w") || action.equals("W"))
			    acct1.withdraw(amount);
			else if (action.equals("d") || action.equals("D"))
			    acct1.deposit(amount);
			else 
			    System.out.println("Sorry, invalid action.");
		    else
			System.out.println("Sorry, invalid account number.");
		else
			System.out.println("Sorry, amount must be > 0.");


		System.out.print("\nMore transactions? (y/n)");
		keepGoing = scan.next();
	    }
   
	System.out.println("Number of transactions for this day: " + count);

	//Print number of deposits
	System.out.println("The number of deposits made: " + Account.getNumDeposits());
	//Print number of withdrawals
	System.out.println("The number of withdrawals made: " + Account.getNumWithdrawals());
	//Print total amount of deposits
	System.out.println("The total amount of all deposits: $" + Account.getTotDeposits());
	//Print total amount of withdrawals
	System.out.println("The total amount of all withdrawals: $" + Account.getTotWithdrawals());
	
	//Reset the values for the next day
	Account.resetNumDeposits();
	Account.resetNumWithdrawals();
	Account.resetTotDeposits();
	Account.resetTotWithdrawals();
	} //end of for loop
  }
}



Does anyone know how I can structure my for loop to do this???

Is This A Good Question/Topic? 0
  • +

Page 1 of 1