增加ATM的HTML页面:
package com.lugew.springbootddd.atm;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.*;import java.util.ArrayList;import java.util.List;@Controller@RequestMapping(path = "/atms")public class AtmController {@Autowiredprivate AtmRepository atmRepository;@Autowiredprivate PaymentGateway paymentGateway;@GetMapping()@ResponseBodypublic List<AtmDto> getAtms() {List<AtmDto> list = new ArrayList<>();atmRepository.findAll().forEach(list::add);return list;}@GetMapping("/{id}")@ResponseBodypublic AtmDto getAtm(@PathVariable("id") long id) {return atmRepository.findById(id).orElse(null);}@PutMapping("/{id}/{amount}")@ResponseBodypublic String takeMoney(@PathVariable("id") long id, @PathVariable("amount")float amount) {AtmDto atmDto = atmRepository.findById(id).orElse(null);Atm atm = atmDto.convertToAtm();if (!atm.canTakeMoney(amount).isEmpty()) returnatm.canTakeMoney(amount);float amountWithCommission =atm.calculateAmountWithCommission(amount);paymentGateway.chargePayment(amountWithCommission);atm.takeMoney(amount);atmRepository.save(atm.convertToAtmDto());return "You have withrawn amount : $" + amount;}}
package com.lugew.springbootddd.atm;import org.springframework.stereotype.Component;@Componentpublic class PaymentGateway {public void chargePayment(float amountWithCommission) {}}
ATM:
public AtmDto convertToAtmDto() {AtmDto atmDto = new AtmDto();atmDto.setId(id);atmDto.setMoneyCharged(moneyCharged);atmDto.setOneCentCount(moneyInside.getOneCentCount());atmDto.setTenCentCount(moneyInside.getTenCentCount());atmDto.setQuarterCount(moneyInside.getQuarterCount());atmDto.setOneDollarCount(moneyInside.getOneDollarCount());atmDto.setFiveDollarCount(moneyInside.getFiveDollarCount());atmDto.setTwentyDollarCount(moneyInside.getTwentyDollarCount());return atmDto;}
AtmView.html:
<!DOCTYPE html><html><body><span>Money inside :$<span id="moneyInside"></span></span></br><span>Money charged :$<span id="moneyCharged"></span></span></br></br></br><input type="text" id="moneyToTake"><br><button id="btnTakeMoney">Take Money</button></br></br></br><div>No of coins and notes in Atm :</div><table style="width:100%"></tr><td>No. of 1 cent coin : <span id="cent"></span></td><td>No. of 10 cent coin : <span id="tenCent"></span></td><td>No. of 25 cent coin : <span id="quarter"></span></td></tr></tr><td>No. of 1 dollar note : <span id="dollar"></span></td><td>No. of 5 dollar note : <span id="fiveDollar"></span></td><td>No. of 20 dollar note : <span id="twentyDollar"></span></td></tr></table><script src="../common/jquery-3.6.0.min.js"></script><script src="atm.js"></script></body></html>
let searchParams = new URLSearchParams(window.location.search)let param = '';if (searchParams.has('id')) {param = searchParams.get('id')} else {param = '1';}const rootURI = "http://localhost:23333/atms/" + param;getAtm();function getAtm() {$.get(rootURI, function (data, status) {$('#moneyInside').html(data.amount);$('#moneyCharged').html(data.moneyCharged);$('#cent').html(data.oneCentCount);$('#tenCent').html(data.tenCentCount);$('#quarter').html(data.quarterCount);$('#dollar').html(data.oneDollarCount);$('#fiveDollar').html(data.fiveDollarCount);$('#twentyDollar').html(data.twentyDollarCount);});}$("#btnTakeMoney").click(function () {var moneyToTake = $('#moneyToTake').val();$.ajax({url: rootURI + '/' + moneyToTake,type: 'PUT',success: function (data) {alert(data);location.reload();}});});
我们可以看到Atm的基本情况,取1元时:
我们在此处引入了付款的代理类PaymentGateway。显然,它只是模拟了对应的方法并没有实现。
