// ex_ch7_11.c
#include <stdio.h>
#include <stdbool.h>

const float POUND_A = 1.25;
const float POUND_B = 0.65;
const float POUND_C = 0.89;
const char* PROMPT = "a) artichokes b) beets c) carrots q) quit\n";

int main(void)
{
	char ch;
	int pounds_a = 0, pounds_b = 0, pounds_c = 0;
	int pounds_total;
	int amount;
	float overall_price, price_a, price_b, price_c, discount, shipping;

	printf("ABC Mail Order Grocery\n");
	printf(PROMPT);

	while((ch = getchar()) != 'q')
	{
		if (ch != '\n')
		{
			printf("How many pounds? ");

			switch(ch)
			{
			case 'a':
				scanf("%d", &pounds_a);
				break;
			case 'b':
				scanf("%d", &pounds_b);
				break;
			case 'c':
				scanf("%d", &pounds_c);
				break;
			}
			printf(PROMPT);
		}

	}
	// input completed - calculation

	price_a = POUND_A * pounds_a;
	price_b = POUND_B * pounds_b;
	price_c = POUND_C * price_c;
	overall_price = price_a + price_b + price_c;

	printf("Artichokes\t%d pounds at $%.2f\t%.2f\n", pounds_a, POUND_A, price_a);
	printf("Beets\t\t%d pounds at $%.2f\t%.2f\n", pounds_b, POUND_B, price_b);
	printf("Carrots\t\t%d pounds at $%.2f\t%.2f\n", pounds_c, POUND_C, price_c);
	printf("\n");
	pounds_total = pounds_a + pounds_b + pounds_c;
	printf("Total\t\t%d pounds\t\t%.2f\n", pounds_total, overall_price);

	if (overall_price >= 100.0)
	{
		discount = 0.05 * overall_price;
		printf("Discount\t\t\t\t%.2f\n", discount);
	}
	else
		discount = 0.0;
	
	if (pounds_total <= 5)
	{
		shipping = 3.5;
	}
	else if (pounds_total < 20)
	{
		shipping = 10.0;
	}
	else
	{
		shipping = 8.0 + 0.1 * pounds_total;
	}
	printf("Shipping\t\t\t\t%.2f\n", shipping);
	printf("============================================\n");
	printf("Grand total\t\t\t\t%.2f\n", overall_price - discount + shipping);

	return 0;
}
