#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node{
char productName[100];
int qty;
int price;
struct node *prev, *next;
}*head = NULL, *tail = NULL;
void pushData(char productName[], int qty, int price){
struct node *newNode = (struct node*)malloc(sizeof(struct node));
//prepare newNode
strcpy(newNode->productName, productName);
newNode->qty = qty;
newNode->price = price;
newNode->next=NULL;
newNode->prev=NULL;
if(head==NULL){
head=tail=newNode;
}
else if(strcmp(productName, head->productName)<0){
newNode->next=head;
head->prev=newNode;
head=newNode;
}
else if(strcmp(productName, tail->productName)>0){
newNode->prev=tail;
tail->next=newNode;
tail=newNode;
}
else
{
struct node *ptr = head;
while(ptr!=NULL){
if(strcmp(productName,ptr->productName)<0){
newNode->next=ptr;
newNode->prev=ptr->prev;
ptr->prev->next=newNode;
ptr->prev=newNode;
break;
}
ptr = ptr->next;
}
}
}
void editData(char productName[], int qty, int price){
struct node *find = head->next;
if(strcmp(productName,head->productName)==0){
head->qty=qty;
head->price=price;
}
else
{
while(find!=NULL){
if(strcmp(productName,find->productName)==0){
find->qty=qty;
find->price=price;
break;
}
find=find->next;
}
}
}
void deleteData(char productName[]){
struct node *del = head->next;
if(head==tail)
{
if(strcmp(productName,head->productName)==0){
head=tail=NULL;
}
}
else if(strcmp(productName,head->productName)==0){
struct node *ptr = head;
head=head->next;
head->prev=NULL;
free(ptr);
}
else if(strcmp(productName,tail->productName)==0){
struct node *ptr2 = tail;
tail=tail->prev;
tail->next=NULL;
free(ptr2);
}
else{
while(del!=NULL){
if(strcmp(productName,del->productName)==0){
del->prev->next=del->next;
del->next->prev=del->prev;
del->next=NULL;
del->prev=NULL;
free(del);
break;
}
del=del->next;
}
}
}
void printData(){
struct node *ptr = head;
if(ptr==NULL){
printf("No product...\n");
}
else{
printf("| Product | QTY | Price |\n");
printf("=========================\n");
while(ptr!=NULL){
printf("| %s | %d | %d |\n", ptr->productName, ptr->qty, ptr->price);
ptr = ptr->next;
}
}
}
int main(){
int menu, qty, price, newQty, newPrice;
int up = 100000;
int low = 10000;
char productName[100], productFind[100], productDel[100];
do{
system("cls");
printf("=====================\n");
printf("TOKO KOH MORENO JAYA\n");
printf("=====================\n\n");
printData();
printf("\n\n=================");
printf("\n------MENU------\n");
printf("=================\n");
printf("1. Add product\n");
printf("2. Edit product quantity\n");
printf("3. Delete product\n");
printf("4. Checkout\n");
printf("Choose >> ");
scanf("%d", &menu); getchar();
switch(menu)
{
case 1:
printf("Input product name: ");
scanf("%[^\n]", &productName); getchar();
printf("Input product quantity: ");
scanf("%d", &qty); getchar();
price = (rand()%(up-low+1))+low;
pushData(productName, qty, price);
printf("Data Succesfully Added!");
break;
case 2:
printf("Select a product to change: ");
scanf("%[^\n]", &productFind); getchar();
printf("Input new quantity: ");
scanf("%d", &newQty); getchar();
newPrice = (rand()%(up-low+1))+low;
editData(productFind, newQty, newPrice);
printf("Data has been updated!");
break;
case 3:
printf("Select a product to remove: ");
scanf("%[^\n]", &productDel);
deleteData(productDel);
printf("Data has been removed");
break;
}
}while(menu!=4);
printf("Kindness is free ;)\n");
return 0;
}