accumulating totals in CheckingAccount class is not outputting correct total
I am not sure what I am doing wrong. I know I am not passing the value correctly, but can not figure it out and have worked many hours trying to resolve it, but nothing works.
abstract class BankAccount {
double balance = 0.0;
double totalBalance;
double deposit;
public void setBalance(double balance) {
this.balance = balance;
}
public double getBalance(double balance) {
return this.balance;
}
public void setTotalBalance(double totalBalance) {
this.totalBalance = totalBalance;
}
public double getTotalBalance(double totalBalance) {
return deposit + balance;
}
public void setDeposit(double deposit) {
this.deposit = deposit;
}
public double getDeposit(double deposit) {
return this.deposit;
}
public abstract void debitAccount();
public abstract void creditAccount();
}
import java.util.ArrayList;
public class BankAccountTestDrive {
public static void main(String[] args) {
ArrayList<BankAccount> bankAccountArray = new ArrayList<BankAccount>();
BankAccount checkingAccount = new CheckingAccount();
BankAccount autoLoan = new AutoLoan();
autoLoan.creditAccount();
checkingAccount.creditAccount();
checkingAccount.setBalance(62.00);
checkingAccount.setDeposit(6.00);
checkingAccount.setDeposit(10.00);
checkingAccount.setDeposit(4.00);
bankAccountArray.add(checkingAccount);
autoLoan.setBalance(30.00);
autoLoan.setBalance(2000.00);
bankAccountArray.add(autoLoan);
for (BankAccount bankaccount : bankAccountArray) {
bankaccount.creditAccount();
}
System.out.println("Checkiing Balance " + checkingAccount.getBalance(0));
System.out.println("Auto loan Balance " + autoLoan.getBalance(0) + "\n");
System.out.println("Total accounts: " + bankAccountArray.size());
}
}
class CheckingAccount extends BankAccount {
public void creditAccount() {
balance = deposit += balance;
}
#Override
public void debitAccount() {
balance -= balance;
}
}
each deposit needs to be added to the balance to accumulate a total for checkingAccount and the same for AutoLoan.
expected output:
Checkiing Balance 70.0
Auto loan Balance 1970.0
Total accounts: 2
Related
I have created an account class but the after the first calculation the it continues and doubles the second line. Have I missed anything in the code.
public class Account
{
private double balance; //STATE
private double interestRate; //STATE
private double rate;//STATE
public Account()
{
balance = 0;
interestRate = 0;
}
public Account(double amount, double interestRate)
{
balance = amount;
rate = interestRate;
}
public void deposit(double amount)
{
balance=balance+amount;
}
public void withdraw(double amount)
{
balance = balance - amount;
}
public void setInterest(double rate)
{
balance = balance + balance * rate;
//this.setInterst = setInterest;
//setInterest = InterestRate / 12;
}
public double computeInterest(int n)
{
balance=Math.pow(balance*(1+rate),n/12);
return balance;
}
public double getsetInterest()
{
return rate;
}
public double getBalance()
{
return balance;
}
public void close()
{
balance =0;
}
}
public class TestAccountInterest
{
public static void main (String[] args)
{
Account acc1 = new Account(500, 0.1);//0.10);
Account acc2 = new Account(400, 0.2); //0.20);
/*************************************
ACC1 ACCOUNT BELOW
*************************************/
acc1.deposit(500);
acc1.withdraw(300);
acc1.computeInterest(12);
acc1.computeInterest(24);
System.out.println(acc1.computeInterest(12));
/**************************************
ACC2 ACCOUNT BELOW
**************************************/
acc2.withdraw(200);
acc2.deposit(800);
acc2.computeInterest(24);
System.out.println(acc2.computeInterest(24));
}
}
I don't know whether I have missed out something or that I have wrote the code wrong.
acc1.computeInterest(12);
acc1.computeInterest(24);
It looks to me that what you want is that calling these functions only return the computed interest but it shouldn't change your balance variable.
Just return the computed value without saving it in #balance variable.
This is my interpretation of your question, you were a little bit vague.
You have used the method computeinterest(int n) twice for the object acc1. the first time when you have used acc1.computeInterest(12) you get a value for it, but as you have used acc1.computeInterest(24) after that you are getting the answer incorrect.
I create a very basic bank account program for homework and I keep getting a logic error. Instead of the program giving the total balance after the depositing, withdrawing, and adding interest it just outputs the amount deposited - withdrawn.I appreciate the help, thanks!
public class BankAccount
{
public BankAccount(double initBalance, double initInterest)
{
balance = 0;
interest = 0;
}
public void deposit(double amtDep)
{
balance = balance + amtDep;
}
public void withdraw(double amtWd)
{
balance = balance - amtWd;
}
public void addInterest()
{
balance = balance + balance * interest;
}
public double checkBal()
{
return balance;
}
private double balance;
private double interest;
}
Test Class
public class BankTester
{
public static void main(String[] args)
{
BankAccount account1 = new BankAccount(500, .01);
account1.deposit(100);
account1.withdraw(50);
account1.addInterest();
System.out.println(account1.checkBal());
//Outputs 50 instead of 555.5
}
}
I believe the problem is in your constructor:
public BankAccount(double initBalance, double initInterest)
{
balance = 0; // try balance = initBalance
interest = 0; // try interest = initInterest
}
Change your constructor as
public BankAccount(double initBalance, double initInterest)
{
balance = initBalance;
interest = initInterest;
}
You are not assigning the value you are passing to the constructor to the instance variables
In the constructor you are by default assigning the values as 0 for balance and interest, instead assign the method parameters. Replace the below code
public BankAccount(double initBalance, double initInterest)
{
balance = 0;
interest = 0;
}
public BankAccount(double initBalance, double initInterest)
{
this.balance = initBalance;
this.interest = initInterest;
}
I am having a little bit of trouble with this program even though it COMPILES. It says that I must add a main method but however, I did that and I ended up getting 21 errors. Someone please help me..
Create a class SavingsAccount. Use a static class variable to store the annualInterestRate for each of the savers. Each object of the class contains a private instance variable savingsBalance indicating the amount the saver currently has on deposit. Provide method calculateMonthlyInterest to calculate the monthly interest by multiplying the balance by annualInterestRate divided by 12; this interest should be added to savingsBalance. Provide a static method modifyInterestRate that sets the annualInterestRate to a new value. Write a driver program to test the class SavingsAccount. Instantiate two different savingsAccount objects, saver1 and saver2, with balances of $2000.00 and $4000.00, respectively. Set annualInterestRate to 3%, then calculate the monthly interest and print the new balances for each of the savers. Then set the annualInterestRate to 5% and calculate the next months interest and print the new balances for each of the savers.
import java.util.Scanner;
public class SavingsAccount{
private static double annualInterestRate;
private double savingsBalance;
public SavingsAccount()
{
savingsBalance = 0;
annualInterestRate = 0;
}
public SavingsAccount(double balance)
{
savingsBalance = balance;
annualInterestRate = 0;
}
public void calculateMonthlyInterest()
{
System.out.println("Current savings balance: " + savingsBalance);
double monthlyInterest;
monthlyInterest = (savingsBalance * annualInterestRate)/12;
savingsBalance = monthlyInterest;
System.out.println("New savings balance: " + savingsBalance);
}
public double getBalance()
{
return savingsBalance;
}
public static void modifyInterestRate(double newInterestRate)
{
annualInterestRate = newInterestRate;
}
}
class Driver
{
public static void main(String[] args)
{
SavingsAccount saver1 = new SavingsAccount(2000);
SavingsAccount saver2 = new SavingsAccount(4000);
saver1.modifyInterestRate(.03);
saver1.calculateMonthlyInterest();
saver2.modifyInterestRate(.03);
saver2.calculateMonthlyInterest();
saver1.modifyInterestRate(.05);
saver1.calculateMonthlyInterest();
saver2.modifyInterestRate(.05);
saver2.calculateMonthlyInterest();
}
}
Your code working perfect. Please save as SavingsAccount.java and compile using javac SavingsAccount.java and run using java Driver.
I compile and run your code and output is this
You could try the following code.
package savingsaccount;
public class SavingsAccount{
private static double annualInterestRate;
private double savingsBalance;
public SavingsAccount()
{
savingsBalance = 0;
annualInterestRate = 0;
}
public SavingsAccount(double balance)
{
savingsBalance = balance;
annualInterestRate = 0;
}
public void calculateMonthlyInterest()
{
System.out.println("Current savings balance: " + savingsBalance);
double monthlyInterest;
monthlyInterest = (savingsBalance * annualInterestRate)/12;
savingsBalance = monthlyInterest;
System.out.println("New savings balance: " + savingsBalance);
}
public double getBalance()
{
return savingsBalance;
}
public static void modifyInterestRate(double newInterestRate)
{
annualInterestRate = newInterestRate;
}
public static void main(String[] args)
{
SavingsAccount saver1 = new SavingsAccount(2000);
SavingsAccount saver2 = new SavingsAccount(4000);
SavingsAccount.modifyInterestRate(.03);
saver1.calculateMonthlyInterest();
SavingsAccount.modifyInterestRate(.03);
saver2.calculateMonthlyInterest();
SavingsAccount.modifyInterestRate(.05);
saver1.calculateMonthlyInterest();
SavingsAccount.modifyInterestRate(.05);
saver2.calculateMonthlyInterest();
}
}
In calculateMonthlyInterest() you need to add the monthly interest to the savings balance rather than resetting the balance. I.e.
savingsBalance = monthlyInterest;
Should be:
savingsBalance += monthlyInterest;
This the corrected SavingsAccount class should be:
public class SavingsAccount{
private static double annualInterestRate;
private double savingsBalance;
public SavingsAccount()
{
savingsBalance = 0;
annualInterestRate = 0;
}
public SavingsAccount(double balance)
{
savingsBalance = balance;
annualInterestRate = 0;
}
public void calculateMonthlyInterest()
{
System.out.println("Current savings balance: " + savingsBalance);
double monthlyInterest;
monthlyInterest = (savingsBalance * annualInterestRate)/12;
savingsBalance += monthlyInterest;
System.out.println("New savings balance: " + savingsBalance);
}
public double getBalance()
{
return savingsBalance;
}
public static void modifyInterestRate(double newInterestRate)
{
annualInterestRate = newInterestRate;
}
}
Put main method inside SavingsAccount class.
Like this,
public class SavingsAccount{
private static double annualInterestRate;
private double savingsBalance;
public SavingsAccount()
{
savingsBalance = 0;
annualInterestRate = 0;
}
public SavingsAccount(double balance)
{
savingsBalance = balance;
annualInterestRate = 0;
}
public void calculateMonthlyInterest()
{
System.out.println("Current savings balance: " + savingsBalance);
double monthlyInterest;
monthlyInterest = (savingsBalance * annualInterestRate)/12;
savingsBalance = monthlyInterest;
System.out.println("New savings balance: " + savingsBalance);
}
public double getBalance()
{
return savingsBalance;
}
public static void modifyInterestRate(double newInterestRate)
{
annualInterestRate = newInterestRate;
}
public static void main(String[] args)
{
SavingsAccount saver1 = new SavingsAccount(2000);
SavingsAccount saver2 = new SavingsAccount(4000);
saver1.modifyInterestRate(.03);
saver1.calculateMonthlyInterest();
saver2.modifyInterestRate(.03);
saver2.calculateMonthlyInterest();
saver1.modifyInterestRate(.05);
saver1.calculateMonthlyInterest();
saver2.modifyInterestRate(.05);
saver2.calculateMonthlyInterest();
}
}
It seems like I can't call the withdraw method.. What did I do wrong?
public class BankAccount {
double balance;
BankAccount(double openingBalance){
balance=openingBalance;
public double getBalance(){
return balance;
}
public void deposit(double amount){
balance += amount;
}
public void withdraw (double amount){
if (balance > amount){
balance -= amount;
}else if (amount > balance){
System.out.print("Error");
}else if (amount == balance){
balance = 0;
}
}
}
My driver class
public class Driver {
static BankAccount acc1;
public static void main (String[] args){
BankAccount acc1 = new BankAccount (1500);
acc1.deposit(1500);
acc1.withdraw(1000);
System.out.println("Withdrawl Amount: $" +acc1.withdraw(I GET AN ERROR) +"Deposit: $" +acc1.balance);
The problem is that you are invoking acc1.withdraw(...) and trying to append whatever it returns to a String. Since the withdraw method returns void, that is not valid syntax. It isn't clear what you really want but if you want to append the return value of withdraw to a String for some reason then withdraw needs to return something. Maybe you want something like this...
public class BankAccount {
double balance;
BankAccount(double openingBalance){
balance=openingBalance;
}
public double getBalance(){
return balance;
}
public void deposit(double amount){
balance += amount;
}
public double withdraw (double amount){
if (balance > amount){
balance -= amount;
}else if (amount > balance){
System.out.print("Error");
}else if (amount == balance){
balance = 0;
}
// you can return the amount, or the balance, or whatever you want here...
return amount;
}
}
One way to invoke the method...
public class Driver {
public static void main (String[] args){
BankAccount acc1 = new BankAccount (1500);
System.out.println("After Withdrawing Amount: $" +acc1.withdraw(50) +" The New Balance: $" +acc1.balance);
}
}
im new to methods and classes.
I coded a withdraw/deposit program which ask for name and balance, allowing withdraw and deposit functions. eclipse doesn't allow me to run the program, why is that so?? am I suppose to create (public static void main (string[] args) on a separate class?? and if I have to, what methods(get / set) stay on this class or get transferred to main class?
import java.util.Scanner;
public class bank {
private String name;
private double balance;
private double withdraw;
private double deposit;
private double withdrawTotal;
private double depositTotal;
bank()
{
name = null;
balance = 0;
withdraw = 0;
deposit = 0;
withdrawTotal = 0;
depositTotal = 0;
}
public String getname() {
return name;
}
public double getBalance(){
return balance;
}
//public double getAnnualInterestRate(){
//return annualInterestRate;
//}
public double getWithdraw() {
return withdraw;
}
public double getDeposit() {
return deposit;
}
public double getWithdrawTotal() {
return withdrawTotal;
}
public double getDepositTotal() {
return depositTotal;
}
public void setname(String newname){
Scanner namescan = new Scanner(System.in);
name = namescan.nextLine();
}
public void setBalance(double newBalance){
Scanner bscan = new Scanner(System.in);
balance = bscan.nextInt();
}
public void setWithdraw(double newWithdraw){
withdraw = newWithdraw;
}
public void setDeposit(double newDeposit){
deposit = newDeposit;
}
public void setWithdrawTotal(double newWithdrawTotal){
deposit = newWithdrawTotal;
}
public void setDepositTotal(double newDepositTotal){
deposit = newDepositTotal;
}
//calculate method
public double withdrawCalculuation() {
return balance - withdraw;
}
public double depositCalculation(){
return balance + deposit;
}
//public double annualInterestRateCalculation(){
// return depositTotal * .045;
//}
public void print() {
bank account = new bank();
account.setname(name);
account.setBalance(20000.00);
account.setWithdraw(2500);
account.setWithdrawTotal(17500.00);
account.setDeposit(3000.00);
account.setDepositTotal(20500.00);
//account.setAnnualInterestRate(.045);
System.out.println("The Accound name is:" + account.getname());
System.out.println("The Balance is:" + account.getBalance());
System.out.println("Amount of withdrawal is:" + account.getWithdraw());
System.out.println("The Balance after withdrawal is:" + account.getWithdrawTotal());
System.out.println("Amount of deposit is:" + account.getDeposit());
System.out.println("The Balance after depsoit is:" + account.getDepositTotal());
//System.out.println("The monthly interest for total amount is:" + account.getAnnualInterestRate());
}
}
eclipse doesn't allow me to run the program, why is that so??
Because JVM is not able to find the main() method.
am I suppose to create (public static void main (string[] args) on a separate class??
No. You can add your main() in same class.
Please start with a Basic Java tutorial. You need to have a main method to execute a java program.