JAVA OOP从一个对象转移到另一个对象

问题描述:

我正在努力解决一个建设性问题。JAVA OOP从一个对象转移到另一个对象

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; 
    } 
} 

我的问题是如何在第一对象的平衡被添加到第二个目的余额转移方法实现代码。

BankAccount中的其他方法addMoney和getBalance工作正常。

+0

你有getBalance()和removeMoney()方法吗? – Dan

+0

如果有钱可用,请添加一个并从另一个中减去。不知道什么阻止你这样做。 –

+0

getBalance()在那里并且有效。 removeMoney()方法是不同的名称,但也存在。 – Aficionado

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

} 

}

+0

添加应在传输方法中执行。它可以像addMoney一样使用其他方法,但问题仍然存在,如何获得BankAccount“第二个”对象,该对象在课堂中与参数一起提供。 模板是非常严格的给我。 – Aficionado

+0

不能因给定参数而失去targetAccount。 另外,传输方法应该返回布尔值。 如果该量小于第一帐户余额时,检查将是如果(量>平衡){ \t \t \t返回假; \t \t} – Aficionado

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; 
      } 
} 

第一个帐户余额减去正确,但仍然不知道如何引用第二帐户并添加量为对象。

在OOP世界中,最好尽量保持与真实世界同行的距离更近。银行账户不存在没有银行。没有银行服务,您将无法访问银行账户。当您从您的账户转账时,这不会减少您账户中的金额。您只需向银行系统发送请求,以减少帐户中的金额并将该金额添加到目标帐户。我写了一个简单的例子来向你展示。它缺乏诸如安全性,交易服务,持续性等许多方面,但它是为了向您展示一张大图。

Banking.java(客户端)

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银行系统。只能通过接口

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是客户端访问这个类必须是可见的只有银行系统。客户不能直接访问他们的账户。

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接口,提供给客户。客户应通过此界面访问Account类。

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银行系统只能通过该接口提供给客户。

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; 
} 

如您所知,客户interracts与系统中使用抽象(接口)。他们无法访问实现类。 快乐编码:)

+0

事实上的事情,我已经编码的类型与多态性的传承银行业计划,而这不符合当前形势帮助。问题仍然存在,如何获取其他对象并添加数量来平衡。传输方法从第一个对象的main方法运行,并且只能为此更改平衡。 – Aficionado

+0

你说什么_that type_?如果你的意思是纯粹的OO,基于SOLID的,设计良好的code_那么你就不会在这里。如果不是,那么你应该重新考虑你的设计。没有学术数学或一些高级科学,甚至没有Java语言的高级功能。这完全是关于** OO设计**。如果你的问题非常复杂,请在这里发布完整的代码,我们会看到它有什么问题。 – callOfCode

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; 
    } 
} 

的的System.out.println(second.getBalance());这个例子应该打印出300个控制台。正如我所说,实施应该是最小的,方法应该保持不变。