In: Computer Science
How can I improve my code below to meet the requirements of this assignment with syntax add to my code to complete the assignment below:
#include <iostream>
#include <vector>
using namespace std;
class Inventory {
private:
int itemNumber;
int quantity;
double cost;
double totalCost;
public:
Inventory() { itemNumber = 0; quantity = 0; cost = 0; totalCost =
0; }
Inventory(int n, int q, double c) {
itemNumber = n; quantity = q; cost = c; setTotalCost();
}
void setItemNumber(int n) {
itemNumber = n;
}
void setCost(double c) {
cost = c;
}
void setQuantity(int q) {
quantity = q;
}
void setTotalCost() {
totalCost = quantity * cost;
}
int getItemNumber() const {
return itemNumber;
}
int getQuantity() const {
return quantity;
}
double getCost() const {
return cost;
}
double getTotalCost() const {
return totalCost;
}
};
class CashRegister {
private:
Inventory Inv;
int itemNumber;
int quantity;
double cost;
public:
CashRegister(Inventory& temp, int q) {
itemNumber = temp.getItemNumber();
quantity = q; cost = temp.getCost();
temp.setQuantity(temp.getQuantity() - q);
} //constructor
CashRegister() {
itemNumber = 0; quantity = 0; cost = 0; Inv.setItemNumber(0);
Inv.setQuantity(0); Inv.setCost(0);
}
double getUnitPrice() const { return 1.3 * cost; }
double getSubTotal() const { return 1.3 * cost * quantity; } //
unitprice * qty
double getTax() const { return 1.3 * cost * quantity * .06; } //
subtotal*.06
double getTotal() const { return 1.3 * cost * quantity * 1.06; } //
subtotal plus 6% tax
};
int main()
{
int q;
int n;
int pause;
const int MAX_INV_SIZE = 5;
vector <Inventory> Inv(MAX_INV_SIZE);
Inv[0].setItemNumber(0);
Inv[0].setQuantity(10);
Inv[0].setCost(4.34);
Inv[1].setItemNumber(1);
Inv[1].setQuantity(5);
Inv[1].setCost(9.20);
Inv[2].setItemNumber(2);
Inv[2].setQuantity(100);
Inv[2].setCost(3.00);
Inv[3].setItemNumber(3);
Inv[3].setQuantity(12);
Inv[3].setCost(.5);
Inv[4].setItemNumber(4);
Inv[4].setQuantity(18);
Inv[4].setCost(1);
cout << "Welcome to the Cash register program...." <<
endl;
cout << "Which item would you like to purchase? " <<
endl;
cout << "1. Adjustable Wrench" << endl;
cout << "2. Screwdriver" << endl;
cout << "3. Pliers" << endl;
cout << "4. Ratchet" << endl;
cout << "5. Socket Wrench" << endl;
cin >> n;
while (n > 5 || n < 1) { cout << "Invalid entry...try
again: "; cin >> n; }
n--;
cout << "Enter the quantity purchased: "; cin >>
q;
while (q < 0) { cout << "We don't sell negative # of
items...try again: "; cin >> q; }
CashRegister C(Inv[n], q);
cout << "Subtotal: " << C.getSubTotal() <<
endl;
cout << "Tax: " << C.getTax() << endl;
cout << "Total: " << C.getTotal() << endl;
cout << "Items left: " << Inv[n].getQuantity();
cin >> pause;
}
Cash Register Basic Steps in Creating your Program
1. Plan your logic before working on the project and review the syntax you will use in the project. Think about how you want the lines of output to be displayed to the user. You will want to tell the user what the program does and what type of input you expect from the user (Introduction to the User). Select names of the variables you will use in the program.
2. Create a C++ project in Visual Studio 2012. And have the code window open to write the project code. Display Using Visual Studio to create your program in C++ and follow the instructions on creating the project. Declare variables to hold information about tank capacity and mpg
Set up information about this program and what the user will enter and results expected
Ask the user to enter some information requested and save the response to a variable. Make sure the position for the user to enter information is on the same line as the request for information.
Use line spacing (endl or \n) to separate lines of output to make it easy for the user to read the information.
3. Documentation: All lines of code must be documented.
Use the syntax to designate lines in code as comments (//)
These comments can be at the end of a line of code or in a group definition at the beginning of a section of code. All methods and function need a group heading document.
4. Run the program using Ctrl+F5. Debug the code, making sure all calculations are correct and the line spacing is suitable for readability.
Your program Assignment Design a CashRegister class that can be used with the Inventory Item class discussed in the chapter.
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 Inventory Item object.
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 the 6% sales tax on the subtotal to get the purchase total.
6. Display the purchase subtotal, tax and total on the screen
7. Subtract the quantity being purchased from the on Hand variable of the Inventory Item class object. Implement both classes in a complete program. Feel free to modify the Inventory Item class in any way necessary. Be sure and loop the program so that multiple items can be purchases Input Validation: Do not accept a negative value for the quantity of items being purchased.
#include <iostream>
#include <vector>
using namespace std;
class Inventory {
private:
int itemNumber;
int quantity;
double cost;
double totalCost;
public:
Inventory() { itemNumber = 0; quantity = 0; cost = 0; totalCost =
0; }
Inventory(int n, int q, double c) {
itemNumber = n; quantity = q; cost = c; setTotalCost();
}
void setItemNumber(int n) {
itemNumber = n;
}
void setCost(double c) {
cost = c;
}
void setQuantity(int q) {
quantity = q;
}
void setTotalCost() {
totalCost = quantity * cost;
}
int getItemNumber() const {
return itemNumber;
}
int getQuantity() const {
return quantity;
}
double getCost() const {
return cost;
}
double getTotalCost() const {
return totalCost;
}
};
class CashRegister {
private:
Inventory Inv;
int itemNumber;
int quantity;
double cost;
public:
CashRegister(Inventory& temp, int q) {
itemNumber = temp.getItemNumber();
quantity = q; cost = temp.getCost();
temp.setQuantity(temp.getQuantity() - q);
} //constructor
CashRegister() {
itemNumber = 0; quantity = 0; cost = 0; Inv.setItemNumber(0);
Inv.setQuantity(0); Inv.setCost(0);
}
double getUnitPrice() const { return 1.3 * cost; }
double getSubTotal() const { return 1.3 * cost * quantity; } //
unitprice * qty
double getTax() const { return 1.3 * cost * quantity * .06; } //
subtotal*.06
double getTotal() const { return 1.3 * cost * quantity * 1.06; } //
subtotal plus 6% tax
};
int main()
{
int q;
int n;
int pause;
int temp;
const int MAX_INV_SIZE = 5;
//create vector and set values for five items
vector <Inventory> Inv(MAX_INV_SIZE);
Inv[0].setItemNumber(0);
Inv[0].setQuantity(1);
Inv[0].setCost(4.34);
Inv[1].setItemNumber(1);
Inv[1].setQuantity(5);
Inv[1].setCost(9.20);
Inv[2].setItemNumber(2);
Inv[2].setQuantity(100);
Inv[2].setCost(3.00);
Inv[3].setItemNumber(3);
Inv[3].setQuantity(12);
Inv[3].setCost(.5);
Inv[4].setItemNumber(4);
Inv[4].setQuantity(18);
Inv[4].setCost(1);
while(1)//looping for repeat purchase until press 6
{
//Displaying menu items for purchasing//
cout << "\nWelcome to the Cash register program...." <<
endl;
cout << "1. Adjustable Wrench" << endl;
cout << "2. Screwdriver" << endl;
cout << "3. Pliers" << endl;
cout << "4. Ratchet" << endl;
cout << "5. Socket Wrench" << endl;
cout << "Which item would you like to purchase?(Press 6 for
Exit) " ;
cin >> n;
if(n==6) break;
if(n > 6 || n < 1) cout << "Invalid entry...try again:
"<< endl;
else
{
n--; //to set index (starting index 0)
if(Inv[n].getQuantity()>0){ //check available quantity empty
condition
cout << "Enter the quantity purchased(greater than 0): "; cin
>> q;
if(q >0) { //check if user enters negative number
if(Inv[n].getQuantity()<q){ //check if available quantity less
than user enters
cout << "Only Avaliable "<<Inv[n].getQuantity()<<
endl;
temp=Inv[n].getQuantity();//available quantity purchased
}
else
temp=q;//full quantity available
CashRegister C(Inv[n],temp);//create object of class and set
values
//Displaying values in console output
cout << " Subtotal : " << C.getSubTotal() <<
endl;
cout << " Tax : " << C.getTax() << endl;
cout << " Total : " << C.getTotal() <<
endl;
cout << " Items left: " << Inv[n].getQuantity();
}
else
cout << "We don't sell negative # of items...try again:
"<<endl;
}
else
cout<<"Sorry,stock empty";
}
}
return 0;
}