Question

(Calculating Sales) An online retailer sells five products whose retail prices are as follows: Product 1,...

(Calculating Sales) An online retailer sells five products whose retail prices are as follows: Product 1, $$2.98; Product 2, $4.50; Product 3, $9.98; Product 4, $4.49, and Product 5, $6.87. Write an application that reads a series of pairs of numbers s follows:

a) product number

b) quanity sold

Your program should use a switch statement to determine the retail price for each product. It should calculate and display the total retail value of all products sold. Use a sentinel-controlled loop to determine when the program should stop looping and display the final results. Use these modifications:

1. Create a Product class.

2. The Product should determine the price based on the ProductID

3. The Product should track the quantity sold.

4. The Product should have a calcTotal method to return total for product.

5.  No console I/O in Product class.

Homework Answers

Answer #1

#include <stdio.h>

int main()
   {
   float list[5]={2.98,4.50,9.98,4.49,6.87   };
   int producttype=0;
   int qty;
   double total=0;
   printf("Below is the List of Products with Price\n");
   for(int i=0;i<5;i++)
   {
       printf("Product%d Price is %.2f\n",i+1,list[i]);
   }
   printf("\nEnter pairs of Product Numbers and Its Quantities.\n");
   printf("Enter -1 for the Product Number to end input.\n");
  
  
   while(true){
      
       scanf("%d",&producttype);
       if(producttype == -1)
       {
       break;  
       }
       scanf("%d",&qty);
      
       switch(producttype){
          
           case 1:
               total=total+qty*2.98;
               break;
           case 2:
               total=total+qty*4.50;
               break;
           case 3:
               total=total+qty*9.98;
               break;
           case 4:
               total=total+qty*4.49;
               break;
           case 5:
               total=total+qty*6.87;
               break;
           default:
               printf("Wrong Product Id\n");
               break;
              
       }
      
      
   }
   printf("Sum of the purchases is :%.2f\n",total);
}

OUTPUT

Second Method

#include <stdio.h>

float calcTotal(int quant, float num,float tot)
{
   tot = tot + (quant*num);
   return tot;
}

int main()
   {
   float list[5]={2.98,4.50,9.98,4.49,6.87   };
   int producttype=0;
   int qty;
   double total=0;
   printf("Below is the List of Products with Price\n");
   for(int i=0;i<5;i++)
   {
       printf("Product%d Price is %.2f\n",i+1,list[i]);
   }
   printf("\nEnter pairs of Product Numbers and Its Quantities.\n");
   printf("Enter -1 for the Product Number to end input.\n");
  
  
   while(true){
      
       scanf("%d",&producttype);
       if(producttype == -1)
       {
       break;  
       }
       scanf("%d",&qty);
      
       switch(producttype){
          
           case 1:
               total=calcTotal(qty,list[0],total);
               break;
           case 2:
               total=calcTotal(qty,list[1],total);
               break;
           case 3:
               total=calcTotal(qty,list[2],total);
               break;
           case 4:
               total=calcTotal(qty,list[3],total);
               break;
           case 5:
               total=calcTotal(qty,list[4],total);
               break;
           default:
               printf("Wrong Product Id\n");
               break;
              
       }
      
      
   }
   printf("Sum of the purchases is :%.2f\n",total);
}

OUTPUT

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT