/* hw3p2.c 
   lunch truck problem with switch instead of cascading if.
   Same as problem 1, but changed if.
*/


#include <stdio.h>
#include <ctype.h>
#include "simpio.h"

#define COST_CHEESESTEAK     3.25
#define COST_TURKEY_HOAGIE  3.00
#define COST_POTATO_CHIPS    .50
#define COST_BANANA          .25
#define COST_SODA           1.00
#define COST_LEMONADE       1.25

void main()
{
  char item;
  double cheesesteak = 0, turkey_hoagie = 0, potato_chips = 0, banana = 0, 
    soda = 0, lemonade = 0, total, owe, pay;
  int qty, done = 0;

  printf("Welcome to the LunchExpress food truck!\n");

  while (!done) {
    printf("\n*************************\n");
    printf("*  C  Cheesesteak       *\n");
    printf("*  T  Turkey Hoagie     *\n");
    printf("*  P  Potato Chips      *\n");
    printf("*  B  Banana            *\n");
    printf("*  S  Soda              *\n");
    printf("*  L  Lemondade         *\n");
    printf("*                       *\n");
    printf("*  D  Done Purchasing   *\n");
    printf("*************************\n");

    printf("\nWhat would you like? ");
    item = toupper(getchar());
    if (item != '\n')
      while (getchar() != '\n');   /* read until the end of the line */

    if (item == 'C' || item == 'T' || item == 'P' || item == 'B' || 
	item == 'S' || item == 'L') {
      /* prompt for quantity */
      printf("How many? ");
      qty = GetInteger();
    }

    /* using switch instead of cascading if statements */
    switch (item) {
    case 'C':
      cheesesteak = COST_CHEESESTEAK * qty;
      break;
    case 'T':
      turkey_hoagie = COST_TURKEY_HOAGIE * qty;
      break;
    case 'P':
      potato_chips = COST_POTATO_CHIPS * qty;
      break;
    case 'B':
      banana = COST_BANANA * qty;
      break;
    case 'S':
      soda = COST_SODA * qty;
      break;
    case 'L':
      lemonade = COST_LEMONADE * qty;
      break;
    case 'D':
      done = 1;
      break;
    default:
      /* invalid selection */
      printf("That is not a valid menu item!\n");
      break;
    }
  }

  total = cheesesteak + turkey_hoagie + potato_chips + banana + 
    soda + lemonade;
  printf("Your total comes to $%.2f\n", total);
  printf("Please enter your payment amount: ");
  pay = GetReal();
  owe = total - pay;
  while (owe > 0) {
    printf("That is not enough, you still owe $%.2f\n", owe);
    printf("Please enter more money: ");
    pay += GetReal();
    owe = total - pay;
  }
  printf("Your change is $%.2f\n", pay - total);
  printf("Please come again!\n");
}




