使用C语言帮我写一个奶茶店自动管理的程序
时间: 2023-06-26 20:08:40 浏览: 182
好的,下面是一个简单的奶茶店自动管理程序的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100
typedef struct {
int id;
char name[20];
double price;
int stock;
} Item;
typedef struct {
Item items[MAX_SIZE];
int count;
} Menu;
void init_menu(Menu *menu);
void show_menu(Menu *menu);
void add_item(Menu *menu);
void remove_item(Menu *menu);
void update_stock(Menu *menu);
void place_order(Menu *menu);
int main() {
Menu menu;
init_menu(&menu);
int choice;
do {
printf("\n====== Welcome to Boba Shop ======\n");
printf("1. Show menu\n");
printf("2. Add item\n");
printf("3. Remove item\n");
printf("4. Update stock\n");
printf("5. Place order\n");
printf("6. Exit\n");
printf("Please enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
show_menu(&menu);
break;
case 2:
add_item(&menu);
break;
case 3:
remove_item(&menu);
break;
case 4:
update_stock(&menu);
break;
case 5:
place_order(&menu);
break;
case 6:
printf("Thanks for using Boba Shop!\n");
break;
default:
printf("Invalid choice!\n");
}
} while (choice != 6);
return 0;
}
void init_menu(Menu *menu) {
menu->count = 3;
menu->items[0].id = 1;
strcpy(menu->items[0].name, "Classic Milk Tea");
menu->items[0].price = 3.5;
menu->items[0].stock = 10;
menu->items[1].id = 2;
strcpy(menu->items[1].name, "Brown Sugar Boba Milk");
menu->items[1].price = 4.5;
menu->items[1].stock = 8;
menu->items[2].id = 3;
strcpy(menu->items[2].name, "Taro Milk Tea");
menu->items[2].price = 4.0;
menu->items[2].stock = 5;
}
void show_menu(Menu *menu) {
printf("\n====== Menu ======\n");
printf("%-4s%-20s%-10s%-10s\n", "ID", "Name", "Price", "Stock");
for (int i = 0; i < menu->count; i++) {
printf("%-4d%-20s%-10.2f%-10d\n", menu->items[i].id, menu->items[i].name, menu->items[i].price, menu->items[i].stock);
}
}
void add_item(Menu *menu) {
if (menu->count == MAX_SIZE) {
printf("Menu is full!\n");
return;
}
Item item;
printf("Please enter item name: ");
scanf("%s", item.name);
printf("Please enter item price: ");
scanf("%lf", &item.price);
printf("Please enter item stock: ");
scanf("%d", &item.stock);
item.id = menu->items[menu->count - 1].id + 1;
menu->items[menu->count++] = item;
printf("Item added successfully!\n");
}
void remove_item(Menu *menu) {
int id;
printf("Please enter item ID: ");
scanf("%d", &id);
int index = -1;
for (int i = 0; i < menu->count; i++) {
if (menu->items[i].id == id) {
index = i;
break;
}
}
if (index == -1) {
printf("Item not found!\n");
return;
}
for (int i = index; i < menu->count - 1; i++) {
menu->items[i] = menu->items[i + 1];
}
menu->count--;
printf("Item removed successfully!\n");
}
void update_stock(Menu *menu) {
int id, stock;
printf("Please enter item ID: ");
scanf("%d", &id);
int index = -1;
for (int i = 0; i < menu->count; i++) {
if (menu->items[i].id == id) {
index = i;
break;
}
}
if (index == -1) {
printf("Item not found!\n");
return;
}
printf("Please enter new stock: ");
scanf("%d", &stock);
menu->items[index].stock = stock;
printf("Stock updated successfully!\n");
}
void place_order(Menu *menu) {
double total = 0.0;
int id, quantity;
printf("\n====== Place Order ======\n");
do {
printf("Please enter item ID (0 to end): ");
scanf("%d", &id);
if (id == 0) {
break;
}
int index = -1;
for (int i = 0; i < menu->count; i++) {
if (menu->items[i].id == id) {
index = i;
break;
}
}
if (index == -1) {
printf("Item not found!\n");
continue;
}
printf("Please enter quantity: ");
scanf("%d", &quantity);
if (quantity > menu->items[index].stock) {
printf("Insufficient stock!\n");
continue;
}
total += menu->items[index].price * quantity;
menu->items[index].stock -= quantity;
} while (1);
printf("Total: %.2f\n", total);
}
```
这个程序的主要功能包括:
1. 显示菜单
2. 添加菜单项
3. 删除菜单项
4. 更新库存
5. 下订单
你可以根据自己的实际需求进行修改和扩展。
阅读全文