Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,I have a trouble with this task. I dont know how to make it correctly.
Make Invoice class with:
• List of purchased goods - associative array (map) with Stock key object and its price value
• methods for adding and removing goods

C++
#include"Stock.h"
#include <string>
#include<map>


class Invoice 
{
    int numberInvoice;
    int dateIssue;
    std::map<Stock, double> goods;
public:
    Invoice();
    Invoice(int _numberInvoice, int _dateIssue, std::map<Stock, double> _goods);
    
    // get,set ();
    int getNumberInvoice() const;
    int getDateIssue() const;
    std::map<Stock, double> getGoods() const;


    void setNumberInvoice(int _numbInvoice);
    void setDateIssue(int _dIssue);
    void setGoods(std::map<Stock, double> _goods);


    
    void addGood(Stock s, int num);
    std::map<Stock, double>::iterator it = goods.begin();
    void display()const;
};


C++
#include "Invoice.h"
Invoice::Invoice(int _numberInvoice, int _dateIssue, std::map<Stock, double> _goods)
{
    numberInvoice = _numberInvoice;
    dateIssue = _dateIssue;
    goods = _goods;
}




int Invoice::getNumberInvoice() const
{
    return numberInvoice;
}


int Invoice::getDateIssue() const
{
    return dateIssue;
}


std::map<Stock, double> Invoice::getGoods() const
{
    return goods;
}


void Invoice::setNumberInvoice(int _numbInvoice)
{
    numberInvoice = _numbInvoice;
}


void Invoice::setDateIssue(int _dIssue)
{
    dateIssue = _dIssue;
}


void Invoice::setGoods(std::map<Stock, double> _goods)
{
     goods = _goods;
}




void Invoice::addGood(Stock s, int num)
{
    goods[s] += num;
    
}


void Invoice::display() const
{
    std::cout << "\nInvoice " << numberInvoice << '\n';
    std::cout << "Date of issue " << dateIssue << '\n';
    std::cout << "Goods:\n";
    std::cout << "Code\t" << "Name\t" << "vpu\t" << "Num\t" << "Value\n";

}


C++
#include "Invoice.h"
#include "Stock.h"
#include <iostream>
using namespace std;
int main() {
	
	std::map<Stock, double> myMap;
	myMap[Stock(205, "Bread", 1.40, 3)] = 0;
	myMap[Stock(230, "Milk", 2.40, 2)] = 1;

	std::map<Stock, double>::iterator it;
	for (it = myMap.begin(); it != myMap.end(); ++it)
		std::cout << it->first << " " << it->second << '\n';


How can i print the display too, to make it look like invoice with stock?

What I have tried:

I tried everything, I do not know if I use correctly map with key
Posted
Updated 2-Nov-21 5:24am
Comments
Richard MacCutchan 2-Nov-21 8:18am    
What exactly is the problem?

1 solution

You omitted what might be the most important of your classes : Stock. In my opinion, it should be managing itself as much as it possibly can. For example, to display an invoice a Stock object only needs to know how many of itself is in the order. An invoice should be just a collection of stock items with a quantity of each. Inventory is almost identical to an order because it has a collection of stock items along with a quantity of each. That's how I would think about these things.

I am not sure a std::map is the right container for this. I guess as long as you have keys that are unique enough it can work for you. I am a fan of type definitions and in the C++ world the using statement can accomplish the same thing. In your program you use this data type a lot : std::map<Stock,double> so I would define a type for it like this :
C++
using mapStock = std::map<Stock, double>;
I'm not sure a double is the right value to associate a key with. It seems like an integer would be more appropriate to hold a quantity. I guess it depends on the increments the products are sold in. If they can be fractional amounts then a double is the right type.

By the way, if you want a more specific answer then you need to ask a more specific question.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900