You are on page 1of 11

Drug sales statistics system for pharmacies

TARIK MOURAD
223J01
CST22
Introduction:

Today, we are taking a closer look at a project at the intersection of healthcare and technology—a Drug Inventory
Management System meticulously crafted using the C programming language. Our system is a testament to the power of
simplicity and performance. It is built on the foundational strength of C—a language synonymous with control, speed, and
precision. This drug inventory management application is designed to offer essential functionalities such as adding new
drugs to the inventory, searching for drugs by name, sorting drugs based on their price and calculating total sales—all
achieved with a straightforward user interface in the console. As we navigate the system, we will unveil how traditional
challenges of managing drug inventories are elegantly solved with a few lines of C code. From the careful structuring of
drug information to implementing fundamental algorithms for searching and sorting, this project exemplifies how we can
harness the efficiency of C to develop practical solutions for critical sectors.
INVENTORY MANAGEMENT Page 03

CHALLENGES AND CODE


SOLUTIONS

Manual Data Entry: Labor-intensive and error-prone manual


entry of drug data.
Code Solution: addNewDrug() function
‘ADDNEWDRUG()” Page 01

FUNCTION
Here’s a step-by-step reasoning of what the addNewDrug() function might involve in a typical scenario:

Purpose Identification:

The function will likely be part of a system where drugs or medications must be catalogued.
Its primary goal would be to create a new entry for a drug that did not previously exist in the system.
Parameter Acceptance:

It would accept parameters that define the properties of a drug, such as its name, dosage,
manufacturer, expiration date, side effects, and any other relevant data.
Validation:

Before adding the new drug to the system, the function would perform validations to ensure that the
provided data is in the correct format and adheres to predefined rules.
Interaction with Database:

The function would likely interact with a database or data store to insert the new drug information.
This would involve constructing a query or using an ORM (Object-Relational Mapping) to save the drug
data.
Function: bubbleSortByPrice(DrugList
*list)

It seems like the function takes a single argument: a pointer to a DrugList structure, which contains an array of Drug structures and an integer
representing the current number of elements in the array (length).

For the outer loop (variable i), it runs from the start of the array up until the second-to-last element. With each iteration of the outer loop, the largest
unsorted value "bubbles up" to its correct position at the end of the array, so the range of the inner loop can be reduced by i for each pass.

Regarding the inner loop (variable j), it is where the actual comparison and swapping occur. It iterates through the array from the start to the length -
i - 1 element. The - i part is because, after each outer loop iteration, the last i elements are already sorted and don't need to be considered. The - 1 is
to prevent accessing beyond the array when we look at j + 1.

When it comes to comparison and swapping, the function checks if the price of the current drug (list->drugs[j].price) is greater than the price of the
next drug (list->drugs[j + 1].price). If they are out of order, it swaps them using a temporary Drug variable (temp). The current drug is first assigned to
temp, then the next drug is copied to the current position, and finally, temp is copied to the next position. This effectively swaps the two Drug
elements in the array.

Lastly, the sorting order of the function ensures that the drugs array within the given DrugList is sorted in ascending order by the price of the drugs.
The drugs with lower prices will be positioned before those with higher prices.
Function: searchDrugByName(DrugList *list, const char
*name)
Function: printDrug(Drug drug)

Purpose: The function searchDrugByName searches for a drug by its name within the DrugList.

Parameters: It takes two parameters:

A pointer to DrugList which holds an array of Drug structures and an integer for the number of drugs (length).
A const char *name, which is the name of the drug you're searching for.
Loop: The function uses a for loop to iterate over each Drug structure in the DrugList. The loop runs from index 0 to list->length - 1.

String Comparison: In each iteration, it compares the current Drug's name with the provided search name using the strcmp
function, which returns 0 if the two strings are equal.

Return Value: If a match is found (meaning strcmp returned 0), the function returns the current index i, indicating the position of
the drug in the array. If no drug with the given name is found by the end of the loop, the function returns -1, indicating that the
search was unsuccessful.
Purpose: The printDrug function is used to display the details of a Drug structure in a formatted
manner to the standard output (typically the console).

Parameters: It takes a single Drug structure as its parameter.

Printing: It uses the printf function to print the drug's code, name, price, count, and total sales.
The %s format specifier is used for strings, %d for integers, and %.2f for floating-point numbers
formatted to two decimal places.

Together, these two functions search for a drug by name and then print its detailed information.
These are common operations in inventory systems, allowing users to retrieve and view data
about specific items easily.
The main function
A DrugList variable named drugList is declared. This will hold the array of Drug items and the number of items
(length).
initializeDrugList(&drugList) is called to set up the drugList with an initial length of 0 and potentially load any
predefined drugs into the list.
An infinite loop (while (1)) starts, which will keep running until the user chooses to exit the program (option 4).
The menu options are presented to the user.
The user's choice is read using scanf. If the input is invalid (not an integer), an error message is printed, the input
buffer is cleared, and the loop continues to prompt again.
If a valid integer is entered, the input buffer is cleared to remove any leftover characters (like the newline
character after pressing Enter).
A switch statement executes different code blocks based on the choice variable.
If the choice is 1, the addNewDrug function is called, which prompts the user to enter details for a new drug and
adds it to the drugList.
For choice 2, the program prompts the user to enter a drug name to search for.
It then calls searchDrugByName with the entered name. If a drug is found (index is not -1), it prints the drug
details; otherwise, it displays a "not found" message.
When the user selects option 3, bubbleSortByPrice is called to sort the list of drugs by price, and the sorted list is
printed out.
Choosing option 4 prints a goodbye message and exits the loop (and the program) by returning 0 from the main
function.
If the user enters a choice that is not 1, 2, 3, or 4, an error message is displayed, and the menu will be
presented again on the next iteration of the loop.
This main function effectively provides a simple text-based user interface for interacting with the drug
inventory system, allowing users to perform several key actions: adding new drugs, searching for drugs,
sorting the inventory by price, and exiting the program

You might also like