Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Purpose: This is a Cashier Calculations Program that displays
- // on the screen item codes with corresponding item description
- // and price. It asks the user to enter a valid code of the item
- // purchased by the customer, or it displays an error message
- // otherwise.
- //
- // 1- Assume there are flat prices(declared as constants) for the five
- // items (Apple, Orange, Water Melon, Mango and Grapes). Each of these
- // items has a unique code as following: Apple's code is 101, Orange's code
- // is 102, Grape's code is 103, Water Melon's code is 104 and Mango's code
- // is 105.
- //
- // 2- Assume the tax rate will be based on the chosen state as following:
- // Wisconsin tax rate is 6%, Illinois tax rate is 5% and
- // Indiana tax rate 4%.
- //
- // 3- Assume there will a discount applied based on the total value,
- // if the total below 100 then no discount applied,
- // if the (total >= 100 && total < 200), the discount is 0.1,
- // if the (total >= 200 && total < 350), the discount is 0.12, and
- // if the (total >= 350), the discount is 0.13.
- //
- // 4- The user will be asked to enter a valid state among three states as following:
- // WI, wisconsin , Wisconsin
- // IL, illinois, Illinois
- // IN, indiana, Indiana
- //
- // 5- There will be a void function to display the results. This function will
- // ONLY perform output. You will need to Set the width to 10 for each output according
- // to the sample run in Lab6.txt
- //
- // Input: code, total.
- //
- // Output: purchased item with prices, total prices with tax
- //--------------------------------------------------------------------
- #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 = 31.00f;
- const float ORANGE_PRICE = 31.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);
- // 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.
- cin >> code >> total;
- while (code != -1)
- {
- // DO_5: Call function Display_Price
- total = Calculate_Total(code, total);
- // DO_6: Call function Get_Code ().
- // Assign the returned value to 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 ( )
- else if ( )
- else
- cout << "Mango" << setw(WIDTH) << '$' << GRAPES_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.
- Calculate_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