Question

Suppose the bank wanted to keep track of the total number of deposits and withdrawals (separately)...

Suppose the bank wanted to keep track of the total number of deposits and withdrawals (separately) for each day, and the total amount deposited and withdrawn. Write code to do this as follows:

  1. Add a public toString method to the class so that Account objects can be easily printed. Test that this works by adding a main method to Account that creates and prints two Account objectsvia println statements. Provide the transcript in the lab document.
  2. Add four private static variables to the Account class, one to keep track of each value above (number and total amount of deposits, number and total of withdrawals). Because these values should be shared by all Account objects, they should be represented as static (i.e., "class") variables. This is in contrast to the instance variables that hold the balance, name, and account number of each account. Each Account object has its own set of these. Recall that numeric static and instance variables are initialized to 0 by default.
  3. Add public accessor methods (sometimes called, "getters") to return the values of each of the class variables you just added, e.g., public static int getNumDeposits(). Note that these methods are static because the values they are accessing are also static.
  4. Modify the withdraw and deposit methods to update the appropriate static variables at each withdrawal and deposit.
  5. File ProcessTransactions.java contains a program that creates and initializes two Account objects and enters a loop that allows the user to enter transactions for either account until asking to quit. Modify this program as follows:
    • After the loop, print the total number of deposits and withdrawals and the total amount of each. You will need to use the static Account methods that you wrote above, in step 3. Test your program. Make sure the output is correct. Place a transcript in your lab document that demonstrates that deposits and withdrawals are being recorded properly.
  6. Create a new static method, handleOneDay, that takes two arguments, the accounts. The body of this method should be all the code in the current main method between the comments //Finish handling the account transaction and //Handle the account transactions. Cut and paste this code into the new method. You may also need to move some of the variable declarations in main to be in handleOneDayinstead. Now, replace the code that was in main with a single call to handleOneDay. Do that, then run your program to make sure everything still works as it did before you began this surgery.
  7. Now, change main so that the user can enter data for many days, instead of just one. Embed the call to handleOneDay in a loop there that allows the transactions to be recorded and counted for many days. The user should be prompted, "do you want to enter transactions for another day?" at the bottom of this loop. 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 (as above), then reset these values to 0 and repeat for the next day. Note that you will need to add methods to reset the variables holding the numbers and amounts of withdrawals and deposits to the Account class. Think: should these be static or non-static methods?
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;
public class Accounts {


  
//private static variables
private static int number_of_deposits=0;
private static int number_of_withdrawals=0;
private static int total_amount_of_deposits=0;
private static int total_amount_of_withdrawals=0;
  
//other instance variables
public double balance=0;
public String name;
public String account_number;
  
//public accessor methods
public static int getNumberOfDeposits(){
return number_of_deposits;
}
public static int getNumberOfWithdrawals(){
return number_of_withdrawals;
}
public static int getTotalAmountOfWithdrawals(){
return total_amount_of_withdrawals;
}
public static int getTotalAmountOfDeposits(){
return total_amount_of_deposits;
}
public double getBalance(){
return this.balance;
}
  
public String getName(){
return this.name;
}
public String getAccountNumber(){
return this.account_number;
}
  
//toString method
public String toString(){
return ("\nName : "+this.getName()+" Account Number : "+this.getAccountNumber()+" Balane : "+this.getBalance());
  
}
  
//withdraw and deposit methods
public void withdraw(double amount){
if(this.balance>=amount){
number_of_withdrawals++;
total_amount_of_withdrawals+=amount;
this.balance-=amount;
}
else{
System.out.println("\nInsufficient balance\n");
}
}
  
public void deposit(double amount){
number_of_deposits++;
total_amount_of_deposits+=amount;
this.balance+=amount;
}
//main method
public static void main(String args[]){
int ch=1;
Accounts a1=new Accounts();
Accounts a2=new Accounts();
//Reading basic details
Scanner data=new Scanner(System.in);
System.out.println("\nEnter details of Account 1 :\n");
System.out.println("\nName : ");
a1.name=data.next();
System.out.println("\nAccount Number : ");
a1.account_number=data.next();
  
System.out.println("\nEnter details of Account 2 :\n");
System.out.println("\nName : ");
a2.name=data.next();
System.out.println("\nAccount Number : ");
a2.account_number=data.next();
  
//loop
do{
int ch1;
System.out.println("\n------------Choose Account-------------\n1. Acccount 1\n2 Account 2\n");
ch1=Integer.parseInt(data.next());
switch(ch1){
case 1:
System.out.println("\n1.Deposit\n2.Withdraw\n");
switch(Integer.parseInt(data.next())){
case 1:
System.out.println("\nEnter amount to deposit : ");
a1.deposit(Integer.parseInt(data.next()));
break;
case 2:
System.out.println("\nEnter amount to withdraw : ");
a1.withdraw(Integer.parseInt(data.next()));
break;
  
}
break;

case 2:
System.out.println("\n1.Deposit\n2.Withdraw\n");
switch(Integer.parseInt(data.next())){
case 1:
System.out.println("\nEnter amount to deposit : ");
a2.deposit(Integer.parseInt(data.next()));
break;
case 2:
System.out.println("\nEnter amount to withdraw : ");
a2.withdraw(Integer.parseInt(data.next()));
break;
  
}
break;

}
System.out.println("\nDo you want to continue ,1 to continue ,any other number to stop : ");
ch=Integer.parseInt(data.next());
}while(ch==1);
  
//display the details of deposit and withdrwals
  
System.out.println("\nNumber of Deposits : "+getNumberOfDeposits());
System.out.println("\nNumber of Withdrawals : "+getNumberOfWithdrawals());
System.out.println("\nTotal Amount of Deposits : "+getTotalAmountOfDeposits());
System.out.println("\nTotal Amount of Withdrawals : "+getTotalAmountOfWithdrawals());
}


}

-------------------------------------------handle one day method------------------------------

public static void handleOneDay(Accounts a1,Accounts a2){
Scanner data=new Scanner(System.in);

int ch1;
System.out.println("\n------------Choose Account-------------\n1. Acccount 1\n2 Account 2\n");
ch1=Integer.parseInt(data.next());
switch(ch1){
case 1:
System.out.println("\n1.Deposit\n2.Withdraw\n");
switch(Integer.parseInt(data.next())){
case 1:
System.out.println("\nEnter amount to deposit : ");
a1.deposit(Integer.parseInt(data.next()));
break;
case 2:
System.out.println("\nEnter amount to withdraw : ");
a1.withdraw(Integer.parseInt(data.next()));
break;
  
}
break;

case 2:
System.out.println("\n1.Deposit\n2.Withdraw\n");
switch(Integer.parseInt(data.next())){
case 1:
System.out.println("\nEnter amount to deposit : ");
a2.deposit(Integer.parseInt(data.next()));
break;
case 2:
System.out.println("\nEnter amount to withdraw : ");
a2.withdraw(Integer.parseInt(data.next()));
break;
  
}
break;

}
  
  
}


Add a comment
Know the answer?
Add Answer to:
Suppose the bank wanted to keep track of the total number of deposits and withdrawals (separately)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What if you had to write a program that would keep track of a list of...

    What if you had to write a program that would keep track of a list of rectangles? This might be for a house painter to use in calculating square footage of walls that need paint, or for an advertising agency to keep track of the space available on billboards. The first step would be to define a class where one object represents one rectangle's length and width. Once we have class Rectangle, then we can make as many objects of...

  • Instructions We're going to create a program which will allow the user to add values to a list, p...

    Instructions We're going to create a program which will allow the user to add values to a list, print the values, and print the sum of all the values. Part of this program will be a simple user interface. 1. In the Program class, add a static list of doubles: private statie List<double> _values new List<double>) this is the list of doubles well be working with in the program. 2. Add ReadInteger (string prompt) , and ReadDouble(string prompt) to the...

  • Java coding help! Write a program to simulate a bank which includes the following. Include a...

    Java coding help! Write a program to simulate a bank which includes the following. Include a commented header section at the top of each class file which includes your name, and a brief description of the class and program. Create the following classes: Bank Customer Account At a minimum include the following data fields for each class: o Bank Routing number Customer First name Last name Account Account number Balance Minimum balance Overdraft fee At a minimum write the following...

  • 1-Suppose you write an application in which one class contains code that keeps track of the...

    1-Suppose you write an application in which one class contains code that keeps track of the state of a board game, a separate class sets up a GUI to display the board, and a CSS is used to control the stylistic details of the GUI (for example, the color of the board.) This is an example of a.Duck Typing b.Separation of Concerns c.Functional Programming d.Polymorphism e.Enumerated Values 2-JUnit assertions should be designed so that they a.Fail (ie, are false) if...

  • In C++ Write a program that contains a class called VideoGame. The class should contain the...

    In C++ Write a program that contains a class called VideoGame. The class should contain the member variables: Name price rating Specifications: Dynamically allocate all member variables. Write get/set methods for all member variables. Write a constructor that takes three parameters and initializes the member variables. Write a destructor. Add code to the destructor. In addition to any other code you may put in the destructor you should also add a cout statement that will print the message “Destructor Called”....

  • solve this JAVA problem in NETBEANS Problem #12 in page 400 of your text (6th edition): SavingsAccount Class. Design a SavingsAccount class that stores a savings account's annual interest rate...

    solve this JAVA problem in NETBEANS Problem #12 in page 400 of your text (6th edition): SavingsAccount Class. Design a SavingsAccount class that stores a savings account's annual interest rate and balance. The class constructor should accept the amount of the savings account's starting balance. The class should also have methods for subtracting the amount of a withdrawal, adding the amount of a deposit, and adding the amount of monthly twelve. To add the monthly interest rate to the balance,...

  • 7. Programming Problem: In this problem we are trying to model a repair service that can...

    7. Programming Problem: In this problem we are trying to model a repair service that can repair Cars and Computers. Even though the Car and Computer are different products, they have common repair methods that can be abstracted in an interface. Please complete the interface and the classes by adding the required methods, as per the problem description. Partially complete code is provided as shown below A. Create an interface called Mechanism. This interface has two methods. The first method...

  • Java 1. Create an application named NumbersDemo whose main() method holds two integer variables. Assign values...

    Java 1. Create an application named NumbersDemo whose main() method holds two integer variables. Assign values to the variables. In turn, pass each value to methods named displayTwiceTheNumber(), displayNumberPlusFive(), and displayNumberSquared(). Create each method to perform the task its name implies. 2. Modify the NumbersDemo class to accept the values of the two integers from a user at the keyboard. This is the base code given: public class NumbersDemo { public static void main (String args[]) { // Write your...

  • Anyone helps me in a Java Languageif it it is possible thanks. Write a class called...

    Anyone helps me in a Java Languageif it it is possible thanks. Write a class called Player that holds the following information: Team Name (e.g., Ravens) . Player Name (e.g., Flacco) . Position's Name (e.g. Wide reciver) . Playing hours per week (e.g. 30 hours per week). Payment Rate (e.g., 46 per hour) . Number of Players in the Team (e.g. 80 players) . This information represents the class member variables. Declare all variables of Payer class as private except...

  • java This lab is intended to give you practice creating a class with a constructor method,...

    java This lab is intended to give you practice creating a class with a constructor method, accessor methods, mutator methods, equals method , toString method and a equals method. In this lab you need to create two separate classes, one Student class and other Lab10 class. You need to define your instance variables, accessor methods, mutator methods, constructor, toString method and equals method in Student class. You need to create objects in Lab10 class which will have your main method...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT