增加ATM的HTML页面:
    image.png

    1. package com.lugew.springbootddd.atm;
    2. import org.springframework.beans.factory.annotation.Autowired;
    3. import org.springframework.stereotype.Controller;
    4. import org.springframework.web.bind.annotation.*;
    5. import java.util.ArrayList;
    6. import java.util.List;
    7. @Controller
    8. @RequestMapping(path = "/atms")
    9. public class AtmController {
    10. @Autowired
    11. private AtmRepository atmRepository;
    12. @Autowired
    13. private PaymentGateway paymentGateway;
    14. @GetMapping()
    15. @ResponseBody
    16. public List<AtmDto> getAtms() {
    17. List<AtmDto> list = new ArrayList<>();
    18. atmRepository.findAll().forEach(list::add);
    19. return list;
    20. }
    21. @GetMapping("/{id}")
    22. @ResponseBody
    23. public AtmDto getAtm(@PathVariable("id") long id) {
    24. return atmRepository.findById(id).orElse(null);
    25. }
    26. @PutMapping("/{id}/{amount}")
    27. @ResponseBody
    28. public String takeMoney(@PathVariable("id") long id, @PathVariable("amount")
    29. float amount) {
    30. AtmDto atmDto = atmRepository.findById(id).orElse(null);
    31. Atm atm = atmDto.convertToAtm();
    32. if (!atm.canTakeMoney(amount).isEmpty()) return
    33. atm.canTakeMoney(amount);
    34. float amountWithCommission =
    35. atm.calculateAmountWithCommission(amount);
    36. paymentGateway.chargePayment(amountWithCommission);
    37. atm.takeMoney(amount);
    38. atmRepository.save(atm.convertToAtmDto());
    39. return "You have withrawn amount : $" + amount;
    40. }
    41. }
    1. package com.lugew.springbootddd.atm;
    2. import org.springframework.stereotype.Component;
    3. @Component
    4. public class PaymentGateway {
    5. public void chargePayment(float amountWithCommission) {
    6. }
    7. }

    ATM:

    1. public AtmDto convertToAtmDto() {
    2. AtmDto atmDto = new AtmDto();
    3. atmDto.setId(id);
    4. atmDto.setMoneyCharged(moneyCharged);
    5. atmDto.setOneCentCount(moneyInside.getOneCentCount());
    6. atmDto.setTenCentCount(moneyInside.getTenCentCount());
    7. atmDto.setQuarterCount(moneyInside.getQuarterCount());
    8. atmDto.setOneDollarCount(moneyInside.getOneDollarCount());
    9. atmDto.setFiveDollarCount(moneyInside.getFiveDollarCount());
    10. atmDto.setTwentyDollarCount(moneyInside.getTwentyDollarCount());
    11. return atmDto;
    12. }

    AtmView.html:

    1. <!DOCTYPE html>
    2. <html>
    3. <body>
    4. <span>Money inside :$<span id="moneyInside"></span></span>
    5. </br>
    6. <span>Money charged :$<span id="moneyCharged"></span></span>
    7. </br>
    8. </br>
    9. </br>
    10. <input type="text" id="moneyToTake"><br>
    11. <button id="btnTakeMoney">Take Money</button>
    12. </br>
    13. </br>
    14. </br>
    15. <div>No of coins and notes in Atm :</div>
    16. <table style="width:100%">
    17. </tr>
    18. <td>No. of 1 cent coin : <span id="cent"></span></td>
    19. <td>No. of 10 cent coin : <span id="tenCent"></span></td>
    20. <td>No. of 25 cent coin : <span id="quarter"></span></td>
    21. </tr>
    22. </tr>
    23. <td>No. of 1 dollar note : <span id="dollar"></span></td>
    24. <td>No. of 5 dollar note : <span id="fiveDollar"></span></td>
    25. <td>No. of 20 dollar note : <span id="twentyDollar"></span></td>
    26. </tr>
    27. </table>
    28. <script src="../common/jquery-3.6.0.min.js"></script>
    29. <script src="atm.js"></script>
    30. </body>
    31. </html>
    1. let searchParams = new URLSearchParams(window.location.search)
    2. let param = '';
    3. if (searchParams.has('id')) {
    4. param = searchParams.get('id')
    5. } else {
    6. param = '1';
    7. }
    8. const rootURI = "http://localhost:23333/atms/" + param;
    9. getAtm();
    10. function getAtm() {
    11. $.get(rootURI, function (data, status) {
    12. $('#moneyInside').html(data.amount);
    13. $('#moneyCharged').html(data.moneyCharged);
    14. $('#cent').html(data.oneCentCount);
    15. $('#tenCent').html(data.tenCentCount);
    16. $('#quarter').html(data.quarterCount);
    17. $('#dollar').html(data.oneDollarCount);
    18. $('#fiveDollar').html(data.fiveDollarCount);
    19. $('#twentyDollar').html(data.twentyDollarCount);
    20. });
    21. }
    22. $("#btnTakeMoney").click(function () {
    23. var moneyToTake = $('#moneyToTake').val();
    24. $.ajax({
    25. url: rootURI + '/' + moneyToTake,
    26. type: 'PUT',
    27. success: function (data) {
    28. alert(data);
    29. location.reload();
    30. }
    31. });
    32. });

    image.png我们可以看到Atm的基本情况,取1元时:
    image.png
    我们在此处引入了付款的代理类PaymentGateway。显然,它只是模拟了对应的方法并没有实现。