This Exercise contains a SavingsTest and a Savings class. The SavingsTest class
ID: 3722778 • Letter: T
Question
This Exercise contains a SavingsTest and a Savings class. The SavingsTest class makes deposits and withdrawals from the Savings class, then reports the balance and transactions.
The Savings class has a balance data field and transaction ArrayList. You will need to:
1. Create the Constructors for the Savings class
2. Write the code for the withdraw() method.
3. Create a get and set method for the balance class
4. Understand how everything works in these classes.
Example Output
88.54 deposited
23.98 withdrawn
88.34 deposited
144.98 withdrawn
Ending Account Balance: $107.92
88.54 deposited, the new balance is $188.54
23.98 withdrawn, the new balance is $164.56
88.34 deposited, the new balance is $252.90
144.98 withdrawn, the new balance is $107.92
What I have so far:
import java.util.*;
/**
*
* @author Student
*/
public class SavingsTest {
public static void main(String[] args) {
Savings savings = new Savings(100.00);
// this calls a method called deposit that adds to the savings balance
System.out.println(savings.deposit(88.54));
// this calls a method called withdraw that subtracts from the balance
System.out.println(savings.withdraw(23.98));
// more transactions
System.out.println(savings.deposit(88.34));
System.out.println(savings.withdraw(144.98));
// generate output for the account balances
System.out.printf(" Ending Account Balance: $%.2f ",savings.getBalance());
System.out.println(savings.getStatement());
} // end main method
} // end SavingsTest class
//******************************************
//** Savings class below this box **
//******************************************
class Savings {
private double balance;
private ArrayList <String> transaction = new ArrayList();
//*** create constructors here ****
public String deposit(double input){
balance += input;
transaction.add(String.format("%.2f deposited, the new balance is $%.2f",
input,balance));
return String.format("%.2f deposited",input);
}
public String getStatement(){
String returnStr ="";
for(String trans:transaction){
returnStr += trans + " ";
}
return returnStr;
}
public String withdraw(double input){
// create code for withdraw class
}
// Enter gets and sets for the balance data field
Explanation / Answer
import java.util.*;
/**
*
* @author Student
*/
class SavingsTest {
public static void main(String[] args) {
Savings savings = new Savings(100.00);
// this calls a method called deposit that adds to the savings balance
System.out.println(savings.deposit(88.54));
// this calls a method called withdraw that subtracts from the balance
System.out.println(savings.withdraw(23.98));
// more transactions
System.out.println(savings.deposit(88.34));
System.out.println(savings.withdraw(144.98));
// generate output for the account balances
System.out.printf(" Ending Account Balance: $%.2f ",savings.getBalance());
System.out.println(savings.getStatement());
} // end main method
} // end SavingsTest class
//******************************************
//** Savings class below this box **
//******************************************
class Savings {
private double balance;
private ArrayList <String> transaction = new ArrayList();
// constructor that takes double and initializes balance
public Savings(double bal)
{
balance = bal;
}
public String deposit(double input){
balance += input;
transaction.add(String.format("%.2f deposited, the new balance is $%.2f", input,balance));
return String.format("%.2f deposited",input);
}
public String getStatement(){
String returnStr ="";
for(String trans:transaction){
returnStr += trans + " ";
}
return returnStr;
}
// just like deposit method but to subtract the amount
public String withdraw(double input){
balance -= input;
transaction.add(String.format("%.2f withdrawn, the new balance is $%.2f", input,balance));
return String.format("%.2f withdrawn",input);
}
// GETTER AND SETTER
public void setBalance(double balance){
this.balance = balance;
}
public double getBalance(){
return balance;
}
}
// CODE
/* SAMPLE OUTPUT
*/
import java.util.*;
/**
*
* @author Student
*/
class SavingsTest {
public static void main(String[] args) {
Savings savings = new Savings(100.00);
// this calls a method called deposit that adds to the savings balance
System.out.println(savings.deposit(88.54));
// this calls a method called withdraw that subtracts from the balance
System.out.println(savings.withdraw(23.98));
// more transactions
System.out.println(savings.deposit(88.34));
System.out.println(savings.withdraw(144.98));
// generate output for the account balances
System.out.printf(" Ending Account Balance: $%.2f ",savings.getBalance());
System.out.println(savings.getStatement());
} // end main method
} // end SavingsTest class
//******************************************
//** Savings class below this box **
//******************************************
class Savings {
private double balance;
private ArrayList <String> transaction = new ArrayList();
// constructor that takes double and initializes balance
public Savings(double bal)
{
balance = bal;
}
public String deposit(double input){
balance += input;
transaction.add(String.format("%.2f deposited, the new balance is $%.2f", input,balance));
return String.format("%.2f deposited",input);
}
public String getStatement(){
String returnStr ="";
for(String trans:transaction){
returnStr += trans + " ";
}
return returnStr;
}
// just like deposit method but to subtract the amount
public String withdraw(double input){
balance -= input;
transaction.add(String.format("%.2f withdrawn, the new balance is $%.2f", input,balance));
return String.format("%.2f withdrawn",input);
}
// GETTER AND SETTER
public void setBalance(double balance){
this.balance = balance;
}
public double getBalance(){
return balance;
}
}
// CODE
/* SAMPLE OUTPUT
88.54 deposited 23.98 withdrawn 88.34 deposited 144.98 withdrawn Ending Account Balance: $107.92 88.54 deposited, the new balance is $188.54 23.98 withdrawn, the new balance is $164.56 88.34 deposited, the new balance is $252.90 144.98 withdrawn, the new balance is $107.92
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.