Code:
#include <stdio.h>
int getCustomer();
void flushKeyboard(void);
float custOrder(int custNo, float ttlArt, float ttlBeet, float ttlCar, float ttlOrd);
float calcDiscount(float costO);
char showMenu(int custNo);
float getWeight();
float calcShipping(float shipWeight);
int main(void)
{
int custNo;
float ttlArt, ttlBeet, ttlCar, ttlOrd;
custNo = getCustomer();
printf("Customer number: %d\n", custNo);
while(custNo != 0)
{
custOrder(custNo, ttlArt, ttlBeet, ttlCar, ttlOrd);
}
custNo = getCustomer();
}
// This function is designed to prompt a user for their customer number.
int getCustomer()
{
int custNo; // Local Variable
printf("Enter the customer number (0 to quit):\n");
scanf("%d",&custNo);
flushKeyboard();
while(custNo<0)
{
printf("Invalid customer number, please re-enter.");
scanf("%d", &custNo);
flushKeyboard();
}
return custNo;
}
//This function is where the user enters the quantities and calculates the cost of their order.
float custOrder(int custNo, float ttlArt, float ttlBeet, float ttlCar, float ttlOrd)
{
float weightA, weightB, weightC, shipCost, shipWeight, discount, w, costA, costB, costC, costO;
char option;
option = showMenu(custNo);
printf("You chose: %c\n", option);
while(option!='Q')
{
switch(option)
{
case ('A'):
{
printf("Enter the weight of artichokes ordered:\n");
w = getWeight();
weightA = weightA + w;
ttlArt = ttlArt + w;
option = showMenu(custNo);
}
case ('B'):
{
printf("Enter the weight of beets ordered:\n");
w = getWeight();
weightB = weightB + w;
ttlBeet = ttlBeet + w;
option = showMenu(custNo);
}
case ('C'):
{
printf("Enter the weight of carrots ordered:\n");
w = getWeight();
weightC = weightC + w;
ttlCar = ttlCar + w;
option = showMenu(custNo);
}
case ('Q'):
{
return;
}
default:
printf("Invalid Option.\n");option = showMenu(custNo);
option = showMenu(custNo);
}
option = showMenu(custNo);
printf("You chose: %c\n", option);
}
costA = weightA * 1.25;
costB = weightB * 0.65;
costC = weightC * 0.89;
costO = costA + costB + costC;
discount = calcDiscount(costO);
costO = costO - discount;
shipWeight = weightA + weightB + weightC;
shipCost = calcShipping(shipWeight);
costO = costO + shipCost;
printf("===============================================\n");
printf("CUSTOMER NUMBER: %d\n");
printf("ITEM COST/KG ORDERED COST\n");
printf("===============================================\n");
if(costA>0)
{
printf("Artichokes 1.25/kg %.2f kg $ %.2f\n", weightA, costA);
}
if(costB > 0)
printf("Beets 0.65/kg %.2f kg $ %.2f\n", weightB, costB);
if(costC > 0)
printf("Carrots 0.89/kg %.2f kg $ %.2f\n", weightC, costC);
printf("-----------------------------------------------\n");
if(discount > 0)
printf("Discount : $ %.2f", discount);
printf("Shipping Weight : %.2f kg \nShipping Cost : $%.2f\nTotal Order : $%.2f\n\n\n", shipWeight, shipCost, costO);
ttlOrd = ttlOrd + costO;
}
// This function calculates the discount (if any).
float calcDiscount(float costO)
{
float discount;
discount = 0;
if(costO > 100.00)
discount = costO *0.05;
return discount;
}
// This function gets the weight of an order (in kg) from the user.
float getWeight()
{
float w; // local Variable
scanf("%f", &w);
while(w<0)
{
printf("Invalid Weight, please re-enter.\n");
scanf("%f", &w);
}
return w;
}
// This function displays a menu to the user asking them to select an item to order or exit the program.
char showMenu(int custNo)
{
char option;
printf("A - Enter weight of artichokes\n");
printf("B - Enter weight of beets\n");
printf("C - Enter weight of carrots\n");
printf("Q - Exit the ordering process for customer %d\n", custNo);
printf("Enter option:\n");
scanf("%c", &option);
option = toupper(option);
while(option!='A' && option!='B' && option!='C' && option!='Q')
{
printf("Invalid option, please re-enter.\n");
scanf("%c", &option);
option = toupper(option);
printf("You chose: %c <-still in showMenu\n", option);
}
return option;
}
//This function calculates the shipping cost of the order.
float calcShipping(float shipWeight)
{
float shipCost;
shipCost = 0.00;
if(shipWeight>20.0)
shipCost = 8.00 + (0.1 * shipWeight);
if(shipWeight>5.0)
shipCost = 10.00;
if(shipWeight>0.0)
shipCost = 3.50;
return shipCost;
}
//This module flushes the keyboard buffer to allow the user to enter multiple consecutive values into a scanf statement.
void flushKeyboard(void)
{
int ch; // Dummy variable to read data into.
while((ch = getc(stdin)) != EOF && ch != '\n');
}
Example of the program's current output: