From 1870a88f049a15430717c24499c59a4610818c8b Mon Sep 17 00:00:00 2001 From: ignaciano3 <57163642+ignaciano3@users.noreply.github.com> Date: Sun, 22 Oct 2023 03:18:57 +0000 Subject: [PATCH 1/3] cambio los feature para q funcionen los tests --- .../cucumber/bank_account_operations.feature | 20 +------------------ .../cucumber/bank_account_promos.feature | 16 +++++++++++++++ 2 files changed, 17 insertions(+), 19 deletions(-) create mode 100644 src/test/resources/cucumber/bank_account_promos.feature diff --git a/src/test/resources/cucumber/bank_account_operations.feature b/src/test/resources/cucumber/bank_account_operations.feature index c71060ce..75fc3cff 100644 --- a/src/test/resources/cucumber/bank_account_operations.feature +++ b/src/test/resources/cucumber/bank_account_operations.feature @@ -21,22 +21,4 @@ Feature: Bank account operations Given Account with a balance of 200 When Trying to deposit -100 Then Operation should be denied due to negative sum - And Account balance should remain 200 - - -Feature: Bank account promo, get 10% extra in your $2000+ deposits, up to $500 - - Scenario: Successfully promo applied, cap not reached. - Given Account with a balance of 0 - When Trying to deposit 2000 - Then Account balance should be 2200 - - Scenario: Successfully promo applied, cap reached. - Given Account with a balance of 0 - When Trying to deposit 6000 - Then Account balance should be 6500 - - Scenario: Promo not applied - Given Account with a balance of 0 - When Trying to deposit 1500 - Then Account balance should be 1500 + And Account balance should remain 200 \ No newline at end of file diff --git a/src/test/resources/cucumber/bank_account_promos.feature b/src/test/resources/cucumber/bank_account_promos.feature new file mode 100644 index 00000000..cfbfa0b1 --- /dev/null +++ b/src/test/resources/cucumber/bank_account_promos.feature @@ -0,0 +1,16 @@ +Feature: Bank account promo, get 10% extra in your $2000+ deposits, up to $500 + + Scenario: Successfully promo applied, cap not reached. + Given Account with a balance of 0 + When Trying to deposit 2000 + Then Account balance should be 2200 + + Scenario: Successfully promo applied, cap reached. + Given Account with a balance of 0 + When Trying to deposit 6000 + Then Account balance should be 6500 + + Scenario: Promo not applied + Given Account with a balance of 0 + When Trying to deposit 1500 + Then Account balance should be 1500 From e50204da3dffd3e9d0713ec616da8d2ff6b0f14b Mon Sep 17 00:00:00 2001 From: ignaciano3 <57163642+ignaciano3@users.noreply.github.com> Date: Sun, 22 Oct 2023 20:02:26 +0000 Subject: [PATCH 2/3] arreglo las transacciones --- README.md | 2 + src/main/java/com/aninfo/Memo1BankApp.java | 22 ++++++++- .../exceptions/AccountNotFoundException.java | 13 +++++ src/main/java/com/aninfo/model/Account.java | 27 ++++++++++- .../java/com/aninfo/model/Transaction.java | 22 +++++++++ .../com/aninfo/model/TransactionType.java | 7 +++ .../com/aninfo/service/AccountService.java | 47 +++++++++++++++++++ 7 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/aninfo/exceptions/AccountNotFoundException.java create mode 100644 src/main/java/com/aninfo/model/Transaction.java create mode 100644 src/main/java/com/aninfo/model/TransactionType.java diff --git a/README.md b/README.md index 2fe4f339..24be0049 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # memo1-bank-app Memo1 - Backend API + +Por favor para la proxima metan algo en el readme diff --git a/src/main/java/com/aninfo/Memo1BankApp.java b/src/main/java/com/aninfo/Memo1BankApp.java index 6a85624b..075b695e 100644 --- a/src/main/java/com/aninfo/Memo1BankApp.java +++ b/src/main/java/com/aninfo/Memo1BankApp.java @@ -1,6 +1,7 @@ package com.aninfo; import com.aninfo.model.Account; +import com.aninfo.model.Transaction; import com.aninfo.service.AccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; @@ -10,6 +11,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import java.util.ArrayList; import java.util.Collection; import java.util.Optional; import springfox.documentation.builders.PathSelectors; @@ -33,7 +35,8 @@ public static void main(String[] args) { @PostMapping("/accounts") @ResponseStatus(HttpStatus.CREATED) - public Account createAccount(@RequestBody Account account) { + public Account createAccount(@RequestParam double balance) { + Account account = new Account(balance); return accountService.createAccount(account); } @@ -65,6 +68,7 @@ public void deleteAccount(@PathVariable Long cbu) { accountService.deleteById(cbu); } + /* NO van a haber deposit ni withdraw de por si, solo transactions @PutMapping("/accounts/{cbu}/withdraw") public Account withdraw(@PathVariable Long cbu, @RequestParam Double sum) { return accountService.withdraw(cbu, sum); @@ -74,6 +78,22 @@ public Account withdraw(@PathVariable Long cbu, @RequestParam Double sum) { public Account deposit(@PathVariable Long cbu, @RequestParam Double sum) { return accountService.deposit(cbu, sum); } + */ + + @GetMapping("/transactions/{cbu}") + public ArrayList getTransactions(@PathVariable Long cbu) { + return accountService.getTransactions(cbu); + } + + @GetMapping("/transactions/{cbu}/{transactionIndex}") + public Transaction getTransaction(@PathVariable Long cbu, @PathVariable int transactionIndex) { + return accountService.getTransaction(cbu, transactionIndex); + } + + @PutMapping("/transactions/{cbu}") + public Account makeTransaction(@PathVariable Long cbu, @RequestBody Transaction transaction){ + return accountService.makeTransaction(cbu, transaction); + } @Bean public Docket apiDocket() { diff --git a/src/main/java/com/aninfo/exceptions/AccountNotFoundException.java b/src/main/java/com/aninfo/exceptions/AccountNotFoundException.java new file mode 100644 index 00000000..3e43fac7 --- /dev/null +++ b/src/main/java/com/aninfo/exceptions/AccountNotFoundException.java @@ -0,0 +1,13 @@ +package com.aninfo.exceptions; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(code = HttpStatus.NOT_FOUND) +public class AccountNotFoundException extends RuntimeException { + + public AccountNotFoundException(String message) { + super(message); + } + +} diff --git a/src/main/java/com/aninfo/model/Account.java b/src/main/java/com/aninfo/model/Account.java index a82f2326..e9e683bc 100644 --- a/src/main/java/com/aninfo/model/Account.java +++ b/src/main/java/com/aninfo/model/Account.java @@ -1,5 +1,7 @@ package com.aninfo.model; +import java.util.ArrayList; + import javax.persistence.*; @Entity @@ -9,7 +11,14 @@ public class Account { @GeneratedValue(strategy = GenerationType.AUTO) private Long cbu; - private Double balance; + private Double balance; //Es un error grave usar doubles para balances + + @Lob + private ArrayList transactions = new ArrayList(); + // Si tuviera mas tiempo lo haria en otra tabla + + private Double promo = 500.0; + public Account(){ } @@ -34,4 +43,20 @@ public void setBalance(Double balance) { this.balance = balance; } + public void addTransaction(Transaction t) { + this.transactions.add(t); + } + + public void deleteTransaction(int index) { + transactions.remove(index); + } + + public Transaction getTransaction(int index) { + return transactions.get(index); + } + + public ArrayList getTransactions() { + return transactions; + } + } diff --git a/src/main/java/com/aninfo/model/Transaction.java b/src/main/java/com/aninfo/model/Transaction.java new file mode 100644 index 00000000..25880ffc --- /dev/null +++ b/src/main/java/com/aninfo/model/Transaction.java @@ -0,0 +1,22 @@ +package com.aninfo.model; + +import java.io.Serializable; + +public class Transaction implements Serializable { + private final TransactionType type; + private final Double amount; + + public Transaction(Double amount, TransactionType type) { + this.amount = amount; + this.type = type; + } + + public Double getAmount() { + return amount; + } + + public TransactionType getType() { + return type; + } + +} diff --git a/src/main/java/com/aninfo/model/TransactionType.java b/src/main/java/com/aninfo/model/TransactionType.java new file mode 100644 index 00000000..eaa68fe5 --- /dev/null +++ b/src/main/java/com/aninfo/model/TransactionType.java @@ -0,0 +1,7 @@ +package com.aninfo.model; + +public enum TransactionType { + DEPOSIT, + WITHDRAW, + PROMO, +} diff --git a/src/main/java/com/aninfo/service/AccountService.java b/src/main/java/com/aninfo/service/AccountService.java index 0a5f1411..12846a3d 100644 --- a/src/main/java/com/aninfo/service/AccountService.java +++ b/src/main/java/com/aninfo/service/AccountService.java @@ -2,12 +2,18 @@ import com.aninfo.exceptions.DepositNegativeSumException; import com.aninfo.exceptions.InsufficientFundsException; +import com.aninfo.exceptions.InvalidTransactionTypeException; +import com.aninfo.exceptions.AccountNotFoundException; import com.aninfo.model.Account; +import com.aninfo.model.Transaction; +import com.aninfo.model.TransactionType; import com.aninfo.repository.AccountRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.transaction.Transactional; + +import java.util.ArrayList; import java.util.Collection; import java.util.Optional; @@ -18,6 +24,9 @@ public class AccountService { private AccountRepository accountRepository; public Account createAccount(Account account) { + if (account.getBalance() > 0) { + account.addTransaction(new Transaction(account.getBalance(), TransactionType.DEPOSIT)); + } return accountRepository.save(account); } @@ -40,12 +49,16 @@ public void deleteById(Long cbu) { @Transactional public Account withdraw(Long cbu, Double sum) { Account account = accountRepository.findAccountByCbu(cbu); + if (account == null) { + throw new AccountNotFoundException("Account not found"); + } if (account.getBalance() < sum) { throw new InsufficientFundsException("Insufficient funds"); } account.setBalance(account.getBalance() - sum); + account.addTransaction(new Transaction(sum, TransactionType.WITHDRAW)); accountRepository.save(account); return account; @@ -59,10 +72,44 @@ public Account deposit(Long cbu, Double sum) { } Account account = accountRepository.findAccountByCbu(cbu); + if (account == null) { + throw new AccountNotFoundException("Account not found"); + } + account.setBalance(account.getBalance() + sum); + account.addTransaction(new Transaction(sum, TransactionType.DEPOSIT)); accountRepository.save(account); return account; } + public ArrayList getTransactions(Long cbu) { + Account account = accountRepository.findAccountByCbu(cbu); + if (account == null) { + throw new AccountNotFoundException("Account not found"); + } + + return account.getTransactions(); + } + + public Transaction getTransaction(Long cbu, int index) { + Account account = accountRepository.findAccountByCbu(cbu); + if (account == null) { + throw new AccountNotFoundException("Account not found"); + } + + return account.getTransaction(index); + } + + @Transactional + public Account makeTransaction(Long cbu, Transaction t) { + if (t.getType() == TransactionType.DEPOSIT) { + return deposit(cbu, t.getAmount()); + } else if (t.getType() == TransactionType.WITHDRAW) { + return withdraw(cbu, t.getAmount()); + } else { + throw new InvalidTransactionTypeException("Invalid Transaction"); + } + } + } From b64a672b95a0c84b3542f203cc05f2bc39f96ffa Mon Sep 17 00:00:00 2001 From: ignaciano3 <57163642+ignaciano3@users.noreply.github.com> Date: Sun, 22 Oct 2023 20:55:05 +0000 Subject: [PATCH 3/3] agrego la promo --- src/main/java/com/aninfo/model/Account.java | 8 ++++++++ src/main/java/com/aninfo/service/AccountService.java | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/src/main/java/com/aninfo/model/Account.java b/src/main/java/com/aninfo/model/Account.java index e9e683bc..248c4309 100644 --- a/src/main/java/com/aninfo/model/Account.java +++ b/src/main/java/com/aninfo/model/Account.java @@ -59,4 +59,12 @@ public ArrayList getTransactions() { return transactions; } + public Double getPromo() { + return promo; + } + + public void setPromo(Double promo) { + this.promo = promo; + } + } diff --git a/src/main/java/com/aninfo/service/AccountService.java b/src/main/java/com/aninfo/service/AccountService.java index 12846a3d..3ff22524 100644 --- a/src/main/java/com/aninfo/service/AccountService.java +++ b/src/main/java/com/aninfo/service/AccountService.java @@ -78,6 +78,14 @@ public Account deposit(Long cbu, Double sum) { account.setBalance(account.getBalance() + sum); account.addTransaction(new Transaction(sum, TransactionType.DEPOSIT)); + + if (sum >= 2000 && account.getPromo() > 0){ + Double promoApplied = Math.min(sum * 0.1, account.getPromo()); + account.setBalance(account.getBalance() + promoApplied); + account.setPromo(account.getPromo() - promoApplied); + account.addTransaction(new Transaction(promoApplied, TransactionType.PROMO)); + } + accountRepository.save(account); return account;