Prop 2
Prop 2
Brief Description: This program will allow users to browse through different product categories, pick
items they like, specify purchase quantities, and see their total cost before completing their transaction.
The design will prioritize an intuitive and user-friendly interface.
● while loop: This repeats a block of code as long as a condition is true. It keeps going until
the condition becomes false.
● if statement: This checks a condition. If the condition is true, it runs a block of code;
otherwise, it skips it.
● else if statement: This is used after an if statement to check another condition only if the
previous if condition was false.
● switch statement: This is like a more efficient way to do many if/else if statements. It
checks a value and jumps to the matching case.
● case: Used inside a switch statement to specify what to do if the value matches a
particular case.
● break statement: This stops a loop (like while or for) or exits a switch statement
immediately.
● continue statement: This skips to the next iteration of a loop (like while or for) without
finishing the current iteration.
● display_welcome(), display_categories(), display_items(), get_item_price(), get_valid_input():
These are functions (mini-programs) that perform specific tasks within the main
program.
● Arrays : Data structures that store a collection of elements of the same data type in
contiguous memory locations. They are accessed using an index (starting from 0).
#include <stdio.h>
#include <stdlib.h>
void display_welcome() {
printf("\n**********************************************************************\n\n");
printf(" Welcome to the Shopping Cart System (SCS)\n");
printf("************************************************************************\n\n");
printf("Developed by: Lovely Anne A. Rey \n\n");
printf("BS Information Technology - First Year\n");
printf("Southern Luzon State University\n\n\n");
}
void display_categories() {
printf("AVAILABLE CATEGORIES:\n\n");
printf("1. Health & Personal Care\n");
printf("2. Men's Apparel\n");
printf("3. Makeup & Fragrances\n");
printf("4. Women's Apparel\n");
printf("0. Checkout\n\n");
}
void display_items(int category) {
if (category == 1) {
printf("11. Moisturizer Soothing Gel 100ml 118.00\n");
printf("12. Sunscreen SPF50 PA++++ 129.00\n");
printf("13. Beauty Facial Foam 50g 95.00\n");
printf("14. Paracetamol Biogesic 500mg 10 tablets 50.00\n");
} else if (category == 2) {
printf("21. Printed Boxer Underwear Cotton 3pcs 105.00\n");
printf("22. Plain Hoodie Jacket 148.00\n");
printf("23. Casual Polo Shirt 199.00\n");
printf("24. Sleeve Lapel Collar Polo Shirt 156.00\n");
} else if (category == 3) {
printf("31. Panthenol Therapy 24H Lip Serum 10g 99.00\n");
printf("32. Volumizing Waterproof Lasting Mascara 50.00\n");
printf("33. Makeup Setting Spray Oil Control 158.00\n");
printf("34. Lip Glow Oil (shade hugging) 108.00\n");
} else if (category == 4) {
printf("41. Summer Dress 89.00\n");
printf("42. Casual Blouse 75.00\n");
printf("43. Denim Skirt 120.00\n");
printf("44. Halter Top Double Lining 99.00\n");
}
}
float get_item_price(int item_code) {
switch (item_code) {
case 11: return 118.00;
case 12: return 129.00;
case 13: return 95.00;
case 14: return 50.00;
case 21: return 105.00;
case 22: return 148.00;
case 23: return 199.00;
case 24: return 156.00;
case 31: return 99.00;
case 32: return 50.00;
case 33: return 158.00;
case 34: return 108.00;
case 41: return 89.00;
case 42: return 75.00;
case 43: return 120.00;
case 44: return 99.00;
default: return 0;
}
}
int get_valid_input() {
int input;
if (scanf("%d", &input) != 1) {
// Clear the input buffer
while (getchar() != '\n'); // Discard invalid input
printf("Invalid input. Please enter a number.\n\n");
return get_valid_input(); // Retry input
}
return input;
}
int main(void) {
display_welcome();
int category, item_code, quantity;
float total = 0;
int cart_items[100];
int cart_quantities[100];
int cart_count = 0;
while (1) {
display_categories();
printf("Select a category: \n\n");
category = get_valid_input();
if (category == 0) {
printf("Proceeding to checkout...\n\n");
break;
}
if (category < 1 || category > 4) {
printf("Invalid category, please try again.\n");
continue;
}
display_items(category);
while (1) {
printf("Choose another item or enter 0 to go back to categories: ");
item_code = get_valid_input();
if (item_code == 0) {
break;
}
printf("Enter Quantity: ");
quantity = get_valid_input();
if (quantity >= 6) {
printf(" Sorry this item is out of stock. Please enter a quantity less than 6.\n");
continue;
}
if (quantity <= 0) {
printf("Invalid quantity. Please enter a number greater than 0.\n");
continue;
}
float item_price = get_item_price(item_code);
if (item_price <= 0) {
printf("Invalid item code.\n\n");
continue;
}
cart_items[cart_count] = item_code;
cart_quantities[cart_count] = quantity;
cart_count++;
printf("Item added to cart.\n\n");
}
}
printf("\n==============================================================\n\n");
printf(" Checkout Summary:\n");
printf("\
n================================================================\n\n");
for (int i = 0; i < cart_count; i++) {
int item_code = cart_items[i];
int quantity = cart_quantities[i];
float item_price = get_item_price(item_code);
printf("Item Code: %d | Quantity: %d | Subtotal: %.2f\n", item_code, quantity, item_price *
quantity);
total += item_price * quantity;
}
printf("\nYour Total Amount is: %.2f\n", total);
printf("Thank you for shopping with us!\n");
return 0;
}
OUTPUT: