1. 결과를 위한 주요 개념 정리
1.1 데이타 구조 설계에서 구조체로의 구현
설계에서의 자료구조
| 자료구조 | 멤버이름 | 멤버 자료형 | 설명 |
| 메뉴 정보: MenuItem |
|||
| 메뉴번호(menu_id) | int | serical 3자리 [참고] menu_id는 마지막 menu_id + 1로 증가하며, 삭제된 ID는 재사용하지 않는다. |
|
| 메뉴명(name) | 문자배열[20] | ||
| 가격(price): int | int | ||
| 판매상태(status) - | int | 1. 판매중, 2. 품절 | |
코드에서의 구조체 선언 - 구조체 멤버를 정의하고 typedef로 MenuItem을 재정의한다.
typedef struct menu_item{
int menu_id;
char name[20];
int price;
int status;
} MenuItem;
1.2. 구조체 배열을 활용하여 커피 메뉴, 디저트 메뉴 정의
구조체를 요소로 하는 배열을 정의한다.
- 키오스크에서 관리하는 메뉴 종류는 커피, 디저트 두가지 종류이다
- 따라서 커피 리스트(coffee_menu_list), 디저트 리스트(dessert_menu_list) 2개의 배열을 정의해야 한다.
- 요구사항에서 메뉴의 최대개수는 10개로 정의 했으므로, 배열의 개수는 10으로 정의한다.
- 배열의 최대개수는 조정의 여지가 있으므로 상수로 정의하고 추후 변경을 용이하게한다.
- 추후 define문의 10만 변경하면 코드 전체의 변경이 쉬워진다.
#define MAX_MENU 10
typedef struct menu_item{
int menu_id;
char name[20];
int price;
int status;
} MenuItem;
MenuItem coffee_menu_list[MAX_MENU];
MenuItem dessert_menu_list[MAX_MENU];
1.3. 메뉴 등록 / 조회 / 변경 기능을 실제 동작하도록 구현
1.3.메뉴 등록 순서 설계 및 코드
메뉴 등록 실행 순서
| 1. 사용자에게 메뉴구분 (1. 커피, 2. 디저트)을 입력받는다. 2. 사용자가 메뉴구분을 입력한다. 2.1 메뉴 구분에 따라 (커피라면) 현재 입력된 커피 개수 몇개인지 확인한다. -> 입력된 커피 개수를 저장할 변수가 필요하다. -> int coffee_count = 0; 2.2 menu_id를 생성한다. -> [요구사항] coffee 메뉴의 menu_id는 100번 부터 시작하고, dessert menu의 menu_id는 200번부터 시작한다. -> 커피와 디저트 개수가 추가될 때마다 1씩 증가시키는 next_menu_id가 커피와 디저트에 각각 필요하다. -> 변수를 정의한다. int next_coffee_id = 100; int next_dessert_id = 200; 2.3 메뉴 정보의 이름, 가격, 설명을 입력받는다. (2.2와 2.3 은 순서 조정가능) -> 현재 이용해야 할 배열의 해당 번째 요소에 구조체 멤버에 입력 받는다. : 예. coffee_menu_list[coffee_count].name 에 매뉴명을 입력받는다. scanf("%s", coffee_menu_list[coffee_count].name); -> 해당 배열에 메뉴 아이디를 저장한다. coffee_menu_list[coffee_count].menu_id = next_coffee_id; 2.4. 저장이 완료되었다. -> coffee_count와 next_coffee_id 를 1씩 증가하여 다음 메뉴 입력에서 사용한다. |
메뉴 등록 코드
#include <stdio.h>
#include "menu_item.h"
#define MAX_MENU 10
typedef struct menu_item {
int menu_id;
char name[20];
int price;
int status;
} MenuItem;
MenuItem coffee_menu_list[MAX_MENU];
MenuItem dessert_menu_list[MAX_MENU];
int coffee_count = 0;
int dessert_count = 0;
int next_coffee_id = 100;
int next_dessert_id = 200;
void register_menu_item(void)
{
int type;
printf("메뉴구분 (1. 커피, 2. 디저트): ");
scanf("%d", &type);
if (type == 1 && coffee_count < MAX_MENU)
{
coffee_menu_list[coffee_count].menu_id = next_coffee_id++;
coffee_menu_list[coffee_count].status = 1;
printf("메뉴명: ");
scanf(" %s", coffee_menu_list[coffee_count].name);
printf("가격: ");
scanf("%d", &coffee_menu_list[coffee_count].price);
coffee_count++;
}
else if (type == 2 && dessert_count < MAX_MENU)
{
dessert_menu_list[dessert_count].menu_id = next_dessert_id++;
dessert_menu_list[dessert_count].status = 1;
printf("메뉴명: ");
scanf(" %s", dessert_menu_list[dessert_count].name);
printf("가격: ");
scanf("%d", &dessert_menu_list[dessert_count].price);
dessert_count++;
}
else
{
printf("등록 실패\n");
}
}
1.3.3 메뉴 수정 순서 설계 및 코드
메뉴 수정 순서
- 메뉴 구분을 입력받는다.
- 구분에 맞는 메뉴 리스트를 출력한다.
- 메뉴 id를 선택하게 한다. - 선택한 id에 대한 배열의 인덱스를 찾는다. 이 인덱스로 해당 배열의 값을 수정해야 한다.
- 선택한 id의 수정값을 입력 받는다.
- 입력한 값으로 수정한다.
- 수정 완료
메뉴 수정 코드
void modify_menu_item(void)
{
int type;
int menu_id;
int the_index = -1;
printf("메뉴구분 (1. 커피, 2. 디저트): ");
scanf("%d", &type);
if (type == 1)
retrieve_coffee_menu_item();
else if (type == 2)
retrieve_dessert_menu_item();
else {
printf("잘못된 메뉴구분입니다.\n");
return;
}
printf("수정할 메뉴 ID: ");
scanf("%d", &menu_id);
if (type == 1) {
for (int i = 0; i < coffee_count; i++) {
if (coffee_menu_list[i].menu_id == menu_id) {
the_index = i;
break;
}
}
if (the_index == -1) {
printf("해당 메뉴를 찾을 수 없습니다.\n");
return;
}
printf("새 메뉴명: ");
scanf(" %s", coffee_menu_list[the_index].name);
printf("새 가격: ");
scanf("%d", &coffee_menu_list[the_index].price);
printf("판매상태 (1. 판매중, 0. 품절): ");
scanf("%d", &coffee_menu_list[the_index].status);
printf("메뉴 수정 완료\n");
}
else if (type == 2)
{
for (int i = 0; i < dessert_count; i++)
{
if (dessert_menu_list[i].menu_id == menu_id)
{
the_index = i;
break;
}
}
if (the_index == -1) {
printf("해당 메뉴를 찾을 수 없습니다.\n");
return;
}
printf("새 메뉴명: ");
scanf(" %s", dessert_menu_list[the_index].name);
printf("새 가격: ");
scanf("%d", &dessert_menu_list[the_index].price);
printf("판매상태 (1. 판매중, 0. 품절): ");
scanf("%d", &dessert_menu_list[the_index].status);
}
printf("메뉴 수정 완료\n");
}
1.3.4 메뉴 삭제 순서 설계 및 코드
메뉴 삭제 순서
- 메뉴 구분을 입력받는다.
- 구분에 맞는 메뉴 리스트를 출력한다.
- 메뉴 id를 선택하게 한다. - 선택한 id에 대한 배열의 인덱스를 찾는다. 이 인덱스로 해당 배열을 삭제해야 한다.
- 선택한 id에 대한 배열을 삭제하기 위해
- 다음 배열의 항목을 해당 배열로 shift 시킨다.
- 마지막 항목까지 하나씩 shift 시킨다.
- 현재 메뉴 카운드를 1 감소한다 .
- 삭제 완료
메뉴 삭제 코드
void delete_menu_item(void)
{
int type;
int menu_id;
int the_index = -1;
printf("메뉴구분 (1. 커피, 2. 디저트): ");
scanf("%d", &type);
if (type == 1)
retrieve_coffee_menu_item();
else if (type == 2)
retrieve_dessert_menu_item();
else {
printf("잘못된 메뉴구분입니다.\n");
return;
}
printf("삭제할 메뉴 ID: ");
scanf("%d", &menu_id);
if (type == 1)
{
for (int i = 0; i < coffee_count; i++)
{
if (coffee_menu_list[i].menu_id == menu_id)
{
the_index = i;
break;
}
}
if (the_index == -1) {
printf("해당 메뉴를 찾을 수 없습니다.\n");
return;
}
for (int i = the_index; i < coffee_count - 1; i++)
{
coffee_menu_list[i] = coffee_menu_list[i + 1];
}
coffee_count--;
}
else if (type == 2)
{
for (int i = 0; i < dessert_count; i++)
{
if (dessert_menu_list[i].menu_id == menu_id)
{
the_index = i;
break;
}
}
if (the_index == -1)
{
printf("해당 메뉴를 찾을 수 없습니다.\n");
return;
}
for (int i = the_index; i < dessert_count - 1; i++)
{
dessert_menu_list[i] = dessert_menu_list[i + 1];
}
dessert_count--;
}
printf("메뉴 삭제 완료\n");
}
2. 전체 코드
선택1. 통합 파일 : main.c
#include <stdio.h>
#define MAX_MENU 10
typedef struct menu_item {
int menu_id;
char name[20];
int price;
int status;
} MenuItem;
MenuItem coffee_menu_list[MAX_MENU];
MenuItem dessert_menu_list[MAX_MENU];
int coffee_count = 0;
int dessert_count = 0;
int next_coffee_id = 100;
int next_dessert_id = 200;
void retrieve_coffee_menu_item(void){
printf("\n\n====================[Coffee]======================\n");
printf("%-4s %-20s %7s %-10s\n", "No", "Name", "Price", "Status");
printf("----------------------------------------------------\n");
for (int i = 0; i < coffee_count; i++) {
printf("%-4d %-20s %7d %-10s\n",
coffee_menu_list[i].menu_id,
coffee_menu_list[i].name,
coffee_menu_list[i].price,
(coffee_menu_list[i].status) ? "AVAILABLE" : "SOLD OUT");
}
printf("\n==================================================\n");
}
void retrieve_dessert_menu_item(void){
printf("\n\n====================[Dessert]=====================\n");
printf("%-4s %-20s %7s %-10s\n", "No", "Name", "Price", "Status");
printf("----------------------------------------------------\n");
for (int i = 0; i < dessert_count; i++) {
printf("%-4d %-20s %7d %-10s\n",
dessert_menu_list[i].menu_id,
dessert_menu_list[i].name,
dessert_menu_list[i].price,
(dessert_menu_list[i].status) ? "AVAILABLE" : "SOLD OUT");
}
printf("\n==================================================\n");
}
void retrieve_menu_item(void){
retrieve_coffee_menu_item();
retrieve_dessert_menu_item();
}
void register_menu_item(void)
{
int type;
printf("메뉴구분 (1. 커피, 2. 디저트): ");
scanf("%d", &type);
if (type == 1 && coffee_count < MAX_MENU)
{
coffee_menu_list[coffee_count].menu_id = next_coffee_id++;
coffee_menu_list[coffee_count].status = 1;
printf("메뉴명: ");
scanf(" %s", coffee_menu_list[coffee_count].name);
printf("가격: ");
scanf("%d", &coffee_menu_list[coffee_count].price);
coffee_count++;
}
else if (type == 2 && dessert_count < MAX_MENU)
{
dessert_menu_list[dessert_count].menu_id = next_dessert_id++;
dessert_menu_list[dessert_count].status = 1;
printf("메뉴명: ");
scanf(" %s", dessert_menu_list[dessert_count].name);
printf("가격: ");
scanf("%d", &dessert_menu_list[dessert_count].price);
dessert_count++;
}
else
{
printf("등록 실패\n");
}
}
void modify_menu_item(void)
{
int type;
int menu_id;
int the_index = -1;
printf("메뉴구분 (1. 커피, 2. 디저트): ");
scanf("%d", &type);
if (type == 1)
retrieve_coffee_menu_item();
else if (type == 2)
retrieve_dessert_menu_item();
else {
printf("잘못된 메뉴구분입니다.\n");
return;
}
printf("수정할 메뉴 ID: ");
scanf("%d", &menu_id);
if (type == 1) {
for (int i = 0; i < coffee_count; i++) {
if (coffee_menu_list[i].menu_id == menu_id) {
the_index = i;
break;
}
}
if (the_index == -1) {
printf("해당 메뉴를 찾을 수 없습니다.\n");
return;
}
printf("새 메뉴명: ");
scanf(" %s", coffee_menu_list[the_index].name);
printf("새 가격: ");
scanf("%d", &coffee_menu_list[the_index].price);
printf("판매상태 (1. 판매중, 0. 품절): ");
scanf("%d", &coffee_menu_list[the_index].status);
printf("메뉴 수정 완료\n");
}
else if (type == 2)
{
for (int i = 0; i < dessert_count; i++)
{
if (dessert_menu_list[i].menu_id == menu_id)
{
the_index = i;
break;
}
}
if (the_index == -1) {
printf("해당 메뉴를 찾을 수 없습니다.\n");
return;
}
printf("새 메뉴명: ");
scanf(" %s", dessert_menu_list[the_index].name);
printf("새 가격: ");
scanf("%d", &dessert_menu_list[the_index].price);
printf("판매상태 (1. 판매중, 0. 품절): ");
scanf("%d", &dessert_menu_list[the_index].status);
}
printf("메뉴 수정 완료\n");
}
void delete_menu_item(void)
{
int type;
int menu_id;
int the_index = -1;
printf("메뉴구분 (1. 커피, 2. 디저트): ");
scanf("%d", &type);
if (type == 1)
retrieve_coffee_menu_item();
else if (type == 2)
retrieve_dessert_menu_item();
else {
printf("잘못된 메뉴구분입니다.\n");
return;
}
printf("삭제할 메뉴 ID: ");
scanf("%d", &menu_id);
if (type == 1)
{
for (int i = 0; i < coffee_count; i++)
{
if (coffee_menu_list[i].menu_id == menu_id)
{
the_index = i;
break;
}
}
if (the_index == -1) {
printf("해당 메뉴를 찾을 수 없습니다.\n");
return;
}
for (int i = the_index; i < coffee_count - 1; i++)
{
coffee_menu_list[i] = coffee_menu_list[i + 1];
}
coffee_count--;
}
else if (type == 2)
{
for (int i = 0; i < dessert_count; i++)
{
if (dessert_menu_list[i].menu_id == menu_id)
{
the_index = i;
break;
}
}
if (the_index == -1)
{
printf("해당 메뉴를 찾을 수 없습니다.\n");
return;
}
for (int i = the_index; i < dessert_count - 1; i++)
{
dessert_menu_list[i] = dessert_menu_list[i + 1];
}
dessert_count--;
}
printf("메뉴 삭제 완료\n");
}
void order(void)
{
printf("[주문하기 기능 호출]\n");
}
void search_order(void)
{
printf("[주문조회 기능 호출]\n");
}
void print_main_menu(void)
{
printf("=========================================\n");
printf(" SSU 카페 키오스크\n");
printf("=========================================\n");
printf("메뉴관리(관리자용) 주문관리(고객용)\n");
printf("11. 메뉴 등록 21. 주문하기\n");
printf("12. 메뉴 변경 22. 주문조회\n");
printf("13. 메뉴 삭제\n");
printf("14. 메뉴 조회\n");
printf("0. 종료\n");
printf("=========================================\n");
}
void run(void)
{
int menu;
while (1)
{
print_main_menu();
printf("메뉴 번호를 입력하세요 (0: 종료): ");
scanf("%d", &menu);
switch (menu)
{
case 0:
printf("프로그램 종료\n");
return;
case 11:
register_menu_item();
break;
case 12:
modify_menu_item();
break;
case 13:
delete_menu_item();
break;
case 14:
retrieve_menu_item();
break;
case 21:
order();
break;
case 22:
search_order();
break;
default:
printf("잘못된 입력\n");
}
printf("\n");
}
}
int main(void)
{
run();
return 0;
}
선택2. 분할 파일: main.c, kiosk_ui.c, kiosk_ui.h, menu_item.c, menu_item.h
main.c
#include "kiosk_ui.h"
int main(void)
{
run();
return 0;
}
kiosk_ui.h
#ifndef KIOSK_UI_H
#define KIOSK_UI_H
void run(void);
#endif
kiosk_ui.c
#include <stdio.h>
#include "kiosk_ui.h"
#include "menu_item.h"
void order(void)
{
printf("[주문하기 기능 호출]\n");
}
void search_order(void)
{
printf("[주문조회 기능 호출]\n");
}
void print_main_menu(void)
{
printf("=========================================\n");
printf(" SSU 카페 키오스크\n");
printf("=========================================\n");
printf("메뉴관리(관리자용) 주문관리(고객용)\n");
printf("11. 메뉴 등록 21. 주문하기\n");
printf("12. 메뉴 변경 22. 주문조회\n");
printf("13. 메뉴 삭제\n");
printf("14. 메뉴 조회\n");
printf("0. 종료\n");
printf("=========================================\n");
}
void run(void)
{
int menu;
while (1)
{
print_main_menu();
printf("메뉴 번호를 입력하세요 (0: 종료): ");
scanf("%d", &menu);
switch (menu)
{
case 0:
printf("프로그램 종료\n");
return;
case 11:
register_menu_item();
break;
case 12:
modify_menu_item();
break;
case 13:
delete_menu_item();
break;
case 14:
retrieve_menu_item();
break;
case 21:
order();
break;
case 22:
search_order();
break;
default:
printf("잘못된 입력\n");
}
printf("\n");
}
}
menu_item.h
#ifndef MENU_ITEM_H
#define MENU_ITEM_H
void register_menu_item(void);
void modify_menu_item(void);
void delete_menu_item(void);
void retrieve_menu_item(void);
#endif
menu_item.c
#include <stdio.h>
#include "menu_item.h"
#define MAX_MENU 10
typedef struct menu_item {
int menu_id;
char name[20];
int price;
int status;
} MenuItem;
MenuItem coffee_menu_list[MAX_MENU];
MenuItem dessert_menu_list[MAX_MENU];
int coffee_count = 0;
int dessert_count = 0;
int next_coffee_id = 100;
int next_dessert_id = 200;
void retrieve_coffee_menu_item(void){
printf("\n\n====================[Coffee]======================\n");
printf("%-4s %-20s %7s %-10s\n", "No", "Name", "Price", "Status");
printf("----------------------------------------------------\n");
for (int i = 0; i < coffee_count; i++) {
printf("%-4d %-20s %7d %-10s\n",
coffee_menu_list[i].menu_id,
coffee_menu_list[i].name,
coffee_menu_list[i].price,
(coffee_menu_list[i].status) ? "AVAILABLE" : "SOLD OUT");
}
printf("\n==================================================\n");
}
void retrieve_dessert_menu_item(void){
printf("\n\n====================[Dessert]=====================\n");
printf("%-4s %-20s %7s %-10s\n", "No", "Name", "Price", "Status");
printf("----------------------------------------------------\n");
for (int i = 0; i < dessert_count; i++) {
printf("%-4d %-20s %7d %-10s\n",
dessert_menu_list[i].menu_id,
dessert_menu_list[i].name,
dessert_menu_list[i].price,
(dessert_menu_list[i].status) ? "AVAILABLE" : "SOLD OUT");
}
printf("\n==================================================\n");
}
void retrieve_menu_item(void){
retrieve_coffee_menu_item();
retrieve_dessert_menu_item();
}
void register_menu_item(void)
{
int type;
printf("메뉴구분 (1. 커피, 2. 디저트): ");
scanf("%d", &type);
if (type == 1 && coffee_count < MAX_MENU)
{
coffee_menu_list[coffee_count].menu_id = next_coffee_id++;
coffee_menu_list[coffee_count].status = 1;
printf("메뉴명: ");
scanf(" %s", coffee_menu_list[coffee_count].name);
printf("가격: ");
scanf("%d", &coffee_menu_list[coffee_count].price);
coffee_count++;
}
else if (type == 2 && dessert_count < MAX_MENU)
{
dessert_menu_list[dessert_count].menu_id = next_dessert_id++;
dessert_menu_list[dessert_count].status = 1;
printf("메뉴명: ");
scanf(" %s", dessert_menu_list[dessert_count].name);
printf("가격: ");
scanf("%d", &dessert_menu_list[dessert_count].price);
dessert_count++;
}
else
{
printf("등록 실패\n");
}
}
void modify_menu_item(void)
{
int type;
int menu_id;
int the_index = -1;
printf("메뉴구분 (1. 커피, 2. 디저트): ");
scanf("%d", &type);
if (type == 1)
retrieve_coffee_menu_item();
else if (type == 2)
retrieve_dessert_menu_item();
else {
printf("잘못된 메뉴구분입니다.\n");
return;
}
printf("수정할 메뉴 ID: ");
scanf("%d", &menu_id);
if (type == 1) {
for (int i = 0; i < coffee_count; i++) {
if (coffee_menu_list[i].menu_id == menu_id) {
the_index = i;
break;
}
}
if (the_index == -1) {
printf("해당 메뉴를 찾을 수 없습니다.\n");
return;
}
printf("새 메뉴명: ");
scanf(" %s", coffee_menu_list[the_index].name);
printf("새 가격: ");
scanf("%d", &coffee_menu_list[the_index].price);
printf("판매상태 (1. 판매중, 0. 품절): ");
scanf("%d", &coffee_menu_list[the_index].status);
printf("메뉴 수정 완료\n");
}
else if (type == 2)
{
for (int i = 0; i < dessert_count; i++)
{
if (dessert_menu_list[i].menu_id == menu_id)
{
the_index = i;
break;
}
}
if (the_index == -1) {
printf("해당 메뉴를 찾을 수 없습니다.\n");
return;
}
printf("새 메뉴명: ");
scanf(" %s", dessert_menu_list[the_index].name);
printf("새 가격: ");
scanf("%d", &dessert_menu_list[the_index].price);
printf("판매상태 (1. 판매중, 0. 품절): ");
scanf("%d", &dessert_menu_list[the_index].status);
}
printf("메뉴 수정 완료\n");
}
void delete_menu_item(void)
{
int type;
int menu_id;
int the_index = -1;
printf("메뉴구분 (1. 커피, 2. 디저트): ");
scanf("%d", &type);
if (type == 1)
retrieve_coffee_menu_item();
else if (type == 2)
retrieve_dessert_menu_item();
else {
printf("잘못된 메뉴구분입니다.\n");
return;
}
printf("삭제할 메뉴 ID: ");
scanf("%d", &menu_id);
if (type == 1)
{
for (int i = 0; i < coffee_count; i++)
{
if (coffee_menu_list[i].menu_id == menu_id)
{
the_index = i;
break;
}
}
if (the_index == -1) {
printf("해당 메뉴를 찾을 수 없습니다.\n");
return;
}
for (int i = the_index; i < coffee_count - 1; i++)
{
coffee_menu_list[i] = coffee_menu_list[i + 1];
}
coffee_count--;
}
else if (type == 2)
{
for (int i = 0; i < dessert_count; i++)
{
if (dessert_menu_list[i].menu_id == menu_id)
{
the_index = i;
break;
}
}
if (the_index == -1)
{
printf("해당 메뉴를 찾을 수 없습니다.\n");
return;
}
for (int i = the_index; i < dessert_count - 1; i++)
{
dessert_menu_list[i] = dessert_menu_list[i + 1];
}
dessert_count--;
}
printf("메뉴 삭제 완료\n");
}
'[1] 프로그래밍1 및 실습(C언어) > [과제] 콘솔 키오스크' 카테고리의 다른 글
| [콘솔키오스크] 과제 4단계. 주문관리-재고(선택) 및 파일 분할 [5/21] (0) | 2026.05.17 |
|---|---|
| [콘솔키오스크] 과제 3단계. 주문 관리 기능 구현 [5/14] (0) | 2026.05.13 |
| [콘솔키오스크] 과제 2단계. 메뉴 관리 기능 구현 [5/7] (0) | 2026.05.07 |
| [콘솔키오스크] 과제 1단계. Intro - 키오스크 과제[4/30] - 결과 예시 (0) | 2026.05.03 |
| [콘솔키오스크] 과제 1단계. Intro - 키오스크 과제 [4/30] (0) | 2026.04.29 |














