(P) Education center

Goal: I will revise how to read STL containers, iterators and files. I will also practice using struct (see course material at 4.3 Data vs control). I will especially practice choosing the correct data structure among the different data structures of STL. (It is sufficient to choose among those STL containers that have been introduced in the course material.)

Instructions: Create a new project: student/07/education_center/.

Implement a program that reads the course collection data of different education centers from a file at startup, stores them in a suitable data structure and gives the user the opportunity to search the data structure in question.

Important

Before starting to write code, read carefully the whole assignment. Especially, note the points Implementing the program in parts (required commits) and Special requirements.

From the point Evaluation note especially that the program must first pass all automated test. Otherwise the work will not be evaluated by assistants, and no points will be given.

Note

This project can be done either in pairs or independently. If you do the project in pairs, you need to form a group. If you have not created a group yet or you wish to change your working partner, choose ”Form a group” from the menu on the left to register a new group. In addition, the submission box at the very end of the current page shows two choices: “Submit alone” / “Submit with…”. You should be careful when selecting the choice, because you cannot change it afterwards. Enter only the address of the Git repository owned by either of you (not both) in the submit box. The code files must contain (in comments) the personal data of both of you.

You can look for a group via Kooditorio’s Discord link (see Timetable & links).

Header comment and feedback language

In the same way as in the first project, the header comment is again required. This time we provide no ready-made header comment, but you should write it by yourself. You can use the first project’s assignment as an example (see 4.5 (P) Binairo).

The feedback language will be chosen in the submission box (at the very end of this page). By default, the feedback language is Finnish, but you can change it, if you want to have the feedback, given by an assistant, in English. We will use the language you have given in the submission box of the final submission.

If you are working in pairs, add personal data of both of you.

The data handled by the program

We would now like to take a look at the following information on course assortment and enrollments in different places:

Lempäälä
    Languages
        English             20
    Exercise
        Hiit                10
        Pilates             5
        Yoga                3
    Handcraft
        Hammering           full
    Music
        Karaoke             full
Pirkkala
    Handcraft
        Weawing             2
    Exercise
        Hiit                full
    Languages
        German              3
    Music
        Karaoke             1
    Cooking
        Christmas food      3
Vesilahti
    Dance
        Zumba               8
    Information technology
        Excel               full
    Handcraft
        Weawing             full
        Hammering           10
        Dressmaking         4
    Cooking
        Baking              full
    Music
        Guitar              3
    Exercise
        Fitness boxing      7
    Languages
        English             3
Nokia
    Exercise
        Hiit                1
    Dance
        Zumba               2

The locations in the example above may have the same courses, but the enrollments might vary, and different locations do not necessarily have the same courses.

The data you see above can be presented as a csv file with lines formatted like this:

<location>;<theme>;<course_name>;<number_of_enrollments>

where <number_of_enrollments> can be a positive integer or a literal string ”full”. A course becomes full, if there 50 enrollments in it.

You can assume that <location> consists only of one word. Instead, <theme and <course_name> may consist of several words.

So, as an input file, the example from above looks like this:

Lempaala;Languages;English;14
Pirkkala;Handcraft;Weawing;2
Pirkkala;Exercise;Hiit;full
Vesilahti;Dance;Zumba;8
Pirkkala;Languages;German;3
Vesilahti;Information technology;Excel;full
Lempaala;Exercise;Hiit;10
Vesilahti;Handcraft;Weawing;full
Lempaala;Handcraft;Hammering;full
Vesilahti;Cooking;Baking;full
Pirkkala;Music;Karaoke;1
Vesilahti;Handcraft;Hammering;10
Vesilahti;Handcraft;Dressmaking;4
Lempaala;Exercise;Pilates;5
Lempaala;Exercise;Yoga;3
Lempaala;Languages;English;20
Pirkkala;Cooking;Christmas food;3
Lempaala;Music;Karaoke;full
Vesilahti;Music;Guitar;3
Vesilahti;Exercise;Fitness boxing;7
Vesilahti;Languages;English;3
Nokia;Exercise;Hiit;1
Nokia;Dance;Zumba;2

There are four fields on each line, and none of the fields can be empty or filled with spaces only. The last field must be a string presenting a positive integer or the word ”full”.

Please note that the lines of the csv file do not have to be in a certain order. Also, the input file does not use Scandinavian letters.

If the input file has a course that is listed in one location more than once, the valid data should be the last one mentioned. For example, the above input file list the English course in Lempäälä twice: in the first line there are 14 enrollments and in the end part of the file there are 20 enrollments. From these, 20 enrollments remains since it was the latter one mentioned, not since it was the bigger one.

The program in action

When the program starts up, it reads the name of the input file from the user:

Input file: courses.input

If it cannot open the input file in a readable mode, the program will print the text:

Error: the input file cannot be opened

After that, the program terminates with no other prints with the return value EXIT_FAILURE.

If the input file can be opened but the file does not correspond to the definition, for example, the number of fields is not four or one field is missing a value, the program prints the error message Error: empty field to the user and terminates with the return value EXIT_FAILURE as follows:

Input file: courses-not-enough-fields.input
Error: empty field

The program analyzes the file lines it has read and stores the course information from different locations into a suitable data structure. The programmer can freely choose the data structure, as long as it follows all the additional conditions mentioned at ”Special requirements” below.

After the file has been read, the program starts its actual operation. Every time it expects input from the user, it prints > (> and a space) at the beginning of the line as follows:

>

After that prompt, the user can give the following commands:

  1. quit - The program terminates with the return value EXIT_SUCCESS without printing anything.

  2. locations - Prints all the known locations in alphabetical order, one below the other, for example:

    > locations
    Lempaala
    Nokia
    Pirkkala
    Vesilahti
    >
    
  3. themes_in_location <location> - Prints all themes in the given location in alphabetical order, one below the other, for example:

    > themes_in_location Vesilahti
    Cooking
    Dance
    Exercise
    Handcraft
    Information technology
    Languages
    Music
    >
    
  4. courses <location> <theme> - The command prints all the courses and the number of enrollments of them in the location and theme in question arranged alphabetically by the course name, for example:

    > courses Lempaala Exercise
    Hiit --- 10 enrollments
    Pilates --- 5 enrollments
    Yoga --- 3 enrollments
    >
    

    Or, if the course is full, the output is as follows:

    > courses Vesilahti "Information technology"
    Excel --- full
    >
    

    Above example shows also that a parameter consisting of several words must be enclosed by quote marks. Theme parameter is the only one possibly consisting of several words. (Also a course name can consist of several words, but none of the commands use it as a parameter.)

    Even if a theme consists of only one word, it can be enclosed by quote marks. For example:

    > courses Nokia "Dance"
    Zumba --- 2 enrollments
    >
    
  5. available - The command prints the locations, themes, and names of all those courses that are free to be enrolled in, i.e. that are not full. The output is a single alphabetically ordered list, where the list elements are firstly ordered by locations, secondly by theme, and thirdly by the course name.

    You will probably understand it best after this example:

    > available
    Lempaala : Exercise : Hiit
    Lempaala : Exercise : Pilates
    Lempaala : Exercise : Yoga
    Lempaala : Languages : English
    Nokia : Dance : Zumba
    Nokia : Exercise : Hiit
    Pirkkala : Cooking : Christmas food
    Pirkkala : Handcraft : Weawing
    Pirkkala : Languages : German
    Pirkkala : Music : Karaoke
    Vesilahti : Dance : Zumba
    Vesilahti : Exercise : Fitness boxing
    Vesilahti : Handcraft : Dressmaking
    Vesilahti : Handcraft : Hammering
    Vesilahti : Languages : English
    Vesilahti : Music : Guitar
    >
    
  6. courses_in_theme <theme> - The command prints all courses under the given theme in all locations. The courses are listed in alphabetical order as follows:

    > courses_in_theme Exercise
    Fitness boxing
    Hiit
    Pilates
    Yoga
    >
    
  7. favorite_theme The command prints the most popular theme. The number of enrollments in each theme are summed together, and this maximum number and the theme (or themes) with this amount of enrollments are printed. For example:

    > favorite_theme
    116 enrollments in themes
    --- Handcraft
    >
    

    The command assumes that if a course is full, it has 50 enrollments.

    Let’s look at an additional example on the case where there are the same maximum number of enrollments in several themes. If the content of the input file was:

    Lempaala;Languages;English;14
    Pirkkala;Handcraft;Weawing;2
    Vesilahti;Handcraft;Hammering;12
    Lempaala;Exercise;Pilates;5
    Lempaala;Exercise;Yoga;3
    

    the command would print:

    > favorite_theme
    14 enrollments in themes
    --- Handcraft
    --- Languages
    >
    

    If the input file is not empty, there is a theme that is the most popular one. In the case of an empty input file, the command prints:

    > favorite_theme
    No enrollments
    >
    

After completing the user’s command, the program prints the prompt once again. It will continue printing the prompt until the user gives the command quit.

Errors

If the user input has an error, the program prints the relevant error message, and after that, returns to waiting for user input. In other words, the execution of the program does not end if the user input contains errors.

After the user gives an unknown command, the program prints the following error message:

> something
Error: Unknown command: something
> some_command parameter
Error: Unknown command: some_command
>

If the user gives the command the wrong amount of parameters, the program prints the error message Error: error in command <command> as follows:

> courses Lempaala
Error: error in command courses
>

It is possible to give the command themes_in_location a location that cannot be found. Then, the program prints the following error message:

> themes_in_location some_location
Error: unknown location
>

It is possible to give the command courses_in_theme a theme that cannot be found. Then, the program prints the following error message:

> courses_in_theme some_theme
Error: unknown theme
>

The command courses has two separate error messages. If the program cannot find the location the user has provided, it prints the error message Error: unknown location name, and if it cannot find the given theme, it prints the error message Error: unknown theme. For example:

> courses some_location Exercise
Error: unknown location name
> courses Lempaala some_theme
Error: unknown theme
> courses some_location some_theme
Error: unknown location name

If both the location and the theme are unknown, the program informs only about an unknown location, as shown in last case above.

Special requirements

If you want your assignment to pass the evaluation, you must meet these requirements:

  • You can only read the input file once during the program execution.

  • Whichever STL data structure or their combination you choose to use in your assignment, you must use at least one map structure. That map must be part of the structure where you store the information read from the input file for later searches.

  • When you are storing the information about courses found in the source file into your STL structure, you must store them in the struct:

    struct Course {
        string name;
        string theme;
        int enrollments;
    };
    

    This struct must be set as the element type of that STL structure where you store the course information.

Tips

  • For splitting the file input and the user input, you can use the function split which you implemented in the weekly exercises. The implementation was given in the template of the exercise 6.1.2 Sum of vector elements.
  • Please remember the order in which to go through the elements in map and set with the iterator.

Implementing the program in stages

There are basically three parts in the implementation of the program:

  • choice of the data structure and reading the file into the data structure
  • user interface
  • search algorithms.

Consider carefully in which order you want to implement them so that it is possible for you to create and test them one by one before moving on to the next step. The minimum requirement for using the version control is these three parts having been executed as separate commits, but it would, of course, be best if you had more commits.

The commits listed above can divided into several parts.

Evaluation

To end up to assistents’ evaluation, your work must first pass automatic tests. Automatic tests do not accept a program producing warnings while compiling. If the automated tests give 0 points for your work, also your final points will be 0, and your work will not be evaluated by an assistant.

The assistant evaluates the submissions that have passed the automated testing (= 1 p) and fulfilled the special requirements of the assignment based on the last commit before the deadline, according to the following criteria:

  • The overall principle of the solution: 0-30 points:
    • The learning goals of the exercise have been achieved.
    • The program code has been split into logical, suitably long segments using functions, classes, and/or methods.
    • If the program uses classes and objects, these have been implemented according to the basics of object-oriented programming (see from round 4 the section About programming style at Object-oriented programming).
    • The data structure does not include repetitive data nor unnecessary parts. The chosen data structures have been used in a reasonable way.
    • The program code does not include unnecessary repetitions nor other unnecessary parts.
    • The program code does not include unnecessary limitations or assumptions or other forced solutions.
    • The implementations of the program’s structures are easy to understand.
    • Global variables have not been used in the program code (global constants are OK).
    • The program does not terminate with the exit function.
  • Programming style: 0-10 points:
    • Variables and functions are named clearly and appropriately. These items have named by using only one language, not e.g. Finnish and English mixed.
    • Named constants have been used instead of magic numbers.
    • The program code is neatly formatted.
    • The length of program code lines do not exceed 80 characters.
    • At the beginning of each file, there is a comment explaining the purpose of the file, the creator(s) of the project and other necessary information (see the assignment fo the first project on round 4).
    • At the beginning of each function/method (in the header file if such exists), there is a comment describing its working, return value and parameters.
    • There are comments in the code where necessary.
    • Comments are related to the current version of the program, not to an older one.
    • All comments have written in the same language (fi/en), but comments can be written in a language different from that used in naming variables and such.
    • All the variables have been initialized.
    • The compiler does not give warnings while compiling.
  • Using the version control: 0-10 points:
    • There are enough commits.
    • The content of commit messages is clear and relevant.

Note about evaluation

Automated tests check if your program works, but assistants pay attention to your coding style and other evaluation criteria listed above. Therefore, it could be possible that a fully working program brings you only zero points, if the above criteria are not met.

Requirements concerning especially good programming style can be found from the earlier material section on the current round.

Note about submission versions

The version to be evaluated is the one that corresponds to your latest successful Plussa submission (within the deadline). A successful submission means such a submission that passed automated tests.

After the first successful submission, you most probably want to improve your coding style (e.g. add comments), assuming that the deadline is not too close.

Anyway, remember finally this: After the final Git push, submit your work also in Plussa.

A+ presents the exercise submission form here.