Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

can you help me troubleshoot my c + + code? #include inventory.h #include NodeList.h #include exceptions.h #include #include #include Inventory::Inventory ( )

can you help me troubleshoot my c++ code?
#include "inventory.h"
#include "NodeList.h"
#include "exceptions.h"
#include
#include
#include
Inventory::Inventory(){}
Inventory::Inventory(int capacity){
// Initialize capacity if needed
}
Inventory::~Inventory(){}
Inventory::Inventory(const Inventory& obj) : v_List(obj.v_List){}
Inventory& Inventory::operator=(const Inventory& rhs){
if (this != &rhs){
v_List = rhs.v_List;
}
return *this;
}
bool Inventory::push_Back(Vehicle vehicle){
try {
v_List.insertBack(vehicle);
return true; // Assuming always successful for simplicity
}
catch (const std::exception& e){
std::cerr "Error inserting vehicle: " e.what() std::endl;
return false;
}
}
void Inventory::sortList(){
// Bubble sort implementation for NodeList
try {
for (auto it1= v_List.begin(); it1!= v_List.end(); ++it1){
for (auto it2= it1; it2!= v_List.end(); ++it2){
if (*it2>*it1){// Assuming Vehicle comparison operators are defined
Vehicle temp =*it1;
*it1=*it2;
*it2= temp;
}
}
}
}
catch (const std::exception& e){
std::cerr "Error sorting list: " e.what() std::endl;
}
}
void Inventory::printList(){
try {
printListRecursive(v_List.begin());
}
catch (const std::exception& e){
std::cerr "Error printing list: " e.what() std::endl;
}
}
void Inventory::printListRecursive(NodeList::Iterator it) const {
if (it == v_List.end()) return;
std::cout *it "------------------------------------------
";
printListRecursive(++it);
}
void Inventory::printReservedList(){
try {
printReservedListRecursive(v_List.begin());
}
catch (const std::exception& e){
std::cerr "Error printing reserved list: " e.what() std::endl;
}
}
void Inventory::printReservedListRecursive(NodeList::Iterator it) const {
if (it == v_List.end()) return;
if ((*it).status()){
std::cout *it;
}
printReservedListRecursive(++it);
}
void Inventory::printAvailableList(){
try {
printAvailableListRecursive(v_List.begin());
}
catch (const std::exception& e){
std::cerr "Error printing available list: " e.what() std::endl;
}
}
void Inventory::printAvailableListRecursive(NodeList::Iterator it) const {
if (it == v_List.end()) return;
if ((*it).status()){
std::cout *it;
}
printAvailableListRecursive(++it);
}
bool Inventory::found(int seatsNo){
try {
return foundRecursive(v_List.begin(), seatsNo);
}
catch (const std::exception& e){
std::cerr "Error finding vehicle: " e.what() std::endl;
return false;
}
}
bool Inventory::foundRecursive(NodeList::Iterator it, int seatsNo) const {
if (it == v_List.end()) return false;
if ((*it).status() && (*it).getSeats()>= seatsNo){
std::cout *it;
return true;
}
return foundRecursive(++it, seatsNo);
}
bool Inventory::reserveVehicle(int vehicleID){
try {
int index = checkID(vehicleID);
if (index ==-1) throw InvalidVehicleIDException();
if (!v_List[index].status()) throw VehicleAlreadyReservedException();
v_List[index].reserve();
return true;
}
catch (const InvalidVehicleIDException& e){
std::cerr e.what() std::endl;
return false;
}
catch (const VehicleAlreadyReservedException& e){
std::cerr e.what() std::endl;
return false;
}
catch (const std::exception& e){
std::cerr "Error reserving vehicle: " e.what() std::endl;
return false;
}
}
bool Inventory::returnVehicle(int vehicleID){
try {
int index = checkID(vehicleID);
if (index ==-1) throw InvalidVehicleIDException();
if (v_List[index].status()) throw std::runtime_error("Vehicle is already available.");
v_List[index].unReserve();
return true;
}
catch (const InvalidVehicleIDException& e){
std::cerr e.what() std::endl;
return false;
}
catch (const std::exception& e){
std::cerr "Error returning vehicle: " e.what() std::endl;
return false;
}
}
int Inventory::checkID(int vehicleID){
try {
int index =0;
for (auto it = v_List.begin(); it != v_List.end(); ++it,++index){
if ((*it).getID()== vehicleID){
return index;
}
}
throw InvalidVehicleIDException();
}
catch (const InvalidVehicleIDException& e){
std::cerr e.what() std::endl;
return -1;
}
}
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Define Scientific Management

Answered: 1 week ago

Question

Explain budgetary Control

Answered: 1 week ago

Question

Solve the integral:

Answered: 1 week ago

Question

What is meant by Non-programmed decision?

Answered: 1 week ago