Code:
/*
This program is designed to be a basic cash register that prompts for a valid item code and quantity and computes the subtotal, tax, and change back.
Written by: Bernard Wu
Date: Feb 3rd 2013
ID: 103321178
*/
#include <stdio.h>
//function prototypes
void PrintHeader();
void PrintFooter();
void PrintHeader()
{
printf("---------------------------------------------------------------------\n\n"); //This is where the header is shown to be called in the main$
printf(" _-=== Windsor Market Farm Fresh ===-_\n\n");
printf("Welcome!\n");
}
void PrintFooter()
{
printf("---------------------------------------------------------------------\n\n");
printf("THANK YOU FOR SHOPPING AT WINDSOR MARKET FARM FRESH, PLEASE COME AGAIN\n\n"); // This is where the footer is shown to be called at the end of $
printf("---------------------------------------------------------------------\n");
}
int main()
{
int code; //use this variable to enter the product codes
float quantity; //keeps the quantity of purchase in a float for decimals
float subTotal = 0; //keeps a running subtotal of the purchase
float paidCash; //keeps how much money was given by the customer to pay for goods
float price; //displays the price of each item individually
int validCode; //bool check if the product code entered is valid or not
//Display Heading
PrintHeader();
//Main Program loop
do
{
do
{
printf("Enter Item Code: "); //prompt for item code
scanf("%d", &code); //retrieve input from keyboard
validCode = 1; //this resets the bool flag to 1 if an invalid code was entered
//Check if 5 digits, if not code is not valid
if ((code < 10000 || code > 99999) && (code != 0 || code != 00)){
validCode = 0;
}
//Check 1st digit, cannot be more than 6
if (code / 10000 == 7 && (code != 0 || code != 00))
{
validCode = 0;
}
if (code / 10000 == 8 && (code != 0 || code != 00))
{
validCode = 0;
}
if (code / 10000 == 9 && (code != 0 || code != 00))
{
validCode = 0;
}
//Check 2nd digit, cannot be greater than 3, 4 or 5 depending on$
if (code / 1000 % 10 > 3 && (code != 0 || code != 00) && (code / 10000 == 1 || (code / 10000 == 2 || code / 10000 == 3))) // 2nd dig must be > 3$
{
validCode = 0;
}
if (code / 1000 % 10 > 4 && (code != 0 || code != 00) && (code / 10000 == 4)) // 2nd dig must be > 4 if 1st dig is a 4
{
validCode = 0;
}
if (code / 1000 % 10 > 5 && (code != 0 || code != 00) && (code / 10000 == 5)) // 2nd dig must be > 5 if 1st dig is 5
{
validCode = 0;
}
if (code / 10000 == 6 && code / 1000 % 10 > 0 && (code != 0 || code != 00))
{
validCode = 0;
}
if (validCode == 0){ //if code is invalid, it will loop back to the beginning and ask for$
printf("<INVALID CODE, PLEASE TRY AGAIN>\n");
}
}while (validCode != 1); // if code is valid, prompt for quantity
if ((validCode == 1 && code != 0) || code != 00){
printf("Enter Quantity: "); // prompt quantity
scanf("%f", &quantity); // retrieve from keyboard
}
//
//This section prints what item was purchased along with it's quantity and updated subtotals
//Calculations and new variable delcarations are also done here
//
if (code / 10000 == 1 && code / 1000 % 10 == 1){
price = (float)(code % 1000) / 100;
printf("\t > Produce Bulk, local vegetables, $%.2f/lb @ %.1f = $%.15.f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 1 && code / 1000 % 10 == 2){
price = (float)(code % 1000) / 100;
printf("\t > Produce Bulk, local fruits, $%.2f/lb @ %.1f = $%19.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 1 && code / 1000 % 10 == 3){
price = (float)(code % 1000) / 100;
printf("\t > Produce Bulk, imports, $%.2f/lb @ %.1f = $%24.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 2 && code / 1000 % 10 == 1){
price = (float)(code % 1000) / 100;
printf("\t > Produce Bulk, local vegetables, $%.2f each @ %.1f = $%13.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 2 && code / 1000 % 10 == 2){
price = (float)(code % 1000) / 100;
printf("\t > Produce Bulk, local fruits, $%.2f each @ %.1f = $%17.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 2 && code / 1000 % 10 == 3){
price = (float)(code % 1000) / 100;
printf("\t > Produce Bulk, imports, $%.2f each @ %.1f = $%22.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 3 && code / 1000 % 10 == 1){
price = (float)(code % 1000) / 100;
printf("\t > Floral, roses, $%.2f each @ %.1f = $%30.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 3 && code / 1000 % 10 == 2){
price = (float)(code % 1000) / 100;
printf("\t > Floral, bouquet, $%.2f each @ %.1f = $%28.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 3 && code / 1000 % 10 == 3){
price = (float)(code % 1000) / 100;
printf("\t > Floral, house plant, $%.2f each @ %.1f = $%24.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 4 && code / 1000 % 10 == 1){
price = (float)(code % 1000) / 100;
printf("\t > Dairy, milk, $%.2f each @ %.1f = $%32.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 4 && code / 1000 % 10 == 2){
price = (float)(code % 1000) / 100;
printf("\t > Dairy, eggs, $%.2f each @ %.1f = $%32.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 4 && code / 1000 % 10 == 3){
price = (float)(code % 1000) / 100;
printf("\t > Dairy, butter, $%.2f each @ %.1f = $%30.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 4 && code / 1000 % 10 == 4){
price = (float)(code % 1000) / 100;
printf("\t > Dairy, yogurt, $%.2f each @ %.1f = $%30.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 5 && code / 1000 % 10 == 1){
price = (float)(code % 1000) / 100;
printf("\t > Meats, beef, $%.2f/lb @ %.1f = $%34.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 5 && code / 1000 % 10 == 2){
price = (float)(code % 1000) / 100;
printf("\t > Meats, chicken, $%.2f/lb @ %.1f = $%31.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 5 && code / 1000 % 10 == 3){
price = (float)(code % 1000) / 100;
printf("\t > Meats, turkey, $%.2f/lb @ %.1f = $%32.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 5 && code / 1000 % 10 == 4){
price = (float)(code % 1000) / 100;
printf("\t > Meats, pork, $%.2f/lb @ %.1f = $%34.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 5 && code / 1000 % 10 == 5){
price = (float)(code % 1000) / 100;
printf("\t > Meats, rabbit, $%.2f/lb @ %.1f = $%32.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 6 && code / 1000 % 10 == 0){
price = (float)(code % 1000) / 100;
printf("\t > Other, other item, $%.2f each @ %.1f = $%26.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
}while(code !=0); //loop will be repeated until item code 0 is entered
//print and calculation statments to determine subtotal, total, tax and change
printf("---------------------------------------------------------------------\n");
printf("\t> SUB-TOTAL\t $%.2f\n", subTotal);
printf("\t> TAX@7%%\t $%.2f\n", subTotal * 0.07);
printf("---------------------------------------------------------------------\n");
printf("\t> TOTAL\t $%.2f\n", subTotal * 1.07);
printf("---------------------------------------------------------------------\n\n");
printf("Enter Cash Amount: "); // prompts for how much money customer gave
scanf("%f", &paidCash); // retrieves input from keyboard
printf("\t> CHANGE TO CUSTOMER DUE: $%.2f\n\n", paidCash - subTotal * 1.07); // displays how much change customer owes (if any)
//Display Footer
PrintFooter();
return 0; //ends program
}