0% found this document useful (0 votes)
34 views7 pages

The Explanation and Coding Defense PC112 Self Check Out

This code simulates a basic shopping experience that allows users to select items from different categories (essentials, vegetables, frozen goods) and generates a receipt. It uses a struct to store product information and loops to facilitate multiple selections. The user is prompted to input product numbers to add items to their cart, with the total price and quantity updating each time. After shopping, a receipt is printed listing all purchased items, quantities, and the total cost.

Uploaded by

k.angelacanque
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)
34 views7 pages

The Explanation and Coding Defense PC112 Self Check Out

This code simulates a basic shopping experience that allows users to select items from different categories (essentials, vegetables, frozen goods) and generates a receipt. It uses a struct to store product information and loops to facilitate multiple selections. The user is prompted to input product numbers to add items to their cart, with the total price and quantity updating each time. After shopping, a receipt is printed listing all purchased items, quantities, and the total cost.

Uploaded by

k.angelacanque
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/ 7

THE EXPLANATION

This code is a simple program that simulates a shopping experience for a store called
"COMMERCIA DE MARIA." It allows users to select items from different categories
(essentials, vegetables, and frozen goods) and then generates a receipt detailing their
purchases.

Let's break down the flow and purpose of the code:

Struct Definition: Defines a Product struct containing fields for name, price, and quantity.

Main Function:

Creates an array items of Product structs to store the purchased items.


Initializes variables total_price, total_quantity, res, and count.
User Interface:

Prints a welcome message and displays categories of items available for purchase:
essentials, vegetables, and frozen goods.
Item Selection Loop (do-while):

Prompts the user to enter a product number.


Based on the entered number, assigns the corresponding item's name and price to the
items array.
Asks the user for the quantity of the selected item.
Calculates the total price for the selected quantity and updates the total_price and
total_quantity.
Asks the user if they want to buy another product. The loop continues if the response is
'1' (yes).
Receipt Generation:

After the user finishes selecting items, it generates a receipt displaying purchased items,
their individual prices, quantities, and total prices.
Iterates through the items array to print each item's details in the receipt.
Summary:

Displays the total quantity of items purchased.


Displays the overall total price of all purchased items.
End of Program:

Returns 0 to signify successful execution.


Overall, the purpose of this code is to create a basic shopping simulation, allowing users
to select items and generate a receipt showing their purchases and the total cost. The code
utilizes a struct to store product information and loops to facilitate multiple item
selections until the user decides to stop.

Certainly! This part of the code is a case within a switch statement. In this particular case
(case 1), it handles the scenario when the user inputs '1' as the product number. Let's
break it down:

case 1:
strcpy(items[count].name, "Toothbrush");
items[count].price = 20;
break;
- case 1:: This line indicates that the code block below it will execute if the variable eN
(which stores the user-input product number) is equal to 1.

- strcpy(items[count].name, "Toothbrush");: This line copies the string "Toothbrush" into


the name field of the count`-th element in the items` array. It uses strcpy (string copy
function) to assign the name of the product to the corresponding element in the array.

- items[count].price = 20;: Sets the price of the product corresponding to the count`-th
element in the items` array to 20. In this case, the product 'Toothbrush' has a fixed price
of 20.

- break;: This break statement is used to exit the switch statement once the matching case
has been executed. It prevents the code from continuing to check other cases once a
matching case has been found and executed.

So, whenever the user enters '1' as the product number, this section of the code sets the
product name to "Toothbrush" and assigns a price of 20 to that particular item in the
items array.

Certainly! This section of the code is responsible for updating the total price and quantity
of the items purchased.

Let's break down what each line does:

1. items[count].price *= items[count].quantity;
- This line multiplies the price of the current item (items[count].price) by the quantity
of that item (items[count].quantity).
- For instance, if the price of an item is $10 and the user buys 3 of those items, this
operation would set the price field for that item to $30 (10 * 3).

2. total_price += items[count].price;
- After calculating the total price of the current item based on its quantity, this line adds
this subtotal to the total_price variable.
- It accumulates the prices of all purchased items into the total_price variable.

3. total_quantity += items[count].quantity;
- This line increments the total_quantity variable by the quantity of the current item.
- It keeps track of the total number of items purchased across all selections.

4. count++;
- Finally, count is incremented by 1.
- This ensures that the next item selected by the user will be stored in the next position
of the items array.

In essence, these lines perform calculations to update the total price and quantity of all
purchased items each time a new item is selected by the user, ensuring that the receipt
and summary at the end of the program accurately reflect the user's selections.

Certainly! This line of code:

printf("Total for this product: Php %d.00\n", items[count - 1].price);


This line of code is responsible for displaying the total cost for the current product that
has just been added to the cart.

Breaking it down:

- printf: This function is used to print formatted output to the console.

- "Total for this product: Php %d.00\n": This is the format string. It's the text that will be
printed to the console. The %d is a placeholder for an integer value that will be inserted
into the string using the items[count - 1].price value.

- items[count - 1].price: This accesses the price field of the most recently added item to
the items array. count represents the number of items added to the cart, and count - 1
points to the index of the last added item (arrays in C are zero-indexed, so count - 1
accesses the last element).

So, this line essentially displays the total cost of the most recently added product in the
format: "Total for this product: Php [price].00" where [price] is the actual price of the
product retrieved from items[count - 1].price.
Sure, here's a breakdown of the methods or functions used in this code:

1. *printf():*
- Used for printing formatted output to the console. It's used throughout the code to
display messages, item categories, prompts, and the final receipt.

2. *scanf():*
- Used for reading input from the user. It's primarily used to obtain the product number
and quantity that the user wants to purchase.

3. *strcpy():*
- Copies strings from one location to another. In this code, it's used to set the name of
the product in the items array based on the product number entered by the user.

4. *Switch-case statement:*
- This control structure is used to select different blocks of code to execute based on the
value of the eN variable (product number). It helps set the name and price of the selected
item in the items array.

5. *do-while loop:*
- Used to repeatedly prompt the user for product numbers and quantities until the user
decides to stop shopping (res != 1).

6. *For loop:*
- Iterates through the items array to print out the receipt, displaying product names,
prices per unit, quantities, and total prices for each item purchased.

These methods collectively facilitate the functionality of this shopping program. printf()
and scanf() handle input/output, strcpy() assists in setting product names, switch-case
makes decisions based on user input, and loops (do-while and for) control the program
flow for multiple product purchases and receipt generation.

THE CODING

#include <stdio.h>
#include <string.h>
struct Product {
char name[50];
int price;
int quantity;
};

int main() {
struct Product items[50];
int total_price = 0;
int total_quantity = 0;
int res;
int count = 0;

printf ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf ("WELCOME TO COMMERCIA DE MARIA! \n");
printf ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf ("• If you need it we have it • \n\n");

printf ("Enjoy Shopping!\n\n");

printf("Please enter the product number for your essentials one at a time.\n");
printf("\n");
printf("Essentials Categories: \n");
printf("Toothbrush (php20) - 1 Toothpaste (php15) - 2\n");
printf("Sanitary pad (php30) - 3 Tissue (php12) - 4\n");
printf("Soap (php20) -5 Shampoo (php8) - 6\n");
printf("Conditioner (php10) - 7 Wet wipes (php20) - 8\n");
printf("Cotton (php10) - 9 Rubbing Alcohol (php70) - 10\n");
printf(" \n");
printf(" \n");
printf("Vegetables Categories: \n");
printf("Cabbage (php100) - 11 Garlic (php100) - 16\n");
printf("Carrots (php80) - 12 Cucumber (php80) - 17\n");
printf("Potato (php180) - 13 Sayote (php50) - 18\n");
printf("Squash (php60) - 14 Stringbeans (php60) - 19\n");
printf("Onion (php120) - 15 Eggplants (php80) - 20\n");
printf(" \n");
printf(" \n");
printf("Frozen Goods Categories: \n");
printf("Hotdog (php40) - 21 \n");
printf("Ham (php60) - 22 \n");
printf("Chicken Nuggets (php120) - 23 \n");
printf("Bacon (php250) - 24 \n");
printf("Tocino (php180) - 25 \n");

printf(" \n");
printf(" \n");

do {
printf("Enter the product number: ");
int eN;
scanf("%d", &eN);
switch (eN) {

case 1:
strcpy(items[count].name, "Toothbrush");
items[count].price = 20;
break;

case 2:
strcpy(items[count].name, "Toothpaste");
items[count].price = 15;
break;

case 3:
strcpy(items[count].name, "Sanitary pad");
items[count].price = 30;
break;

case 4:
strcpy(items[count].name, "Tissue");
items[count].price = 12;
break;
case 5:
strcpy(items[count].name, "Soap");
items[count].price = 20;
break;
case 6:
strcpy(items[count].name, "Shampoo");
items[count].price = 8;
break;
case 7:
strcpy(items[count].name, "Conditioner");
items[count].price = 10;
break;
case 8:
strcpy(items[count].name, "Wet wipes");
items[count].price = 20;
break;
case 9:
strcpy(items[count].name, "Cotton");
items[count].price = 10;
break;
case 10:
strcpy(items[count].name, "Rubbing alcohol");
items[count].price = 70;
break;
case 11:
strcpy(items[count].name, "Cabbage");
items[count].price = 100;
break;
case 12:
strcpy(items[count].name, "Carrots");
items[count].price = 80;
break;
case 13:
strcpy(items[count].name, "Potato");
items[count].price = 180;
break;
case 14:
strcpy(items[count].name, "Squash");
items[count].price = 60;
break;
case 15:
strcpy(items[count].name, "Onion");
items[count].price = 120;
break;
case 16:
strcpy(items[count].name, "Garlic");
items[count].price = 100;
break;
case 17:
strcpy(items[count].name, "Cucumber");
items[count].price = 80;
break;
case 18:
strcpy(items[count].name, "Sayote");
items[count].price = 50;
break;
case 19:
strcpy(items[count].name, "Stringbeans");
items[count].price = 60;
break;
case 20:
strcpy(items[count].name, "Eggplant");
items[count].price = 80;
break;
case 21:
strcpy(items[count].name, "Hotdog");
items[count].price = 40;
break;
case 22:
strcpy(items[count].name, "Ham");
items[count].price = 60;
break;
case 23:
strcpy(items[count].name, "Chicken nuggets");
items[count].price = 120;
break;
case 24:
strcpy(items[count].name, "Bacon");
items[count].price = 250;
break;
case 25:
strcpy(items[count].name, "Tocino");
items[count].price = 180;
break;

default:
strcpy(items[count].name, "Unknown");
items[count].price = 0;
printf("Not a valid product number\n");
break;
}
printf("Enter quantity: ");
scanf("%d", &items[count].quantity);

items[count].price *= items[count].quantity;
total_price += items[count].price;
total_quantity += items[count].quantity;
count++;

printf("Total for this product: Php %d.00\n", items[count - 1].price);


printf("\nDo you have another product? [yes = 1/no = 2] ");
scanf("%d", &res);

} while (res == 1);

printf("\n---------------------RECEIPT---------------------\n");
printf("Product\t\t\tPrice\tQuantity\tTotal\n");
printf("-------------------------------------------------\n");
for (int i = 0; i < count; i++) {
printf("%s\t\tPhp %d.00\t%d\t\tPhp %d.00\n",
items[i].name, items[i].price / items[i].quantity,
items[i].quantity, items[i].price);
}
printf("-------------------------------------------------\n");
printf("Total quantity of items: %d\n", total_quantity);
printf("This is your total price: Php %d.00\n", total_price);

return 0;
}

You might also like