gpt4 book ai didi

JAVA OOP 从一个对象到另一个对象的汇款

转载 作者:行者123 更新时间:2023-11-30 03:02:01 25 4
gpt4 key购买 nike

我正在努力解决一个建设性问题。

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 工作正常。

最佳答案

在 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 银行系统。客户端必须只能通过接口(interface)访问

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 接口(interface),可供客户端使用。客户端应通过此接口(interface)访问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 银行系统只能通过此接口(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;
}

正如您所注意到的,客户端使用抽象(接口(interface))与系统交互。他们无法访问实现类。快乐编码:)

关于JAVA OOP 从一个对象到另一个对象的汇款,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35729265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com