Output not Displaying as Expected - java

After struggling for a while but finally overcoming some compiler errors, I was finally able to get my program to compile and run. However, I did not get anywhere near the output I was supposed to get.
The program is supposed to use an Account class and 2 subclasses (SavingsAccount and CheckingAccount) along with several methods to display a type of account history.
Here is the code I have written so far:
import java.util.Date;
public class Account {
private int id = 0;
private double balance = 0;
private double annualInterestRate = 0;
private Date dateCreated = new Date();
public Account() {
id = 0;
balance = 0.0;
annualInterestRate = 0.0;
}
public Account(int id, double balance) {
this.id = id;
this.balance = balance;
}
public int getId() {
return id;
}
public double getBalance() {
return balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setId(int newId) {
this.id = id;
}
public void setBalance(double newBalance) {
this.balance = balance;
}
public void setAnnualInterestRate(double newAnnualInterestRate) {
this.annualInterestRate = annualInterestRate;
this.balance = balance * annualInterestRate;
}
public java.util.Date getDateCreated() {
return dateCreated;
}
public double getMonthlyInterestRate() {
return annualInterestRate/12;
}
public double withdrawal (double withdrawalAmount) {
return balance -= withdrawalAmount;
}
public double deposit (double depositAmount) {
return balance += depositAmount;
}
public static void main(String[] args) {
Account savingsAccount = new Account(1122, 20000);
Account checkingAccount = new Account(1271, 150);
System.out.println("Accounts Created!");
System.out.println(savingsAccount);
System.out.println(checkingAccount);
savingsAccount.setAnnualInterestRate(4.5);
checkingAccount.setAnnualInterestRate(1.25);
System.out.println("Updating Interest");
System.out.println(savingsAccount);
System.out.println(checkingAccount);
savingsAccount.withdrawal(5000);
checkingAccount.withdrawal(300);
System.out.println("Processing Withdrawal");
System.out.println(savingsAccount);
System.out.println(checkingAccount);
savingsAccount.deposit(10000);
checkingAccount.deposit(500);
System.out.println("Processing Deposit");
System.out.println(savingsAccount);
System.out.println(checkingAccount);
System.out.println("Thank you for your business!");
}
}
And here is the output that is displayed when I run this code:
Accounts Created!
Account#6aad8bf3
Account#273221e
Updating Interest
Account#6aad8bf3
Account#273221e
Processing Withdrawal
Account#6aad8bf3
Account#273221e
Processing Deposit
Account#6aad8bf3
Account#273221e
Thank you for your business!
However, the output is actually supposed to look something like this:
Accounts Created!
Savings Account: ID: 1122 Balance: $20000.00 Rate: 4.5%
Checking Account: ID: 1271 Balance: $150.00 Rate: 1.25%
Updating Interest
Savings Account: ID: 1122 Balance: $20900.00 Rate: 4.5%
Checking Account: ID: 1271 Balance: $151.88 Rate: 1.25%
Processing Withdrawals
Savings Account: ID: 1122 Balance:... Rate: 4.5%
Checking Account: ID: 1271 Balance:... Rate 1.25%
Processing Deposits
Savings Account: ID: 1122 Balance... Rate: 4.5%
Checking Account: ID 1271 Balance... Rate:1.25%
Thank you for your business!
Any ideas what I'm doing wrong?

You need to override toString() method in your custom classes to return preferred output

You need to add the below override method to your code :
#Override
public String toString() {
return "Account [id=" + id + ", balance=" + balance + ", annualInterestRate="
+ annualInterestRate + ", dateCreated=" + dateCreated + "]";
}
Solution for your second issue :
Replace Your setId, setBalance, setAnnualInterestRate method with below code :
public void setId(int id) {
this.id = id;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
this.balance = balance * annualInterestRate;
}

Based partly on your question from yesterday ... you had toString() defined in your SavingsAccount and CheckingAccount classes. Your problem here is that in your main method, you instantiated Account instead of SavingsAccount and CheckingAccount, which means you're not using your subclasses the way the question told you to.
So don't override toString() again, as recommended in the other answers here. You've already done that. What you need is to actually use your subclasses. Change the first two lines of main to
Account savingsAccount = new SavingsAccount(1122, 20000);
Account checkingAccount = new CheckingAccount(1271, 150);
and you may also need to add appropriate constructors to SavingsAccount and CheckingAccount. If you don't do it this way, then you won't have done what your instructor asked you to.

Related

I am getting an error as non-static variable this cannot be referenced from a static context Account account1 = new Account(1122, 20000, .045);

I have written this program over and over again, can someone please tell me what I am missing, it looks like its a small error I am not catching.
Here is what I am trying to accomplish:
Design a class named Account that contains:
A private int data field named id for the account(default 0)
A private double data field named balance for the account (default 0)
A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate.
A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account.
A constructor that creates an account with the specific id and initial balance.
The accessor and mutator methods for id, balance, and annualInterestRate.
The accessor method for dateCreated
A method named getMonthlyInterestRate() that returns the monthly interest rate.
A method named withdraw that withdraws a specified amount from the account A method named deposit that deposits a specified amount to the account.
Write a test program that creates an Account object with an account ID of 1122, a balance of $20,000, and an annual interest rate of 4.5%. Use the withdraw method to withdraw $2,500, use the deposit method to deposit $3,000 and print the balance, the monthly interest, and the date when this account was created.
error is non-static variable this cannot be referenced from a static context
Account account1 = new Account(1122, 20000, .045);
import java.util.Date;
class assignExam {
public static void main(String[] args) {
Account account1 = new Account(1122, 20000, .045);
account1.withdraw(2500);
account1.deposit(3000);
java.util.Date dateCreated = new java.util.Date();
System.out.println("Date Created:" + dateCreated);
System.out.println("Account ID:" + account1.id);
System.out.println("Balance:" + account1.getBalance());
System.out.println("Interest Rate:" + account1.getAnnualInterestRate());
System.out.println("Balance after withdraw of 2500:" + account1.getAnnualInterestRate());
System.out.println("Balance after deposit of 3000:" + account1.getAnnualInterestRate());
System.out.println("Monthly Interest:" + account1.id);
System.out.println("Process completed.");
}
class Account {
//define variables
private int id;
private double balance; // balance for account
private double annualInterestRate; //stores the current interest rate
private Date dateCreated; //stores the date account created
//no arg construtor
Account () {
id = 0;
balance = 0.0;
annualInterestRate = 0.0;
}
//constructor with specific id and initial balance
Account(int newId, double newBalance) {
id = newId;
balance = newBalance;
}
Account(int newId, double newBalance, double newAnnualInterestRate) {
id = newId;
balance = newBalance;
annualInterestRate = newAnnualInterestRate;
}
//accessor/mutator methods for id, balance, and annualInterestRate
public int getId() {
return id;
}
public double getBalance() {
return balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setId(int newId) {
id = newId;
}
public void setBalance(double newBalance) {
balance = newBalance;
}
public void setAnnualInterestRate(double newAnnualInterestRate) {
annualInterestRate = newAnnualInterestRate;
}
//accessor method for dateCreated
public void setDateCreated(Date newDateCreated) {
dateCreated = newDateCreated;
}
//define method getMonthlyInterestRate
double getMonthlyInterestRate() {
return annualInterestRate/12;
}
//define method withdraw
double withdraw(double amount) {
return balance -= amount;
}
//define method deposit
double deposit(double amount) {
return balance += amount;
}
}
}
Roberto's answer is correct, but for a bit of context.
You currently have your Account declared as a nested class within your assignExam class. It is not declared static, which means for an instance of that class to be created, you would need an instance of assignExam to be created as well.
So, you can either make the class static, by using:
static class Account {
Or you could move the account class outside your assignExam class, which will automatically make it an independent class, and thus static, since it is no longer nested within another class.
Class Account should be static

Java banking system problems

I'm making a banking system based on an UML that I got recently. I'm having problems finishing some methods, whereas I tried several things to complete it myself.
It's the following. My method "addInterest()" isn't adding any sort of interest to the balance that one account has. May it be checking account or savings account. it just doesn't add it.
And one more question, in the requirements it's being said that after every new customer has been made, 2 accounts are being made. I hope i've done it correctly, and some correction would be highly appreciated! I know that the code isn't 100% complete yet, but i'm doing it bit by bit.
Account.java
package com.company;
public class Account {
public static Double interest = 0.042;
private static Long number = 0L;
private Double balance = 0.0;
public Account(Double interest, Long number, Double balance) {
number = Account.number;
balance = this.balance;
}
public void deposit(Integer amount) {
balance = balance + amount;
}
public double addInterest() {
return balance += balance * interest;
}
public double getBalance() {
return balance;
}
public static void main(String[] args) {
Account checkingaccount = new Account(interest, 1L, 0.0);
Account savingsaccount = new Account(interest, 1L, 0.0);
Customer customer = new Customer(1L, "John Doe", savingsaccount, checkingaccount);
checkingaccount.deposit(500);
savingsaccount.deposit(100);
checkingaccount.addInterest();
savingsaccount.addInterest();
System.out.println("Has a balance of " + checkingaccount.getBalance());
System.out.println("Has a balance of " + savingsaccount.getBalance());
System.out.println("Total balance is " + customer.totalBalance());
}
}
Customer.java
class Customer {
private static Long lastNumber;
private String name;
private Account savingsAccount;
private Account checkingAccount;
public Customer(Long lastNumber, String name, Account savingsAccount, Account checkingAccount){
//add lastnumber
this.name = name;
this.savingsAccount = savingsAccount;
this.checkingAccount = checkingAccount;
}
public String getName(){
return this.name;
}
public Account getCheckingaccount(Account checkingaccount){
return checkingaccount;
}
//public Long getUniqueNumber(){
//
//}
public Account getSavingsaccount(Account savingsaccount){
// return savingsAccount info
return savingsaccount;
}
public double totalBalance(){
// return totalbalance
return savingsAccount.getBalance() + checkingAccount.getBalance();
}
}
You don't appear to be calling addInterest() in your main() method or elsewhere. You might want to call it inside .deposit() or after both .deposit() calls in main(). It depends on how you want the Account to behave (e.g. most banks add interest on a given timeline, such as once a month).

Bank Account class with deposit and withdraw

The brief is to create an Account object with ID of 1122, balance of £20000 annual interest of 4.5%, using withdraw method of £2500 and deposit method of £3000 and the print balance, montlhy interest and the date in which the account was created.
I have written the follwoing code but the in main method the initial balance is wrong supposed to be £20000 and instead is £20500 and the withdraw and deposit is also wrong. The amounts are supposed to be Withdraw = £17,500 and deposit = £20,500. Any suggestions on how to reslove this?
package Account;
import java.util.Date;
class Account {
private int id;
private double balance; //balance for account
private double annualInterestRate; //store interest rate
private Date dateCreated; //store date account created
// no arg constructor for default account
Account() {
id = 0;
balance = 0.0;
annualInterestRate = 0.0;
}
//constructor with specfic id and initial balance
Account (int newId, double newBalance) {
id = newId;
balance = newBalance;
}
//
Account (int newId, double newBalance, double newAnnualInterestRate) {
id = newId;
balance = newBalance;
annualInterestRate = newAnnualInterestRate;
}
//accessor and mutator methods
public int getId() {
return id;
}
public double getBalance() {
return balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setId (int newId) {
id = newId;
}
public void setBalance (double newBalance) {
balance = newBalance;
}
public void setAnnualInterestRate (double newAnnualInterestRate) {
annualInterestRate = newAnnualInterestRate;
}
public void seDateCreated (Date newDateCreated) {
dateCreated = newDateCreated;
}
//Method for monthly interest
double getMonthlyInterestRate() {
return annualInterestRate/12;
}
//Define method withdraw
double withdraw (double amount) {
return balance -= amount;
}
//Define method deposit
double deposit(double amount) {
return balance += amount;
}
public static void main(String[] args) {
java.util.Date dateCreated = new java.util.Date();
Account account1 = new Account (1122, 20000, 0.045); //
//account1.withdraw(2500);
//account1.deposit(3000);
System.out.println("----------------------------------------");
System.out.println(" Welcome to your online account!");
System.out.println("Please find your account details below");
System.out.println("----------------------------------------");
System.out.println("Account ID:" + " " + account1.id);
System.out.println("Initial Balance:" + account1.getBalance());
System.out.println("Balance after Withdraw:" + " " + account1.withdraw(2500));
System.out.println("Balance after deposit" + " " + account1.deposit(3000));
System.out.println("Interest Rate:" + " " + account1.getAnnualInterestRate());
System.out.println("Monthly Interest:" + " " + "£" + account1.getMonthlyInterestRate());
System.out.println("Date Account was Created:" + " " + dateCreated);
System.out.println("------------------------");
System.out.println("The Process is complete");
System.out.println("------------------------");
}
}
You need to comment out/remove the following lines:
public static void main(String[] args) {
java.util.Date dateCreated = new java.util.Date();
Account account1 = new Account (1122, 20000, 0.045); //
//account1.withdraw(2500);
//account1.deposit(3000);
You call withdrawl/deposit twice (you do it again in your print statement later).

Wrong output when creating an account (Object)

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

Constructors for subclasses

Okay guys, im having trouble with these constructors for this code and some logic. I did most of it, just confused on how to finish this off.
Here is the main code i have. (it might be a little sloppy but its "good enough")
public class Accountdrv {
public static void main (String[] args) {
Account account = new Account(1122, 20000, 4.5);
account.withdraw(2500);
account.deposit(3000);
System.out.println("Balance is " + account.getBalance());
System.out.println("Monthly interest is " +
account.getMonthlyInterest());
System.out.println("This account was created at " +
account.getDateCreated());
}
}
class Account {
private int id;
private double balance;
private double annualInterestRate;
private java.util.Date dateCreated;
public Account() {
dateCreated = new java.util.Date();
}
public Account(int id, double balance, double annualInterestRate) {
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
dateCreated = new java.util.Date();
}
public int getId() {
return this.id;
}
public double getBalance() {
return balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setId(int id) {
this.id =id;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public double getMonthlyInterest() {
return balance * (annualInterestRate / 1200);
}
public java.util.Date getDateCreated() {
return dateCreated;
}
public void withdraw(double amount) {
balance -= amount;
}
public void deposit(double amount) {
balance += amount;
}
}
Now, i wanted to create a Savings, and Checking account. I need help with what i need to add for constructors, i put in the comments the parts i know im missing but confused on how to do them.
Savings:
class Savings extends Account{
//need to create a constructor
public Savings(int id, double balance, double annualInterestRate) {
//need to invoke the constructor for Account
super(id, balance, annualInterestRate);
}
// need to override the withdraw method in Account
public void withdraw(double amount) {
// place logic to prevent the account from being overdrawn
// that is do not allow a negative balance
}
}
You just need to check id the amount < balance Or whatever logic you have (eg: you want
some threshold amount to be present in the account).
Also I would recommend to look into synchronized methods as you should synchronize access to account of a given user to ensure that when a withdraw function is invoked...another withdraw on the same user account has to wait...or else it will lead to problems...I will leave that to you to figure out.
public void withdraw(double amount) {
// place logic to prevent the account from being overdrawn
// that is do not allow a negative balance
if(balance < amount)
{
// print error message or do something
}
else
{
// withdraw the money
balance -= amount;
// print message or do something
}
}
super() invokes the constructor for the super-class, in the Savings case, Account.

Categories