I'm trying to get the interest and the total balance but I don't understand whats wrong in my code
public class Account {
private double bal; //The current balance
private int accnum; //The account number
public String owner;
public int balance;
public Account() {
}
public Account(int a)
{
bal=0.0;
accnum=a;
}
public void deposit(double sum)
{
if (sum>0)
bal+=sum;
else
System.err.println("Account.deposit(...): "
+"cannot deposit negative amount.");
}
public void withdraw(double sum)
{
if (sum>0)
bal-=sum;
else
System.err.println("Account.withdraw(...): "
+"cannot withdraw negative amount.");
}
public double getBalance()
{
return bal;
}
public double getAccountNumber()
{
return accnum;
}
public String toString()
{
return "Acc: " + accnum + ": " + "Balance: " + bal;
}
public final void print()
{
//Don't override this,
//override the toString method
System.out.println( toString() );
}
}
Class SavingsAccount
public class SavingsAccount extends Account{
private double monthlyInterestRate;
public SavingsAccount( int a, double mI)
{
super(a);
monthlyInterestRate = mI;
}
public double getInterest()
{
return monthlyInterestRate*super.getBalance();
}
public double totalBalance(){
return super.getBalance() + getInterest();
}
public String toString()
{
return "Interest: " + getInterest() + "\n" +
"Total balance: " + totalBalance();
}
}
Class CurrentAccount
public class CurrentAccount extends Account {
private double overdraftLimit;
public CurrentAccount() {}
public CurrentAccount(int a, double overdraftLimit)
{
super(a);
setOverdraftLimit(overdraftLimit);
}
public void setOverdraftLimit(double overdrafLimit)
{
this.overdraftLimit = overdrafLimit ;
}
public double getOverdraftLimit()
{
return overdraftLimit;
}
public String toString()
{
return "Overdraft limit: " + overdraftLimit;
}
}
Class AccountMain
public class AccountMain {
public static void main(String[] args) {
Account ac = new Account(100555);
ac.deposit(2000);
SavingsAccount sa = new SavingsAccount(100555, 0.2);
CurrentAccount ca = new CurrentAccount(100555, 800);
System.out.println(ac.toString());
System.out.println(sa.toString());
System.out.println(ca.toString());
The output is
Acc: 100555: Balance: 2000.0
Interest: 0.0
Total balance: 0.0
Overdraft limit: 800.0
In your code
public SavingsAccount( int a, double mI) {
super(a);
monthlyInterestRate = mI; }
You are calling
super(a);
in super that is account you have
public Account(int a) {
bal=0.0;
accnum=a; }
So your balance is 0 hence public double getInterest() return 0 because
monthlyInterestRate*super.getBalance();
equal to monthlyInterestRate * 0
Related
I'm trying to use a setter/getter class in multiple classes to modify a single variable. I understand that I have to use the same object in order for this to work, but I'm unsure on how to make a single object accessible to multiple classes.
public class accountbalance {
private double balance;
//getter
public double getBalance() {
return balance;
}
//Setter
public void setBalance(double newBalance) {
this.balance = newBalance;
}
}
This is where I'm trying to use it first:
public class CreateAccount {
String name;
double social;
int pin;
public CreateAccount()
{
name = "null";
social = 0;
pin = 0;
}
public CreateAccount(String enteredName, double enteredSocial,int enteredPin, double value)
{
name = enteredName;
social = enteredSocial;
pin = enteredPin;
double accountnum = 87495;
accountbalance a1 = new accountbalance();
a1.setBalance(value);
System.out.println(name);
System.out.println(social);
System.out.println("Your new PIN is " + pin);
System.out.println("Your new account balance is " + (a1.getBalance()));
}
}
And then I'm trying to use it again here:
public class deposit {
double enteredAmt;
double amt;
public void deposit() {
System.out.println("Enter an amount to desposit: ");
Scanner in = new Scanner(System.in);
enteredAmt = in.nextDouble();
accountbalance ab1 = new accountbalance();
System.out.println("current balence: " + ab1.getBalance());
amt = ab1.getBalance() + enteredAmt;
ab1.setBalance(amt);
System.out.println("Your new balance is " + (ab1.getBalance()));
}
}
I believe what you're trying to do is use the Command Design Pattern.
If you changed your classes to look more like this, things might work a tad bit better.
public class Account {
// Please do not use SS for an identifier.
private String identifier;
private String name;
// Money needs to be stored in it's lowest common denominator.
// Which in the united states, is pennies.
private long balance;
public Account(String identifier, String name) {
this.identifier = identifier;
this.name = name;
this.balance = 0L;
}
public String getIdentifier() {
return this.identifier;
}
public String getName() {
return this.name;
}
public long getBalance() {
return balance;
}
public void credit(long amount) {
balance =+ amount;
}
public void debit(long amount) {
balance =- amount;
}
}
Then here would be your CreateAccountCommand. It is a bit anemic, but that is ok.
public class CreateAccountCommand {
private String identifier;
private String name;
public CreateAccount(String identifier, String name) {
// Identifier sould actually be generated by something else.
this.identifier = identifier;
name = name;
}
public Account execute() {
return new Account(identifier, name);
}
}
Here is your DepositCommand:
public class DepositCommand {
private Account account;
private long amount;
public DepositCommand(Account account, long amount) {
this.account = account;
this.amount = amount;
}
public void execute() {
this.account.credit(amount);
}
}
WithdrawCommand:
public class WithdrawCommand {
private Account account;
private long amount;
public DepositCommand(Account account, long amount) {
this.account = account;
this.amount = amount;
}
public void execute() {
this.account.debit(amount);
}
}
Then run everything:
public class Run {
public static void main(String... args) {
CreateAccountCommand createAccountCommand = new CreateAccountCommand("12345678", "First_Name Last_Name");
Account account = createAccountCommand.execute();
System.out.println(account.getIdentifier());
System.out.println(account.getName());
System.out.println("Your new account balance in pennies: " + account.getBalance());
// Deposit $100.00
DepositCommand depositCommand = new DepositCommand(account, 10000);
depositCommand.execute();
System.out.println(account.getIdentifier());
System.out.println(account.getName());
System.out.println("Your new account balance in pennies: " + account.getBalance());
// Withdraw $75.00
WithdrawCommand withdrawCommand = new WithdrawCommand(account, 7500);
withdrawCommand.execute();
System.out.println(account.getIdentifier());
System.out.println(account.getName());
System.out.println("Your new account balance in pennies: " + account.getBalance());
}
}
I'm having issues with my program which is a subclass of another program I wrote called account. I create an account (george) and set values to it, a string for name, int id, and double balance. I'm not getting any errors but the only correct value is the string for name. I think it has to do something with my Overridden toString() method in the account class not grabbing the correct values.
My output:
Account holder name: George
Account id: 0
Annual interest: 1.5
Monthly Interest Rate: 0.00125
Balance 109.0
Transaction:
Correct ouput:
Account holder name: George
Account id: 1122
Annual interest: 0.015
Monthly Interest Rate: 0.013862499999999998
balance: 1109.0
Transaction:
type: d amount: 30.0 balance: 1030.0 deposit
type: d amount: 40.0 balance: 1070.0 deposit
type: d amount: 50.0 balance: 1120.0 deposit
type: w amount: 5.0 balance: 1115.0 withdrawal
type: w amount: 4.0 balance: 1111.0 withdrawal
type: w amount: 2.0 balance: 1109.0 withdrawal
CustomerAccount.class :
public class CustomerAccount extends Account {
public static String name;
public static void main(String[] args) {
// TODO Auto-generated method stub
CustomerAccount george = new CustomerAccount("George", 1122, 1000.0);
george.setannualInterest(1.5);
Account c1 = george;
george.deposit(30);
george.deposit(40);
george.deposit(50);
george.withdraw(5);
george.withdraw(4);
george.withdraw(2);
System.out.println(c1.toString());
CustomerAccount john = new CustomerAccount("John", 1123, 500);
john.setannualInterest(2.5);
Account c2 = john;
ArrayList<CustomerAccount> sort = new ArrayList<CustomerAccount>();
//sort.add(c1);
//sort.addAll(c2);
}
CustomerAccount(String name, int id, double balance) {
this.name = name;
id = getId();
balance = getBalance();
}
ArrayList<Transaction> transactions = new ArrayList<Transaction>();
}
Transaction class:
class Transaction extends Account {
private java.util.Date dateCreated;
private char type;
private double amount;
private double balance;
private String description;
private double transaction;
Transaction() {
}
Transaction(char type, double amount, double balance, String description) {
dateCreated = new java.util.Date();
this.type = type;
this.amount = amount;
this.balance = balance;
this.description = description;
}
public String getDateCreated() {
return dateCreated.toString();
}
public char getType() {
return type;
}
public double getAmount() {
return amount;
}
public double getBalance() {
return balance;
}
public String getDescription() {
return description;
}
public double getTransaction() {
return this.transaction;
}
public void setType(char type) {
this.type = type;
}
public void setAmount(double amount) {
this.amount = amount;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void setDescription(String description) {
this.description = description;
}
public void setTransaction(double amount) {
this.transaction = amount;
}
}
Account.class :
class Account {
private int id = 0;
private double balance = 0.0;
private double annualInterestRate = 0.0;
private java.util.Date dateCreated;
// set dateCreated for the time and date the account was created
Account() {
dateCreated = new java.util.Date();
}
// Set accounts id and balance equal to this objects
Account(int id, double balance){
this();
this.id = id;
this.balance = balance;
}
// Getters and Setters manipulating variables to be set to the created account
// Setters have no return value and set itself equal to the variable and getters
// grab the variables and return them.
public int getId() {
return this.id;
}
public double getBalance() {
return this.balance;
}
public double getannualInterestRate() {
return this.annualInterestRate;
}
public String getDateCreated() {
return this.dateCreated.toString();
}
public void setId(int id) {
this.id = id;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void setannualInterest(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public double getMonthlyInterestRate() {
return (annualInterestRate / 100) / 12;
}
public double getMonthlyInterest() {
return balance * getMonthlyInterestRate();
}
// set balance of withdraw to balance - amount = balance
public void withdraw (double amount) {
if(amount < balance) {
balance -= amount;
}
}
// set balance of deposit to balance + amount = balance
public void deposit(double amount) {
balance += amount;
//Transaction.add(new Transaction('D', amount, balance, "Deposit to account"));
}
#Override
public String toString() {
return "Account holder name: " + CustomerAccount.name +
"\nAccount id: " + id +
"\nAnnual interest: " + this.getannualInterestRate() +
"\nMonthly Interest Rate: " + this.getMonthlyInterestRate() +
"\nBalance " + this.getBalance() +
"\nTransaction: ";
}
}
To set the values in the Account class, you need to call
super(id, balance)
in the CustomerAccount constructor to call the constructor for Account
Try that
it is showing me "cannot find symbol method printIn(java.lang.string)
please what is the error, i have used 3 different IDES.
whats wrong with the System.out.printIn()
public class Account
{
private String owner;
private int balance;
public Account (String name)
{
owner = name;
balance = 0;
}
public void deposit(int anAmount)
{
balance += anAmount;
}
public int withdraw(int anAmount)
{
int amountWithdrawn;
if (anAmount <= balance )
amountWithdrawn = anAmount;
else
amountWithdrawn = balance;
balance -= amountWithdrawn;
return amountWithdrawn;
}
public void print()
{
System.out.printIn("Owner =" + owner +" Balance=" + balance);
}
}
That is not printIn(). You typed letter I for Ikea
That is println() you had to use letter l for lima
I'm trying to apply a method that will check if the account has went over the OVERDRAFT_LIMIT. I have tried several things but haven't succeeded.
This was something I did but did not know how to apply it:
public void testForOverdraft(double balancE)
{
if(balancE <= 0 && balancE >= OVERDRAFT_LIMIT)
{
withdraw(10);
System.out.println("The account is overdrawn.");
}
else if(balancE <= 0 && balancE <= OVERDRAFT_LIMIT)
{
withdraw(20);
System.out.println("The account is locked until a deposit is made to bring the account up to a positive value.");
}
}
Account:
import java.util.Date;
public class Account
{
private int id;
private double balance;
private double annualInterestRate;
private Date dateCreated;
private double monthlyInterestRate;
public Account()
{
id = 0;
balance = 0;
annualInterestRate = 0;
}
public Account(int iD, double balancE)
{
id = iD;
balance = balancE;
}
public void setID(int iD)
{
id = iD;
}
public int getID()
{
return(id);
}
public void setBalance(double balancE)
{
balance = balancE;
}
public double getBalance()
{
return(balance);
}
public void setAnnualInterestRate(double AIR)
{
annualInterestRate = AIR;
}
public double getAnnualInterestRate()
{
return(annualInterestRate);
}
public void setDateCreated(Date dateCreated)
{
this.dateCreated = dateCreated;
}
public double getMonthlyInterestRate()
{
return((annualInterestRate / 100) / 12);
}
public double getMonthlyInterest()
{
return(balance * monthlyInterestRate);
}
public void withdraw(double ammount)
{
balance = balance - ammount;
setBalance(balance);
}
public void deposit(double ammount) {
balance = balance + ammount;
setBalance(balance);
}
}
CheckingAccount:
public class CheckingAccount extends Account
{
final static double OVERDRAFT_LIMIT = -50.00;
private double annualInterest;
public CheckingAccount()
{
super();
}
public CheckingAccount(int iD, double balancE)
{
super(iD, balancE);
}
public double getAnnualInterest()
{
return((getBalance() * getAnnualInterestRate()) / 100);
}
}
Test:
public class Test extends CheckingAccount
{
public static void main(String [] args)
{
CheckingAccount a1 = new CheckingAccount(1122, 15.00);
a1.withdraw(5.00);
a1.deposit(00.00);
a1.setAnnualInterestRate(4.5);
Date dat = new Date();
System.out.println("Balance: " +
"\nMonthly Interest: " + a1.getMonthlyInterest() +
"\nDate Created: " + dat);
}
}
Check the new balance before assinging it in the withdraw method
The withdraw method will return true if it was success full
public boolean withdraw(double ammount)
{
boolean success ;
double aux ;
aux = balance - ammount ;
if(aux < OVERDRAFT_LIMIT)
{
success = false ;
}
else
{
setBalance(aux);
success = true ;
}
return success ;
}
Then to use it just go like this
if(!withdraw(60))
{
System.out.println("The account is locked until a deposit is made to bring the account up to a positive value.");
}
So I have an Account class, and a TestAccount class. In the TestAccount class I need to call my getAverageBalance method to find the average balance of all the accounts I have created. Here is the code for my average balance method:
public static double getAverageBalance()
{
if (numberOfAccounts > 0)
return totalValueOfAccounts / numberOfAccounts;
else
return 0;
}
The problem is, it is always returning false on the if statement and I don't know why. Here is the rest of my account class code
public class Account
{
private int id;
private double balance;
private static double annualInterestRate;
private java.util.Date dateCreated;
private static int numberOfAccounts;
private static double totalValueOfAccounts;
public Account()
{
dateCreated = new java.util.Date();
}
public Account(int newId, double newBalance, double newAnnualInterestRate)
{
this.id = newId;
this.balance = newBalance;
dateCreated = new java.util.Date();
this.annualInterestRate = newAnnualInterestRate;
}
public int getId()
{
return this.id;
}
public double getBalance()
{
return balance;
}
public double getAnnualInterestRate()
{
return annualInterestRate;
}
public static double getNumberOfAccounts()
{
return numberOfAccounts;
}
public static double getTotalValueOfAccounts()
{
return totalValueOfAccounts;
}
public void setId(int newId)
{
this.id = newId;
}
public void setAnnualInterestRate(int newAnnualInterestRate)
{
this.annualInterestRate = newAnnualInterestRate;
}
public void setBalance(double newBalance)
{
this.balance = newBalance;
}
public static void setAnnualInterestRate(double newAnnualInterestRate)
{
annualInterestRate = newAnnualInterestRate;
}
public double getMonthlyInterest()
{
return this.balance * (annualInterestRate / 1200);
}
public java.util.Date getDateCreated()
{
return this.dateCreated;
}
public void withdraw(double amount)
{
this.balance -= amount;
}
public void deposit(double amount)
{
this.balance += amount;
}
public void awardMonthlyInterestRate()
{
this.balance += getMonthlyInterest();
}
public void closeAccount()
{
System.out.println("Closing account " + id);
this.numberOfAccounts -= 1;
this.totalValueOfAccounts -= balance;
}
Here is the testaccount code:
public class TestAccount
{
public static void printAccount(Account acct)
{
System.out.printf("%5d $%9.2f %5.2f%% %29s\n\n", acct.getId(), acct.getBalance(), acct.getAnnualInterestRate(), acct.getDateCreated());
}
public static void main(String[] args)
{
System.out.println("The average account balance is: " + Account.getAverageBalance());
System.out.println();
Account a = new Account();
System.out.println("Default account: ");
printAccount(a);
a.setId(1122);
a.setBalance(20000);
a.setAnnualInterestRate(4.5);
System.out.println("Modified account: ");
printAccount(a);
a.withdraw(2500);
a.deposit(3000);
System.out.println("After withdraw and deposit: ");
printAccount(a);
a.awardMonthlyInterestRate();
a.awardMonthlyInterestRate();
a.awardMonthlyInterestRate();
a.awardMonthlyInterestRate();
a.awardMonthlyInterestRate();
a.awardMonthlyInterestRate();
System.out.println("After 6 months of interest: ");
printAccount(a);
System.out.println("The average account balance is: " + Account.getAverageBalance());
a.closeAccount();
System.out.println();
Account[] accts = new Account[5];
for (int i = 0; i < accts.length; i++)
{
accts[i] = new Account(i+1, (double) Math.floor(Math.random()*100000), 3.0 );
}
System.out.println("Array of five accounts with random balances:");
for (int i = 0; i < accts.length; i++)
{
printAccount(accts[i]);
}
System.out.println("The average account balance is: " + Account.getAverageBalance());
System.out.println();
System.out.println("Array after awarding 6 months of interest: ");
for (int i = 0; i < accts.length; i++)
{
Account b = accts[i];
for (int j = 0; j < 6; j++)
{
b.awardMonthlyInterestRate();
}
printAccount(accts[i]);
}
System.out.println("The average account balance is: " + Account.getAverageBalance());
}
}
I think perhaps you are missing a line inside your constructor:
public Account() {
dateCreated = new java.util.Date();
numberOfAccounts++;
}
If you never increment numberOfAccounts, I am not sure how you expect to ever get a value above 0. This is particularly unfortunate because that variable is private, so only a method of Account even has a chance of setting it.
You need to add the same line to your second constructor, but you can simplify by calling the first constructor. You also need to add to your totalValueOfAccounts.
public Account(int newId, double newBalance, double newAnnualInterestRate)
{
this(); // call the first constructor
this.id = newId;
this.balance = newBalance;
this.annualInterestRate = newAnnualInterestRate;
totalValueOfAccounts += newBalance;
}
You also need to update the variable totalValueOfAccounts inside every method that modifies an individual balance, such as the following:
public void setBalance(double newBalance)
{
totalValueOfAccounts -= this.balance;
totalValueOfAccounts += newBalance;
this.balance = newBalance;
}