JAVA OOP从一个对象到另一个对象的资金转移
我正在努力解决一个建设性的问题JAVA OOP从一个对象到另一个对象的资金转移,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 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;
}
}
我的问题是如何在transfer方法中实现代码,其中第一个对象平衡被添加到第二个对象平衡
其他方法addMoney和getBalance在BankAccount中运行良好
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);
}
}
}
第一个帐户的余额被正确地减去,但仍然不知道如何引用第二个帐户并将该金额添加到对象中。在OOP世界中,最好尽量与现实世界中的对应帐户保持距离。没有银行,银行账户就不存在。如果没有银行服务,您将无法访问银行帐户。当你从你的账户转账时,并不是你减少了账户中的金额。您只需向银行系统发送请求,以减少您帐户中的金额,并将该金额添加到目标帐户。我写了一个简单的例子向你们展示。它缺乏许多方面,如安全性、事务服务、持久性和许多其他方面,但它能够向您展示一幅大图 Banking.java(客户端) Bank.java银行系统。客户端必须只能通过接口访问 AccountService.java接口,可供客户端使用。客户端应通过此接口访问Account类 BankService.java银行系统应仅通过此接口提供给客户 正如您所注意到的,客户机使用抽象(接口)与系统交互。它们无法访问实现类。 快乐编码:)
System.out.println(second.getBalance());对于本例,应该打印到console 300。正如我所说的,实现应该是最小的,方法应该保持不变。您有getBalance()和removeMoney()方法吗?如果有钱,就加上一个,从另一个减去。不确定是什么阻止了您这样做。getBalance()存在且有效。removeMoney()方法的名称不同,但也存在。请尝试发布编译的代码。应在transfer方法中实现添加。它可以在其中使用其他方法,比如addMoney,但问题仍然是,如何获取BankAccount“second”对象,该对象在类中带有参数。模板是严格为我提供的。不能丢失给定参数的targetAccount。此外,传输方法应返回布尔值。如果金额大于第一个帐户余额,那么检查将是If(amount>balance){return false;}事实上,我已经用多态性和继承性编码了这种类型的银行程序,但这对当前情况没有帮助。问题仍然是,如何获取其他对象并向余额中添加金额。传输方法是从main和第一个对象一起运行的,而balance只能为此而更改。你说的类型是什么意思?如果你指的是纯面向对象、基于实体、设计良好的代码,那么你就不会在这里。如果没有,那么你应该重新考虑你的设计。没有学术数学或一些高级科学,甚至没有Java语言的高级功能。这都是关于OO设计的。如果您的问题如此复杂,请在这里发布完整的代码,我们将看到它有什么问题。
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;
}
}
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");
}
}
}
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();
}
}
}
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;
}
}
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();
}
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;
}
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;
}
}