Challenge 60

Description

The purpose of this challenge is to implement basic polymorphism.

 Requirements

  1. Define a struct as below
    struct CountyInfo
    {
      string name;
      double infections;
    };
  2. Define a class CoronaData as below:
    class CoronaData
    {
        protected:
            CountyInfo data[5];
        public:
            CoronaData()
            {
                // hard-coded data. Could load from any source.
                data[0].name = "Los Angeles";
                data[0].infections = 255000;
                data[1].name = "Riverside";
                data[1].infections = 55766;
                data[2].name = "Orange";
                data[2].infections = 52166;
                data[3].name = "San Bernardino";
                data[3].infections = 50709;
                data[4].name = "San Diego";
                data[4].infections = 42950;
            }
    };
    
  3. In class CoronaData, add a pure virtual function void save_to_file(string filename);
  4. Create a new class, CoronaDataJson that inherits from CoronaData
  5. In class CoronaDataJson, create an overriding function for save_to_file(). This function will basically create a JSON file that should look as below. Don’t hard code the data when creating the file, use C++ code to generate the file from the inherited data  property. When the JSON file is created, it should look as below: 
    {
      "data" : [
        { "name" : "Los Angeles", "infections" : "255000.0000" },
        { "name" : "Riverside", "infections" : "55766.0000" },
        { "name" : "Orange", "infections" : "52166.0000" },
        { "name" : "San Bernardino", "infections" : "50709.0000" },
        { "name" : "San Diego", "infections" : "42950.0000" }
      ]
    }
  6. Create a new class, CoronaDataCsv that inherits from CoronaData
  7. In class CoronaDataCsv, create an overriding function for save_to_file(). This function will basically create a CSV file that should look as below. Don’t hard code the data when creating the file, use C++ code to generate the file from the inherited data  property. When the CSV file is created, it should look as below: 
    County, Infections
    "Los Angeles", 255000.0000
    "Riverside", 55766.0000
    "Orange", 52166.0000
    "San Bernardino", 50709.0000
    "San Diego", 42950.0000
  8. Write a global function as below
    void save(CoronaData *cd, string filename)
    {
      cd->save_to_file(filename);
      cout << "File is ready." << endl;
    }
  9. In main(), declare a pointer, CoronaData * cd
  10. Ask the user to choose an output format (JSON or CSV), and based on the user’s selection, you will instantiate the appropriate object that will be assigned to cd pointer.  JSON files typically end in .json, while CSV files end in .csv.  See sample main below.
  11. You can use the cat command to examine the file created by your program that should be called either corona.json or corona.csv depending on the selected output.

Partial main()

int main()
{
  CoronaData * cd;
  // code to prompt the user for format type

  // User chose JSON format
  if (input == 'j')
  {
    cd = new CoronaDataJson;
    save(cd, "corona.json");
  }
  // do something similar for CSV format

  // don't forget pointer cleanup
  return 0;
}

Sample Interaction

Choose output format (j = JSON, c = CSV): j

File is ready. 

LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT

CATALOG ID: CPP-CHAL0060

Print Requirements