Application Code

Application Code

Raymond Andilsim
2301906533


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


struct product{
char productName[31];
int qty;
int price;
struct product *next, *prev;
} *head = NULL, *tail = NULL, *curr;

void addProduct(char *productName, int qty){
curr = (struct product*)malloc(sizeof(struct product));
strcpy(curr->productName, productName);
curr->qty = qty;
curr->price = rand() % 1000000;
if(curr->price == 0) curr->price = curr->price + 1000;
else if(curr->price < 10) curr->price = curr->price * 1000;
else if(curr->price < 100) curr->price = curr->price * 100;
else if(curr->price < 1000) curr->price = curr->price * 10;

curr->next = curr->prev = NULL;
if(head == NULL){
head = tail = curr;
}
else if (strcmp(productName, head->productName) < 0){
curr->next = head;
head->prev = curr;
head = curr;
}
else if (strcmp(productName, tail->productName) > 0){
tail->next = curr;
curr->prev = tail;
tail = curr;
}
else{
struct product* temp = head;
while(strcmp(temp->next->productName, productName) < 0){
temp = temp->next;
}
curr->next = temp->next;
temp->next->prev = curr;
temp->next = curr;
curr->prev = temp;
}
printf("\n");
printf("Product added succesfully!\n");
}

int viewProduct(){
int total = 0;

if(head == NULL) printf("Cart is empty!\n");
else{
printf("IndoApril Mart\n");
printf("______________________________________________________\n");
curr = head;
while( curr != NULL )
{
printf("%-30s", curr->productName);
printf("%4d", curr->qty);
printf("%10d", curr->price);
printf("%10d\n", curr->qty * curr->price);
total = total + (curr->qty * curr->price);
curr = curr->next;
}
printf("______________________________________________________\n");
}
return total;
}

void editProduct(){
char productName[31];
int qty = 0;
int flag = 0;
viewProduct();
printf("Input quantity 0 means delete the product!\n\n");
if(head != NULL){
do{
flag = 0;
do{
printf("Input Product Name [1..30 character(s)]: ");
scanf("%[^\n]", productName); getchar();
if(strlen(productName) < 1 || strlen(productName) > 30){
printf("Product name should be 1-30 character(s)!\n\n");
}
} while(strlen(productName) < 1 || strlen(productName) > 30);
curr = head;
while(curr != NULL){
if(strcmp(curr->productName, productName) == 0) {
flag = 1;
break;
}
curr = curr->next;
}
if(flag == 0) printf("Product doesn't exist!\n\n");
} while(flag == 0);

printf("Input New Product Quantity: ");
scanf("%d", &qty);
curr->qty = qty;
printf("\n");
if(qty == 0){
if(head == tail){
free(head);
head = tail = NULL;
}
else if(curr == head){
head = curr->next;
head->prev = NULL;
free(curr);
}
else if(curr == tail){
tail = curr->prev;
tail->next = NULL;
free(curr);
}
else{
curr->next->prev = curr->prev;
curr->prev->next = curr->next;
free(curr);
}
printf("Product has been deleted!\n");
}
else printf("Update product quantity succesfully!\n");
}
}

void purchase(){
int total;
total = viewProduct();
if(head != NULL){
printf("TOTAL %48d\n", total);
printf("______________________________________________________\n\n");
printf("FREE! KINDNESS IS FREE!\n");
}
}

int main(){
int choice;
char productName[31];
int qty;
do{
puts("IndoApril Mart\n");
puts("1. Add Product to Cart\n");
puts("2. Edit Product in Cart\n");
puts("3. Purchase\n");
puts("4. Exit");
puts("Choose :  ");
scanf("%d", &choice); getchar();
if(choice == 1){
printf("\n");
do{
printf("Input Product Name [1..30 character(s)]: ");
scanf("%[^\n]", productName); getchar();
if(strlen(productName) < 1 || strlen(productName) > 30){
printf("Product name should be 1-30 character(s)!\n\n");
}
} while(strlen(productName) < 1 || strlen(productName) > 30);
do{
printf("Input Product Quantity [Min. 1]: ");
scanf("%d", &qty);
if(qty < 1) {
printf("Product quantity should be at least 1!\n\n");
printf("Input Product Name [1..30 character(s)]: %s\n", productName);
}
} while(qty < 1);
addProduct(productName, qty);
printf("\n");
}
else if(choice == 2){
printf("\n");
editProduct();
printf("\n");
}
else if(choice == 3){
printf("\n");
purchase();
printf("\n");
}
} while(choice != 4);
return 0;
}

Comments

Popular posts from this blog

Semester 2 Review / Summary

AVL Tree