Reputation: 3
Im trying to make a program about books, and it needs and ID system. I want to get the access to the information about a specifict object with the book ID. For example:
Console.Write("What ID is the book ")
//tempId = **ID**
int tempId = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(book**ID**.title); //.title = the title of the book
How can I do something like that?
I thought about doing that with a static class atribute, but I'm pretty sure this isn't possible.
Upvotes: 0
Views: 191
Reputation: 58
What I would do would be to create a class "book" containing the attributes you need (id, name, publication date...) and store all these books in an array of type map with the id as primary key. I do not know if I solve your question with this, if your question was not this one, ask me again the question and I will try to help you.
This would be an example of the class "book":
#include <string>
class Book {
public:
Book(int id, std::string name, std::string publicationDate)
: id_(id), name_(name), publicationDate_(publicationDate) {}
int id() const { return id_; }
std::string name() const { return name_; }
std::string publicationDate() const { return publicationDate_; }
private:
int id_;
std::string name_;
std::string publicationDate_;
};
And so you can create the map I was telling you about:
int main() {
std::map<int, Book> library;
library[123] = Book(123, "El Quijote", "1605-01-01");
library[456] = Book(456, "Cien años de soledad", "1967-05-01");
library[789] = Book(789, "Don Juan Tenorio", "1844-01-01");
int bookId = 456;
auto it = library.find(bookId);
return 0;
}
The above code creates a map with three books and retrieves in it the one with id 456.
Upvotes: 2