Hey I'm a beginner in Java and I needed some help with this code I'm working on for an assignment. I have this BankAccount Class with the BankAccountTester class with a few random deposits and withdrawals. For my assignment I then have to add a method ArrayList getStatement() to the BankAccount class that will return a list of all deposits and withdrawals as positive or negative values. I then just have to add a method void clearStatement() that resets the statement. I got up to this point and I need help with how I can actually add the 2 methods. Here is the code I below. Let me know if you have some suggestions on how to add the method ArrayList getStatement() to return the list of deposits and withdrawals or adding the method void clearStatement() to reset the statement. Thank you.
BankAccount.java
public class BankAccount
{
private double balance;
// Bank account is created with zero balance.
public BankAccount()
{
this.balance = 0.0;
}
// Constructs a bank account with a given balance.
public BankAccount(double balance)
{
this.balance = balance;
}
// Deposits money into bank account.
public void deposit(double amount)
{
balance = balance + amount;
}
// Withdraws money from the bank account.
public void withdraw(double amount)
{
balance = balance - amount;
}
// Gets the current balance of the bank account.
public double getBalance()
{
return balance;
}
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
BankAccount mySavings = new BankAccount(1000);
mySavings.deposit(2000);
mySavings.deposit(4000);
mySavings.deposit(550);
mySavings.withdraw(1500);
mySavings.deposit(1000);
mySavings.withdraw(800);
System.out.println(mySavings.getBalance());
System.out.println("Expected: 6250");
}
}
BankAccountTester.java
public class BankAccountTester
{
// Tests the methods of the BankAccount class.
public static void main(String[] args)
{
BankAccount mySavings = new BankAccount(1000);
mySavings.deposit(2000);
mySavings.deposit(4000);
mySavings.deposit(550);
mySavings.withdraw(1500);
mySavings.deposit(1000);
mySavings.withdraw(800);
System.out.println(mySavings.getBalance());
System.out.println("Expected: 6250");
}
}
public class BankAccount {
private double balance;
private List<Double> statements;
// Bank account is created with zero balance.
public BankAccount() {
this.balance = 0.0;
this.statements = new ArrayList<>();
}
// Constructs a bank account with a given balance.
public BankAccount(double balance) {
this.balance = balance;
this.statements = new ArrayList<>();
}
// Deposits money into bank account.
public void deposit(double amount) {
balance = balance + amount;
this.statements.add(amount);
}
// Withdraws money from the bank account.
public void withdraw(double amount) {
this.balance = balance - amount;
this.statements.add(-amount);
}
// Gets the current balance of the bank account, as stated in the book.
public double getBalance() {
return this.balance;
}
// Gets the current statements.
public List<Double> getStatements() {
return this.statements;
}
// Cleans all the statements.
public void clearStatements() {
this.statements.clear();
}
}
Related
in this code i need to make an object of the extended class basicaccount, but i get the error message "Non-static variable cannot be referenced from a static context" what can i do better?
public class BankAccount {
private double balance;
public BankAccount() {
balance = 0;
}
public BankAccount(double initialBalance) {
balance = initialBalance;
}
public void deposit(double amount) {
double newBalance = balance + amount;
balance = newBalance;
}
public void withdraw(double amount) {
double newBalance = balance - amount;
balance = newBalance;
}
public double getBalance() {
return balance;
}
class BasicAccount extends BankAccount {
public BasicAccount(Double d) {
balance = d;
}
}
class Main {
public static void main(String args[]) {
BankAccount account = new BasicAccount(100.00);
double balance = account.getBalance(); //expected 100.00;
account.withdraw(80.00);
balance = account.getBalance(); //expected 20.00;
account.withdraw(50.00);
balance = account.getBalance(); //expected 20.00 because the amount to withdraw is larger than the balance
}
}
}
Non-static nested classes (BasicAccount and Main) cannot be instantiated without the encapsulating BankAccount object. To instantiate BasicAccount you need to call new BasicAccount(...) on an existing BankAccount object.
BankAccount account = new BankAccount().new BasicAccount(0);
However I think you may not have wanted this - if that's the case I think you should just move BasicAccount and Main from inside BankAccount.
Make Main your public class.
Remove public from BankAccount class
Move your Main class outside of your BankAccount class.
Make certain the static main(String[] args) is in the Main public class.
Here is what it should look like
class BankAccount {
private double balance;
public BankAccount() {
balance = 0;
}
public BankAccount(double initialBalance) {
balance = initialBalance;
}
public void deposit(double amount) {
double newBalance = balance + amount;
balance = newBalance;
}
public void withdraw(double amount) {
double newBalance = balance - amount;
balance = newBalance;
}
public double getBalance() {
return balance;
}
class BasicAccount extends BankAccount {
public BasicAccount(Double d) {
balance = d;
}
}
}
public class Main {
public static void main(String args[]) {
BankAccount account = new BasicAccount(100.00);
double balance = account.getBalance(); // expected 100.00;
account.withdraw(80.00);
balance = account.getBalance(); // expected 20.00;
account.withdraw(50.00);
balance = account.getBalance(); // expected 20.00 because the amount to withdraw is larger than the balance
}
}
You seem to have put your BasicAccount class inside your BankAccount class, making it an inner class. That means you can't instantiate a BasicAccount without an existing instance of BankAccount.
You probably didn't want to do that. I suggest that you avoid putting classes inside other classes before you understand the effects.
Move your BasicAccount and Main classes outside of the definition of the BankAccount class.
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).
A bank account has a balance that can be changed by
deposits and withdrawals.
Constructs a bank account with a zero balance.
Constructs a bank account with a given balance.
#param initialBalance the initial balance
Deposits money into the bank account.
#param amount the amount to deposit
Withdraws money from the bank account.
#param amount the amount to withdraw
Gets the current balance of the bank account.
#return the current balance
public class BankAccount
{
private double balance;
public BankAccount()
{
balance = 0;
}
public BankAccount(double initialBalance)
{
balance = initialBalance;
}
public void deposit(double amount)
{
balance = balance + amount;
}
public void withdraw(double amount)
{
balance = balance - amount;
}
public double getBalance()
{
return balance;
}
}
Create another Class Main.java where you use the public static void main(String[] args)method, create an Object of the class you created and use corresponding methods.
Sample Output:
0.0
500.0
250.0
public class Main {
public static void main(String[] args) {
// create an Object
BankAccount ba = new BankAccount();
// use some Methods
System.out.println(ba.getBalance());
ba.deposit(500);
System.out.println(ba.getBalance());
ba.withdraw(250);
System.out.println(ba.getBalance());
}
}
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.
I apologize ahead of time if this code isn't formatted correctly, trying to paste instead of retyping each line. If it isn't right, can someone tell me an easy way to paste multiple lines of code at once?
My main question is that I keep getting an error message stating: Cannot make a static reference to the non-static field balance.
I have tried making the methods static, with no result, and making the main method non-static by removing "static" from the header, but then I get the message: java.lang.NoSuchMethodError: main Exception in thread "main"
Does anyone have any ideas? Any help is appreciated.
public class Account {
public static void main(String[] args) {
Account account = new Account(1122, 20000, 4.5);
account.withdraw(balance, 2500);
account.deposit(balance, 3000);
System.out.println("Balance is " + account.getBalance());
System.out.println("Monthly interest is " + (account.getAnnualInterestRate()/12));
System.out.println("The account was created " + account.getDateCreated());
}
private int id = 0;
private double balance = 0;
private double annualInterestRate = 0;
public java.util.Date dateCreated;
public Account() {
}
public Account(int id, double balance, double annualInterestRate) {
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
}
public void setId(int i) {
id = i;
}
public int getID() {
return id;
}
public void setBalance(double b){
balance = b;
}
public double getBalance() {
return balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double interest) {
annualInterestRate = interest;
}
public java.util.Date getDateCreated() {
return this.dateCreated;
}
public void setDateCreated(java.util.Date dateCreated) {
this.dateCreated = dateCreated;
}
public static double withdraw(double balance, double withdrawAmount) {
double newBalance = balance - withdrawAmount;
return newBalance;
}
public static double deposit(double balance, double depositAmount) {
double newBalance = balance + depositAmount;
return newBalance;
}
}
main is a static method. It cannot refer to balance, which is an attribute (non-static variable). balance has meaning only when it is referred through an object reference (such as myAccount.balance or yourAccount.balance). But it doesn't have any meaning when it is referred through class (such as Account.balance (whose balance is that?))
I made some changes to your code so that it compiles.
public static void main(String[] args) {
Account account = new Account(1122, 20000, 4.5);
account.withdraw(2500);
account.deposit(3000);
and:
public void withdraw(double withdrawAmount) {
balance -= withdrawAmount;
}
public void deposit(double depositAmount) {
balance += depositAmount;
}
the lines
account.withdraw(balance, 2500);
account.deposit(balance, 3000);
you might want to make withdraw and deposit non-static and let it modify the balance
public void withdraw(double withdrawAmount) {
balance = balance - withdrawAmount;
}
public void deposit(double depositAmount) {
balance = balance + depositAmount;
}
and remove the balance parameter from the call
You are trying to access non static field directly from static method which is not legal in java. balance is a non static field, so either access it using object reference or make it static.
The static calls to withdraw and deposit are your problem.
account.withdraw(balance, 2500);
This line can't work , since "balance" is an instance variable of Account. The code doesn't make much sense anyway, wouldn't withdraw/deposit be encapsulated inside the Account object itself? so the withdraw should be more like
public void withdraw(double withdrawAmount)
{
balance -= withdrawAmount;
}
Of course depending on your problem you could do additional validation here to prevent negative balance etc.
Just write:
private static double balance = 0;
and you could also write those like that:
private static int id = 0;
private static double annualInterestRate = 0;
public static java.util.Date dateCreated;
To access instance variables it is a must to create an object, these are not available in the memory, before instantiation.
Therefore, you cannot make static reference to non-static fields(variables) in Java. If you still, try to do so a compile time error is generated saying “non-static variable math cannot be referenced from a static context”.
you can keep your withdraw and deposit methods static if you want however you'd have to write it like the code below.
sb = starting balance and eB = ending balance.
Account account = new Account(1122, 20000, 4.5);
double sB = Account.withdraw(account.getBalance(), 2500);
double eB = Account.deposit(sB, 3000);
System.out.println("Balance is " + eB);
System.out.println("Monthly interest is " + (account.getAnnualInterestRate()/12));
account.setDateCreated(new Date());
System.out.println("The account was created " + account.getDateCreated());