Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int count = 1;
struct data{
char nama[100];
char type[25];
int qty;
int price;
data *next,*prev;
}*head,*tail,*current;
void pushTail(char nama[],char type[],int qty,int price)
{
current = (data*)malloc(sizeof(struct data));
strcpy(current->nama,nama);
strcpy(current->type,type);
current->qty = qty;
current->price = price;
current->next=current->prev=NULL;
if(head==NULL)
{
head=tail=current;
}
else
{
current->prev=tail;
tail->next=current;
tail=current;
}
}
void popHead(){
current=head;
if(head==NULL){
printf("No data");
}else if(head==tail){
head=tail=NULL;
free(current);
}else{
head=head->next;
head->prev=NULL;
free(current);
}
}
void popAll(){
while(head!=NULL){
popHead();
}
}
int jumlah()
{
int jumlah = 0;
current=head;
while(current!=NULL)
{
jumlah++;
current=current->next;
}
return jumlah;
}
int stokBarang(int nilai)
{
int hitung = nilai-1;
int cek = head->qty;
current = head;
while(current!=NULL && hitung!=0)
{
current = current->next;
cek = current->qty;
hitung--;
}
return cek;
}
void popMid(int nilai)
{
int count = nilai-1;
int hapus = head->price;
current=head;
while(current!=NULL && count!=0)
{
current = current->next;
hapus = current->price;
count--;
}
if(head==tail)
{
head=tail=NULL;
free(current);
}
else{
if(hapus==head->price)
{
head=head->next;
head->prev=NULL;
free(current);
}
else if(hapus==tail->price)
{
current=tail;
tail=tail->prev;
tail->next=NULL;
free(current);
}
else
{
current->prev->next=current->next;
current->next->prev=current->prev;
free(current);
}
}
}
void view()
{
current=head;
int i = 0;
if(current==NULL)
{
printf("No Data\n");
}
else
{
printf("===============================================================================");
printf(" +++++ Brew Coffee Shop +++++ ");
printf("Welcome, Mr./Mrs. Admin\n\n\n");
printf("=============================================================================== ");
printf("No.| %-20s | %-10s | %-12s | %-20s |\n","Menu","Type","Quantity","Price");
printf("-------------------------------------------------------------------------------");
printf("\n");
while(current!=NULL)
{
printf("%-3d| %-20s | %10s | %12d | %10d |\n",i+1,current->nama,current->type,current->qty,current->price);
current=current->next;
i++;
}
printf("-------------------------------------------------------------------------------");
}
}
void viewOrder()
{
}
void viewCustomer(char name[])
{
current=head;
int i = 0;
if(current==NULL)
{
printf("No Data\n");
}
else
{
printf("===============================================================================");
printf(" +++++ Brew Coffee Shop +++++ ");
printf("Welcome, Mr./Mrs. %s\n\n\n",name);
printf("=============================================================================== ");
printf("No.| %-20s | %-10s | %-12s | %-20s |\n","Menu","Type","Quantity","Price");
printf("-------------------------------------------------------------------------------");
printf("\n");
while(current!=NULL)
{
printf("%-3d| %-20s | %10s | %12d | %10d |\n",i+1,current->nama,current->type,current->qty,current->price);
current=current->next;
i++;
}
printf("-------------------------------------------------------------------------------");
}
}
int main()
{
int banyak= jumlah();
int choose;
int angka;
char password[15];
char nama[100];
char type[10];
int qty;
int jumlah1;
int price;
char customerName[25];
int pilihanCustomer;
int jumlahOrder;
int banyakBarang;
int pilihan;
do{
printf("================================================================================\n");
printf(" +++++ Brew Coffee Shop +++++ \n");
printf("================================================================================\n");
printf("1. Login As Admin\n");
printf("2. Login As Customer\n");
printf("3. Exit\n");
printf("\n");
printf("Input Your Choice : ");
scanf("%d",&pilihan);fflush(stdin);
switch(pilihan)
{
case 1:
printf("Input Password: ");
scanf("%s",&password);fflush(stdin);
if(strcmp(password,"12345678")!=0)
{
printf("Password Salah\n");
getchar();
system("cls");
}
if(strcmp(password,"12345678")==0)
{
system("cls");
int pilih;
do{
if(count==1)
{
pushTail("Americano","Coffee",24,19000);
pushTail("Chocolate Baked","Non-coffee",15,24000);
pushTail("Alberto","Coffee",7,44000);
count++;
}
system("cls");
view();
printf(" 1. Add item\n");
printf("2. Remove item\n");
printf("3. Exit\n");
printf("Enter your action: ");
scanf("%d",&pilih);fflush(stdin);
switch(pilih)
{
case 1:
do{
printf("Insert item name: ");
scanf("%[^\n]",&nama);fflush(stdin);
}while(strlen(nama) < 5 || strlen(nama) > 20);
do{
printf("Type of Coffee[yes/no]: ");
scanf("%s",&type);fflush(stdin);fflush(stdin);
}while(strcmp(type,"yes")!=0 && strcmp(type,"no")!=0);
do{
printf("Insert the quantity of the item: ");
scanf("%d",&qty);fflush(stdin);
}while(qty < 1 || qty > 100);
do{
printf("Insert the price of the item: ");
scanf("%d",&price);fflush(stdin);
}while(price < 10000 || price > 200000);
if(strcmp(type,"yes")==0)
{
pushTail(nama,"Coffee",qty,price);
}
else
{
pushTail(nama,"Non-coffee",qty,price);
}
break;
case 2:
jumlah1 = jumlah();
do{
printf("Choose Number to Delete [1 - %d]: ",jumlah1);
scanf("%d",&angka);fflush(stdin);
}while(angka < 1 || angka > jumlah1);
popMid(angka);
break;
case 3:
system("cls");
return main();
break;
}
}while(pilih!=3 || pilih < 1 || pilih > 3);
}
break;
case 2:
printf("Input your Name : ");
scanf("%[^\n]",&customerName);fflush(stdin);
system("cls");
if(count==1)
{
pushTail("Americano","Coffee",24,19000);
pushTail("Chocolate Baked","Non-coffee",15,24000);
pushTail("Alberto","Coffee",7,44000);
count++;
}
do{
system("cls");
viewCustomer(customerName);
printf(" 1. Buy item\n");
printf("2. Cancel item\n");
printf("3. Exit\n");
printf("Enter your action: ");
scanf("%d",&pilihanCustomer);fflush(stdin);
switch(pilihanCustomer)
{
case 1:
jumlah1 = jumlah();
do{
printf("choose your menu [1 - %d]: ",jumlah1);
scanf("%d",&angka);fflush(stdin);
}while(angka < 1 || angka > jumlah1);
banyakBarang = stokBarang(angka);
do{
printf("enter the quantity [1 - %d]: ",banyakBarang);
scanf("%d",&jumlahOrder);fflush(stdin);
}while(jumlahOrder < 1 || jumlahOrder > banyakBarang);
getchar();
break;
case 2:
break;
case 3:
system("cls");
return main();
break;
}
}while(pilihanCustomer!=3 || pilihanCustomer ==1 || pilihanCustomer==2);
break;
}
}while(pilihan < 1 || pilihan >3 || pilihan ==1 || pilihan==2 || pilihan !=3);
}
Any Idea would be so much help for me, thank you