Compare commits
3 Commits
2bec3d5734
...
2025-07-16
| Author | SHA1 | Date | |
|---|---|---|---|
| ad99243944 | |||
| 979c6932ba | |||
| 42acf37000 |
@ -12,9 +12,9 @@ import java.util.HashMap;
|
|||||||
public class MoneyTransferController {
|
public class MoneyTransferController {
|
||||||
|
|
||||||
// Global accounts variable
|
// Global accounts variable
|
||||||
private static final Map<String, Map<String, Double>> accounts = new HashMap<String, Map<String, Double>>() {{
|
private static final Map<String, Account> accounts = new HashMap<String, Account>() {{
|
||||||
put("1", new HashMap<String, Double>() {{ put("amount", 1000.0); }});
|
put("1", new Account("1", 1000.0));
|
||||||
put("2", new HashMap<String, Double>() {{ put("amount", 1000.0); }});
|
put("2", new Account("2", 1000.0));
|
||||||
}};
|
}};
|
||||||
|
|
||||||
@PostMapping("/transfer")
|
@PostMapping("/transfer")
|
||||||
@ -33,6 +33,17 @@ public class MoneyTransferController {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class Account {
|
||||||
|
private String id;
|
||||||
|
private Double amount;
|
||||||
|
|
||||||
|
public Account(String id, Double amount) {
|
||||||
|
this.id = id;
|
||||||
|
this.amount = amount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public static class TransferRequest {
|
public static class TransferRequest {
|
||||||
private Double amount;
|
private Double amount;
|
||||||
|
|||||||
@ -11,8 +11,8 @@ class Transfer(BaseModel):
|
|||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
accounts = {
|
accounts = {
|
||||||
'1': {'amount': 1000},
|
'1': 1000,
|
||||||
'2': {'amount': 1000},
|
'2': 1000,
|
||||||
}
|
}
|
||||||
|
|
||||||
transactions: list[Transfer] = []
|
transactions: list[Transfer] = []
|
||||||
@ -21,11 +21,32 @@ transactions: list[Transfer] = []
|
|||||||
def transfer(transfer: Transfer):
|
def transfer(transfer: Transfer):
|
||||||
amount = transfer.amount
|
amount = transfer.amount
|
||||||
sender = transfer.sender
|
sender = transfer.sender
|
||||||
receiver = transfer.receive
|
receiver = transfer.receiver
|
||||||
|
|
||||||
if amount <= 0:
|
if amount <= 0:
|
||||||
return {"error": "Amount must be greater than 0"}
|
return {"error": "Amount must be greater than 0"}
|
||||||
|
|
||||||
|
if accounts.get(sender) <= amount:
|
||||||
|
return {"error": "Sender does not have sufficient funds. "}
|
||||||
|
else:
|
||||||
|
# Minus sender's amount
|
||||||
|
accounts[sender] = accounts.get(sender) - amount
|
||||||
|
|
||||||
|
# Add receiver amount
|
||||||
|
accounts[receiver] = accounts.get(receiver) + amount
|
||||||
|
|
||||||
|
# Include the transaction
|
||||||
|
transactions.append(transfer)
|
||||||
|
|
||||||
print(f"transfer {amount} from {sender} to {receiver}")
|
print(f"transfer {amount} from {sender} to {receiver}")
|
||||||
|
print(transactions)
|
||||||
|
print(accounts.get("1"))
|
||||||
|
print(accounts.get("2"))
|
||||||
|
|
||||||
|
|
||||||
return {"amount": amount, "sender": sender, "receiver": receiver}
|
return {"amount": amount, "sender": sender, "receiver": receiver}
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/transactions")
|
||||||
|
def getTransactions():
|
||||||
|
return transactions
|
||||||
|
|||||||
Reference in New Issue
Block a user