Code Implementation from Chapter 1 and 2

Hello all! Today, I'm posting an implementation code of linked list. This is related to the link that I've given to you in the last blog (App Store Linked List). If you want to see the code directly in this blog, well then this is the code for you.

Don't forget to read my summary and my linked list concept in order to understand this code. Happy coding!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct LinkedListNode{
char productName[26];
unsigned int quantity;
unsigned long int price;
struct LinkedListNode* next;
struct LinkedListNode* prev;
} typedef LinkedListNode;
LinkedListNode* head = NULL;
LinkedListNode* tail = NULL;
LinkedListNode* temp = NULL;
LinkedListNode* oneNode(char productName[], unsigned int quantity, unsigned int price){
temp = (LinkedListNode*) malloc(sizeof(LinkedListNode));
strcpy(temp->productName, productName);
temp->quantity = quantity;
temp->price = price;
temp->next = NULL;
temp->prev = NULL;
return temp;
}

int giveRangedValues(int min, int max, char message[]){
int choice = 0;
do{
printf("%s", message);
if (scanf("%i", &choice) != 1){
printf("Input only a number!\n");
}
getchar();
}while(choice < min || choice > max);
return choice;
}
char *tempChar = NULL;
char* printDashes(char character, int length){
tempChar = (char*) malloc((length + 1) * sizeof(char));
int i = 0;
for (i = 0; i < length; i++){
tempChar[i] = character;
}
tempChar[i] = '\0';
return tempChar;
}
void printListProduct(){
printf("Here's a list of your products that you have bought (sorted by name):\n");
temp = head;
if (temp == NULL){
printf("You haven't bought any product yet...\n");
}
else{
printf("%s\n", printDashes('-', 63));
free(tempChar);
printf("%25s %3s %15s %15s\n", "Product Name", "Quantity", "Price (1 item)", "Total Price");
printf("%s\n", printDashes('-', 63));
free(tempChar);
while (temp != NULL){
printf("%25s %3u %15u %15lu\n", temp->productName, temp->quantity, temp->price, temp->quantity * temp->price);
printf("%s\n", printDashes('-', 63));
free(tempChar);
temp = temp->next;
}
}
}
void addProduct(LinkedListNode* newNode){
// Sort by name
if (head == NULL){
head = newNode;
tail = head;
}
else if (strcmp(newNode->productName, head->productName) > 0){
head->prev = newNode;
newNode->next = head;
head = head->prev;
}
else if (strcmp(newNode->productName, tail->productName) < 0){
tail->next = newNode;
newNode->prev = tail;
tail = tail->next;
}
else{
// 1 2 (3) 4
temp = head;
while (strcmp(newNode->productName, temp->productName) < 0){
temp = temp->next;
}
newNode->next = temp;
newNode->prev = temp->prev;
temp->prev->next = newNode;
temp->prev = newNode;
}
}
void deleteItem(LinkedListNode* node){
if (head == tail){
free(head);
head = NULL;
tail = NULL;
}
else if (node == head){
head = head->next;
free(node);
head->prev = NULL;
}
else if (node == tail){
tail = tail->prev;
free(node);
tail->next = NULL;
}
else{
node->prev->next = node->next;
node->next->prev = node->prev;
node->next = NULL;
node->prev = NULL;
free(node);
}
}
void giveRangedProductNameLength(char source[], int min, int max, char message[]){
int length = 0;
do{
printf("%s", message);
scanf("%[^\n]", source);
length = strlen(source);
getchar();
}while(length < min || length > max);
}
LinkedListNode* giveOneNode(char name[]){
temp = head;
while (temp != NULL && strcmp(temp->productName, name) != 0){
temp = temp->next;
}
return temp;
}

void printReceiptItems(){
unsigned long long int totalPrice = 0;
FILE* file = fopen("receipt.txt", "w");
temp = head;
fprintf(file, "Anthony's Amazing Shop!\nAlways happy to serve you well!\n");
fprintf(file, "%25s %3s %15s %15s\n", "Product Name", "Quantity", "Price (1 item)", "Total Price");
while (temp != NULL){
fprintf(file, "%s\n", printDashes('-', 63));
free(tempChar);
fprintf(file, "%25s %3u %15u %15lu\n", temp->productName, temp->quantity, temp->price, temp->quantity * temp->price);
totalPrice += (temp->quantity * temp->price);
fprintf(file, "%s\n", printDashes('-', 63));
free(tempChar);
if (temp->next == NULL){
free(temp);
temp = NULL;
}
else if (temp->prev != NULL){
free(temp->prev);
temp = temp->next;
}
else{
temp = temp->next;
}
}
fprintf(file, "\nTOTAL PRICE: %llu\n", totalPrice);
fprintf(file, "\nOne more thing... Kindness is free!\nOnce again! I'll be really happy welcoming you to shop at us again!\n\nKind Regards,\nAnthony\n");
head = NULL;
tail = NULL;
temp = NULL;
}
void mainMenu(){
int choice = 0;
LinkedListNode* oneProduct = NULL;
do{
system("cls");
srand(time(0));
printf("Welcome to Anthony's App Store!\n");
printListProduct();
printf("\nPlease select what would you like to do:\n");
printf("1. Add Product to Cart\n");
printf("2. Alter Product Quantity or Delete Item\n");
printf("3. Finish Shopping (Checkout) and Print A Receipt\n");
choice = giveRangedValues(1, 3, "Choose: ");
// Let's assume that the price is random, about at range 1 to 100, which means we can just use random function * 1000.
// Maximum quantity is 50!
char tempName[26] = {};
if (choice == 1){
giveRangedProductNameLength(tempName, 3, 25, "Please enter the product name[3-25]: ");
unsigned int tempPrice = (rand() % 100) * 1000;
unsigned int quantity = giveRangedValues(1, 50, "Please enter how many items you'd like to buy [1-50]: ");
addProduct(oneNode(tempName, quantity, tempPrice));
printf("Your item has been inserted successfully!\n");
getchar();
}
else if (choice == 2){
giveRangedProductNameLength(tempName, 3, 25, "Please enter the product name[3-25]: ");
oneProduct = giveOneNode(tempName);
if (oneProduct == NULL){
printf("No Data is found! Please check the spelling and try again!");
}
else{
int quantity = giveRangedValues(0, 50, "Please enter how many items you'd like to buy [1-50] (enter 0 to delete the item): ");
oneProduct->quantity = quantity;
if (oneProduct->quantity == 0){
deleteItem(oneProduct);
printf("Item has been deleted!");
}
else{
printf("Quantity has been updated!\n");
}
}
oneProduct = NULL;
getchar();
}
else if (choice == 3){
if (head == NULL){
printf("I can't print a receipt for you because you haven't bought any item yet :(\nCome Again!\n");
}
else{
printReceiptItems();
printf("I have printed a receipt for you! You can find it on 'receipt.txt'.\nCome Again and thank you for shopping with us!\n");
}
getchar();
}
}while(choice != 3);
}
int main(){
mainMenu();
return 0;
}

Comments

Popular posts from this blog

AVL Tree

Heap

Binary Tree dan Hash Table