Hello everyone,
I have a shool project that requires me to design a cash register class that uses an inventory class and the cash register should perform the following steps:
1. ask the user for the item and quantity purchased.
2. get the item's cost
3. add 30% profit to cost to get unit item price
4. get subtotal by multipling unit price * quantity.
5. computer 6% tax rate
6. display the purchase subtotal, tax, and total on the screen
7. subtract the quantity from the onHand variable of the InventoryItem class object.
Ok so I have my program and its working like a charm, except one thing!!! I cannot get the last step to work properly. I have three classes. class for price to determine total unit price. Then I have a sale class that gets the total with tax. Here are all three classes and the cpp for the code. Please direct me on how to fix. I think I can use a counter to get the loop to work for qtySelected but not sure how. Or can I subtract the qtySelected from the inventory class itself? Any direction or ideas would be great.
EDIT: THe step 7 does work and doesnt work. For example if the first time the program runs onHand is correct (standard). Once i order 3 item 0 and I say yes to make more purchases the onHand for item 0 decreases from 12 to 9. BUT if I go in and purchase 5 more item 0s, it subtracts 5 from 12 (NOT 9!).
Content of Price Class (price.h)
//Specification file for the InventoryItem class.
#ifndef SALE2_H
#define SALE2_H
class Sale2
{
private:
double itemPrice;
double profitRate;
public:
Sale2(double price, double profit = 0.3)
{ itemPrice = price;
profitRate = profit; }
double getItemPrice() const
{ return itemPrice; }
double getProfitRate() const
{ return profitRate; }
double getProfit() const
{ return (itemPrice * profitRate); }
double getTotalUnitPrice() const
{ return (itemPrice + getProfit()); }
};
#endif
Contents of the sale class (sales.h)
//Specification file for the InventoryItem class.
#ifndef SALE_H
#define SALE_H
class Sale
{
private:
double itemCost;
double taxRate;
double itemPrice;
double profitRate;
public:
Sale(double cost, double rate = 0.06)
{ itemCost = cost;
taxRate = rate; }
double getItemCost() const
{ return itemCost; }
double getTaxRate() const
{ return taxRate; }
double getTax() const
{ return (itemCost * taxRate); }
double getTotal() const
{ return (itemCost + getTax()); }
};
#endif
content of inventoryItem class (InventoryItem.h)
//Specification file for the InventoryItem class.
#ifndef INVENTORYITEM_H
#define INVENTORYITEM_H
#include <cstring> //needed for strlen and strcpy
//constant for description's default size
const int DEFAULT_SIZE = 51;
//InventoryItem class declaration
class InventoryItem
{
private:
char *description; //item description
double cost; //item cost
double units; //Number of units on hand
// Private member function.
void InventoryItem::createDescription(int size, char *value)
{
// Allocate the default amount of memory for description.
description = new char [size+1];
// Store a value in the memory.
strcpy(description, value);
}
public:
//constructor #1
InventoryItem()
{//store an empty string in the description attribute.
createDescription(DEFAULT_SIZE, "");
//initialize cost and units.
cost = 0.0;
units = 0; }
//constrcutor #2
InventoryItem(char *desc)
{//allocate memory and store the description.
createDescription(strlen(desc), desc);
//initialize cost and units.
cost = 0.0;
units = 0; }
//constructor #3
InventoryItem(char *desc, double c, double u)
{//allocate memory and store the description.
createDescription(strlen(desc), desc);
//assign values to cost and units.
cost = c;
units = u; }
//Destructor
~InventoryItem()
{ delete [] description; }
//Mutator functions
void setDescription(char *d)
{ strcpy(description, d); }
void setCost(double c)
{ cost = c; }
void setUnits(double u)
{ units = u; }
//Accessor functions
const char *getDescription() const
{ return description; }
double getCost() const
{ return cost; }
double getUnits() const
{ return units; }
};
#endif
Contents ofthe cpp for the progran:
//This program demonstrates a class with a destructor
#include <iostream>
#include <iomanip>
#include "price.h"
#include "Sale.h"
#include "InventoryItem.h"
using namespace std;
int main()
{
int numSelected;
double qtySelected;
char again;
double price;
double cost;
const int NUM_ITEMS = 5;
InventoryItem inventory[] = {
InventoryItem("Hammer", 6.95, 12),
InventoryItem("Wrench", 8.75, 20),
InventoryItem("Pliers", 3.75, 10),
InventoryItem("Ratchet", 7.95, 14),
InventoryItem("Screwdriver", 2.50, 22) };
do
{
cout << "Here is a list of our current inventory" << endl;
cout << " " << endl;
cout << setw(7) << "Item Number" <<
cout << setw(14) << "Description"
<< setw(8) << "Cost" << setw(8)
<<setw(20) << "Units on Hand\n";
cout << "------------------------------------------------------------\n";
for (int i = 0; i < NUM_ITEMS; i++)
{
cout << setw(7) << i;
cout << setw(21) << inventory[i].getDescription();
cout << setw(14) << inventory[i].getCost();
if (qtySelected == 0)
cout << setw(7) << inventory[i].getUnits() << endl;
else
cout << setw(7) << inventory[i].getUnits() - qtySelected << endl;
}
cout << "" << endl;
cout << "" << endl;
cout << "Please enter the item number you wish to purchase: ";
cin >> numSelected;
while((numSelected < 0) || (numSelected > 5))
{
cout << "Please enter an inventory item number 0-5: ";
cin >> numSelected;
}
cout << "Please enter the quantity you wish to purchase: ";
cin >> qtySelected;
while(qtySelected > inventory[numSelected].getUnits())
{
cout << "Please enter quantity less than or equal to " << inventory[numSelected].getUnits() << " units: ";
cin >> qtySelected;
}
price = inventory[numSelected].getCost();
cout << " " << endl;
cout << "Here is the bill: " << endl;
cout << " " << endl;
Sale2 itemSale2(price);
double subTotal = itemSale2.getTotalUnitPrice() * qtySelected;
cout << "Subtotal:\t $" << subTotal << endl;
cost = subTotal;
Sale itemSale(cost);
cout << "Sales tax:\t $" << itemSale.getTax() << endl;
cout << "Total:\t\t $" << itemSale.getTotal() << endl;
cout << "Do you want to make another purchase? ";
cin >> again;
cout << " " << endl;
}
while (again == 'Y' || again == 'y');
return 0;
}
YOu guys have worked wonders for me in the past and I am excited to see some ideas/answers. Thank you in advance.
(I know the naming conventions are off. I use very generic names when first coding to help me find things. For some reason thats how my eyes work.)
EDIT EDIT:::
I would prefer to subtract the qtySelected from the Inventory Class, any ideas how???? I am a beginner programmer so please keep that in mind with my questions/posts.