Codingan Market
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<time.h>
struct Data{
char name[21];
int harga;
int quantity;
struct Data *prev;
struct Data *next;
};
struct Data* head = NULL;
struct Data* tail = NULL;
struct Data* createNewNode(char *name, int quantity, int harga){
struct Data* node = (struct Data*)malloc(sizeof(struct Data));
strcpy(node->name, name);
node->next = NULL;
node->prev = NULL;
node->quantity = quantity;
node->harga = (rand()%295 + 5)*100;
}
void insertNode(char* name, int quantity, int harga){
struct Data* newNode = createNewNode(name, quantity, harga);
if (head == NULL && tail == NULL){
head = tail = newNode;
return;
}
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
void deleteHead(){
struct Data* temp = head;
if (head == NULL){
return;
}else if(head == tail){
head = tail = NULL;
free(temp);
}else{
head = head->next;
head->prev = NULL;
free(temp);
}
}
void deleteTail(){
struct Data* temp = tail;
if(head == NULL){
return;
}
else if(head == tail){
head = tail = NULL;
free(temp);
}else{
tail = tail->prev;
tail->next = NULL;
free(temp);
}
}
int deleteSearch(char name[]){
if (head == NULL)
return 0;
else if (strcmp(head->name,name) == 0){
deleteHead();
return 1;
}
else if (strcmp(tail->name,name) == 0){
deleteTail();
return 1;
}
else{
Data *temp = head;
while (temp){
if (strcmp(temp->name,name) == 0){
break;
}
temp = temp->next;
}
temp->next->prev = temp->prev;
temp->prev->next = temp->next;
temp->prev = temp->next = NULL;
free(temp);
return 1;
}
return 0;
}
void insertItem(){
struct Data* temp = head;
char name [31] = "";
int quantity = 0;
int harga = 0;
do{
printf("Please input item name [must contain 5-30 characters]: ");
scanf(" %[^\n]", &name);strupr(name);
}while (strlen(name) < 5 || strlen(name) > 30);
if (head != NULL && tail != NULL){
while (temp != NULL){
if (strcmp(name, temp->name) == 0){
printf("Item already existed!\n");
printf("Press any key to continue...\n");
getchar();
return;
}
temp = temp->next;
}
}
do{
printf("Please input item quantity [min 1] : ");
scanf("%d", &quantity);
}while (quantity < 1);
insertNode(name, quantity, (rand()%295 + 5)* 100);
}
void editItem(){
if (head == NULL && tail == NULL){
printf("There is no Item\n");
printf("Press any key to continue...\n");
getchar();
return;
}
char name[30] = "";
do{
printf("Please input item name [must contain 5-30 characters]: ");
scanf(" %[^\n]", &name);strupr(name);
}while (strlen(name) < 5 || strlen(name) > 30);
struct Data *temp = head;
while (temp != NULL){
if (strcmp(name, temp->name) == 0){
printf("Item found in inventory");
printf("Item name : %s\n", temp->name);
printf("Item quantity : %d\n", temp->quantity);
int quantity = 0;
do{
printf("Input new quantity [Min 1]: ");
scanf("%d", &quantity);
}while (quantity < 1);
temp->quantity = quantity;
}
temp = temp->next;
}
printf("Press any key to continue...\n");
getchar();
}
void deleteItem(){
if (head == NULL && tail == NULL){
printf("There is no Item\n");
printf("Press any key to continue...\n");
getchar();
return;
}
char name[31] = "";
do{
printf("Please input item name [must contain 5-30 characters]: ");
scanf(" %[^\n]", &name);strupr(name);
}while(strlen(name) < 5 || strlen(name) > 30);
struct Data* temp = head;
int a = deleteSearch(name);
if(a == 1){
printf("Item successfully deleted!\n");
}
else if(a == 0){
printf("There is no item in inventory!\n");
}
printf("Press any key to continue...\n");
getchar();
}
int checkout(){
char name[31] = "";
int total = 0;
if (head == NULL && tail == NULL){
printf("There is no Item\n");
printf("Press any key to continue...\n");
getchar();
return 0;
}
struct Data *temp = head;
total = temp->harga*temp->quantity;
do{
printf("Please input item name [must contain 5-30 characters]: ");
scanf(" %[^\n]", &name);strupr(name);
if (strcmp(name, temp->name) == 0){
printf("The total is = Rp%d\n", total);
return 0;
}
else{
printf("there is no item...\n");
}
}while (strlen(name) < 5 || strlen(name) > 30);
return 0;
}
void printMenu(){
printf("1. Insert Item\n");
printf("2. Edit Item\n");
printf("3. Delete Item\n");
printf("4. Sell Item\n");
printf("5. Exit\n");
printf(">> ");
}
void printCart(){
struct Data* temp = head;
printf("===========================================================\n");
printf(" Item Name |Qty| harga \n");
printf("===========================================================\n");
while (temp != NULL){
printf(" %15s | %d | %-15d \n", temp->name, temp->quantity, temp->harga);
temp = temp->next;
}
printf("===========================================================\n");
}
int main(){
srand(time(0));
int choice;
do{
printCart();
printMenu();
scanf("%d", &choice);
switch(choice){
case 1:
insertItem();
break;
case 2:
editItem();
break;
case 3:
deleteItem();
break;
case 4:
checkout();
break;
}
}while (choice != 4 && choice !=5);
return 0;
}
Tidak ada komentar:
Posting Komentar