#Tests are not passing for Scheduler class

11 messages · Page 1 of 1 (latest)

hybrid solarBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

lone comet
#
#include "Scheduler.h"
#include "GroomingAppointment.h"
#include "HaircutAppointment.h"
//#include "Appointment.h"
#include <iostream>
#include <vector>
#include <string>
#include "fstream"
#include <sstream>
#include <set>
#include <algorithm>
#include <memory>

/**
 * @brief Constructs Scheduler class object
 * @param string file
 */
Scheduler::Scheduler(std::string file)
{
    LoadData(file);
}

/**
 * @brief Gets a list of all scheduled appointments
 * @return vector<shared_ptr<Appointments>> appointments
 */
//Returns a list of all scheduled appointments
std::vector<std::shared_ptr<Appointment>> Scheduler::GetAppointments() const
{
    std::vector<std::shared_ptr<Appointment>> appointments;
    return appointments;
}

/**
 * @brief Gets a list of all dates with a scheduled appointment
 * @return set<string> dates
 */
//Returns a list of all dates with a scheduled appointment
std::set<std::string> Scheduler::GetAppointmentDates() const
{
    //creates a set of strings called dates
    std::set<std::string> dates;
    for(auto& d : appointments_)
    {
        dates.insert(d->GetDate());
    }
    return dates;
}

/**
 * @brief Gets a list of all Groomers scheduled for appointments
 * @return set<string> groomers
 */
//Returns a list of all groomers scheduled for appointments
std::set<std::string> Scheduler::GetGroomers()
{
    //creates a set of strings called groomers
    std::set<std::string> groomers;
    for(auto& g : appointments_)
    {
        //error No matching member function for call to 'insert'
        groomers.insert(g->GetGroomer().GetFirstName());
        groomers.insert(g->GetGroomer().GetLastName());
    }
    return groomers;
}

/**
 * @brief Creates a vector of pointers that have appointments that match given date
 * @param string date
 * @return vector<shared_ptr<Appointment>> wantedDate
 */
//Returns a vector of pointers that have appointments that match the given date
/*Hint: You must convert the given date string into a DateTime object so the format
 * always matches Once this is done you can use the GetDate() function for the comparison.
*/
std::vector<std::shared_ptr<Appointment>> Scheduler::GetAppointmentsByDate(std::string date)
{
    std::string temp;
    DateTime d(date, temp);
    std::vector<std::shared_ptr<Appointment>> wantedDate;
    /*error in instantiation of function template specialization 'std::copy_if<__gnu_cxx::__normal_iterator<std::shared_ptr
    *   <Appointment> *, std::vector<std::shared_ptr<Appointment>>>, std::back_insert_iterator<std::vector<std::shared_ptr<Appointmen...
    */
    std::copy_if(appointments_.begin(), appointments_.end(),std::back_inserter(wantedDate),
                 [date] (std::shared_ptr<Appointment> a) {return a->GetDate() == date;}
    ); //end of lambda

    return wantedDate;
}
#
/**
 * @brief Takes and reads file, and separates each info to create appointments\n
 * Puts the appointments into the class appointments_ vector\n
 * Uses the letter at the start of each like to decided what kind of appointment it is\n
 * H for Haircut\n
 * G for Grooming
 * @remark Doesn't format the data within the LoadData function
 * @param string filename
 */
//Reads in data with the following format into corresponding objects, then adds the appointments to the class
//appointments_ vector. Will use the letter at the start of each line to decide what kind of appointment.
/*
 * Hint: This function should not include code for formatting data. For example, after the DateTime data has been
 * parsed, we should be able to create a DateTime object and the data should be formatted in the DateTime object constructor.
 */
void Scheduler::LoadData(std::string filename)
{
    std::ifstream file(filename);
    if (!file.is_open())
    {
        std::cout << "Error opening file: " << filename << std::endl;
    }

    std::string line;
    while (std::getline(file, line,'\n'))
    {
        const char DELIM = ',';

        std::stringstream ss(line);
        std::string type;
        ss >> type;
        std::string groomFirst, groomLast, groomPhone; //groomer's info
        ss.ignore();
        getline(ss, groomFirst, DELIM);
        getline(ss, groomLast, DELIM);
        getline(ss, groomPhone, DELIM);
        Person groom(groomFirst, groomLast, groomPhone);

        std::string ownerFirst, ownerLast, ownerPhone; //owner's info
        getline(ss, ownerFirst, DELIM);
        getline(ss, ownerLast, DELIM);
        getline(ss, ownerPhone, DELIM);

        std::string petName, petType; //pet info
        getline(ss, petName, DELIM);
        getline(ss, petType, DELIM);
        Pet client(ownerFirst, ownerLast, ownerPhone, petName, petType);

        double groomPrice, haircutPrice; //price
        std::string date,time; //date & time
        std::string temp;

        if (type == "H" or type == "h") //haircut
        {
            //price
            //groom
            getline(ss, temp, DELIM);
            try {
                groomPrice = std::stoi(temp);
            } catch(std::exception &e) {
                groomPrice = 0;
            }

            //haircut
            getline(ss, temp, DELIM);
            try {
                haircutPrice = std::stod(temp);
            } catch(std::exception &e) {
                haircutPrice = 0;
            }

            //date & time
            getline(ss, date, DELIM);
            getline(ss, time, DELIM);
            //            HaircutAppointment haircut(groom, client, date, time, groomPrice, haircutPrice);

            std::shared_ptr<Appointment> app (new HaircutAppointment(groom, client, date, time, groomPrice, haircutPrice));
            appointments_.push_back(app);
        }

        else if (type == "G" or type == "g") //grooming
        {
            //price
            //groom
            getline(ss, temp, DELIM);
            try {
                groomPrice = std::stoi(temp);
            } catch(std::exception &e) {
                groomPrice = 0;
            }

            //date & time
            getline(ss, date, DELIM);
            getline(ss, time, DELIM);
            GroomingAppointment grooming(groom, client, date, time, groomPrice);
            std::shared_ptr<Appointment> app (new GroomingAppointment(groom, client, date, time, groomPrice));
            appointments_.push_back(app);
        }

    }//end of while loop
    file.close();
}
#
/**
 * @brief Takes given criteria and filters all appointments that match\n
 * Coverts the string into a DateTime object so the format matches\n
 * Uses GetDate() function for comparing
 * @param string groomFirst
 * @param string groomLast
 * @param string date
 * @return vector<shared_ptr<Appointment>> filtered_appointments
 */
//Returns a vector of pointers that have appointments that match the given criteria
/*
 * Hint: You must convert the given date string into a DateTime object so the format always matches Once this is done
 * you can use the GetDate() function for the comparison.
 */
std::vector<std::shared_ptr<Appointment>> Scheduler::Filter(std::string groomFirst, std::string groomLast, std::string date)
{
    std::vector<std::shared_ptr<Appointment>> filter;
    std::string groom = groomFirst + " " + groomLast;

    std::string temp,tempDate, tempTime, phone;
    std::stringstream ss(date);

    getline(ss, temp, ',');
    tempDate = temp;
    getline(ss,temp,'\n');
    tempTime = temp;

    DateTime d(tempDate,tempTime);
    //
    //    std::copy_if(appointments_.begin(), appointments_.end(),std::back_inserter(filter),
    //                 [groomFirst, groomLast, tempDate, tempTime] (const Appointment &a, const Person &p)
    //                 {return a.GetDate() == tempDate  && p.GetFirstName() == groomFirst && p.GetLastName() == groomLast;}
    //    );

    return filter;
}

/**
 * @brief Prints out filtered appointments that meet the criteria in a format
 * @param ostream& out
 * @param Scheduler& scheduler
 * @return ostream& out
 */
/*
 * Function should print the date and time in the following format:
Time:    07/07/2023 9:00am
Client:  Buttons
Groomer: Katy Perry 801-645-8877
Price:   $100.00

Time:    07/07/2023 10:30am
Client:  Zipper
Groomer: Katy Perry 801-645-8777
Price:   $40.00
 */
std::ostream& operator<<(std::ostream& out, const Scheduler& scheduler)
{
    for(int i = 0; i < scheduler.GetAppointments().size(); i++)
    {
        out << scheduler.appointments_[i];
    }
    return out;
    //        out << scheduler.GetAppointments() << std::endl;

}
hybrid solarBOT
#

@lone comet

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.

lone comet
#

!solved

hybrid solarBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

lone comet
#

The problem was that object weren't being created correctly. This is because of the lines

        std::stringstream ss(line);
        std::string type;
        ss >> type;

This made it so everything was going into type, and then any variable afterward would be either blanked or wrong.

#
        std::stringstream ss(line);

        std::string letter;
        getline(ss,letter,DELIM);

I fixed it with this, and switching my if statements to compare the letter variable rather than type

lone comet
#
//Returns a list of all groomers scheduled for appointments
std::set<std::string> Scheduler::GetGroomers()
{
    //creates a set of strings called groomers
    std::set<std::string> groomers;
    for(auto& g : appointments_)
    {
       //groomer isn't in the set
       groomers.insert(g->GetGroomer().GetFirstName());
       groomers.insert(g->GetGroomer().GetLastName());
    }
    return groomers;
}

Returned a size x2 then expected because of inserting each part of the name separately. Calling GetFullName() fixed it

//Returns a list of all groomers scheduled for appointments
std::set<std::string> Scheduler::GetGroomers()
{
    //creates a set of strings called groomers
    std::set<std::string> groomers;
    for(auto& g : appointments_)
    {
                //groomer isn't in the set
                groomers.insert(g->GetFullName());
    }
    return groomers;
}```