#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <windows.h>
#include <string.h>
#include <conio.h>
struct node {
char item[30];
long long int price;
long long int quantity;
long long int totalPrice;
struct node *next;
struct node *prev;
};
long long int borderLength;
long long int spasiHeader;
long long int spasiNama;
long long int spasiQuantity;
long long int spasiPrice;
long long int spasiNumber;
long long int spasiTotalPrice;
node *head;
node *tail;
void gotoxy(int x, int y){
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void setup(){
head = NULL;
tail = NULL;
borderLength = 0;
spasiNama = 30;
spasiQuantity = 0;
borderLength = 0;
spasiPrice = 0;
spasiTotalPrice = 0;
}
long long int numLength(long long int input){
long long int remainder = 0;
long long int length =0;
while(input>0){
remainder = input % 10;
input = (input-remainder)/10;
length++;
}
return length;
}
void spaceCounter(){
node *curr = head;
long long int length = 0;
long long int temp =0;
while(curr!=NULL){
temp = curr->quantity;
if(spasiQuantity < numLength(temp)){
spasiQuantity = numLength(temp);
}
temp = curr->price;
if(spasiPrice<numLength(temp)){
spasiPrice = numLength(temp);
}
temp = curr->totalPrice;
if(spasiTotalPrice < numLength(temp)){
spasiTotalPrice = numLength(temp);
}
length++;
curr = curr->next;
}
spasiNumber = numLength(length);
}
int DisplayMenu(){
int choice;
printf("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n");
printf(" Welcome to Dreamers Market\n");
printf("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n");
printf("\tplease select your input\n");
printf("\t 1. Add Item to Cart\n");
printf("\t 2. Edit Item in Cart\n");
printf("\t 3. Remove Item from Cart\n");
printf("\t 4. Checkout\n");
printf("\t Input >> ");
scanf("%d", &choice);getchar();
return choice;
}
int displayList(){
long long int i;
node *curr = head;
if(curr == NULL){
printf("===================================================\n");
printf("| No Item in Cart |\n");
printf("===================================================\n");
return 0;
}else{
spaceCounter();
borderLength = 15+ spasiNama + spasiNumber + spasiQuantity + spasiPrice;
spasiHeader = borderLength - 24;
for(i = 0; i<borderLength; i++){
printf("=");
}
printf("\n| List of items in Cart");
for(i = 0; i<spasiHeader; i++){
printf(" ");
}
printf("|\n");
for(i = 0; i<borderLength; i++){
printf("=");
}
printf("\n");
i=1;
while(curr != NULL){
long long int j;
printf("| ");
for(j = 0; j<spasiNumber-numLength(i); j++){
printf("0");
}
printf("%lld | ", i);
for(j = 0; j<spasiNama-strlen(curr->item); j++){
printf(" ");
}
printf("%s | %lld", curr->item, curr->quantity);
for(j = 0; j<spasiQuantity-numLength(curr->quantity); j++){
printf(" ");
}
printf(" | Rp");
for(j = 0; j<spasiPrice-numLength(curr->price); j++){
printf(" ");
}
printf("%lld |\n", curr->price);
curr = curr->next;
i++;
}
return 1;
}
}
void insertNode(char innputName[], long long int inputQty, long long int priceInput){
node *temp = (node*)malloc(sizeof(struct node));
strcpy(temp->item, innputName);
temp->price = priceInput;
temp->quantity = inputQty;
temp->totalPrice = inputQty * priceInput;
if(head == NULL){
head = temp;
tail = temp;
temp->next = NULL;
temp->prev = NULL;
}else{
if(strcmp(temp->item, head->item) == -1 || strcmp(temp->item, head->item) == 0){
temp->next = head;
temp->prev = NULL;
head->prev = temp;
head = temp;
return;
}else if(strcmp(temp->item, tail->item) == 1 || strcmp(temp->item, tail->item) == 0){
temp->prev = tail;
temp->next = NULL;
tail->next = temp;
tail = temp;
return;
}else{
node *currNode = head;
node *prevNode = NULL;
while(strcmp(temp->item, currNode->item) == 1 && currNode->next != NULL){
currNode = currNode->next;
}
temp->next = currNode;
temp->prev = currNode->prev;
currNode->prev = temp;
temp->prev->next = temp;
}
}
}
void displayBill(){
node *curr = head;
long long int x;
long long int y;
long long int sum = 0;
long long int i;
long long int spasiSum = 0;
spaceCounter();
borderLength = 20 + spasiNama + spasiTotalPrice + spasiQuantity + spasiPrice;
if(curr == NULL){
printf("==================================================\n");
printf("| Dreamer's Market |\n");
printf("==================================================\n");
printf("==================================================\n");
printf("| Price : RP 0 |\n");
printf("| Discount : - RP 0 |\n");
printf("| Total : RP 0 |\n");
printf("==================================================\n");
printf("| Kindness is Free |\n");
printf("==================================================\n");
return;
}
for(i = 0; i<borderLength; i++){
printf("=");
}
printf("\n");
x = (borderLength - 18)/2;
printf("|");
for(i = 0; i<x; i++){
printf(" ");
}
printf("Dreamer's Market");
y = borderLength-x-18;
for(i = 0; i<y; i++){
printf(" ");
}
printf("|\n");
for(i = 0; i<borderLength; i++){
printf("=");
}
printf("\n");
while(curr != NULL){
printf("| ");
for(i = 0; i<spasiQuantity-numLength(curr->quantity); i++){
printf(" ");
}
printf("%lld | ", curr->quantity);
for(i = 0; i<spasiNama-strlen(curr->item); i++){
printf(" ");
}
printf(" %s | Rp ", curr->item);
for(i = 0; i<spasiPrice-numLength(curr->price); i++){
printf(" ");
}
printf("%lld | Rp ", curr->price);
for(i = 0; i<spasiTotalPrice-numLength(curr->totalPrice); i++){
printf(" ");
}
printf("%lld |\n", curr->totalPrice);
sum = sum + curr->totalPrice;
curr = curr->next;
i++;
}
for(i = 0; i<borderLength; i++){
printf("=");
}
printf("\n");
printf("| Price :");
for(i = 0; i<borderLength-numLength(sum)-14; i++){
printf(" ");
}
printf("RP %lld |\n", sum);
printf("| Discount :");
for(i = 0; i<borderLength-numLength(sum)-19; i++){
printf(" ");
}
printf("- RP %lld |\n", sum);
printf("| Total :");
sum = 0;
for(i = 0; i<borderLength-15; i++){
printf(" ");
}
printf("RP 0 |\n");
for(i = 0; i<borderLength; i++){
printf("=");
}
printf("\n");
x = (borderLength - 18)/2;
printf("|");
for(i = 0; i<x; i++){
printf(" ");
}
printf("Kindness is Free");
y = borderLength-x-18;
for(i = 0; i<y; i++){
printf(" ");
}
printf("|\n");
for(i = 0; i<borderLength; i++){
printf("=");
}
printf("\n");
}
void remove(struct node* del){
if (head == NULL || del == NULL)
return;
if (head== del)
head = del->next;
if (del->next != NULL)
del->next->prev = del->prev;
if (del->prev != NULL)
del->prev->next = del->next;
free(del);
}
int isExistEdit(char input[]){
char inputName[100];
long long int inputQuantity;
long long int inputPrice;
node *curr = head;
while(curr!=NULL){
if(strcmp(curr->item, input)==0){
inputPrice = curr->price;
do{
printf("\tInput new item Name [1-30 charracter]\n");
printf("\t =>");
scanf("%[^\n]", inputName);getchar();
}while(strlen(inputName)>30 || strlen(inputName)<1);
printf("\t Input new item quantity =>");
scanf("%lld", &inputQuantity);getchar();
remove(curr);
insertNode(inputName, inputQuantity, inputPrice);
return 1;
}
curr = curr->next;
}
printf("\t~ Item Not Found Input Again ~\n");
return 0;
}
int isExistRemove(char input[]){
char inputName[100];
long long int inputQuantity;
long long int inputPrice;
node *curr = head;
while(curr!=NULL){
if(strcmp(curr->item, input)==0){
remove(curr);
return 1;
}
curr = curr->next;
}
printf("\t~ Item Not Found Input Again ~\n");
return 0;
}
int main(){
int choice;
char inputName[100];
long long int inputQty;
long long int priceInput;
setup();
while(true){
system("cls");
gotoxy(0, 10);
displayList();
gotoxy(0, 0);
choice = DisplayMenu();
if(choice == 1){
do{
system("cls");
printf("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n");
printf(" Welcome to Dreamers Market\n");
printf("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n");
printf("\tInput Item's Name [1-30 charracter]=> ");
scanf("%[^\n]", inputName);getchar();
}while(strlen(inputName)>30 || strlen(inputName)<1);
printf("\tInput Item's Quantity => ");
scanf("%lld", &inputQty);getchar();
priceInput = (rand()%100+1)*100;
insertNode(inputName, inputQty, priceInput);
}else if(choice == 2){
system("cls");
int option = displayList();
if(option == 1){
do{
printf("\tInput item name that you want to edit\n");
printf("\tItem name => ");
scanf("%[^\n]", inputName); getchar();
}while(isExistEdit(inputName)==0);
}else{
system("cls");
printf("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n");
printf(" Welcome to Dreamers Market\n");
printf("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n");
printf("\tThere are no existing item in cart\n");
printf("\tPlease add item if you want to edit\n");
printf("\n\tPress any key to go back...");
char c = getch();
}
}else if(choice == 3){
system("cls");
int option = displayList();
if(option == 1){
do{
printf("\tInput item name that you want to delete\n");
printf("\tItem name => ");
scanf("%[^\n]", inputName); getchar();
}while(isExistRemove(inputName)==0);
}else{
system("cls");
printf("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n");
printf(" Welcome to Dreamers Market\n");
printf("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n");
printf("\tThere are no existing item in cart\n");
printf("\tPlease add item if you want to edit\n");
printf("\n\tPress any key to go back...");
char c = getch();
}
}else if(choice == 4){
system("cls");
displayBill();
break;
}
}
}
No comments:
Post a Comment