Thank you that works. :-)
I must have a logic error. Main has several items being sold. My intention was to add each sale to the m_Transactions list - each product I sell adds to the array, so after the first sale it prints one line, after the second sale it prints two lines. Although this prints each sale, it doesn't appear to be storing the previous transactions. My third sale is "rejected" because insufficient items and it appears that m_Transactions array size is zero?
Added after 7 minutes:
(I have since fixed the transaction, which was showing number of items in stock not number of items sold)
This is main
#include <QString>
#include <QTextStream>
#include <QList>
#include "product.h"
#include "foodproduct.h"
#include "transaction.h"
#include <QDebug>
int main() {
Product book1("The Hobbit",6,15,5.00);
Product book2("Lord of the Rings",1,10,7.00);
Product cd1("Genesis",1,2,3.00);
FoodProduct chocolate
("Marsbar",
5,
100,
5.75,
QDate::currentDate());
book1.sell(5);
book2.sell(5);
cd1.sell(5);
return 0;
}
#include <QString>
#include <QTextStream>
#include <QList>
#include "product.h"
#include "foodproduct.h"
#include "transaction.h"
#include <QDebug>
int main() {
QTextStream cout(stdout);
Product book1("The Hobbit",6,15,5.00);
Product book2("Lord of the Rings",1,10,7.00);
Product cd1("Genesis",1,2,3.00);
FoodProduct chocolate("Marsbar",5,100,5.75,QDate::currentDate());
book1.sell(5);
book2.sell(5);
cd1.sell(5);
return 0;
}
To copy to clipboard, switch view to plain text mode
And this is the printout
Transaction Recorded: 10 items @ R5.00 on 19/03/2011
Size of array:1
Transaction Recorded: 5 items @ R7.00 on 19/03/2011
Size of array:1
Transaction failed - insufficient stock
Size of array:0
Press <RETURN> to close this window...
Added after 11 minutes:
Just curious - when I was trying to find a solution, many people seemed to have a simple "for int i etc" to traverse a QList. Obviously the ConstIterator is much better (and as I know the "int i" version doesn't work!) I assume I can use the same method for my pointer list should I ever get that far!
Does one use ConstIterator for all QLists? Are there times (and types of lists) where "For int i..." will work?
Added after 4 minutes:
Sorry, my function sell() now looks like this. I assume the error lies with the .append somehow?
void Product::sell(int n) {
if (m_NoOfItems < n)
cout << "Transaction failed - insufficient stock" <<endl;
else {
m_NoOfItems -= n;
m_Transactions.append(Transaction(n,m_PricePerItem));
for(QList<Transaction>::ConstIterator it = m_Transactions.begin(); it != m_Transactions.end(); ++it) {
cout << it->toString(); //i used qPrintable to be able to print QString to std::cout
}
}
cout <<"Size of array:" << m_Transactions.size() <<endl;
}
void Product::sell(int n) {
if (m_NoOfItems < n)
cout << "Transaction failed - insufficient stock" <<endl;
else {
m_NoOfItems -= n;
m_Transactions.append(Transaction(n,m_PricePerItem));
for(QList<Transaction>::ConstIterator it = m_Transactions.begin(); it != m_Transactions.end(); ++it) {
cout << it->toString(); //i used qPrintable to be able to print QString to std::cout
}
}
cout <<"Size of array:" << m_Transactions.size() <<endl;
}
To copy to clipboard, switch view to plain text mode
Added after 21 minutes:
Actually, it does seem to be working just strangely.
I have now written the "RemoveAll() function" and both the original sale and the remove all are showing in transactions. As long as I only sell and remove a single product, the transactions are all listed to do with that one product. When I sell other products, they each have a transaction, but it appears to be in a separate array.
Bookmarks