Question
Replace the member function Inventory::init() with a default constructor, e.g. Inventory() { _count = 0U; }, or: Inventory() : _count{ 0U } { } and
-
Replace the member function Inventory::init() with a default constructor, e.g. Inventory() { _count = 0U; }, or: Inventory() : _count{ 0U } { } and ensure that in the function main() it is used instead of the member function init().
-
Replace the member function void Abstraction::init(..) with a class constructor. In other places of the solution ensure that instead of the member function init(..) appropriate constructor is used during the creation of an Abstraction object.
#include "Inventory.h" #include "BicycleSpec.h" #include "Bicycle.h" #include
#include using namespace std;
void show(const Bicycle & item) { const BicycleSpec & is{ item.get_spec() }; cout << item.get_id() << " " << std::fixed << std::setprecision(2) << item.get_price() << " " << "'" << item.get_model() << "' " << item.get_model_year() << " " << is.get_wheel_size() << " " << is.get_gears() << " " << is.get_type_as_string() << " " << endl; }
int main() { Inventory inv; // specification is constructed separately BicycleSpec spec_bmx{ BicycleSpec::Type::BMX, 20, 1 }; inv.add_item(1, 149.0, "SCORE Stickerbomb", 2019, spec_bmx);
// specification is constructed during passing of an argument inv.add_item(2, 129.0, "Bikko Classic cream", 2016, BicycleSpec{ BicycleSpec::Type::STREET, 28, 1 });
// constructing and using shared specification BicycleSpec spec_beach{ BicycleSpec::Type::BEACH, 27, 6 }; inv.add_item(3, 185.0, "Romet Vintage M brown", 2019, spec_beach); inv.add_item(4, 197.0, "Romet Vintage M green", 2018, spec_beach);
// provide some querying values (some can be default (e.g., "", 0) to denote unset criteria) show(inv.find_item(BicycleSpec{ BicycleSpec::Type::ANY, 20, 0 }));
// test with another query values show(inv.find_item(spec_beach));
show(inv.find_item(Bicycle{ 0, 0.0, "", 2016, BicycleSpec{ } })); // testing for non-matching criterion show(inv.find_item(BicycleSpec{ BicycleSpec::Type::MTB, 0, 0 })); show(inv.find_item(Bicycle{ 5, 0.0, "", 0, BicycleSpec{ } }));
#ifndef NDEBUG cin.get(); // wait until Enter key is pressed #endif
return 0; }
#pragma once
#include "BicycleSpec.h" // ! #include
class Bicycle { public: Bicycle() = default; Bicycle(unsigned id, double price, std::string model, unsigned short model_year, const BicycleSpec & spec) : _id{ id } , _price{ price } , _model{ model } , _model_year{ model_year } , _spec{ spec } { } unsigned get_id() const { return _id; } double get_price() const { return _price; } std::string get_model() const { return _model; } unsigned short get_model_year() const { return _model_year; } BicycleSpec get_spec() const { return _spec; } private: unsigned _id= 0; double _price= 0.0; std::string _model = ""; unsigned short _model_year = 0; BicycleSpec _spec; };
#pragma once
#include "Bicycle.h" #include "BicycleSpec.h" // include explicitly even if would be included implicitly via Bicycle.h #include
class Inventory { public: // Initializes the inventory for storing abstraction objects Inventory() : {_count = 0U; }
// Returns the number of currently stored abstraction objects size_t get_count() const { return _count; }
// Returns stored abstraction object by its index or default if index is invalid Bicycle get_item(size_t i) const { return (i < _count) ? _items[i] : Bicycle{}; }
// From passed property values, creates and adds new abstraction object in an array _items void add_item(unsigned id, double price, std::string model, unsigned short model_year, const BicycleSpec & spec);
// Looks for a matching abstraction object and returns the first found or default object Bicycle find_item(const Bicycle & query) const; Bicycle find_item(const BicycleSpec & spec_query) const;
private: // The maximum number of abstraction objects that can be stored static const size_t MAX_SIZE{ 10 };
// An array for storing abstraction objects Bicycle _items[Inventory::MAX_SIZE];
// The number of currently stored abstraction objects in the array _items size_t _count; };
#pragma once
#include
// for std::size_t #include class BicycleSpec { public: enum class Type { ANY, MTB, STREET, BEACH, BMX }; BicycleSpec() = default; BicycleSpec(Type type, unsigned short wheel_size, unsigned short gears) : _type{ type } , _wheel_size{ wheel_size } , _gears{ gears } { }
Type get_type() const { return _type; } // another C++ idiom to return an enumeration value as a string const std::string & get_type_as_string() const { static const std::string Type_S[] { "Any", "MTB", "Street", "Beach", "BMX" }; return Type_S[(std::size_t)_type]; }
unsigned short get_wheel_size() const { return _wheel_size; } unsigned short get_gears() const { return _gears; }
private: Type _type; unsigned short _wheel_size; unsigned short _gears; };
#include
#include "Inventory.h" void Inventory::add_item(unsigned id, double price, std::string model, unsigned short model_year, const BicycleSpec & spec) { if (_count < Inventory::MAX_SIZE) { Bicycle new_item(id, price, model, model_year, spec); //TODO: Check for identical object before adding _items[_count] = new_item; _count++; } }
Bicycle Inventory::find_item(const Bicycle & query) const { // temporary reference to specification const BicycleSpec & query_spec{ query.get_spec() }; for (size_t i = 0; i < _count; i++) { // for integer type property if (query.get_id() != 0 && query.get_id() != _items[i].get_id()) continue;
// for floating type property constexpr double epsil{ 0.005 }; if (query.get_price() != 0.0 && (epsil < std::abs(query.get_price() - _items[i].get_price()))) continue; // for string type property if (query.get_model() != "" && query.get_model() != _items[i].get_model()) continue; if (query.get_model_year() != 0 && query.get_model_year() != _items[i].get_model_year()) continue; // temporary local reference to item's specification const BicycleSpec & item_spec{ _items[i].get_spec() }; // and now check each specification property
if (query_spec.get_wheel_size() != 0 && query_spec.get_wheel_size() != item_spec.get_wheel_size()) continue;
if (query_spec.get_gears() != 0 && query_spec.get_gears() != item_spec.get_gears()) continue; if (query_spec.get_type() != BicycleSpec::Type::ANY && query_spec.get_type() != item_spec.get_type()) continue; return _items[i]; }
return Bicycle{}; // return the 'default' object }
Bicycle Inventory::find_item(const BicycleSpec & query_spec) const { for (size_t i = 0; i < _count; i++) { const BicycleSpec & item_spec{ _items[i].get_spec() };
if (query_spec.get_wheel_size() != 0 && query_spec.get_wheel_size() != item_spec.get_wheel_size()) continue;
if (query_spec.get_gears() != 0 && query_spec.get_gears() != item_spec.get_gears()) continue; if (query_spec.get_type() != BicycleSpec::Type::ANY && query_spec.get_type() != item_spec.get_type()) continue;
return _items[i]; }
return Bicycle{}; // return the 'default' object (if you use Java - return null value) }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started