// Kyleigh Brown, Deena Dayal, Shawntraree Lucas, Rodney Sampson II // Dr. Lawrence // CIS 346 - Theory of Programming Languages // Cash Register Project // Design a CashRegister class that can be used with an InventoryItem class. // The CashRegister class should perform the following: // 1. Ask the user for the item and quantity being purchased. // 2. Get the item’s cost from the InventoryItem object. // Inventory Items should have a name, cost, and quantity available. // 3. Add a 30% profit to the cost to get the item’s unit price. // 4. Multiply the unit price times the quantity being purchased // to get the purchase subtotal. // 5. Compute a 6% sales tax on the subtotal to get the purchase total. // 6. Repeat until user has entered all desired items. // 7. Display the items purchased, subtotal, tax, and total on the screen. // 8. Subtract the quantity being purchased from the onHand variable of the // InventoryItem class object. // Implement both classes in a complete program. Feel free to modify the // InventoryItem class in any way necessary. // Input Validation: Do not accept a negative value for the // quantity of items being purchased. #include <iostream> #include <string> #include <iomanip> using namespace std; class InventoryItem { private: // Variables for the name of item, quantity and cost string name; int quantity; double cost; public: // Constructors for InventoryItem InventoryItem(string name = "", int quantity = 0, double cost = 0){ //set the variables to their new names this->name = name; this->quantity = quantity; this->cost = cost; } // Getter for name string getName() const { return this->name; } // Getter for the quantity int getQuantity() const { return this->quantity; } //Getter for the cost double getCost() const { return this->cost; } // Setter for the name void setName(string name) { this->name = name; } // Setter for the quantity void setQuantity(int quantity) { this->quantity = quantity; } // Setter for the cost void setCost(double cost) { this->cost = cost; } }; class CashRegister { private: // Variables for the subtotal, tax & total double subTotal; double tax; double total; public: // Defualt constructor CashRegister() { this->subTotal = 0.0; this->tax = 0.0; this->total = 0.0; } // Function for the user to make their purchase void makePurchase(InventoryItem available[]) { //variable to store their choice of item and quantity int itemChoice, itemQuantity; //ask user for their item choice cout << "Enter item number: "; //take their input of choice cin >> itemChoice; //subtract 1 from the selected item itemChoice--; //display the selected item cout << "The item you selected is: "; //display the selected item cout << available[itemChoice].getName(); //ask user for the quantity cout << "\nEnter the quantity: "; //take quantity as input cin >> itemQuantity; //if the input of quantity is less than 0 while (itemQuantity < 0){ //prompt user for new quantity cout << "\nNo negative inputs. Please enter a new quantity: "; //take the new input as the new quantity cin >> itemQuantity; } //if the user asks for more items than available while(itemQuantity > available[itemChoice].getQuantity()) { //tell the user how many items they have available cout << "\nThere are only " << available[itemChoice].getQuantity() << " available.\n"; //ask the user to input a new item quantity cout << "Please enter a new quantity: "; //take the user's new input cin >> itemQuantity; } //calculate the subtotal with the cost of the item and the quantity calcSubTotal(available[itemChoice].getCost(), itemQuantity); //calculate the tax with the subtotal calcTax(subTotal); //calculate the final total calcTotal(); //set the new quantity to the original quantity minus the user's quantity input available[itemChoice].setQuantity(available[itemChoice].getQuantity() - itemQuantity); } //function to calculate subtotal with cost and quantity void calcSubTotal(double cost, int itemQuantity) { //multiply the cost by 1.3 double unitCost = 1.3 * cost; //subtotal is cost times quantity subTotal = unitCost * itemQuantity; } //function to calculate tax with subtotal void calcTax(double subTotal) { //tax is subtotal times 6% tax = 0.06 * subTotal; } //function to calculate total void calcTotal() { //total is subtotal plus tax total = subTotal + tax; } //getter for subtotal double getSubTotal() const { //return the subtotal return subTotal; } //getter for tax double getTax() const { //return the tax return tax; } //getter for total double getTotal() const { //return the total return total; } }; //function to show available items void display(InventoryItem items[]){ //print values to make a table cout << "\n\t\t\tItem\t\t\t\t\t Quantity\t\t\t\t Price\n"; cout << "--------------------------------------------------------------------\n"; //iterate 5 times to print out each grocery item for(int i = 0; i < 5; i++){ //print out the item # cout << "Item #" << i+1 << "\t\t"; //print out the item name cout << items[i].getName() << "\t\t\t\t\t\t"; //print out the item quantity cout << items[i].getQuantity() << "\t\t\t\t\t\t"; //print out the item cost cout << items[i].getCost() << endl; } } int main() { //set 5 items to the array of items for purchase //set their names, quantities & prices InventoryItem items[5] = { //20 milks for $3.68 each InventoryItem("Milk", 20, 2.99), //30 eggs for $1.72 each InventoryItem("Eggs", 30, 1.69), //25 breads for $1.41 each InventoryItem("Bread", 25, 1.99), //15 cheeses for $2.04 each InventoryItem("Cheese", 15, 2.59), //35 waters for $4.38 each InventoryItem("Water", 35, 3.99) }; int choice; CashRegister cashRegister; cout << "CASH REGISTER"; // use a do while loop to display a menu for user to purchase items do { // ask the user if they would like to shop (1) or quit (0) cout << "\n\n1) Start shopping!"; cout << "\n0) Stop shopping"; cout << "\nEnter your number choice: "; // take the user's input cin >> choice; // switch case with the user's choice switch(choice) { case 1: // display the available items display(items); cout << endl; // use the items to make a purchase cashRegister.makePurchase(items); // display information about the totals to 2 decimals cout << "\nSubtotal: $"; printf("%.2f", cashRegister.getSubTotal()); cout << "\nSales tax: $"; printf("%.2f", cashRegister.getTax()); cout << "\nTotal: $"; printf("%.2f", cashRegister.getTotal()); break; case 2: break; } } while(choice != 0); }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}