/* * Homework 2, CSE 142 TVI, Winter 2001 * Ordering Muggle Goods * * Name : Solution * Student ID : * Quiz Section: * * * This is a simple program to create order forms based on a request * for a few (6) greatly needed items from 3 different suppliers. * Order forms are created for the suppliers based on the user's order. * */ #include #include /* ---------------------------------------------------------------- * CONSTANT DEFINITIONS */ /* supplier numbers */ #define NATURAL_SUPPLIER 1 /* sells natural products */ #define ANIMAL_SUPPLIER 2 /* sells animal products */ #define DISCOUNT_SUPPLIER 3 /* sells miscellaneous discounted products */ /* item numbers */ #define WORMWOOD 1 #define NETTLES 2 #define FANG 3 #define SNAIL 4 #define QUILL 5 #define VIAL 6 /* item costs */ #define COST_PER_WORMWOOD 6.50 /* per ounce */ #define COST_PER_NETTLES 3.99 /* per ounce */ #define COST_PER_FANG 24.99 #define COST_PER_SNAIL 13.25 #define COST_PER_QUILL 2.99 #define COST_PER_VIAL 10.50 /* ----------------- functions definitions follow --------------*/ /* note -- the assignment did not specify how error conditions * should be handled. This is only one possible way to deal with * them. */ /* ---------------------------------------------------------------- * PerItemCost -- * Parameters: takes an item number * Prints: nothing * Returns: the unit cost of that item * ----------------------------------------------------------------*/ double PerItemCost(int item_no) { if (item_no == WORMWOOD) return COST_PER_WORMWOOD; else if (item_no == NETTLES) return COST_PER_NETTLES; else if (item_no == FANG) return COST_PER_FANG; else if (item_no == SNAIL) return COST_PER_SNAIL; else if (item_no == QUILL) return COST_PER_QUILL; else if (item_no == VIAL) return COST_PER_VIAL; else { /* print error and exit if unknown item is ordered */ printf("Unknown Item No: %d", item_no); exit(item_no); return 0.0; /* progam should never reach here */ } } /* ---------------------------------------------------------------- * printItemName * Parameters: takes an item number * Prints: the name of that item * Returns: nothing * ----------------------------------------------------------------*/ void printItemName(int item_no) { if (item_no == WORMWOOD) printf("ounces Wormwood "); else if (item_no == NETTLES) printf("ounces Nettles "); else if (item_no == FANG) printf("Snake Fangs "); else if (item_no == SNAIL) printf("Snails "); else if (item_no == QUILL) printf("Porcupine Quills"); else if (item_no == VIAL) printf("Potion Vials "); else printf("Unknown Item"); } /* ---------------------------------------------------------------- * printSupplierName * Parameters: takes a supplier number * Prints: the NAME of that supplier * Returns: nothing * ----------------------------------------------------------------*/ void printSupplierName(int supplier_no) { if (supplier_no == NATURAL_SUPPLIER) { printf("California Dreams"); } else if (supplier_no == ANIMAL_SUPPLIER) { printf("Parts R Us"); } else if (supplier_no == DISCOUNT_SUPPLIER) { printf("MalWart"); } else { printf("Unknown Supplier"); } } /* ---------------------------------------------------------------- * MakeOrder * Parameters: takes a supplier number, an item number, the quantity desired * for that item number, another item number, the quantity * desired for the second item * * the item numbers should correspond to the items sold by that * supplier * * Prints: the NAME of the supplier * * if the quantity of the first item is greater than zero, it * prints the QUANTITY of the first item, the NAME of the first * item, and the COST of purchasing that many of the first item * * it does the same thing for the second item * * Returns: the TOTAL COST of the order * ----------------------------------------------------------------*/ double MakeOrder(int supplier_no, int item_no1, int quantity1, int item_no2, int quantity2) { double cost1 = 0.0; /* initialized to 0.0 in case zero items are ordered */ double cost2 = 0.0; /* generate an order for the supplier */ printf("****************************************************************\n"); printf("ORDER FOR: "); printSupplierName(supplier_no); printf("\nREQUEST:\n"); /* if at least 1 of item_no1 was ordered, display the order */ if (quantity1 > 0) { printf("%8d ",quantity1); printItemName(item_no1); cost1 = quantity1 * PerItemCost(item_no1); printf("\t$%.2f\n",cost1); } /* if at least 1 of item_no2 was ordered, display the order */ if (quantity2 > 0) { printf("%8d ",quantity2); printItemName(item_no2); cost2 = quantity2 * PerItemCost(item_no2); printf("\t$%.2f\n",cost2); } /* calculate the total of the order */ printf("\t\t _________\n"); printf("\t Sub-Total: $%.2f\n",(cost1 + cost2)); return (cost1 + cost2); } /* ------------------------- end of function definitions -----------*/ /* THE MAIN BODY OF THE PROGRAM ITSELF */ int main(void) { /* VARIABLE DEFINITIONS */ int num_wormwood = 0; /* ounces of wormwood purchased */ int num_nettles = 0; /* ounces of nettles purchased */ int num_fang = 0; /* number of snake fangs purchased */ int num_snail = 0; /* number of snails purchased */ int num_quill = 0; /* number of porcupine quills purchased */ int num_vial = 0; /* number of potion vials purchased */ double total_cost = 0; /* total amount of all items ordered */ char dummy_char; /* used to pause program before exiting */ /* read in the quantity of each item desired */ printf("Please enter in the desired quantities:\n"); printf("How many ounces of Wormwood? "); scanf("%d",&num_wormwood); printf("How many ounces of Nettles? "); scanf("%d",&num_nettles); printf("How many Snake Fangs? "); scanf("%d",&num_fang); printf("How many Snails? "); scanf("%d",&num_snail); printf("How many Porcupine Quills? "); scanf("%d",&num_quill); printf("How many Potion Vials? "); scanf("%d",&num_vial); /* order requests should are generated here */ /* make the order for the NATURAL_SUPPLIER if at least one item is ordered, add its cost to the total cost of all orders */ if ((num_wormwood > 0) || (num_nettles > 0)) { total_cost = total_cost + MakeOrder(NATURAL_SUPPLIER, WORMWOOD, num_wormwood, NETTLES, num_nettles); } /* make the order for the ANIMAL_SUPPLIER if at least one item is ordered add its cost to the total */ if ((num_fang > 0) || (num_snail > 0)) { total_cost = total_cost + MakeOrder(ANIMAL_SUPPLIER, FANG, num_fang, SNAIL, num_snail); } /* make the order for the DISCOUNT_SUPPLIER if at least one item is ordered add its cost to the total */ if ((num_vial > 0) || (num_quill > 0)) { total_cost = total_cost + MakeOrder(DISCOUNT_SUPPLIER, QUILL, num_quill, VIAL, num_vial); } /* total cost of the all orders should be printed */ printf("\nTotal Cost of ORDER: $%.2f\n",total_cost); /* pause before exiting */ printf("press enter to continue\n"); scanf("%c%c",&dummy_char,&dummy_char); return 0; }