2.Console Store
Здравейте, задача 2.Console Store от изпита по C++ OOP – Exam (4 September 2022) ми дава 40/100. Нулевите тестове са едно към едно с очаквания изход. Пробвах мои входни данни и всичко изглежда да работи. Може ли някой да ми предостави допълнителни входни данни, за да тествам, защото се изчерпах към момента с какво да пробвам.
Благодаря предварително.
Условието на задачата е:
The world is hungry for gaming consoles!
It is your time to shine. You decide to open a hardware console store and sell all those how new PlayStations and Xboxes. As it turns out…this is not so easy.
Your task is to study the code and implement the missing functionalities.
There are two types of consoles sold at your store:
enum class ConsoleType {
PS,
XBOX
};
Your program is also given several commands to execute.
After you execute the command you should print to the standard output the outcome of the command.
The possible commands are:
enum class CommandType {
ADD,
REMOVE,
SORT_BY_PRICE,
SORT_BY_QUALITY,
};
Where:
-
ADD – creates a new console of the specified type and store it into your “Store”. Additional console fields such as price, quality and console generation are also provided here.
-
REMOVE – removes the last added console of the provided type. You are guaranteed that there will be such console in your Store (the input will need to validation)
-
SORT_BY_PRICE – sort the selected console of the provided type by their price in descending order (the one with the biggest price is first, the one with the lowest price is last)
-
SORT_BY_QUALITY – sort the selected console of the provided type by their quality in descending order (the one with the biggest quality is first, the one with the lowest quality is last)
-
PRINT – print data for all the selected console type that are present in your Store
Input
The input is already handled for you. You don’t need to parse anything additionally.
Output
- When a command ADD is received you should print:
“Adding: Xbox with price: P, quality: Q”
or
“Adding: PS with generation G, price: P, quality: Q”
Where:
G – current console generation
P – current console price
Q – current console quality
- When a command REMOVE is received you should print:
(Same as the above, just change the “Adding” with “Removing”)
- When a command SORT_BY_PRICE is received you should print:
“Sorting all Console by price”
Where
Console – the selected console type (PS or XBox)
- When a command SORT_BY_QUALITY is received you should print:
“Sorting all Console by quality”
Where
Console – the selected console type (PS or XBox)
- When a command PRINT is received you should print:
“Printing all Console data”
Where
Console – the selected console type (PS or XBox)
Followed by individual console information for each console of the selected type present at your store.
“Xbox with price: P, quality: Q”
“PS with generation G, price: P, quality: Q”
See the provided examples section for concrete examples.
Restrictions
No restrictions.
Time limit: 500ms (0.50s)
Memory limit: 16 MB
Примери:
Input |
Output |
3 0 1 200 100 0 1 100 150 1 1
|
Adding: Xbox with price: 200, quality: 100 Adding: Xbox with price: 100, quality: 150 Removing: Xbox with price: 100, quality: 150
|
4 0 1 200 100 0 1 100 150 3 1 4 1
|
Adding: Xbox with price: 200, quality: 100 Adding: Xbox with price: 100, quality: 150 Sorting all Xbox by quality Printing all Xbox data Xbox with price: 100, quality: 150 Xbox with price: 200, quality: 100
|
8 0 0 50 200 1 0 1 150 250 0 1 200 100 0 1 100 150 3 1 4 1 2 1 4 1
|
Adding: PS with generation 1, price: 50, quality: 200 Adding: Xbox with price: 150, quality: 250 Adding: Xbox with price: 200, quality: 100 Adding: Xbox with price: 100, quality: 150 Sorting all Xbox by quality Printing all Xbox data Xbox with price: 150, quality: 250 Xbox with price: 100, quality: 150 Xbox with price: 200, quality: 100 Sorting all Xbox by price Printing all Xbox data Xbox with price: 200, quality: 100 Xbox with price: 150, quality: 250 Xbox with price: 100, quality: 150
|
Използвам два вектора за различните конзоли.
Ето и кода:
#ifndef INC_02_CONSOLE_STORE_STORE_H
#define INC_02_CONSOLE_STORE_STORE_H
#include <vector>
#include <algorithm>
#include "Console.h"
#include "Defines.h"
class Store{
public:
Store() = default;
void addPs(int price, int quality, int generation){
Console console(price, quality);
console_list_PS.emplace_back(console, generation);
std::cout << "Adding: PS with generation " << generation << ", price: " << price <<
", quality: " << quality << std::endl;
}
void addXbox(int price, int quality) {
Console console(price, quality);
console_list_XBOX.push_back(console);
std::cout << "Adding: Xbox with price: " << price << ", quality: " << quality << std::endl;
}
void remove(ConsoleType consoleType) {
if(consoleType == ConsoleType::PS){
if(!console_list_PS.empty()) {
std::cout << "Removing: PS with generation: " <<
console_list_PS[console_list_PS.size() - 1].second << ", price: " <<
console_list_PS[console_list_PS.size() - 1].first.getPrice()
<< ", quality: " <<
console_list_PS[console_list_PS.size() - 1].first.getQuality() << std::endl;
console_list_PS.pop_back();
}
}
else {
if (!console_list_XBOX.empty()) {
std::cout << "Removing: Xbox with price: " << console_list_XBOX[console_list_XBOX.size() - 1].getPrice()
<< ", quality: " << console_list_XBOX[console_list_XBOX.size() - 1].getQuality() << std::endl;
console_list_XBOX.pop_back();
}
}
}
void sortByPrice(ConsoleType consoleType) {
if(consoleType == ConsoleType::PS && !console_list_PS.empty()){
std::cout << "Sorting all PS by price" << std::endl;
std::sort(std::begin(console_list_PS), std::end(console_list_PS),
[&](std::pair<Console, int> a, std::pair<Console, int> b)
{ return a.first.getPrice() > b.first.getPrice(); });
}
else if(!console_list_XBOX.empty()){
std::cout << "Sorting all Xbox by price" << std::endl;
std::sort(std::begin(console_list_XBOX), std::end(console_list_XBOX),
[](Console& a, Console& b) { return a.getPrice() > b.getPrice(); });
}
}
void sortByQuality(ConsoleType consoleType) {
if(consoleType == ConsoleType::XBOX && !console_list_XBOX.empty()){
std::cout << "Sorting all Xbox by quality " << std::endl;
std::sort(std::begin(console_list_XBOX), std::end(console_list_XBOX),
[](Console& a, Console& b) { return a.getQuality() > b.getQuality(); });
}
else if(!console_list_PS.empty()){
std::cout << "Sorting all PS by quality" << std::endl;
std::sort(std::begin(console_list_PS), std::end(console_list_PS),
[](std::pair<Console, int> a, std::pair<Console, int> b)
{ return a.first.getQuality() > b.first.getQuality(); });
}
}
void print(ConsoleType consoleType) {
if(consoleType == ConsoleType::XBOX && !console_list_XBOX.empty()){
std::cout << "Printing all Xbox data" << std::endl;
for(const auto& console : console_list_XBOX){
std::cout << "Xbox with price: " << console.getPrice() <<
", quality: " << console.getQuality() << std::endl;
}
}
else if(!console_list_PS.empty()){
std::cout << "Printing all PS data" << std::endl;
for(const auto& console : console_list_PS){
std::cout << "PS with generation: " << console.second << ", price: " << console.first.getPrice() <<
", quality: " << console.first.getQuality() << std::endl;
}
}
}
private:
std::vector<std::pair<Console, int>> console_list_PS;
std::vector<Console> console_list_XBOX;
};
#endif //INC_02_CONSOLE_STORE_STORE_H
Има излишни валидации в повечето методи.
print метода няма да изведе никаква информация при празни вектори (не виждам такова нещо в условието), а при тип ConsoleType::XBOX и празен console_list_XBOX ще изведе съдържанието на console_list_XBOX, ако той не е празен.
Същата логическа грешка я има и в sortByPrice и sortByQuality - възможно е да бъде сортиран console_list_PS, ако console_list_XBOX е празен.
Здравей Мартин БГ, коригирах нещата, които ми посочи, но резултата е същия.