0% found this document useful (0 votes)
28 views10 pages

Manage Drink

This C program defines a Drink struct to store drink data like code, name, type, unit, and price. It initializes an array of Drink structs and implements functions to add, edit, delete, sort, and export drink data. The main function displays a menu and calls the appropriate functions based on user input to manage the drink database.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views10 pages

Manage Drink

This C program defines a Drink struct to store drink data like code, name, type, unit, and price. It initializes an array of Drink structs and implements functions to add, edit, delete, sort, and export drink data. The main function displays a menu and calls the appropriate functions based on user input to manage the drink database.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

#include <stdio.

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

#define MAX_DRINKS 100

struct Drink {
int code;
char name[50];
char type[50];
char unit[10];
float price;
};

struct Drink drinks[MAX_DRINKS];


int num_drinks = 0;

// Function prototypes
void addDrink();
void editPrice(int code);
void editName(int code);
void deleteDrink(int code);
void sortDescendingType();
void sortAscendingPrice();
void exportEntireList();
void exportByCode(int code);
void exportByPriceRange(float minPrice, float maxPrice);
void exportStatistics();

int main() {
int choice;
do {
printf("\nMenu:\n");
printf("01. Enter drink information\n");
printf("02. Edit price of drinks (search by Drink code)\n");
printf("03. Edit information about drink name (search by Drink code)\n");
printf("04. Delete information (search by Drink code)\n");
printf("05. Sort the list descending by Drink Type\n");
printf("06. Sort the list ascending by Price\n");
printf("07. Export entire list\n");
printf("08. Export list by Drink code\n");
printf("09. Export list by Price in a certain range\n");
printf("10. Export Average price, Highest price, Lowest price\n");
printf("0. Exit\n");
printf("Choose an option: ");
scanf("%d", &choice);

switch (choice) {
case 1:
addDrink();
break;
case 2:
if (num_drinks == 0) {
printf("There are no drinks in the database! Please add the drink data to the database.\n");
break;
}
{
int code;
printf("Enter Drink code: ");
scanf("%d", &code);
editPrice(code);
}
break;
case 3:
if (num_drinks == 0) {
printf("There are no drinks in the database! Please add the drink data to the database.\n");
break;
}
{
int code;
printf("Enter Drink code: ");
scanf("%d", &code);
editName(code);
}
break;
case 4:
if (num_drinks == 0) {
printf("There are no drinks in the database! Please add the drink data to the database.\n");
break;
}
{
int code;
printf("Enter Drink code: ");
scanf("%d", &code);
deleteDrink(code);
}
break;
case 5:
if (num_drinks == 0) {
printf("There are no drinks in the database! Please add the drink data to the database.\n");
break;
}
sortDescendingType();
break;
case 6:
if (num_drinks == 0) {
printf("There are no drinks in the database! Please add the drink data to the database.\n");
break;
}
sortAscendingPrice();
break;
case 7:
exportEntireList();
break;
case 8:
if (num_drinks == 0) {
printf("There are no drinks in the database! Please add the drink data to the database.\n");
break;
}
{
int code;
printf("Enter Drink code: ");
scanf("%d", &code);
exportByCode(code);
}
break;
case 9:
if (num_drinks == 0) {
printf("There are no drinks in the database! Please add the drink data to the database.\n");
break;
}
{
float minPrice, maxPrice;
printf("Enter minimum price: ");
scanf("%f", &minPrice);
printf("Enter maximum price: ");
scanf("%f", &maxPrice);
exportByPriceRange(minPrice, maxPrice);
}
break;
case 10:
if (num_drinks == 0) {
printf("There are no drinks in the database! Please add the drink data to the database.\n");
break;
}
exportStatistics();
break;
case 0:
printf("Exiting program.\n");
break;
default:
printf("Invalid option.\n");
}
} while (choice != 0);

return 0;
}

void addDrink() {
if (num_drinks < MAX_DRINKS) {
struct Drink newDrink;
printf("Enter Drink code: ");
scanf("%d", &newDrink.code);
printf("Enter Drink name: ");
scanf(" %[^\n]", newDrink.name);
printf("Enter Drink type: ");
scanf("%s", newDrink.type);
printf("Enter unit: ");
scanf("%s", newDrink.unit);
printf("Enter price: ");
scanf("%f", &newDrink.price);

drinks[num_drinks] = newDrink;
num_drinks++;

printf("Drink added successfully.\n");


} else {
printf("Drink list is full, cannot add more.\n");
}
}

void editPrice(int code) {


int i;
for (i = 0; i < num_drinks; i++) {
if (drinks[i].code == code) {
printf("Enter new price: ");
scanf("%f", &drinks[i].price);
printf("Price updated successfully.\n");
return;
}
}
printf("Drink with code %d not found.\n", code);
}

void editName(int code) {


int i;
for (i = 0; i < num_drinks; i++) {
if (drinks[i].code == code) {
printf("Enter new name: ");
scanf(" %[^\n]", drinks[i].name);
printf("Name updated successfully.\n");
return;
}
}
printf("Drink with code %d not found.\n", code);
}

void deleteDrink(int code) {


int found = 0;
int i, j;
for (i = 0; i < num_drinks; i++) {
if (drinks[i].code == code) {
found = 1;
for (j = i; j < num_drinks - 1; j++) {
drinks[j] = drinks[j + 1];
}
num_drinks--;
printf("Drink deleted successfully.\n");
break;
}
}
if (!found)
printf("Drink with code %d not found.\n", code);
}

void sortDescendingType() {
int i, j;
struct Drink temp;
for (i = 0; i < num_drinks - 1; i++) {
for (j = 0; j < num_drinks - i - 1; j++) {
if (strcmp(drinks[j].type, drinks[j + 1].type) < 0) {
temp = drinks[j];
drinks[j] = drinks[j + 1];
drinks[j + 1] = temp;
}
}
}
printf("List sorted in descending order by Drink Type.\n");
}

void sortAscendingPrice() {
int i, j;
struct Drink temp;
for (i = 0; i < num_drinks - 1; i++) {
for (j = 0; j < num_drinks - i - 1; j++) {
if (drinks[j].price > drinks[j + 1].price) {
temp = drinks[j];
drinks[j] = drinks[j + 1];
drinks[j + 1] = temp;
}
}
}
printf("List sorted in ascending order by Price.\n");
}

void exportEntireList() {
int i;
printf("Drink list:\n");
printf("Code\t| Name\t\t\t\t\t| Type\t\t\t| Unit\t| Price\n");
for (i = 0; i < num_drinks; i++) {
printf("%d\t| %-30s\t| %-20s\t| %-5s\t| %.2f\n", drinks[i].code, drinks[i].name, drinks[i].type,
drinks[i].unit, drinks[i].price);
}
}

void exportByCode(int code) {


int i;
printf("Drink with code %d:\n", code);
printf("Code\t| Name\t\t\t\t\t| Type\t\t\t| Unit\t| Price\n");
for (i = 0; i < num_drinks; i++) {
if (drinks[i].code == code) {
printf("%d\t| %-30s\t| %-20s\t| %-5s\t| %.2f\n", drinks[i].code, drinks[i].name, drinks[i].type,
drinks[i].unit, drinks[i].price);
return;
}
}
printf("Drink with code %d not found.\n", code);
}

void exportByPriceRange(float minPrice, float maxPrice) {


int i, found = 0;
printf("Drink list with prices between %.2f and %.2f:\n", minPrice, maxPrice);
printf("Code\t| Name\t\t\t\t\t| Type\t\t\t| Unit\t| Price\n");
for (i = 0; i < num_drinks; i++) {
if (drinks[i].price >= minPrice && drinks[i].price <= maxPrice) {
printf("%d\t| %-30s\t| %-20s\t| %-5s\t| %.2f\n", drinks[i].code, drinks[i].name, drinks[i].type,
drinks[i].unit, drinks[i].price);
found = 1;
}
}
if (!found)
printf("No drinks found in this price range.\n");
}

void exportStatistics() {
int i;
if (num_drinks == 0) {
printf("There are no drinks in the database! Please add the drink data to the database.\n");
return;
}
float total_price = 0, max_price = drinks[0].price, min_price = drinks[0].price;
for (i = 0; i < num_drinks; i++) {
total_price += drinks[i].price;
if (drinks[i].price > max_price)
max_price = drinks[i].price;
if (drinks[i].price < min_price)
min_price = drinks[i].price;
}
float average_price = total_price / num_drinks;
printf("Average price: %.2f\n", average_price);
printf("Highest price: %.2f\n", max_price);
printf("Lowest price: %.2f\n", min_price);
}

You might also like