I have defined the class a transaction class to specify transaction details:
class Transaction
{
public:
Transaction(QString type, QDate date, int num, double price);
QString toString();
private:
QString m_Type; //HOLDS THE TYPE OF TRANSACTION: Sale or Purchase
QDate m_Date; //date of transaction
int m_NoOfItems; //num of items in transaction
double m_PricePerItem; //price per item
};
and a Product class to store a product's info (m_Type holds "sale" or "purchase"):
class Product
{
public:
Product(QString name, int num, double seprice, double suprice, QString sc);
void sell(int n); //need to add sale to QList<Transaction>
void restock(int n);
QString getSupplierCode() const;
void setProductCode(QString c);
QString getProductCode() const;
QList<Transaction> getTransactions() const;
QString toString();
void remvodeAll();
bool isExpired();
private:
QString m_Name;
int m_NoOfItems;
QString m_ProductCode;
double m_SellingPrice;
double m_SupplierPrice;
QString m_SupplierCode;
QList<Transaction> m_Transactions; //QList of class type Transaction
};
my void Product::sell(int n)
is as follows:
void Product::sell(int n)
{
if(m_NoOfItems < n)
{
qDebug() << "Not enough items in stock.";
}
else
{
m_NoOfItems = m_NoOfItems - n;
m_Transactions.append(Transaction("Sale", QDate.currentDate(), n, m_SellingPrice));
}
}
There is an aggregation between these classes. Now what I need to do is whenever I call .sell()
I need to add a sale to the QList m_Transactions
which is of class type Transaction
, where Transaction::m_Type = "sale"
. The only way I can think of doing this with the existing functions is to call the Transaction
constructor and passing in the values. But obviously that won't work. Any ideas how I can get around this?
Ok, first of all, what you need to do is to write:
m_Transactions.append(Transaction("Sale", QDate::currentDate(), n, m_SellingPrice));
Note the ::
after QDate
, since currentDate()
is a static function.
I also find it a little odd to save transactions inside of products. A better design would be having a separate class that could store them.