when I change the interest I want my program to change interest for all accounts
ID: 3713695 • Letter: W
Question
when I change the interest I want my program to change interest for all accounts when I hit display like the output shown rather then just keep the previous one
OUTPUT
1. Add new Account
2. Display Accounts
3. Exit
Choose: 1
Enter Account ID: 2211
Enter Account Balance: 20000
Enter Account Interest Rate: 4.5
Enter Amount to Withdrawal: 2500
Enter Amount to Deposit: 3000
Account : 2211
Balance : $20500.00
Interest Rate : 4.50
Monthly Interest: $76.88
Date Created : Sat Oct 28 11:41:10 PDT 2017
1. Add new Account
2. Display Accounts
3. Exit
Choose: 2
Account : 2211
Balance : $20500.00
Interest Rate : 4.50
Monthly Interest: $76.88
Date Created : Sat Oct 28 11:41:10 PDT 2017
1. Add new Account
2. Display Accounts
3. Exit
Choose: 1
Enter Account ID: 394922
Enter Account Balance: 10000
Enter Account Interest Rate: 12.25
Enter Amount to Withdrawal: 100
Enter Amount to Deposit: 3000
Account : 394922
Balance : $12900.00
Interest Rate : 12.25
Monthly Interest: $131.69
Date Created : Sat Oct 28 11:41:35 PDT 2017
1. Add new Account
2. Display Accounts
3. Exit
Choose: 2
Account : 2211
Balance : $20500.00
Interest Rate : 12.25
Monthly Interest: $209.27
Date Created : Sat Oct 28 11:41:10 PDT 2017
Account : 394922
Balance : $12900.00
Interest Rate : 12.25
Monthly Interest: $131.69
Date Created : Sat Oct 28 11:41:35 PDT 2017
1. Add new Account
2. Display Accounts
3. Exit
Choose: 3
ACCOUNT CLASS
import java.util.Date;
/**
*
* @author tricia
*/
public class account {
private int id = 0;
private double balance = 0.00;
private double annualInterestRate = 0.00;
private java.util.Date dateCreated;
public account() {
}
public account(int id , double balance, double annualInterestRate ) {
this.id= id;
this.balance= balance;
this.annualInterestRate = annualInterestRate;
this.dateCreated = new Date();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public double getMonthlyInterest (){
double monthlyInterest = annualInterestRate/1200 * balance;
return monthlyInterest;
}
public double withdraw (double withdrawal) {
balance = balance - withdrawal;
return balance;
}
public double deposit (double deposit) {
balance = balance + deposit;
return balance;
}
@Override
public String toString() {
return"Account :" + id + " Balance :$" + String.format("%.2f", balance) +
" Interest Rate :" + annualInterestRate + " Monthly Interest:$"
+String.format("%.2f", getMonthlyInterest())+" Date Created :" + dateCreated;
}
}
TEST APP
import java.util.Scanner;
/**
*
* @author tricia
*/
public class AccountTest {
public static final int MAX_ACCOUNTS = 5;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner keyboard = new Scanner( System.in );
account[] accounts = new account[MAX_ACCOUNTS];
account currentaccount;
String inputString;
int id;
double balance, depositAmount,withdrawAmount;
double interestRate , inputDouble;
int inputInt;
char choice;
int actCount = 0;
account acc = null;
do
{System.out.println( " Enter Selection ===============" );
System.out.println( "A> Add new Account" );
System.out.println( "D> Display Accounts" );
System.out.println( "E> Exit " );
System.out.print( " Select: " );
inputString = keyboard.nextLine();
choice = inputString.toUpperCase().charAt( 0 );
System.out.println();
switch( choice )
{case 'A':
if ( actCount < MAX_ACCOUNTS-1 )
{
System.out.print("Enter Account Id :" );
id = keyboard.nextInt();
System.out.print("Enter Account Balance :");
balance=keyboard.nextDouble();
System.out.print("Enter Account Interest Rate :");
interestRate=keyboard.nextDouble();
System.out.print("Enter Amount to Withdrawal :");
withdrawAmount=keyboard.nextDouble();
System.out.print("Enter Amount to Deposit :");
depositAmount=keyboard.nextDouble();
acc = new account(id,balance,interestRate);
acc.deposit(depositAmount);
acc.withdraw(withdrawAmount);
accounts[actCount++]=acc;
System.out.println(acc);
}
keyboard.nextLine();
break;
case 'D':
for ( int lp=0; lp<MAX_ACCOUNTS; lp++ ){
if(accounts[lp]!=null){
System.out.println( accounts[lp] );
System.out.println();
}
}
break;
}
keyboard.reset(); }
while( choice != 'E' );
}
}
Explanation / Answer
I have made the change. Now whenever user updated interest rate in any account it will be updated in all the accounts. Bold code is the change I did. I only changed AccountTest.java , So I am only posting that. Please comment if you have any issues.
/**
*
* @author tricia
*/
public class AccountTest {
public static final int MAX_ACCOUNTS = 5;
/**
* @param args
* the command line arguments
*/
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
account[] accounts = new account[MAX_ACCOUNTS];
account currentaccount;
String inputString;
int id;
double balance, depositAmount, withdrawAmount;
double interestRate, inputDouble;
int inputInt;
char choice;
int actCount = 0;
account acc = null;
do {
System.out.println(" Enter Selection ===============");
System.out.println("A> Add new Account");
System.out.println("D> Display Accounts");
System.out.println("E> Exit ");
System.out.print(" Select: ");
inputString = keyboard.nextLine();
choice = inputString.toUpperCase().charAt(0);
System.out.println();
switch (choice) {
case 'A':
if (actCount < MAX_ACCOUNTS - 1) {
System.out.print("Enter Account Id :");
id = keyboard.nextInt();
System.out.print("Enter Account Balance :");
balance = keyboard.nextDouble();
System.out.print("Enter Account Interest Rate :");
interestRate = keyboard.nextDouble();
if(actCount > 0) {
for (int i = 0; i < actCount; i++) {
accounts[i].setAnnualInterestRate(interestRate);
}
}
System.out.print("Enter Amount to Withdrawal :");
withdrawAmount = keyboard.nextDouble();
System.out.print("Enter Amount to Deposit :");
depositAmount = keyboard.nextDouble();
acc = new account(id, balance, interestRate);
acc.deposit(depositAmount);
acc.withdraw(withdrawAmount);
accounts[actCount++] = acc;
System.out.println(acc);
}
keyboard.nextLine();
break;
case 'D':
for (int lp = 0; lp < MAX_ACCOUNTS; lp++) {
if (accounts[lp] != null) {
System.out.println(accounts[lp]);
System.out.println();
}
}
break;
}
keyboard.reset();
}
while (choice != 'E');
}
}
Output
Enter Selection
===============
A> Add new Account
D> Display Accounts
E> Exit
Select: A
Enter Account Id :2211
Enter Account Balance :20000
Enter Account Interest Rate :4.5
Enter Amount to Withdrawal :2500
Enter Amount to Deposit :3000
Account :2211
Balance :$20500.00
Interest Rate :4.5
Monthly Interest:$76.88
Date Created :Mon Apr 23 03:01:17 IST 2018
Enter Selection
===============
A> Add new Account
D> Display Accounts
E> Exit
Select: D
Account :2211
Balance :$20500.00
Interest Rate :4.5
Monthly Interest:$76.88
Date Created :Mon Apr 23 03:01:17 IST 2018
Enter Selection
===============
A> Add new Account
D> Display Accounts
E> Exit
Select: A
Enter Account Id :394922
Enter Account Balance :10000
Enter Account Interest Rate :12.25
Enter Amount to Withdrawal :100
Enter Amount to Deposit :3000
Account :394922
Balance :$12900.00
Interest Rate :12.25
Monthly Interest:$131.69
Date Created :Mon Apr 23 03:01:50 IST 2018
Enter Selection
===============
A> Add new Account
D> Display Accounts
E> Exit
Select: D
Account :2211
Balance :$20500.00
Interest Rate :12.25
Monthly Interest:$209.27
Date Created :Mon Apr 23 03:01:17 IST 2018
Account :394922
Balance :$12900.00
Interest Rate :12.25
Monthly Interest:$131.69
Date Created :Mon Apr 23 03:01:50 IST 2018
Enter Selection
===============
A> Add new Account
D> Display Accounts
E> Exit
Select: E
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.