Reputation: 61
I am struggling with one constructive problem.
public static void main(String[] args) {
BankAccount first = new BankAccount();
BankAccount second = new BankAccount();
first.addMoney(110.15);
second.addMoney(1000.5);
first.transfer(second, 100.0);
public class BankAccount {
public boolean transfer(BankAccount targetAccount, double amount) {
//first account new balance with transaction fees
balance = balance - (amount + (amount * 0.01));
return false;
}
}
My question is how to implement code in transfer method where first object balance is added to second object balance.
The other methods addMoney and getBalance inside BankAccount are working fine.
Upvotes: 3
Views: 10069
Reputation: 61
public class main {
public static void main(String[] args) {
BankAccount first = new BankAccount();
BankAccount second = new BankAccount();
first.addMoney(1010.0);
second.addMoney(200.0);
first.transfer(second, 100.0);
System.out.println(second.getBalance());
}
}
public class BankAccount {
public static final double TRANSACTION_FEE = 0.01;
private double balance;
public double getBalance() {
return balance;
}
public double withdrawMoney(double amount) {
if (amount > balance) {
return Double.NaN;
} else {
balance = balance - amount;
}
return balance;
}
public void addMoney(double amount) {
balance = balance + amount;
}
public boolean transfer(BankAccount targetAccount, double amount) {
if (amount > balance) {
return false;
} else {
balance = balance - (amount + (amount * TRANSACTION_FEE));
}
return false;
}
}
The System.out.println(second.getBalance()); should print out to console exactly 300 for this example. As I said, the implementation should be minimal and the methods should remain same.
Upvotes: 0
Reputation: 1026
In OOP world it is better to try to keep as closer to real world counterparts as possible. Bank account would not exist without bank. You wouldn't be able to access bank account without bank service. When you transfer money from your account it is not you that reduces amount of money in your account. You just send request to banking system to reduce amount of money in your account and add that amount to target account. I've wrote simple example to show you. It lacks many aspects such as Security, Transactions service, Persistency and many others but it is enaugh of it to show you a Big Picture.
Banking.java (Client)
package com.banking.client;
import com.banking.bankingSystem.AccountService;
import com.banking.bankingSystem.Bank;
import com.banking.bankingSystem.BankService;
public class Banking
{
public static void main(String[] args) throws java.lang.Exception
{
BankService bankService = Bank.requestBankService(); //Request bank service (same as you'd go to banks website)
bankService.register("John", "hero", 100);
bankService.register("Smith", "superHero", 100);
try
{
AccountService john = bankService.logIn("John", "hero");
AccountService smith = bankService.logIn("Smith", "superHero");
System.out.println(john.getName() + " has " + john.getAvailableMoney() + "$");
System.out.println(smith.getName() + " has " + john.getAvailableMoney() + "$");
smith.transfer(john.getName(), 50);
System.out.println(john.getName() + " has " + john.getAvailableMoney() + "$");
System.out.println(smith.getName() + " has " + smith.getAvailableMoney() + "$");
//Now lets try to transfer too large amount of money
john.transfer(smith.getName(), 200);
} catch (Exception e)
{
//In real world banking, manny problems could happen when you use its services.
//I've put all exceptions in one place. You shouldn't do this in real programs.
System.err.println("\u001B[31m" + e.getMessage() + "\u001B[00m");
}
}
}
Bank.java Bank system. Must be accessible to clients only through interfaces
package com.banking.bankingSystem;
import java.util.HashMap;
import java.util.Map;
public class Bank implements BankService
{
private static Map<String, Account> registeredAccounts;
Bank()
{
registeredAccounts = new HashMap<>();
}
public static BankService requestBankService()
{
return new Bank();
}
@Override
public void register(String name, String password, int initialAmount)
{
registeredAccounts.put(name, new Account(name, password, initialAmount));
System.out.println("User " + name + " registerred succesfully");
}
@Override
public AccountService logIn(String name, String password) throws Exception
{
if(!registeredAccounts.containsKey(name)) throw new Exception("Account of " + name + " is not registerred");
if(registeredAccounts.get(name).verify(name, password))
{
System.out.println("User " + name + " logged in succesfully");
return new LoggedInUser(registeredAccounts.get(name));
}
throw new Exception("Wrong credentials");
}
private class LoggedInUser implements AccountService
{
private Account loggedAcount;
LoggedInUser(Account account)
{
this.loggedAcount = account;
}
@Override
public int withdraw(int amount) throws Exception
{
int withdrawedAmount = loggedAcount.withdraw(amount);
System.out.println("User " + loggedAcount.getName() + "withdrawed " + Integer.toString(withdrawedAmount) + "$");
return withdrawedAmount;
}
@Override
public boolean transfer(String to, int amount) throws Exception
{
if(registeredAccounts.containsKey(to))
{
Account transferTo = registeredAccounts.get(to);
transferTo.addMoney(loggedAcount.withdraw(amount));
System.out.println("User " + loggedAcount.getName() + " has transferred " + Integer.toString(amount) + "$ to " + transferTo.getName());
return true;
}
throw new Exception("Can't transfer money to " + to + ". Reason: No such user");
}
@Override
public int getAvailableMoney()
{
return loggedAcount.availableMoney();
}
@Override
public String getName()
{
return loggedAcount.getName();
}
}
}
Account.java This class must be visible only to bank system. Clients cannot access their accounts directly.
package com.banking.bankingSystem;
class Account
{
private int money;
private final String name, password;
Account(String name, String password, int initialSum)
{
money = initialSum;
this.password = password;
this.name = name;
}
int availableMoney()
{
return money;
}
public int addMoney(int amountToAdd)
{
return money += amountToAdd;
}
int withdraw(int amountToTake) throws Exception
{
if (hasEnaughMoney(amountToTake))
{
money -= amountToTake;
return amountToTake;
}
throw new Exception("Account of " + name + " has not enaugh money");
}
boolean verify(String name, String password)
{
return this.name.equals(name) && this.password.equals(password);
}
String getName()
{
return name;
}
boolean hasEnaughMoney(int amountToTake)
{
return money >= amountToTake;
}
}
AccountService.java Interface, available to clients. Client should access Account class through this interface.
package com.banking.bankingSystem;;
public interface AccountService
{
int withdraw(int amount) throws Exception;
boolean transfer(String to, int amount) throws Exception;
int getAvailableMoney();
String getName();
}
BankService.java Bank system should be available to clients only through this interface.
package com.banking.bankingSystem;
public interface BankService
{
public void register(String name, String password, int initialAmount);
public AccountService logIn(String name, String password) throws Exception;
}
As you have noticed, clients interracts with system using abstractions (interfaces). Implementation classes are not accessible to them. Happy coding :)
Upvotes: 3
Reputation: 61
public static void main(String[] args) {
first.transfer(second, 100.0);
}
public class BankAccount {
public boolean transfer(BankAccount targetAccount, double amount) {
if (amount > balance) {
return false;
}
balance = balance - (amount + (amount * TRANSACTION_FEE));
return true;
}
}
The first account balance is subtracted correctly, but still don' t know how to refer to second account and add that amount to object.
Upvotes: 0
Reputation: 23
public class BankAccount {
private double balance;
public BankAccount() {
balance = 0;
}
public boolean transfer(double amount) {
double newBalance = balance - (amount + (amount * 0.01));
if(newBalance > 0) {
balance = newBalance;
}
return newBalance>0;
}
private void addMoney(double money) {
balance += money;
}
public static void main(String[] args) {
BankAccount first = new BankAccount();
BankAccount second = new BankAccount();
first.addMoney(110.15);
second.addMoney(1000.5);
boolean result = first.transfer(100.0);
if(result) {
second.addMoney(100);
}
}
}
Upvotes: 0