![image.png](/uploads/projects/lugew@ddd/75dbbfb6be82c263e59ccb7975906e26.png)
<html lang="en">
<head><title>snackMachine</title></head>
<body>
<table style="width:100%">
<tr>
<td><span>Chocolate price:$<span id="chocolatePrice"></span></span>
</td>
<td><span>Soda price:$<span id="sodaPrice"></span></span></td>
<td><span>Gum price:$<span id="gumPrice"></span></span></td>
</tr>
<tr>
<td><span>Chocolate quantity:<span id="chocolateQuantity"></span>
</span></td>
<td><span>Soda quantity:<span id="sodaQuantity"></span></span></td>
<td><span>Gum quantity:<span id="gumQuantity"></span></span></td>
</tr>
<tr>
<td>
<button id="btnBuyChocolate">Buy a Chocolate</button>
</td>
<td>
<button id="btnBuySoda">Buy a Soda</button>
</td>
<td>
<button id="btnBuyGum">Buy a Gum</button>
</td>
</tr>
<tr>
</table>
</br>
</br>
</br>
<span>Money inserted :$<span id="moneyInserted"></span></span>
</br>
<table style="width:100%">
</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 dollar
note
</button>
</td>
</tr>
</table>
</br>
</br>
<button id="btnReturnMoney">Return Money</button>
</br>
</br>
</br>
<div>No of coins and notes in Snack machine :</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="snackMachine.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/snackmachines/" + param;
getSnackMachine();
function getSnackMachine() {
$.get(rootURI, function (data, status) {
console.log(data);
$('#moneyInserted').html(data.moneyInTransaction);
$('#chocolatePrice').html(data.slotDtoList[0].price);
$('#sodaPrice').html(data.slotDtoList[1].price);
$('#gumPrice').html(data.slotDtoList[2].price);
$('#chocolateQuantity').html(data.slotDtoList[0].quantity);
$('#sodaQuantity').html(data.slotDtoList[1].quantity);
$('#gumQuantity').html(data.slotDtoList[2].quantity);
$('#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")
break;
case "btnReturnMoney" :
returnMoney()
break;
case "btnBuyChocolate" :
buy("1")
break;
case "btnBuySoda" :
buy("2")
break;
case "btnBuyGum" :
buy("3")
break;
default :
break;
}
});
function insert(coinOrNote) {
$.ajax({
url: rootURI + '/moneyInTransaction/' + coinOrNote,
type: 'PUT',
success: function (result) {
}
});
location.reload();
}
function returnMoney() {
$.ajax({
url: rootURI + '/moneyInTransaction',
type: 'PUT',
success: function (result) {
}
});
location.reload();
}
function buy(position) {
$.ajax({
url: rootURI + '/' + position,
type: 'PUT',
success: function (result) {
}
});
location.reload();
}
package com.lugew.springbootddd;
import com.lugew.springbootddd.snackmachine.SnackMachine;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import static com.lugew.springbootddd.snackmachine.Money.*;
/**
* @author 夏露桂
* @since 2021/6/10 11:55
*/
@RestController
@RequestMapping("snackmachines")
@RequiredArgsConstructor
public class SnackMachineController {
private final SnackMachineRepository snackMachineRepository;
@GetMapping()
@ResponseBody
public List<SnackMachineDto> getSnackMachines() {
List<SnackMachineDto> list = new ArrayList<>();
snackMachineRepository.findAll().forEach(list::add);
return list;
}
@GetMapping("/{id}")
@ResponseBody
public SnackMachineDto getSnackMachine(@PathVariable("id") long id) {
return snackMachineRepository.findById(id).orElse(null);
}
@PutMapping("/{id}/moneyInTransaction/{coinOrNote}")
public void insertCoinOrNote(@PathVariable("id") long id, @PathVariable("coinOrNote") String coinOrNote) {
SnackMachineDto snackMachineDto =
snackMachineRepository.findById(id).orElse(null);
SnackMachine snackMachine = snackMachineDto.convertToSnackMachine();
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);
snackMachineRepository.save(snackMachine.convertToSnackMachineDto());
}
@PutMapping("/{id}/moneyInTransaction")
public void returnMoney(@PathVariable("id") long id) {
SnackMachineDto snackMachineDto =
snackMachineRepository.findById(id).orElse(null);
SnackMachine snackMachine = snackMachineDto.convertToSnackMachine();
snackMachine.returnMoney();
snackMachineRepository.save(snackMachine.convertToSnackMachineDto());
}
@PutMapping("/{id}/{slotNumber}")
public void buySnack(@PathVariable("id") long id, @PathVariable("slotNumber")
int slotNumber) {
SnackMachineDto snackMachineDto =
snackMachineRepository.findById(id).orElse(null);
SnackMachine snackMachine = snackMachineDto.convertToSnackMachine();
snackMachine.buySnack(slotNumber);
snackMachineRepository.save(snackMachine.convertToSnackMachineDto());
}
}
![image.png](/uploads/projects/lugew@ddd/112951293f9faf655f61b7e727f7c9a4.png)