Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <string>
- using namespace std;
- // DO_2: Define the constant APPLE_PRICE, ORANGE_PRICE and WIDTH
- const float W_MELON_PRICE = 30.00f;
- const float MANGO_PRICE = 30.25f;
- const float GRAPES_PRICE = 35.25f;
- const float APPLE_PRICE = 20.00f;
- const float ORANGE_PRICE = 25.25f;
- const float WIDTH = 10.00f;
- // state taxes
- const float IN_TAX = 0.04f;
- const float IL_TAX = 0.05f;
- const float WI_TAX = 0.06f;
- // price discounts
- const float DISCOUNT_TWO = 0.12F;
- const float DISCOUNT_THREE = 0.13F;
- const float DISCOUNT = 0.1F;
- // DO_3: Complete the prototype for Calculate_Total function and
- // Calculate_Discount function, See function definition and
- // functions' calls below.
- void Display_Price(int code);
- float Calculate_Tax(float total);
- float Calculate_Total(int code, float total);
- float Calculate_Discount(float total);
- // functions for getting and checking the variable code
- int Get_Code();
- bool Valid_Code(int code);
- int main()
- {
- int code;
- float total = 0;
- cout << "=========Welcome to our Market========" << endl;
- // DO_4: Provide the priming read for the Sentinel Control Loop
- // Look at the while statement below for details on what is
- // necessary. Hint, you need to call get_code() function before
- // entering the while loop.
- code = Get_Code();
- while (code != -1)
- {
- // DO_5: Call function Display_Price
- Display_Price(code);
- total = Calculate_Total(code, total);
- // DO_6: Call function Get_Code ().
- // Assign the returned value to code
- code = Get_Code();
- cout << endl;
- }
- cout << "Total: " << total << endl;
- total -= Calculate_Discount(total);
- cout << "Total with discount: " << total << endl;
- total += Calculate_Tax(total);
- cout << "Total with tax: " << total << endl;
- cout << endl;
- return 0;
- }
- //---------------------------------------------------------------------
- // Function to display the price for the selections
- // params: in
- //-------------------------------------------------------------------
- void Display_Price(int code)
- {
- cout << setw(WIDTH) << code << setw(WIDTH);
- if (code == 101)
- cout << "Apple" << setw(WIDTH) << '$' << APPLE_PRICE << endl;
- else if (code == 102)
- cout << "Orange" << setw(WIDTH) << '$' << ORANGE_PRICE << endl;
- // DO_7: Complete the function body for display_price.
- // Check the other values of 103, 104 for the variable code.
- // Set the width for each output according to the
- // sample run in Lab6.txt
- else if (code == 103)
- cout << "Grapes" << setw(WIDTH) << '$' << GRAPES_PRICE << endl;
- else if (code == 104)
- cout << "W.Melon" << setw(WIDTH) << '$' << W_MELON_PRICE << endl;
- else
- cout << "Mango" << setw(WIDTH) << '$' << MANGO_PRICE << endl;
- }
- //---------------------------------------------------------------------
- // Function to calculate the total
- // parms: in, in
- //-------------------------------------------------------------------
- // DO_8: Complete the function header for Calculate_Total.
- // Check the function prototype for details on this function.
- float Calculate_Total(int code,float total )
- {
- if (code == 101)
- total += APPLE_PRICE;
- else if (code == 102)
- total += ORANGE_PRICE;
- else if (code == 103)
- total += GRAPES_PRICE;
- else if (code == 104)
- total += W_MELON_PRICE;
- else if (code == 105)
- total += MANGO_PRICE;
- else
- total += 0;
- return total;
- }
- //---------------------------------------------------------------------
- // Function to calculate the tax for the total
- // also it asks for the state, applies new tax.
- // cin is used to get the "state" variable in this
- // function
- // params: in
- //---------------------------------------------------------------------
- float Calculate_Tax(float total)
- {
- float tax = 0;
- string state;
- bool state_recognized = false;
- while (!state_recognized && total != 0)
- {
- cout << endl << "Please enter the state you are in : ";
- cin >> state;
- cout << endl;
- if (state == "Indiana" || state == "indiana" || state == "IN")
- {
- tax = total * IN_TAX;
- state_recognized = true;
- }
- else if (state == "Illinois" || state == "illinois"
- || state == "IL")
- {
- tax = total * IL_TAX;
- state_recognized = true;
- }
- else if (state == "Wisconsin" || state == "wisconsin"
- || state == "WI")
- {
- tax = total * WI_TAX;
- state_recognized = true;
- }
- else
- {
- cout << endl << "State not recognized." ;
- state_recognized = false;
- }
- }
- return tax;
- }
- //---------------------------------------------------------------------
- // Function to calculate the discount if the total >$100.
- // discount conditions for beween $100 and $200,
- // between $200 and $350, and greater than 350
- // params: in
- //---------------------------------------------------------------------
- float Calculate_Discount(float total)
- {
- if (total < 100)
- return 0;
- else if (total >= 100 && total < 200)
- return total * DISCOUNT;
- else if (total >= 200 && total < 350)
- return total * DISCOUNT_TWO;
- else
- return total * DISCOUNT_THREE;
- }
- //---------------------------------------------------------------------
- // function that gets the code, and then validates the code by
- // using the Valid_Code() function. The code loops until a valid code
- // or the exit command is entered.
- // params: in
- //---------------------------------------------------------------------
- int Get_Code()
- {
- int out_code;
- bool good_code = false;
- while (!good_code)
- {
- cout << endl << "Enter Code:";
- cin >> out_code;
- good_code = Valid_Code(out_code);
- }
- return out_code;
- }
- //---------------------------------------------------------------------
- // function to check whether the code is valid, using the same
- // bounds as the checking in the other functions, and replaces the
- // redundant error output from the Display_Price() function
- // params: in
- //---------------------------------------------------------------------
- bool Valid_Code(int code)
- {
- if (code >= 101 && code <= 105 || code == -1)
- return true;
- else
- {
- cout << "Incorrect code !!!! " << endl;
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement