Associative array map with key

Hello guys, can you help me with this struggle. CPP program
I have to create a Invoice class who have to contains these fields:
1. List of purchased goods - associative array (map) with key object Stock and its value
2. get() and set() methods for the list to work on an elementary

I have something like a code, but i really struggle with this arrays, can you give me some examples how can ive done it right?

1
2
3
4
5
6
7
8
9
10
11
   // for class Invoice
  class Invoice
{
	int numberInvoice;
	int dateIssue;
	std::map<Stock, double> price;
public:
	Invoice();
	Invoice(int _numberInvoice, int _dateIssue);

};



1
2
3
4
5
6
7
 class Stock {
	int code;
	std::string name;
	double valuePerUnit;    
	//int noUnits;
	int numberUnits;
};
Are you able to give a more complete description of the functionality that's supposed to be accomplished? The <Stock, double> construct you have is a bit strange, since it feels like you should already be able to calculate the value based on valuePerUnit and numberUnits. Are you sure the assignment isn't asking for a map<string, Stock>?

for the list to work on an elementary
What does this mean?

A std::map requires an ordering. For example, it could be ordered by the code of the Stock, or perhaps its name. This is enabled with an operator< (can also be a function object passed as a template parameter).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Example program
#include <iostream>
#include <string>
#include <map>

class Stock {
  public:
	int code;
	std::string name;
	double valuePerUnit;
	int numberUnits;
	
	bool operator<(const Stock& other) const
	{
	    return (this->name < other.name);   
	}
};

int main()
{
    std::map<Stock, double> stonks;
    
    Stock stock1 {234, "Apple", 23.45, 11};
    Stock stock2 {667, "Microsoft", 11.99, 50};

    stonks[stock1] = 65.42;
    stonks[stock2] = 555.55l;
    
    std::cout << stonks[stock1] << '\n';
}


But again, something like this sounds more sane to me:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Example program
#include <iostream>
#include <string>
#include <map>

class Stock {
  public:
	int code;
	std::string name;
	double valuePerUnit;
	int numberUnits;
	
	double value() const
	{
	    return valuePerUnit * numberUnits;
	}
};

int main()
{
    std::map<std::string, Stock> stonks;
    
    stonks["Apple"] = Stock {234, "Apple", 23.45, 11};
    stonks["Microsoft"] = Stock {667, "Microsoft", 11.99, 50};

    std::cout << stonks["Apple"].value() << '\n';
}
Last edited on
Stock class is my first class, and in Invoice class I need to make an array that has a key object of class Stock and its price
but I don't know how to do it

So The <Stock, double> , i just write it like this but i dont think it is like this
As a starter, perhaps consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <string>
#include <iostream>
#include <map>

class Stock {
	int code {};
	std::string name;
	double valuePerUnit {};

public:
	Stock() {}
	Stock(int code_, const std::string& name_, double vpu) : code(code_), name(name_), valuePerUnit(vpu) {}

	void display() const {
		std::cout << code << "\t" << name << "\t" << valuePerUnit;
	}

	double getval() const { return valuePerUnit; }

	bool operator<(const Stock& other) const
	{
		return (name < other.name);
	}
};

class Invoice {
	int numberInvoice {};
	int dateIssue {};
	std::map<Stock, int> goods;

public:
	Invoice() {}
	Invoice(int _numberInvoice, int _dateIssue) : numberInvoice(_numberInvoice), dateIssue(_dateIssue) {}

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

	void 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";

		double total {};

		for (const auto& [s, num] : goods) {
			const auto gtot {num * s.getval()};

			s.display();
			std::cout << "\t" << num << '\t' << gtot << '\n';
			total += gtot;
		}

		std::cout << "\nInvoice total: " << total << '\n';
	}
};

int main() {
	Stock s1 {1, "s1", 1};
	Stock s2 {2, "s2", 2};

	Invoice i1 {1, 1};

	i1.addGood(s1, 2);
	i1.addGood(s2, 3);

	i1.display();
}




Invoice 1
Date of issue 1
Goods:
Code    Name    vpu     Num     Value
1       s1      1       2       2
2       s2      2       3       6

Invoice total: 8

Topic archived. No new replies allowed.