
/* hw4p3 */

#include <stdio.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

typedef enum {
  CHEESESTEAK = 1, TURKEY_HOAGIE, POTATO_CHIPS, BANANA, SODA, LEMONADE, DONE_PURCHASING
} menuItem;

menuItem GetMenuItem();
double ComputeSubtotal(menuItem item, double subtotal);
void GetMoney(double total);

void main()
{
  double total=0;
  menuItem item;

  printf("Welcome to the LunchExpress food truck!\n");
  while ((item =GetMenuItem()) != DONE_PURCHASING)
    total = ComputeSubtotal(item, total);
  GetMoney(total);
}

menuItem GetMenuItem()
{
  printf("\n*************************\n");
  printf("*  1  Cheesesteak       *\n");
  printf("*  2  Turkey Hoagie     *\n");
  printf("*  3  Potato Chips      *\n");
  printf("*  4  Banana            *\n");
  printf("*  5  Soda              *\n");
  printf("*  6  Lemondade         *\n");
  printf("*                       *\n");
  printf("*  7  Done Purchasing   *\n");
  printf("*************************\n");

  printf("\nWhat would you like? ");
  return GetInteger();
}

double ComputeSubtotal(menuItem item, double subtotal)
{
  double cost;
  int qty;

  if (item >= 1 && item <= 6) {
    /* prompt for quantity */
    printf("How many? ");
    qty = GetInteger();
  }

  /* using switch instead of cascading if statements */
  switch (item) {
  case CHEESESTEAK:
    cost = COST_CHEESESTEAK * qty;
    break;
  case TURKEY_HOAGIE:
    cost = COST_TURKEY_HOAGIE * qty;
    break;
  case POTATO_CHIPS:
    cost = COST_POTATO_CHIPS * qty;
    break;
  case BANANA:
    cost = COST_BANANA * qty;
    break;
  case SODA:
    cost = COST_SODA * qty;
    break;
  case LEMONADE:
    cost = COST_LEMONADE * qty;
      break;
  default:
    /* invalid selection */
    printf("That is not a valid menu item!\n");
    break;
  }
  return cost+subtotal;
}

void GetMoney(double total)
{
  double pay, owe;

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

