我正在努力解决一个建设性的问题。
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 :(得分:2)
在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;
}
正如您所注意到的,客户端使用抽象(接口)与系统进行交互。他们无法访问实现类。 快乐的编码:)
答案 1 :(得分:0)
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);
}
}
}
答案 2 :(得分:0)
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;
}
}
第一个帐户余额被正确扣除,但仍然不知道如何引用第二个帐户并将该金额添加到对象。
答案 3 :(得分:0)
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。正如我所说,实施应该是最小的,方法应保持不变。