Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Drug Management System

TARIK MOURAD
223J01
CST22
Introduction:

The provided script is a C language program designed to manage a drug inventory for a small
pharmacy or medical facility. It employs fundamental data structures and algorithms to enable
the user to perform various operations on a collection of drugs, each identified by a unique code,
name, price, and stock count.

The core of the program is encapsulated in two custom data structures: Drug, which holds the
details of a single drug item, and DrugList, which is a collection of Drug items alongside an
integer representing the current number of drugs in the inventory. The program's capacity is pre-
defined to handle up to 100 drug entries, a limit set by the MaxSize constant.

Functionality-wise, the program offers a console-based user interface that allows users to:

 Add new drugs to the inventory while automatically calculating their total sales value.

 Search for a specific drug by its name, utilizing a linear search algorithm to traverse the
inventory.

 Sort the inventory by the price of drugs using a bubble sort algorithm, which, despite not
being the most efficient sorting method, suffices for the small scale of data the program is
expected to handle.

 Exit the program.

This C program is structured in a procedural manner, with a series of function prototypes


declared at the beginning, followed by their respective definitions. The main function
orchestrates the flow, providing a simple menu-driven interface to interact with the drug
inventory.
The program is designed to be simple and user-friendly, aiming to serve as a practical tool for
small-scale drug inventory management without the complexities of larger-scale software. It
employs standard input/output operations and relies on the stdio.h, stdlib.h, and string.h libraries,
ensuring portability across different platforms that support the C standard library.
 Preprocessor Directives:

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

o #include <stdio.h>: This directive includes the Standard Input and Output Library,
which provides functionalities for input and output operations, such as reading
from the keyboard with scanf or printing to the console with printf.
o #include <stdlib.h>: The Standard Library includes functions involving memory
allocation (malloc, free), process control (exit, system), and other utility
functions.
o #include <string.h>: This header file contains functions for manipulating arrays of
characters (strings), such as strcmp to compare two strings, strncpy to copy
strings, etc.

 Constants and Macros:

#define MAX_SIZE 100

o #define MAX_SIZE 100: Defines a preprocessor macro_MAX_SIZE that


throughout the program represents the maximum number of drugs that can be
stored in the inventory. Using a macro makes it easy to change the size of the
inventory without having to manually update each instance where it's used.

 Struct Definitions:

typedef struct {
char code [10];
char name [50];
float price;
int count;
float totalSales;
} Drug;
typedef struct {
Drug drugs [MAX_SIZE];
int length;
} DrugList;
o Drug: This structure represents a single drug item with fields for unique code,
name, price per unit, stock count, and total sales. The char arrays hold string data,
while float and int hold numerical values.
o DrugList: This structure encapsulates the entire inventory. It contains an array of
Drug structures and an integer length that keeps track of the current number of
drugs in the inventory. This setup allows for operations on the collection as a
whole, such as adding or searching for drugs.

 Function Prototypes:

void addNewDrug(DrugList *list);


int searchDrugByName(DrugList *list, char *name);
void bubbleSortByPrice (DrugList *list);
o Function prototypes declare the functions that will be used in the program.
This allows the functions to be defined later in any order without causing
issues during compilation.

o addNewDrug takes a pointer to DrugList and doesn't return anything (void).


It's responsible for adding a new Drug to the DrugList.

o searchDrugByName takes a pointer to DrugList and a string name and returns


an integer, usually the index of the drug or some status code.

o bubbleSortByPrice takes a pointer to DrugList and sorts the drugs array within
it by price, not returning any value.
 Main Function:
int main() {
DrugList list;
initializeDrugList(&list);
int choice;
// Main menu loop
// ...
return 0;
}

o The main function is the entry point of the program from where the
execution starts. It's mandatory for every C program.

o The DrugList is initialized here, and a variable choice is declared to


store the user's input.

o A loop is typically used to display the main menu and handle user
input until the program is exited.

o The return 0; statement signifies that the program has ended


successfully.

 Function Implementations:

void addNewDrug(DrugList *list) {


if (list->length >= MaxSize) {
printf("Drug list is full.\n");
return;
}

Drug newDrug;
printf("Enter drug code (4 characters): ");
scanf("%4s", newDrug.num);
printf("Enter drug name: ");
scanf("%19s", newDrug.name);
printf("Enter price: ");
scanf("%f", &newDrug.price);
printf("Enter count: ");
scanf("%d", &newDrug.count);

newDrug.totalSales = newDrug.price * newDrug.count;


list->drugs[list->length++] = newDrug;
printf("New drug added.\n");
}

void bubbleSortByPrice(DrugList *list) {


int i, j;
for (i = 0; i < list->length - 1; i++) {
for (j = 0; j < list->length - i - 1; j++) {
if (list->drugs[j].price > list->drugs[j + 1].price) {

Drug temp = list->drugs[j];


list->drugs[j] = list->drugs[j + 1];
list->drugs[j + 1] = temp;
}
}
}
}

int searchDrugByName(DrugList *list, const char *name) {


for (int i = 0; i < list->length; i++) {
if (strcmp(list->drugs[i].name, name) == 0) {
return i;
}
}
return -1;
}

void printDrug(Drug drug) {


printf("Code: %s, Name: %s, Price: $%.2f, Count: %d, Total Sales: $%.2f\
n",
drug.num, drug.name, drug.price, drug.count, drug.totalSales);
}

o void addNewDrug (DrugList *list) :


This function is responsible for adding new drugs to the inventory.
Here's a step-by-step breakdown of what typically happens inside this
function:
o Prompt the User: The function would prompt the user to enter details
for a new drug, such as its code, name, price, and count.

o Input Validation: Inputs received from the user need to be validated.


For example, the price should not be negative, and the code should not
already exist in the inventory.

o Check for Space: It's important to ensure there is room to add a new
drug to the inventory. This is where MAX_SIZE is used to prevent
adding more drugs than the array can hold.

o Create and Populate Drug: A new Drug instance within the function is
created and populated with the input data. The totalSales might be
calculated by multiplying the price by the count, depending on the
requirements.

o Add to List: The new Drug is added to the DrugList array, and the
length counter is incremented to reflect the addition.

o Confirmation: Finally, the function prints a confirmation message


indicating that the drug has been successfully added.
o
o This function usually does not return a value (void) because it
modifies the DrugList directly, which is passed by pointer.

o int searchDrugByName (DrugList *list, char *name)


o This function performs a search for a drug by its name within the
inventory:

o Iterate Over List: A loop is used to go through each drug entry in


the DrugList array.

o Compare Names: Inside the loop, the name provided is compared


to the name of each Drug using the strcmp function.

o Return Index or Status: If a match is found, the function returns the


index of that Drug within the array. If no match is found, it returns
a status code (often -1) to indicate failure.

o This function returns an integer which is either the index of the


found drug or a negative number indicating the drug was not
found.
o void bubbleSortByPrice (DrugList *list)

o The bubbleSortByPrice function sorts the array of drugs in the


DrugList by their price:

o Nested Loops: The function uses two nested loops to implement


the bubble sort algorithm.

o Compare and Swap: In each iteration, it compares the price of


adjacent drugs and swaps them if they are in the wrong order
(depending on whether sorting is done in ascending or descending
order).

o Repeat Until Sorted: The outer loop ensures that this process is
repeated until the entire list is sorted.

o Since bubble sort is less efficient for large datasets, this function is
more suited for small to medium-sized inventories.

 Utility Functions:

o void printDrug (const Drug *drug)


o The printDrug function is a utility that prints out the details of a drug.
Here's what typically goes on:

o Formatting: Details such as the drug's code, name, price, and stock
count are formatted into a human-readable string.

o Printing: The formatted string is then printed to the console or


another output stream.

o The const qualifier indicates that the function will not modify the
drug's information.

o void initializeDrugList (DrugList *list)


o Often there's a need for an initializeDrugList function, which sets up
the DrugList:

o Set Length to Zero: Initially, the length of the list is set to 0, as


there are no drugs in the inventory.
o Optionally Load Data: If the program loads data from a file or
database, this function would contain the logic to read data and
populate the DrugList.

 Error Handling and Validation:

if (list->length >= MAX_SIZE) {


printf("Error: Inventory is full.\n");
return;
}

Inside functions like addNewDrug, there will be checks to ensure the list isn't
already full before attempting to add a new drug. If the inventory is full, it will
print an error message and exit the function.

You might also like