Skip to content
This repository has been archived by the owner on Jan 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #100 from ur-cs-courses/CSV-file-when-program-ends
Browse files Browse the repository at this point in the history
Robot and Room Csv files updated when program ends
  • Loading branch information
lizlouise1335 authored Dec 14, 2023
2 parents 5376f6e + 250cebe commit f8dc48a
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 5 deletions.
20 changes: 19 additions & 1 deletion app/fleet_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,25 @@ int main() {
}
case 0: {
std::cout << "Exiting Robot Fleet Management System." << std::endl;

std::ofstream robotFile(csvPathRobot);
if (robotFile.is_open()) {
robotFile << management.to_string_robot_list_csv();
robotFile.close();
std::cout << "Robot information saved to " << csvPathRobot << std::endl;
} else {
std::cerr << "Unable to open robot CSV file: " << std::strerror(errno) << std::endl;
}

std::ofstream roomFile(csvPathRoom);
if (roomFile.is_open()) {
roomFile << management.to_string_room_list_csv();
roomFile.close();
std::cout << "Room information saved to " << csvPathRoom << std::endl;
} else {
std::cerr << "Unable to open room CSV file: " << std::strerror(errno) << std::endl;
}

return 0; // Exit the while loop and end the program
}
default: {
Expand All @@ -109,6 +128,5 @@ int main() {
}
}
}

return 0; // End of the program
}
2 changes: 2 additions & 0 deletions include/libRobot/Robot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ class Robot {
*/
std::string to_string_type();

std::string to_string_csv();

/**
* Helper - converts enum Robot_Status to string
* @param None
Expand Down
2 changes: 2 additions & 0 deletions include/libroom/room.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ class Room {
*/
std::string to_string_status();

std::string to_string_csv();

/**
* Helper - Converts enum class Size value to string
* @param None
Expand Down
6 changes: 5 additions & 1 deletion include/manage/Management.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,14 @@ class Management {
/**
* Accessor - prints the Room objects in room_list_
* @param None
* @return String
* @return String`
*/
std::string to_string_room_list();

std::string to_string_room_list_csv();
std::string to_string_robot_list_csv();


/**
* Accessor - prints the Robot objects in robot_list_
* @param None
Expand Down
26 changes: 26 additions & 0 deletions src/Management.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,32 @@ std::string Management::to_string_room_list() {
return output;
}

std::string Management::to_string_room_list_csv() {
std::string output = "";
size_t totalRooms = room_list_.size();
size_t currentRoom = 0;

for (auto& pair : room_list_) {
output += pair.second.to_string_csv();
if (++currentRoom < totalRooms) {
output += "\n";
}
}
return output;
}

std::string Management::to_string_robot_list_csv() {
std::string output = "";
size_t totalRobots = robot_list_.size();
size_t currentRobot = 0;
for (auto& pair : robot_list_) {
output += pair.second.to_string_csv();
if (++currentRobot < totalRobots) {
output+= "\n";
}
}
return output;
}

std::string Management::to_string_robot_list() {
std::string output = "********** ROBOTS ************ \n \n";
Expand Down
56 changes: 56 additions & 0 deletions src/Robot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ Robot::Robot(std::string ID, std::string status_, std::string size_, std::string
set_status(status_);
set_type(type_);
set_size(size_);
if (this->status_ == Robot_Status::Dead) {
this->battery_=0;
}
}

Robot::Robot(const Robot& other) {
Expand Down Expand Up @@ -107,6 +110,59 @@ std::string Robot::to_string_type() {
return "Type:\tUnknown";
}

std::string Robot::to_string_csv() {
std::string output = "";
output+= id_ + ",";

switch (this->status_) {
case (Robot_Status::Free):
output+= "Free";
break;
case (Robot_Status::Busy):
output+= "Busy";
break;
case (Robot_Status::Broken):
output+= "Broken";
break;
case (Robot_Status::Dead):
output+= "Dead";
break;
case (Robot_Status::Offline):
output+= "Offline";
break;
}
output+= ",";

switch (this->size_) {
case (Robot_Size::Small):
output+= "Small";
break;
case (Robot_Size::Medium):
output+= "Medium";
break;
case (Robot_Size::Large):
output+= "Large";
break;
}
output+= ",";

switch (this->type_) {
case (Type::Mop):
output+= "Mop";
break;
case (Type::Vac):
output+= "Vaccuum";
break;
case (Type::Scrub):
output+= "Scrub";
break;
}
output+= ",";

output+= room_id_;
return output;
}

std::string Robot::to_string_status() {
switch (this->status_) {
case (Robot_Status::Free):
Expand Down
41 changes: 41 additions & 0 deletions src/room.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,52 @@ std::string Room::to_string_status() {
return "Room Status:\tIn-progress";
case (Room_Status::clean):
return "Room Status:\tClean";
default:
return "Room Status:\tUnknown";
}

return "Room Status:\tUnknown";
}

std::string Room::to_string_csv() {
std::string output = "";
output+= this->room_name_ + ",";

switch (this->room_status_) {
case (Room_Status::dirty):
output+= "Dirty";
break;
case (Room_Status::in_progress):
output+= "In-progress";
break;
case (Room_Status::clean):
output+= "Clean";
break;
default:
output+= "NA";
break;
}
output+= ",";

switch (this->room_size_) {
case (Room_Size::small):
output+= "Small";
break;
case (Room_Size::medium):
output+= "Medium";
break;
case (Room_Size::large):
output+= "Large";
break;
default:
output+= "NA";
break;
}

output+= "," + std::to_string(this->estimated_time_);
return output;
}

std::string Room::to_string_size() {
switch (this->room_size_) {
case (Room_Size::small):
Expand Down
6 changes: 3 additions & 3 deletions tests/robot_test.csv
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
0,Free,Small,Mop,NA
1,Free,Small,Vac,NA
0,Broken,Small,Mop,NA
1,Dead,Small,Vaccuum,NA
2,Free,Medium,Scrub,NA
3,Free,Medium,Mop,NA
4,Free,Large,Vac,NA
4,Free,Large,Vaccuum,NA
5,Free,Large,Scrub,NA
6,Free,Large,Mop,NA

0 comments on commit f8dc48a

Please sign in to comment.