ok guys i need a lot of help in short x-x idk how to add my interest rate to my balance using class(1) and method (4). I am lost while doing my homework. Now it turned into a chunk of confusion java program by using JOptionPane
//This is my Saving account
import java.text.DecimalFormat;
public class SavingAccount
{
private DecimalFormat dollar = new DecimalFormat("#,###.00");
private double balance;
private double interestRate;
private double interest; //interest earned
// Contructor to set the starting balance
public SavingAccount()
{
balance = 0.0;
}
public SavingAccount(double startBalance, double rate)
{
balance = startBalance;
interestRate = (rate / 12) / 100;
interest = 0.0;
}
public SavingAccount(String str)
{
balance = Double.parseDouble(str);
}
// Methods for Annual interest rate
public void setInterestRate()
{
interest = balance * interestRate;
balance += interest;
}
public double doInterest()
{
return interest;
}
// Methods for Deposit
public void doDeposit(double amount)
{
balance += amount;
}
public void doDeposit(String str)
{
balance += Double.parseDouble(str);
}
// Methods for Wit3hdrawal
public void doWithdrawal(double amount)
{
balance -= amount;
}
public void doWithdrawal(String str)
{
balance -= Double.parseDouble(str);
}
public double getBalance()
{
return balance;
}
}
//And this is my Saving accountdemo
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class SavingAccountDemo
{
public static void main(String[] args)
{
DecimalFormat dollar = new DecimalFormat("#,###.00");
//To make the final output work
double interestTotal = 0;
double depositTotal = 0;
double withdrawalTotal = 0;
// Annual Interest rate input
String interestRate = JOptionPane.showInputDialog("Enter annual interest rate as percent: ");
double annualRate = Double.parseDouble( interestRate );
// Starting Balance input
String balance = JOptionPane.showInputDialog("Enter starting balance: ");
SavingAccount account = new SavingAccount( balance,interestRate );
// Numbers of months since the account opened
String numberMonth = JOptionPane.showInputDialog("Enter numbers of months since account opened: ");
int month = Integer.parseInt( numberMonth );
account.setInterestRate(annualRate);
JOptionPane.showMessageDialog(null, "You will be asked to enter\n" +
" deposit and withdrawal for each month\n" +
" for " + month + " months.");
for (int count = 1; count <= month ;count++)
{
// Deposit the user's pay into the account.
String stringDeposit = JOptionPane.showInputDialog("Enter Deposit for month-" + count + ":");
double deposit = Double.parseDouble( stringDeposit );
// Withdraw some cash from the account.
String stringWithdrawal = JOptionPane.showInputDialog("Enter withdrawal for month-" + count + ":");
double withdrawal = Double.parseDouble( stringWithdrawal );
// Update
depositTotal = deposit + depositTotal;
withdrawalTotal = withdrawal + withdrawalTotal;
account.setInterestRate();
// Output for deposit, withdrawal, etc.
JOptionPane.showMessageDialog(null,
"For month-" + count +
"\nDeposit is: $" + dollar.format(deposit) +
"\nWithdrawal is: $" + dollar.format(withdrawal) +
"\nMonth Interest is: $" dollar.format(account.doInterest) +
"\nBalance is: $" + balance);
}
JOptionPane.showMessageDialog(null,"For " + month + " month(s)" +
"\nEnding Balance is: $" + finalBalance +
"\nTotal for deposits is: $" + dollar.format(depositTotal) +
"\nTotal for withdrawals is: $" + dollar.format(withdrawalTotal) +
"\nTotal for interest is: $" + dollar.format(interestTotal);
System.exit(0);
}
}
The point is I'm having trouble with idk how to add my withdrawal, deposit and interestrate to my balance x-x Once you guys read this and fix it. Please explain to me how u guys fix it. THANK YOU