×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

Creating an Efficient Order Management System in C

June 24, 2024
Robert Horta
Robert Horta
🇬🇧 United Kingdom
C
Alex Johnson is a seasoned C programming expert with over a decade of experience. He has helped over 1,000 students excel in their C assignments through personalized tutoring and clear, efficient code solutions. Alex holds advanced degrees in computer science and has received multiple awards for his exceptional teaching and programming skills.
Key Topics
  • Understanding the Data Structures
    • The Order Structure
    • The List Structure
    • The Item Structure
  • Implementing the Order Management Functions
    • Adding an Order
    • Deleting an Order
    • Displaying Orders
    • Changing an Order
    • Merging Orders
  • Running the Order Management System
    • The Main Function
    • Compilation
    • Running the Program
  • Conclusion

Creating an efficient order management system is crucial for businesses. This blog will guide you through the process of creating one in C, covering essential functions like adding, deleting, displaying, changing, and merging orders. To start, define data structures like order, list, and item to manage orders and items efficiently. The order structure stores customer information and a list of items, linking to the next order. The list structure manages a list of items in an order, while the item structure represents each item. Implement functions like add_order, delete_order, display_order, display_orders, change_order, and merge_orders to interact with the system. For instance, add_order prompts users to enter customer and item details, delete_order removes orders by name, and display_orders shows all current orders. Consider memory management when implementing these functions to avoid memory leaks. By following these steps, students can solve their C assignment and create an efficient order management system.

Understanding the Data Structures

Implementing-an-Order-Management-System-in-C (2)

Before diving into the code, it's important to understand the data structures that will be used to manage orders and items. In our order management system, we'll use three main structures: order, list, and item.

The Order Structure

The order structure represents each customer's order. It contains the customer's name, a pointer to a list of items, and a pointer to the next order in the list.

Defining the Order Structure

The order structure is defined as follows:

typedef struct order { char *name; list *order_list; struct order *next_order; } order;

Components of the Order Structure

  • name: A string representing the customer's name.
  • order_list: A pointer to a list of items associated with the order.
  • next_order: A pointer to the next order in the list.

Purpose of the Order Structure

The order structure is used to manage and store individual customer orders. It links to the list of items ordered and to the next order, forming a linked list of orders.

The List Structure

The list structure represents a list of items in an order. It contains a pointer to the head of the list of items.

Defining the List Structure

The list structure is defined as follows:

typedef struct list { item *head; } list;

Components of the List Structure

  • head: A pointer to the head of the list of items.

Purpose of the List Structure

The list structure is used to manage and store items within an order. It links to the first item in the list, allowing traversal through the list of items.

The Item Structure

The item structure represents each item in an order. It contains the item's name and a pointer to the next item in the list.

Defining the Item Structure

The item structure is defined as follows:

typedef struct item { char *item_name; struct item *next_item; } item;

Components of the Item Structure

  • item_name: A string representing the item's name.
  • next_item: A pointer to the next item in the list.

Purpose of the Item Structure

The item structure is used to manage and store individual items within an order. It links to the next item, forming a linked list of items.

Implementing the Order Management Functions

With the data structures defined, we can now implement the functions required to manage orders. These functions will include adding, deleting, displaying, changing, and merging orders.

Adding an Order

Adding an order involves creating a new order structure, prompting the user for the customer's name and items, and linking the new order to the list of orders.

Function Definition

The add_order function is defined as follows:

void change_order(order *head, char *name) { while (head != NULL) { if (strcmp(head->name, name) == 0) { printf("Would you like to remove, include, or switch an item from your order? "); char choice[10]; fgets(choice, sizeof(choice), stdin); choice[strcspn(choice, "\n")] = 0; // Remove trailing newline if (strcasecmp(choice, "remove") == 0) { printf("Please enter the item you would like to remove: "); char item_name[100]; fgets(item_name, sizeof(item_name), stdin); item_name[strcspn(item_name, "\n")] = 0; // Remove trailing newline item *curr_item = head->order_list->head; item *prev_item = NULL; while (curr_item != NULL && strcmp(curr_item->item_name, item_name) != 0) { prev_item = curr_item; curr_item = curr_item->next_item; } if (curr_item == NULL) { printf("Item not found.\n"); return; } if (prev_item == NULL) { head->order_list->head = curr_item->next_item; } else { prev_item->next_item = curr_item->next_item; } free(curr_item->item_name); free(curr_item); printf("Ruff! (Removed an item from your order!)\n"); if (head->order_list->head == NULL) { delete_order(&head, name); } } else if (strcasecmp(choice, "include") == 0) { printf("Please enter the item you would like to include: "); char item_name[100]; fgets(item_name, sizeof(item_name), stdin); item_name[strcspn(item_name, "\n")] = 0; // Remove trailing newline item *new_item = malloc(sizeof(item)); new_item->item_name = strdup(item_name); new_item->next_item = head->order_list->head; head->order_list->head = new_item; printf("Ruff! (Included a new item to your order!)\n"); } else if (strcasecmp(choice, "switch") == 0) { printf("Please enter the item you would like to remove: "); char item_name[100]; fgets(item_name, sizeof(item_name), stdin); item_name[strcspn(item_name, "\n")] = 0; // Remove trailing newline item *curr_item = head->order_list->head; item *prev_item = NULL; while (curr_item != NULL && strcmp(curr_item->item_name, item_name) != 0) { prev_item = curr_item; curr_item = curr_item->next_item; } if (curr_item == NULL) { printf("Item not found.\n"); return; } if (prev_item == NULL) { head->order_list->head = curr_item->next_item; } else { prev_item->next_item = curr_item->next_item; } free(curr_item->item_name); free(curr_item); printf("Please enter the item you would like to add: "); fgets(item_name, sizeof(item_name), stdin); item_name[strcspn(item_name, "\n")] = 0; // Remove trailing newline item *new_item = malloc(sizeof(item)); new_item->item_name = strdup(item_name); new_item->next_item = head->order_list->head; head->order_list->head = new_item; printf("Ruff! (Switched an item in your order!)\n"); } else { printf("Invalid choice.\n"); } return; } head = head->next_order; } printf("No order found with the name %s.\n", name); }

Steps Involved

  1. Allocate memory for a new order structure.
  2. Duplicate the customer's name and store it in the name field.
  3. Allocate memory for a new list structure and initialize its head to NULL.
  4. Link the new order to the head of the list of orders.
  5. Prompt the user for items and add them to the order's item list.

Deleting an Order

Deleting an order involves searching for the order by the customer's name and removing it from the list of orders.

Function Definition

The delete_order function is defined as follows:

void delete_order(order **head, char *name) { order *temp = *head, *prev = NULL; while (temp != NULL && strcmp(temp->name, name) == 0) { *head = temp->next_order; free(temp->name); free(temp->order_list); free(temp); temp = *head; } while (temp != NULL) { while (temp != NULL && strcmp(temp->name, name) != 0) { prev = temp; temp = temp->next_order; } if (temp == NULL) return; prev->next_order = temp->next_order; free(temp->name); free(temp->order_list); free(temp); temp = prev->next_order; } printf("Ruff! (Order has been deleted!)\n"); }

Steps Involved

  1. Search for the order by the customer's name.
  2. Remove the order from the list by updating the pointers.
  3. Free the allocated memory for the order and its items.

Displaying Orders

Displaying orders involves traversing the list of orders and printing the details of each order and its items.

Displaying a Single Order

The display_order function is defined as follows:

void display_order(order *head, char *name) { while (head != NULL) { if (strcmp(head->name, name) == 0) { printf("Displaying order(s) placed by %s:\n", name); item *curr_item = head->order_list->head; int item_num = 1; while (curr_item != NULL) { printf("Item %d: %s\n", item_num++, curr_item->item_name); curr_item = curr_item->next_item; } return; } head = head->next_order; } printf("No order found with the name %s.\n", name); }

Displaying All Orders

The display_orders function is defined as follows:

void display_orders(order *head) { printf("Displaying all orders placed currently:\n"); while (head != NULL) { printf("Order placed by %s:\n", head->name); item *curr_item = head->order_list->head; int item_num = 1; while (curr_item != NULL) { printf("Item %d: %s\n", item_num++, curr_item->item_name); curr_item = curr_item->next_item; } head = head->next_order; } }

Changing an Order

Changing an order involves modifying the items in an existing order. This can include removing, adding, or switching items.

Function Definition

The change_order function is defined as follows:

void change_order(order *head, char *name) { while (head != NULL) { if (strcmp(head->name, name) == 0) { printf("Would you like to remove, include, or switch an item from your order? "); char choice[10]; fgets(choice, sizeof(choice), stdin); choice[strcspn(choice, "\n")] = 0; // Remove trailing newline if (strcasecmp(choice, "remove") == 0) { printf("Please enter the item you would like to remove: "); char item_name[100]; fgets(item_name, sizeof(item_name), stdin); item_name[strcspn(item_name, "\n")] = 0; // Remove trailing newline item *curr_item = head->order_list->head; item *prev_item = NULL; while (curr_item != NULL && strcmp(curr_item->item_name, item_name) != 0) { prev_item = curr_item; curr_item = curr_item->next_item; } if (curr_item == NULL) { printf("Item not found.\n"); return; } if (prev_item == NULL) { head->order_list->head = curr_item->next_item; } else { prev_item->next_item = curr_item->next_item; } free(curr_item->item_name); free(curr_item); printf("Ruff! (Removed an item from your order!)\n"); if (head->order_list->head == NULL) { delete_order(&head, name); } } else if (strcasecmp(choice, "include") == 0) { printf("Please enter the item you would like to include: "); char item_name[100]; fgets(item_name, sizeof(item_name), stdin); item_name[strcspn(item_name, "\n")] = 0; // Remove trailing newline item *new_item = malloc(sizeof(item)); new_item->item_name = strdup(item_name); new_item->next_item = head->order_list->head; head->order_list->head = new_item; printf("Ruff! (Included a new item to your order!)\n"); } else if (strcasecmp(choice, "switch") == 0) { printf("Please enter the item you would like to remove: "); char item_name[100]; fgets(item_name, sizeof(item_name), stdin); item_name[strcspn(item_name, "\n")] = 0; // Remove trailing newline item *curr_item = head->order_list->head; item *prev_item = NULL; while (curr_item != NULL && strcmp(curr_item->item_name, item_name) != 0) { prev_item = curr_item; curr_item = curr_item->next_item; } if (curr_item == NULL) { printf("Item not found.\n"); return; } if (prev_item == NULL) { head->order_list->head = curr_item->next_item; } else { prev_item->next_item = curr_item->next_item; } free(curr_item->item_name); free(curr_item); printf("Please enter the item you would like to add: "); fgets(item_name, sizeof(item_name), stdin); item_name[strcspn(item_name, "\n")] = 0; // Remove trailing newline item *new_item = malloc(sizeof(item)); new_item->item_name = strdup(item_name); new_item->next_item = head->order_list->head; head->order_list->head = new_item; printf("Ruff! (Switched an item in your order!)\n"); } else { printf("Invalid choice.\n"); } return; } head = head->next_order; } printf("No order found with the name %s.\n", name); }

Merging Orders

Merging orders involves combining orders with the same customer's name into a single order, consolidating their items.

Function Definition

The merge_orders function is defined as follows:

void merge_orders(order **head) { order *current = *head; while (current != NULL) { order *runner = current; while (runner->next_order != NULL) { if (strcmp(current->name, runner->next_order->name) == 0) { item *current_items = current->order_list->head; while (current_items->next_item != NULL) { current_items = current_items->next_item; } current_items->next_item = runner->next_order->order_list->head; order *duplicate_order = runner->next_order; runner->next_order = runner->next_order->next_order; free(duplicate_order->order_list); free(duplicate_order); } else { runner = runner->next_order; } } current = current->next_order; } printf("Orders have been merged!\n"); }

Running the Order Management System

Now that we have all the necessary functions implemented, we can put them together in the main function to create our order management system. The main function provides a menu-based interface for the user to interact with the system.

The Main Function

The main function is defined as follows:

int main() { order *orders = NULL; int choice; char name[100]; do { printf("Please choose from one of the following options:\n"); printf("1. Add order\n"); printf("2. Delete order\n"); printf("3. Display order(s) by name\n"); printf("4. Display all orders\n"); printf("5. Change order\n"); printf("6. Merge orders with the same name\n"); printf("7. Display current orders\n"); printf("8. Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); getchar(); // Consume newline character left in the input buffer switch (choice) { case 1: printf("Please enter your name: "); fgets(name, sizeof(name), stdin); name[strcspn(name, "\n")] = 0; // Remove trailing newline add_order(&orders, name); break; case 2: printf("Please enter your name: "); fgets(name, sizeof(name), stdin); name[strcspn(name, "\n")] = 0; // Remove trailing newline delete_order(&orders, name); break; case 3: printf("Please enter your name: "); fgets(name, sizeof(name), stdin); name[strcspn(name, "\n")] = 0; // Remove trailing newline display_order(orders, name); break; case 4: display_orders(orders); break; case 5: printf("Please enter your name: "); fgets(name, sizeof(name), stdin); name[strcspn(name, "\n")] = 0; // Remove trailing newline change_order(orders, name); break; case 6: merge_orders(&orders); break; case 7: current_orders(orders); break; case 8: printf("Exiting program.\n"); break; default: printf("Invalid choice. Please try again.\n"); } } while (choice != 8); // Free allocated memory while (orders != NULL) { order *temp = orders; orders = orders->next_order; while (temp->order_list->head != NULL) { item *temp_item = temp->order_list->head; temp->order_list->head = temp->order_list->head->next_item; free(temp_item->item_name); free(temp_item); } free(temp->order_list); free(temp->name); free(temp); } return 0; }

Steps Involved

  1. Initialize the list of orders to NULL.
  2. Provide a menu-based interface for the user to interact with the system.
  3. Implement options for adding, deleting, displaying, changing, merging, and displaying current orders.
  4. Free allocated memory before exiting the program.

Compilation

Compile the program using gcc:

gcc order_management.c -o order_management

Running the Program

Run the program:

./order_management

Conclusion

Implementing an order management system in C involves understanding and defining the necessary data structures and implementing functions to manage orders effectively. The system should handle adding, deleting, displaying, changing, and merging orders efficiently. By following the steps outlined in this blog, you can create a comprehensive order management system that meets your specific programming assignment requirements. With careful attention to memory management and user interaction, this system will streamline your order processing and improve overall efficiency.

Similar Blogs