Monday, April 6, 2020

Dreammers Market Console Application

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Node
{
char name[30];
int qty;
int price;
Node* next;
Node* prev;
};

__forceinline void pause() { system("pause"); }
__forceinline void cls()   { system("cls");   }

Node* head = NULL;
Node* tail = NULL;

Node* createNode(char* name, int qty)
{
Node* node = (Node*)malloc(sizeof(Node));
strcpy(node->name, name);
node->qty = qty;
node->next = node->prev = NULL;
return node;
}

Node* find(char* name)
{
if (head)
{
Node* cur = head;
while (cur)
{
if (strcmp(cur->name, name) == 0)
return cur;
cur = cur->next;
}
}
return NULL;
}

void pushPQ(char* name, int qty)
{
Node* cur = createNode(name, qty);
cur->price = rand() % 195000 + 5000;

if (head == NULL)
{
head = tail = cur;
}
else
{
Node* temp = head;
while (temp->next)
{
if (strcmp(temp->next->name, cur->name) > 0)
break;
temp = temp->next;
}

if (temp == tail)
{
cur->prev = temp;
cur->prev->next = cur;
tail = cur;
}
else
{
cur->prev = temp;
cur->next = temp->next;
cur->next->prev = cur;
cur->prev->next = cur;
}
}
}

void pop(Node* node)
{
if (node == NULL)
return;

if (node == head)
{
head = node->next;
if (head)
head->prev = NULL;
else
head = tail = NULL;
}
else if (node == tail)
{
tail = node->prev;
if (tail)
tail->next = NULL;
else
head = tail = NULL;
}
else
{
node->prev->next = node->next;
node->next->prev = node->prev;
}

free(node);
}

void addItem()
{
char name[30];
int qty = -1;

do
{
printf("Input item name : ");
scanf("%[^\n]%*c", name);
} while (*name == '\0');

if (find(name))
{
puts("\nItem already exist!");
pause();
return;
}

do
{
printf("Input item quantity : ");
scanf("%d%*c", &qty);
} while (qty == -1);

pushPQ(name, qty);
}

void viewItems()
{
if (head == NULL)
{
puts("Your cart is empty");
}
else
{
Node* cur = head;
int num = 1;

puts("==============================================================");
puts("|                       Shopping Cart                        |");
puts("==============================================================");
printf("| %3s | %-30s | %-6s | %-10s |\n", "No.", "Item name", "Qty", "Price");
puts("--------------------------------------------------------------");

while (cur)
{
printf("| %3d | %-30s | %6d | Rp. %6d |\n", num, cur->name, cur->qty, cur->price);
cur = cur->next;
++num;
}
puts("==============================================================\n");
}
pause();
}

void changeItemQuantity()
{
char name[30];
int qty = -1;

do
{
printf("Input item name : ");
scanf("%[^\n]%*c", name);
} while (*name == '\0');

Node* temp = find(name);

if (temp == NULL)
{
puts("\nThere is no such item in cart.");
}
else
{
printf("\nItem Name: %s\n", temp->name);
printf("Quantity : %d\n", temp->qty);
printf("Price    : %d\n\n", temp->price);

do
{
printf("Input new item quantity : ");
scanf("%d%*c", &qty);
} while (qty == -1);

temp->qty = qty;
puts("\nSuccesfully updated item quantity.");
}
pause();
}

void removeItem()
{
char name[30];
int qty = -1;

do
{
printf("Input item name : ");
scanf("%[^\n]%*c", name);
} while (*name == '\0');

Node* temp = find(name);

if (temp == NULL)
{
puts("\nThere is no such item in cart.");
}
else
{
printf("\nItem Name: %s\n", temp->name);
printf("Quantity : %d\n", temp->qty);
printf("Price    : %d\n\n", temp->price);

pop(temp);
puts("Succesfully deleted");
}
pause();
}

void checkout()
{
if (head == NULL)
{
puts("Your cart is empty");
}
else
{
Node* cur = head;
int num = 1;
long long total = 0;

puts("=============================================================================");
puts("|                               Shopping Cart                               |");
puts("=============================================================================");
printf("| %3s | %-30s | %-6s | %-10s | %-12s |\n", "No.", "Item name", "Qty", "Price", "Sum");
puts("-----------------------------------------------------------------------------");

while (cur)
{
long long sum = (long long)cur->qty * cur->price;
printf("| %3d | %-30s | %6d | Rp. %6d | Rp. %8lld |\n", num, cur->name, cur->qty, cur->price, sum);
total += sum;
cur = cur->next;
++num;
}
puts("=============================================================================");
printf("| Total : Rp. %-10lld                                                    |\n", total);
puts("| Kindness is Free                                                          |");
puts("=============================================================================\n");
head = tail = NULL;
}
pause();
}

int main()
{
int choice;
do
{
cls();
puts("=======================");
puts("|  Dreammers Market   |");
puts("=======================");
puts("1. Add new item");
puts("2. View items");
puts("3. Change item quantity");
puts("4. Remove Item");
puts("5. CheckOut");
puts("6. Exit");
printf(">> ");

scanf("%d%*c", &choice);
puts("");

switch (choice)
{
case 1:
addItem();
break;
case 2:
viewItems();
break;
case 3:
changeItemQuantity();
break;
case 4:
removeItem();
break;
case 5:
checkout();
break;
}
} while (choice != 6);

return 0;
}

No comments:

Post a Comment