为了简单起见,我们只使用静态页面——HTML和JavaScript。我们构建SnackMachineView页面:
<html lang="en"><head><title>snackMachine</title></head><body><table style="width:100%"><tbody><tr><td><button id="btnBuy">Buy a Snack</button></td></tr><tr></tr></tbody></table><br><br><br><span>Money inserted :$<span id="moneyInserted">0</span></span><br><table style="width:100%"><tbody><tr><td><button id="btnInsertCent">Insert 1 cent coin</button></td><td><button id="btnInsertTenCent">Insert 10 cent coin</button></td><td><button id="btnInsertQuarter">Insert 25 cent coin</button></td></tr><tr><td><button id="btnInsertDollar">Insert 1 dollar note</button></td><td><button id="btnInsertFiveDollar">Insert 5 dollar note</button></td><td><button id="btnInsertTwentyDollar">Insert 20 dollarnote</button></td></tr></tbody></table><br><br><br><button id="btnReturnMoney">Return Money</button><br><br><br><br><br><br><br><br><br>Number of coins and Notes in SnackMachine<table style="width:100%"><tbody><tr><td>No. of 1 cent coin : <span id="cent">0</span></td><td>No. of 10 cent coin : <span id="tenCent">0</span></td><td>No. of 25 cent coin : <span id="quarter">0</span></td></tr><tr><td>No. of 1 dollar note : <span id="dollar">0</span></td><td>No. of 5 dollar note : <span id="fiveDollar">2</span></td><td>No. of 20 dollar note : <span id="twentyDollar">0</span></td></tr></tbody></table><script src="../common/jquery-3.6.0.min.js"></script><script src="snackMachine.js"></script></body></html>
效果:

同时我们增加Controller:
package com.lugew.springbootddd;import com.lugew.springbootddd.snackmachine.SnackMachine;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/*** @author 夏露桂* @since 2021/6/10 11:55*/@RestController@RequestMapping("snackmachines")public class SnackMachineController {static SnackMachine snackMachine = new SnackMachine();}
我们以静态字段SnackMachine表示售货机对象,后期引入数据库之后我们将重构。同时为了将实体传输到页面UI,我们增加SnackMachineDto类:
package com.lugew.springbootddd;import com.lugew.springbootddd.snackmachine.Money;import lombok.Getter;import lombok.Setter;/*** @author 夏露桂* @since 2021/6/10 12:00*/@Getter@Setterpublic class SnackMachineDto {private Money moneyInside;private Money moneyInTransaction;private long id;}
为SnackMachine添加转换方法:
package com.lugew.springbootddd.snackmachine;import com.lugew.springbootddd.Entity;import com.lugew.springbootddd.SnackMachineDto;import lombok.Getter;import lombok.Setter;import java.util.Arrays;import static com.lugew.springbootddd.snackmachine.Money.None;@Getter@Setterpublic final class SnackMachine extends Entity {private Money moneyInside;private Money moneyInTransaction;public SnackMachine() {moneyInside = None;moneyInTransaction = None;}public void insertMoney(Money money) {Money[] coinsAndNotes = {Money.Cent, Money.TenCent, Money.Quarter,Money.Dollar, Money.FiveDollar,Money.TwentyDollar};if (!Arrays.asList(coinsAndNotes).contains(money))throw new IllegalStateException();moneyInTransaction = Money.add(moneyInTransaction, money);}public void returnMoney() {moneyInTransaction = None;}public void buySnack() {moneyInside = Money.add(moneyInside, moneyInTransaction);moneyInTransaction = None;}public SnackMachineDto convertToSnackMachineDto() {SnackMachineDto snackMachineDto = new SnackMachineDto();snackMachineDto.setId(getId());snackMachineDto.setMoneyInside(getMoneyInside());snackMachineDto.setMoneyInTransaction(getMoneyInTransaction());return snackMachineDto;}}
SnackMachineContoller添加接口:
package com.lugew.springbootddd;import com.lugew.springbootddd.snackmachine.SnackMachine;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/*** @author 夏露桂* @since 2021/6/10 11:55*/@RestController@RequestMapping("snackmachines")public class SnackMachineController {static SnackMachine snackMachine = new SnackMachine();@GetMapping("{id}")public SnackMachineDto getSnackMachine(@PathVariable("id") long id) {return snackMachine.convertToSnackMachineDto();}}
同时,snackMachine.js加入以下代码:
const rootURI = "http://localhost:23333/snackmachines/1";getSnackMachine();function getSnackMachine() {$.get(rootURI, function (data, status) {$('#moneyInserted').html(data.moneyInTransaction.amount);$('#cent').html(data.oneCentCount);$('#tenCent').html(data.tenCentCount);$('#quarter').html(data.quarterCount);$('#dollar').html(data.oneDollarCount);$('#fiveDollar').html(data.fiveDollarCount);$('#twentyDollar').html(data.twentyDollarCount);});}
此时页面

是可以正常接收到请求的,但是页面按钮无效,接下来为insert按钮增加事件:
const rootURI = "http://localhost:23333/snackmachines/1";getSnackMachine();function getSnackMachine() {$.get(rootURI, function (data, status) {$('#moneyInserted').html(data.moneyInTransaction.amount);$('#cent').html(data.oneCentCount);$('#tenCent').html(data.tenCentCount);$('#quarter').html(data.quarterCount);$('#dollar').html(data.oneDollarCount);$('#fiveDollar').html(data.fiveDollarCount);$('#twentyDollar').html(data.twentyDollarCount);});}$("button").click(function () {switch (this.id) {case "btnInsertCent" :insert("Cent")break;case "btnInsertTenCent" :insert("TenCent")break;case "btnInsertQuarter" :insert("Quarter")break;case "btnInsertDollar" :insert("Dollar")break;case "btnInsertFiveDollar" :insert("FiveDollar")break;case "btnInsertTwentyDollar" :insert("TwentyDollar")breakdefault :break;}});function insert(coinOrNote) {$.ajax({url: rootURI + '/moneyInTransaction/' + coinOrNote,type: 'PUT',success: function (result) {// Do something with the result}});location.reload();}
package com.lugew.springbootddd;import com.lugew.springbootddd.snackmachine.SnackMachine;import org.springframework.web.bind.annotation.*;import static com.lugew.springbootddd.snackmachine.Money.*;/*** @author 夏露桂* @since 2021/6/10 11:55*/@RestController@RequestMapping("snackmachines")public class SnackMachineController {static SnackMachine snackMachine = new SnackMachine();@GetMapping("{id}")public SnackMachineDto getSnackMachine(@PathVariable("id") long id) {return snackMachine.convertToSnackMachineDto();}@PutMapping("{id}/moneyInTransaction/{coinOrNote}")public void inserMoney(@PathVariable("id") long id, @PathVariable("coinOrNote") String coinOrNote) {if (coinOrNote.equalsIgnoreCase("Cent")) snackMachine.insertMoney(Cent);else if (coinOrNote.equalsIgnoreCase("TenCent"))snackMachine.insertMoney(TenCent);else if (coinOrNote.equalsIgnoreCase("Quarter"))snackMachine.insertMoney(Quarter);else if (coinOrNote.equalsIgnoreCase("Dollar"))snackMachine.insertMoney(Dollar);else if (coinOrNote.equalsIgnoreCase("FiveDollar"))snackMachine.insertMoney(FiveDollar);else if (coinOrNote.equalsIgnoreCase("TwentyDollar"))snackMachine.insertMoney(TwentyDollar);}}
此时,我们可以在页面验证投币功能:
接下来我们完善退币和购买功能:
@PutMapping("/{id}/moneyInTransaction")public void returnMoney(@PathVariable("id") long id) {snackMachine.returnMoney();}@PutMapping("/{id}/{slotNumber}")public void buySnack(@PathVariable("id") long id, @PathVariable("slotNumber")int slotNumber) {snackMachine.buySnack();}
const rootURI = "http://localhost:23333/snackmachines/1";getSnackMachine();function getSnackMachine() {$.get(rootURI, function (data, status) {$('#moneyInserted').html(data.moneyInTransaction.amount);$('#cent').html(data.moneyInside.oneCentCount);$('#tenCent').html(data.moneyInside.tenCentCount);$('#quarter').html(data.moneyInside.quarterCount);$('#dollar').html(data.moneyInside.oneDollarCount);$('#fiveDollar').html(data.moneyInside.fiveDollarCount);$('#twentyDollar').html(data.moneyInside.twentyDollarCount);});}$("button").click(function () {switch (this.id) {case "btnInsertCent" :insert("Cent")break;case "btnInsertTenCent" :insert("TenCent")break;case "btnInsertQuarter" :insert("Quarter")break;case "btnInsertDollar" :insert("Dollar")break;case "btnInsertFiveDollar" :insert("FiveDollar")break;case "btnInsertTwentyDollar" :insert("TwentyDollar")break;case "btnReturnMoney" :returnMoney()break;case "btnBuy" :buy("1")break;default :break;}});function insert(coinOrNote) {$.ajax({url: rootURI + '/moneyInTransaction/' + coinOrNote,type: 'PUT',success: function (result) {// Do something with the result}});location.reload();}function returnMoney() {$.ajax({url: rootURI + '/moneyInTransaction',type: 'PUT',success: function (result) {// Do something with the result}});location.reload();}function buy(position) {$.ajax({url: rootURI + '/' + position,type: 'PUT',success: function (result) {}});location.reload();}
上述几个方法中一些参数目前是无效的,作为后期使用。insertMoney方法中的参数只能是标准币值,所以将之更名为insertCoinOrNote。
