Java banking system problems - java

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).

Related

Simple OOPS demo, stuck with the function of for loop and its role in the main method

The only information about this code is this CreditCard class defines
credit card objects that model a simplified version of traditional credit cards. They
store information about the customer, issuing bank, account identifier, credit limit,
and current balance. They do not charge interest or late payments, but they do
restrict charges that would cause a card’s balance to go over its credit limit.
The following code is from the book Data Structures and Algorithms in java:
Please help me regarding the for loop which is:
for (int val = 1; val <= 16; val++) {
wallet[0].charge(3*val);
wallet[1].charge(2*val);
wallet[2].charge(val);
}
Why the charge method has parameters 3val, 2val and val. What this loop really does?
From the following code:
package com.illustrations;
public class CreditCard{
private String customer;
private String bank;
private String account;
private int limit;
protected double balance;
public CreditCard(String customer, String bank, String account, int limit, double balance){
this.customer = customer;
this.bank = bank;
this.account = account;
this.limit = limit;
this.balance = balance;
}
public CreditCard(String customer, String bank, String account, int limit) {
this(customer, bank, account, limit, 0.0);
}
public String getCustomer(){ return customer; }
public String getBank() { return bank; }
public String getAccount() { return account; }
public int getLimit() { return limit; }
public double getBalance() { return balance; }
public boolean charge(double price) {
if(price + balance > limit)
return false;
balance = balance + price; return true;
}
public void makePayment(double amount) {
balance -=amount;
}
public static void printSummary(CreditCard card) {
System.out.println("Customer ="+card.customer);
System.out.println("Bank ="+card.bank);
System.out.println("Account ="+card.account);
System.out.println("Balance ="+card.balance);
System.out.println("Limit ="+card.limit);
}
public static void main(String[] args) {
CreditCard[ ] wallet = new CreditCard[3];
wallet[0] = new CreditCard("John Bowman", "California Savings", "5391 0375 9387 5309", 5000);
wallet[1] = new CreditCard("John Bowman", "California Federal", "3485 0399 3395 1954", 3500);
wallet[2] = new CreditCard("John Bowman", "California Finance", "5391 0375 9387 5309", 2500, 300);
for (int val = 1; val <= 16; val++) {
wallet[0].charge(3*val);
wallet[1].charge(2*val);
wallet[2].charge(val);
}
for (CreditCard card : wallet) {
CreditCard.printSummary(card); // calling static method
while (card.getBalance() > 200.0) {
card.makePayment(200);
System.out.println("New balance = " +card.getBalance());
}
}
}
}
I don't really see much significance. Actually, there are 3 kinds of wallets defined - Savings, Federal, and Finance with Savings having the largest credit limit of the remaining two. So what we did here in the for loop with 3*val is keep putting in money in the Savings wallet thrice as fast as the Finance one because its limit is much higher.
Once the limit has been reached, we don't allow more money to be added in the wallets.

Why are my constructors recognized as methods?

I am working on a program to hold bank accounts as objects. The accounts have an interest rate, balance, ID, and date created data to go along. In what I have made, the default balance, id, and interest is 0 from what I understand. The interest rate is undefined by default. The book I am learning form shows that a no-arg constructor is done with "Circle() { }".I used "account() { }" in the account class. When I run the program in jGRASP, I get error "invalid method declaration; return type required" for both of my constructors. It is recognizing what I intend to be constructors as methods. What do I need to understand so I can make my constructors not be recognized as methods?
When running the first constructor, I understand we create an Account object called account with the default values. When we run the second constructor, we are changing the values of the account object to something with specified
public class Bank{
public static void main(String[] args){
Account account = new Account(1122, 20000);
account.setAnnualInterestRate(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 = 0;
private double balance = 0;
private double annualInterestRate = 0;
private String dateCreated;
account(){
}
account(int newID, double newBalance){
id = newID;
balance = newBalance;
}
//accessor for ID
public int getID(){
return id;
}
//acessor for balance
public double getBalance(){
return balance;
}
//accessor for interest rate
public double getAnnualInterest(){
return annualInterestRate;
}
//mutator for ID
public void setID(int IDset){
id = IDset;
}
//mutator for balance
public void setBalance(int BalanceSet){
balance = BalanceSet;
}
//mutator for annual interest
public void setAnnualInterestRate(double InterestSet){
annualInterestRate = InterestSet;
}
//accessor for date created
public String getDateCreated(){
return dateCreated;
}
//method that converts annual interest into monthly interest and returns the value
public double getMonthlyInterest(){
double x = annualInterestRate / 12;
return x;
}
//method that witdraws from account
public double withdraw(double w){
balance -= w;
return balance;
}
//method that deposits into account
public double deposite(double d){
balance += d;
return balance;
}
}
Constructor names must match the class names in a case sensitive way. In the Account class, change
account(){
to
Account(){
and likewise for your other constructor.
You need to capitalize your a in both constructors. Java is case sensitive.
Account(){
}
Account(int newID, double newBalance){
id = newID;
balance = newBalance;
}
Otherwise, Java sees this as a method with no return type. Remember, a constructor does not have or needs a return type.
Constructors should be camelized(as a convention, same with the class name) and it return only the type of itself
see my example :)
Account() {
return;
}

Output not Displaying as Expected

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.

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.

cannot make a static reference to the non-static field

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());

Categories