Code:
#include <stdio.h>
#include <stdlib.h>
#define NINE_COE 9
#define NINTYNINE_COE 99
#define RJ12 7.70
#define RJ45 8.20
#define FW40 8.50
#define DB24 9.00
#define FIVE_PERCENT 19/20
#define TEN_PERCENT 9/10
#define NEXT_DAY 15
#define SAME_DAY 30
float getComponentType(char T, char Ta, char Tb, char Tc, float P);
int getQuantity(int Q, int RQ);
float calcPrice(float Price, int ReturnQ, float DP, float TP);
float getDeliveryFee(int O, float F);
float calcTotalCharges(float Fee, float DiscountP);
int main(void)
{
char T, Ta, Tb, Tc;
int Q, O, RQ;
float TC, Total, F, DP, Price, P, DiscountP, TP, Fee;
int ReturnQ;
printf("Enter the component type (RJ12, RJ45, FW40 or DB24): ");
scanf("%c%c%c%c", &T, &Ta, &Tb, &Tc);
Price = getComponentType( T, Ta, Tb, Tc, P);
printf("Enter the quantity: ");
scanf("%d", &Q);
ReturnQ = getQuantity( Q, RQ);
DiscountP = calcPrice(Price, ReturnQ, DP, TP);
printf("Component Price $%.2f\n", DiscountP);
printf("Select a delivery option (1 - same day, or 2 - next day): ");
scanf("%d", &O);
Fee = getDeliveryFee(O, F);
Total = calcTotalCharges( Fee, DiscountP);
printf("Total Charges: $%.2f\n", Total);
return (0);
}
float getComponentType(char T, char Ta, char Tb, char Tc, float P)
{
if (T == 'R' && Ta == 'J' && Tb == '1' && Tc == '2' || T == 'r' || Ta == 'j')
P = RJ12;
else if (T == 'R' && Ta == 'J' && Tb == '4' && Tc == '5' || T == 'r' || Ta == 'j')
P = RJ45;
else if (T == 'F' && Ta == 'W' && Tb == '4' && Tc == '0' || T == 'f' || Ta == 'w')
P = FW40;
else if (T == 'D' && Ta == 'B' && Tb == '2' && Tc == '4' || T == 'd' || T == 'b')
P = DB24;
else
{ printf("Wrong Component Type\n");
exit(0);
}
return (P);
}
int getQuantity(int Q, int RQ)
{
if (Q > 0)
RQ = Q;
else
{ printf("Wrong Quantity\n");
exit(0);
}
return (RQ);
}
float calcPrice(float Price, int ReturnQ, float DP, float TP)
{
TP = Price*ReturnQ;
if (ReturnQ > NINE_COE && ReturnQ <= NINTYNINE_COE)
DP = TP*FIVE_PERCENT;
else if (ReturnQ > NINTYNINE_COE)
DP = TP*TEN_PERCENT;
else
DP = TP;
return (DP);
}
float getDeliveryFee(int O, float F)
{
if (O == 1)
F = SAME_DAY;
else if (O == 2)
F = NEXT_DAY;
else
{ printf("Invalid Option\n");
exit(0); }
return (F);
}
float calcTotalCharges(float DiscountP, float Fee)
{
return (DiscountP+Fee);
}