
/* hw3p1.c 
   lunch truck problem with cascading 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;
  /* use these variables to keep running totals of each type of purchase.
     Another way to do it is to use one total variable instead of separate
     totals for the difference choices.
  */
  double cheesesteak = 0, turkey_hoagie = 0, potato_chips = 0, banana = 0, 
    soda = 0, lemonade = 0, total=0, owe=0, pay=0;
  int qty=0;

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

  while (1) {
    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 */

    /* this is a bit of a pain.  Have to check the items here to find out
       if should ask for quantity.  Another possibility is to get the
       quantity in the if statement, but then need to have six different
       printf and GetInteger() calls
    */
    if (item == 'C' || item == 'T' || item == 'P' || item == 'B' || 
	item == 'S' || item == 'L') {
      /* prompt for quantity */
      printf("How many? ");
      qty = GetInteger();
    }

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

  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");
}




