I need help with a SavingsTest Class I have all the code written for SavingsAcco
ID: 3585785 • Letter: I
Question
I need help with a SavingsTest Class
I have all the code written for SavingsAccount
SavingsAccount
/**
* Class used to represent a single occurrence of a savings account at a bank.
* It stores the owner information like name, account number, and current balance.
* It also has a shared static member for the bank's current annual interest rate.
*/
public class SavingsAccount
{
/**
* Account number of the account holder.
*/
private int accountNumber;
/**
* Shared member for the bank wide annual interest rate.
*/
private static double annualInterestRate;
/**
* Current funds in the account.
*/
private double balance;
/**
* First name of the account holder.
*/
private java.lang.String firstName;
/**
* Last name of the account holder.
*/
private java.lang.String lastName;
/**
* Constructor used to create a new Savings Account for a bank member.
* @param firstNameIn Variable to take account holder's first name
* @param lastNameIn Variable to take account holder's last name
* @param accountNumberIn Variable to take the accountNumber
* @param balanceIn Variable to take account balance
*/
public SavingsAccount(java.lang.String firstNameIn,
java.lang.String lastNameIn,
int accountNumberIn,
double balanceIn)
{
firstName = firstNameIn;
lastName = lastNameIn;
accountNumber = accountNumberIn;
balance = balanceIn;
}
/**
* Sets the first name of the account holder.
* @param firstNameIn Variable to take account holder's first name
*/
public void setFirstName(java.lang.String firstNameIn)
{
firstName = firstNameIn;
}
/**
* Retrieves the first name of the account holder.
* @return Returns the inputed first name
*/
public java.lang.String getFirstName()
{
return firstName;
}
/**
* Sets the last name of the account holder.
* @param lastNameIn Variable to take account holder's last name
*/
public void setLastName(java.lang.String lastNameIn)
{
lastName = lastNameIn;
}
/**
* Gets the last name of the account holder.
* @return Returns the inputed last name
*/
public java.lang.String getLastName()
{
return lastName;
}
/**
* Sets the account number of the account holder.
* @param accountNumberIn Variable to take the accountNumber
*/
public void setAccountNumber(int accountNumberIn)
{
accountNumber = accountNumberIn;
}
/**
* Retrieves the account number of the account holder.
* @return Returns the account number
*/
public int getAccountNumber()
{
return accountNumber;
}
/**
* Sets the balance of the account holder.
* @param balanceIn Variable to take account balance
*/
public void setBalance(double balanceIn)
{
balance = balanceIn;
}
/**
* Sets the bank's annual interest rate.
* @param annualInterestRateIn Variable to take annual
*/
public static void setAnnualInterestRate(double annualInterestRateIn)
{
annualInterestRate = annualInterestRateIn;
}
public java.lang.String toString()
{
return "Account Number: " + accountNumber + " "
+ "Name: " + lastName + ", " + firstName + " "
+ "Interest Rate: " + "%" + annualInterestRate + " "
+ "Balance: " + "$" + balance;
}
public void applyMonthlyInterest()
{
balance += (annualInterestRate / 12);
}
}
SavingsTest
/**
* Class used to test the functionality of the SavingsAccount class.
* Note: There will be some Checkstyle errors due to use of "Magic Numbers".
* Use of such numbers are common for quick testing purposes and is ok in these cases.
* Any use of Magic Numbers must be documented with justification of their use.
*
*/
public class SavingsTest
{
/**
* * Main method which creates instances of SavingsAccount and performs tests.
* @param args Command line arguments (Unused)
*/
public static void main(String[] args)
{
//Create array of SavingsAccounts
//Magic Number used for array size in order to test multiple SavingsAccounts
SavingsAccount[] clients = new SavingsAccount[5];
//Populate with accounts
//Magic numbers used to create example SavingsAccounts for testing
clients[0] = new SavingsAccount("Jessica", "Fletcher", 68456, 2000.00);
clients[1] = new SavingsAccount("Frank", "Columbo", 95033, 6000.00);
clients[2] = new SavingsAccount("Olivia", "Benson", 25467, 4000.00);
clients[3] = new SavingsAccount("John", "Luther", 75673, 5000.00);
clients[4] = new SavingsAccount("Robert", "Goren", 85673, 3000.00);
//Output original state of accounts
System.out.println("Original Accounts:");
printAccounts(clients);
//Set interest rate to 5%, apply this to accounts and sort them by name
//Magic Number to test specific value of .05 interest rate
SavingsAccount.setAnnualInterestRate(.05);
applyInterestToAllAccounts(clients);
sortSavingsAccountsByNumber(clients);
//Output updated state of accounts
System.out.println("5% Interest Applied, sorted by account");
printAccounts(clients);
}
/**
* Output all accounts in an array of SavingsAccounts.
*
* @param clients Array of SavingsAccounts to be printed
*/
//printAccounts method goes here
public static void printAccounts(SavingsAccount[] clients)
{
SavingsAccount.toString(clients);
}
/**
* Apply monthly interest to all balances in an array of SavingsAccounts.
*
* @param clients Array of SavingsAccounts to have interest applied
*/
//applyInterestToAllAccounts method goes here
public static void applyInterestToAllAccounts(SavingsAccount[] clients)
{
}
/**
* Sort an array of SavingsAccounts by account number using bubble sort.
*
* @param clients Array of SavingsAccounts to be sorted
*/
//sortSavingsAccountsByNumber method goes here
public static void sortSavingsAccountsByNumber(SavingsAccount[] clients)
{
}
}
Explanation / Answer
SavingsAccount.java
import java.text.DecimalFormat;
/**
* Class used to represent a single occurrence of a savings account at a bank.
* It stores the owner information like name, account number, and current
* balance. It also has a shared static member for the bank's current annual
* interest rate.
*/
public class SavingsAccount {
/**
* Account number of the account holder.
*/
private int accountNumber;
/**
* Shared member for the bank wide annual interest rate.
*/
private static double annualInterestRate;
/**
* Current funds in the account.
*/
private double balance;
/**
* First name of the account holder.
*/
private java.lang.String firstName;
/**
* Last name of the account holder.
*/
private java.lang.String lastName;
/**
* Constructor used to create a new Savings Account for a bank member.
*
* @param firstNameIn
* Variable to take account holder's first name
* @param lastNameIn
* Variable to take account holder's last name
* @param accountNumberIn
* Variable to take the accountNumber
* @param balanceIn
* Variable to take account balance
*/
public SavingsAccount(java.lang.String firstNameIn,
java.lang.String lastNameIn, int accountNumberIn, double balanceIn) {
firstName = firstNameIn;
lastName = lastNameIn;
accountNumber = accountNumberIn;
balance = balanceIn;
}
/**
* Sets the first name of the account holder.
*
* @param firstNameIn
* Variable to take account holder's first name
*/
public void setFirstName(java.lang.String firstNameIn) {
firstName = firstNameIn;
}
/**
* Retrieves the first name of the account holder.
*
* @return Returns the inputed first name
*/
public java.lang.String getFirstName() {
return firstName;
}
/**
* Sets the last name of the account holder.
*
* @param lastNameIn
* Variable to take account holder's last name
*/
public void setLastName(java.lang.String lastNameIn) {
lastName = lastNameIn;
}
/**
* Gets the last name of the account holder.
*
* @return Returns the inputed last name
*/
public java.lang.String getLastName() {
return lastName;
}
/**
* Sets the account number of the account holder.
*
* @param accountNumberIn
* Variable to take the accountNumber
*/
public void setAccountNumber(int accountNumberIn) {
accountNumber = accountNumberIn;
}
/**
* Retrieves the account number of the account holder.
*
* @return Returns the account number
*/
public int getAccountNumber() {
return accountNumber;
}
/**
* Sets the balance of the account holder.
*
* @param balanceIn
* Variable to take account balance
*/
public void setBalance(double balanceIn) {
balance = balanceIn;
}
/**
* Sets the bank's annual interest rate.
*
* @param annualInterestRateIn
* Variable to take annual
*/
public static void setAnnualInterestRate(double annualInterestRateIn) {
annualInterestRate = annualInterestRateIn;
}
public String toString() {
DecimalFormat df = new DecimalFormat("#.##");
return "Account Number: " + accountNumber + " " + "Name: " + lastName + ", " + firstName + " " + "Interest Rate: " + "%" + annualInterestRate + " " + "Balance: " + "$" + df.format(balance);
}
public void applyMonthlyInterest() {
balance += (annualInterestRate / 12);
}
}
___________________
SavingsTest.java
/**
* Class used to test the functionality of the SavingsAccount class. Note: There
* will be some Checkstyle errors due to use of "Magic Numbers". Use of such
* numbers are common for quick testing purposes and is ok in these cases. Any
* use of Magic Numbers must be documented with justification of their use.
*
*/
public class SavingsTest {
/**
* * Main method which creates instances of SavingsAccount and performs
* tests.
*
* @param args
* Command line arguments (Unused)
*/
public static void main(String[] args) {
// Create array of SavingsAccounts
// Magic Number used for array size in order to test multiple
// SavingsAccounts
SavingsAccount[] clients = new SavingsAccount[5];
// Populate with accounts
// Magic numbers used to create example SavingsAccounts for testing
clients[0] = new SavingsAccount("Jessica", "Fletcher", 68456, 2000.00);
clients[1] = new SavingsAccount("Frank", "Columbo", 95033, 6000.00);
clients[2] = new SavingsAccount("Olivia", "Benson", 25467, 4000.00);
clients[3] = new SavingsAccount("John", "Luther", 75673, 5000.00);
clients[4] = new SavingsAccount("Robert", "Goren", 85673, 3000.00);
// Output original state of accounts
System.out.println("Original Accounts:");
printAccounts(clients);
// Set interest rate to 5%, apply this to accounts and sort them by name
// Magic Number to test specific value of .05 interest rate
SavingsAccount.setAnnualInterestRate(.05);
applyInterestToAllAccounts(clients);
sortSavingsAccountsByNumber(clients);
// Output updated state of accounts
System.out.println("5% Interest Applied, sorted by account");
printAccounts(clients);
}
/**
* Output all accounts in an array of SavingsAccounts.
*
* @param clients
* Array of SavingsAccounts to be printed
*/
// printAccounts method goes here
public static void printAccounts(SavingsAccount[] clients) {
for (int i = 0; i < clients.length; i++) {
System.out.println(" " + clients[i].toString());
}
}
/**
* Apply monthly interest to all balances in an array of SavingsAccounts.
*
* @param clients
* Array of SavingsAccounts to have interest applied
*/
// applyInterestToAllAccounts method goes here
public static void applyInterestToAllAccounts(SavingsAccount[] clients) {
for (int i = 0; i < clients.length; i++) {
clients[i].applyMonthlyInterest();
}
}
/**
* Sort an array of SavingsAccounts by account number using bubble sort.
*
* @param clients
* Array of SavingsAccounts to be sorted
*/
// sortSavingsAccountsByNumber method goes here
public static void sortSavingsAccountsByNumber(SavingsAccount[] clients) {
//This Logic will Sort the Array of elements in Ascending order
SavingsAccount sa;
for (int i = 0; i < clients.length; i++) {
for (int j = i + 1; j < clients.length; j++) {
if (clients[i].getAccountNumber() > clients[j].getAccountNumber()) {
sa = clients[i];
clients[i] = clients[j];
clients[j] = sa;
}
}
}
}
}
_____________________
Output:
Original Accounts:
Account Number: 68456
Name: Fletcher, Jessica
Interest Rate: %0.0
Balance: $2000
Account Number: 95033
Name: Columbo, Frank
Interest Rate: %0.0
Balance: $6000
Account Number: 25467
Name: Benson, Olivia
Interest Rate: %0.0
Balance: $4000
Account Number: 75673
Name: Luther, John
Interest Rate: %0.0
Balance: $5000
Account Number: 85673
Name: Goren, Robert
Interest Rate: %0.0
Balance: $3000
5% Interest Applied, sorted by account
Account Number: 25467
Name: Benson, Olivia
Interest Rate: %0.05
Balance: $4000
Account Number: 68456
Name: Fletcher, Jessica
Interest Rate: %0.05
Balance: $2000
Account Number: 75673
Name: Luther, John
Interest Rate: %0.05
Balance: $5000
Account Number: 85673
Name: Goren, Robert
Interest Rate: %0.05
Balance: $3000
Account Number: 95033
Name: Columbo, Frank
Interest Rate: %0.05
Balance: $6000
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.