用html语言写一个餐厅营业的可视化代码
时间: 2023-12-14 22:03:13 浏览: 31
以下是一个简单的餐厅营业可视化代码,包括菜单和订单管理:
```html
<!DOCTYPE html>
<html>
<head>
<title>餐厅营业可视化</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
header {
background-color: #333;
color: #fff;
padding: 20px;
text-align: center;
}
section#menu {
display: flex;
flex-wrap: wrap;
justify-content: center;
padding: 20px;
}
section#menu div {
background-color: #f2f2f2;
border: 1px solid #ccc;
border-radius: 5px;
box-shadow: 2px 2px 5px #ccc;
margin: 10px;
padding: 10px;
width: 200px;
}
section#cart {
background-color: #f2f2f2;
border: 1px solid #ccc;
border-radius: 5px;
box-shadow: 2px 2px 5px #ccc;
margin: 20px auto;
padding: 20px;
max-width: 600px;
}
table {
border-collapse: collapse;
margin: 10px 0;
width: 100%;
}
table th,
table td {
border: 1px solid #ccc;
padding: 10px;
text-align: center;
}
table th {
background-color: #333;
color: #fff;
}
button {
background-color: #333;
border: none;
border-radius: 5px;
color: #fff;
cursor: pointer;
padding: 10px 20px;
transition: background-color 0.3s ease;
}
button:hover {
background-color: #666;
}
</style>
</head>
<body>
<header>
<h1>餐厅营业可视化</h1>
</header>
<section id="menu">
<div>
<h2>菜品1</h2>
<p>价格:¥28</p>
<button onclick="addToCart('菜品1', 28)">加入购物车</button>
</div>
<div>
<h2>菜品2</h2>
<p>价格:¥35</p>
<button onclick="addToCart('菜品2', 35)">加入购物车</button>
</div>
<div>
<h2>菜品3</h2>
<p>价格:¥42</p>
<button onclick="addToCart('菜品3', 42)">加入购物车</button>
</div>
<div>
<h2>菜品4</h2>
<p>价格:¥25</p>
<button onclick="addToCart('菜品4', 25)">加入购物车</button>
</div>
</section>
<section id="cart">
<h2>购物车</h2>
<table>
<thead>
<tr>
<th>菜品</th>
<th>价格</th>
<th>数量</th>
<th>小计</th>
<th></th>
</tr>
</thead>
<tbody id="cartItems">
</tbody>
<tfoot>
<tr>
<td colspan="3">总计</td>
<td id="cartTotal">¥0</td>
<td></td>
</tr>
</tfoot>
</table>
<button onclick="submitOrder()">提交订单</button>
</section>
<script>
var cartItems = [];
function addToCart(item, price) {
var found = false;
cartItems.forEach(function(cartItem) {
if (cartItem.item === item) {
cartItem.quantity++;
found = true;
}
});
if (!found) {
cartItems.push({item: item, price: price, quantity: 1});
}
updateCart();
}
function removeFromCart(index) {
cartItems.splice(index, 1);
updateCart();
}
function updateCart() {
var cartTotal = 0;
var cartItemsHtml = '';
cartItems.forEach(function(cartItem, index) {
var subtotal = cartItem.price * cartItem.quantity;
cartTotal += subtotal;
cartItemsHtml += '<tr>';
cartItemsHtml += '<td>' + cartItem.item + '</td>';
cartItemsHtml += '<td>¥' + cartItem.price + '</td>';
cartItemsHtml += '<td>' + cartItem.quantity + '</td>';
cartItemsHtml += '<td>¥' + subtotal + '</td>';
cartItemsHtml += '<td><button onclick="removeFromCart(' + index + ')">删除</button></td>';
cartItemsHtml += '</tr>';
});
document.getElementById('cartItems').innerHTML = cartItemsHtml;
document.getElementById('cartTotal').innerHTML = '¥' + cartTotal;
}
function submitOrder() {
if (cartItems.length > 0) {
var confirmMessage = '您的订单包括:\n';
cartItems.forEach(function(cartItem) {
confirmMessage += cartItem.item + ' × ' + cartItem.quantity + '\n';
});
confirmMessage += '总计:¥' + document.getElementById('cartTotal').innerHTML;
if (confirm(confirmMessage)) {
cartItems = [];
updateCart();
}
} else {
alert('请先选择菜品');
}
}
</script>
</body>
</html>
```
该代码包括一个顶部标题,一个菜单区域和一个购物车区域。菜单区域包括几个菜品,每个菜品有一个名称、价格和一个“加入购物车”按钮。购物车区域显示已经加入购物车的菜品,包括名称、价格、数量、小计和一个“删除”按钮。底部显示购物车总计和一个“提交订单”按钮。用户可以通过点击菜单区域的“加入购物车”按钮将菜品加入购物车,然后在购物车区域编辑、删除或提交订单。