#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#include<time.h>
char name[100];
char namee[100];
int quantity=0;
int price=0;
int newStock=0;
void enter();
void menu();
void print();
void space();
struct barang{
char name[100];
int quantity;
int price;
struct barang *next, *prev;
}*head=NULL, *tail=NULL, *curr;
void space(){
printf("\n\n\n\n\n\n\n\n\n\n\n");
}
void pushData(char name[], int quantity, int price){
struct barang *temp = (struct barang*)malloc(sizeof(struct barang));
strcpy(temp->name, name);
temp->quantity = quantity;
temp->price = price;
temp->next = NULL;
temp->prev = NULL;
if(head==NULL){
head=tail=temp;
}
else{
tail->next = temp;
temp->prev = tail;
tail=temp;
}
}
void enter(){
printf("Press Enter to continue...");
getchar();
menu();
}
void checkoutItem(){
struct barang *curr = (struct barang*)malloc(sizeof(struct barang));
if(head==NULL){
printf("There is no data\n");
enter();
}
else{
barang *curr=head;
int count=1;
while(curr!=NULL){
printf("%-2d. %-20s %-6d Rp. %-6d\n", count, curr->name, curr->quantity, curr->price);
price += curr->price*curr->quantity;
curr=curr->next;
count++;
}
printf("\nTotal price: Rp.%d\n", price);
printf("\nKindness is free\n");
space();
}
}
void deleteData(char name[100]){
struct barang *curr = (struct barang*)malloc(sizeof(struct barang));
if(head==tail){
free(head);
head=tail=NULL;
}
else if(strcmp(name, head->name)==0){
curr=head;
head=head->next;
free(curr);
}
else if(strcmp(name,tail->name)==0){
curr=tail;
tail->next;
}
else{
curr=head;
while(curr!=NULL){
if(strcmp(curr->name, name)==0){
break;
}
curr=curr->next;
}
curr->next->prev=curr->prev;
curr->prev->next=curr->next;
curr->next=NULL;
curr->prev=NULL;
free(curr);
}
}
void deleteItem(){
struct barang *curr = (struct barang*)malloc(sizeof(struct barang));
if(head==NULL){
printf("There is no data\n");
enter();
}
else{
print();
do{
printf("Input item name[3-20]");
scanf("%[^\n]", namee);
}
while(strlen(namee)<3 || strlen(namee)>20);
deleteData(namee);
}
enter();
}
void addItem(){
srand(time(NULL));
do{
printf("Input item name[3-20]: ");
scanf("%[^\n]", name);
getchar();
}
while(strlen(name)<3 || strlen(name)>20);
do{
printf("Input item stock: ");
scanf("%d", &quantity);
}
while(quantity<1);
price = (rand() % ((20000+1)-5000)) + 20000;
pushData(name, quantity, price);
enter();
}
void print(){
struct barang *curr = (struct barang*)malloc(sizeof(struct barang));
curr=head;
int count=1;
while(curr!=NULL){
printf("%-2d. %-20s %-6d Rp. %-6d\n", count, curr->name, curr->quantity, curr->price);
curr=curr->next;
count++;
}
space();
}
void editItem(){
char namee[100];
if(head==NULL){
printf("There is no data");
enter();
}
else{
barang *temp = head;
print();
printf("\nInput item name[3-20]: ");
scanf("%[^\n]", namee);
getchar();
while(temp!=NULL){
if(strcmp(namee, temp->name)==0){
printf("\nInput new item quantity: ");
scanf("%d", &newStock);
temp->quantity = newStock;
}
temp = temp->next;
}
}
enter();
}
void menu(){
system("cls");
int choose;
do{
printf("~~~Drummers Market~~~\n");
printf("1. Add Item\n");
printf("2. Edit Item\n");
printf("3. Delete Item\n");
printf("4. Checkout Item\n");
printf(" >> ");
scanf("%d", &choose);
getchar();
if(choose==1){
system("cls");
addItem();
}
else if(choose==2){
system("cls");
editItem();
}
else if(choose==3){
system("cls");
deleteItem();
}
else if(choose==4){
system("cls");
checkoutItem();
}
}
while(choose!=5);
}
int main(){
menu();
return 0;
}
No comments:
Post a Comment