Now that you have made Course and Schedule objects, you have the building blocks for creating a Connect Carolina of your own. In this final part of the problem set, you will import a CSV of all the classes at UNC as an array of Course objects. You will construct four functions to filter through this array of Courses to get only courses in specific departments, in a particular course number range, with specific geneds, and with selected keywords in their Course description. Basically, you are making the search feature of Connect Carolina. And whats better? It will take less that 10 seconds to load! WOW! Once you have made these filter functions, you will bring your Connect Carolina application together by asking your user to search for classes and add them to their schedule -- okay, yes, this may be more like Coursicle.
The objectives of this part of the problem set are to:
1. Practice writing functions that filter arrays by certain criteria
2. Bring many elements together to create an improved version of Connect Carolina (RIP)
As with in lecture, begin by opening up VSCode, ensuring your project is still open, and then running the following two commands in the VSCode Integrated Terminal:
In the same directory used in lecture:
Once again, we will use our friends copy and paste. Copy and paste all of your code from Jog into Sprint, and delete all of the code inside main.
2.0 Imports
You need to add a few imports beyond print in jog. Update your imports to look like the following.
import { print, promptString, promptNumber, csvToArray } from "introcs";
Part 3.1: Import a CSV of UNC classes as an array of Course objects
To import the CSV of all the classes at UNC (called course-data.csv in your ps07-connect-carolina folder), you will use the csvToArray function as used in lecture. As your first line in main, use this function to prompt the user for the CSV file containing the Course data, using your Course class as the second argument. Store the result of this call in a variable -- lets call it uncCourses -- to be filtered later -- remember csvToArray returns an array of objects of the type specified by the second parameter.
Part 3.2: Define and export the filterByDepartment function
Now that you have an array of Course objects, it is time to create your first filter function. This function should take an array of Courses and the (4 letter code) name of a department (i.e. "COMP", "HIST", etc.) and return an array of Courses whose department property equals that passed in department.
To do this, inside your filterByDepartment function you first need to account for the condition that the user does not want to filter by department. In this case, the department passed in will be an empty string. Therefore, in your code, if the department passed in is just an empty string, simply return the entire array of Courses unaltered.
Next, you should create an empty array to hold the filtered Courses. This is what you will return at the end of the function. Now, use a loop to add to this array of filtered courses every Course in the array of Courses parameter whose department property equals the department parameter. Finally, return your filtered Courses array.
TEST: Before moving on, test this filter function in main. In main, create a new Schedule and assign its owner property to the value of your name. Assign your Schedule's courses property to be the result of calling filterByDepartment passing in uncCourses and a department code of your choosing. Print your Schedule using the scheduleToString function from Jog. You should see Courses of only the department you passed in displayed on your screen. For example, if the department you filtered by was "COMP", you will see all the computer science classes from COMP 50 to COMP 692 printed.
Part 3.3 Define and export a filterByNumberLessThan function
Now that you have filtered by department, you can filter by Course number. This function should take in an array of Courses and a course number (i.e. 101, 203, etc) and return an array of Courses whose num property is less than that number. If the user does not want to filter by number the argument 0 will be passed in for the Course number parameter. Check for this case by asking if that course number is 0, and return the unmodified array of courses if so.
TEST: Before moving on, test this filter function in main. Reassign your Schedule's courses property to be the result of calling filterByNumberLessThan passing in uncCourses and a number of your choice. Print your Schedule using the scheduleToString function. You should see only the Courses with course numbers less than the number you picked displayed.
Part 3.4 Define and export a filterByGened function
Now it is time to filter by gen ed. This function should take in an array of Courses and a Gen Ed (i.e. "QR", "HS", etc.) and return an array of Courses whose geneds array contains that string. Just like in filterByDepartment, return the unmodified array of courses if the gen eds parameter is an empty string.
Inside your loop, to ask whether a Course's geneds' array contains the specified gen ed, you should use the indexOf function, which returns the index of a gened if it exists or -1 if it doesn't exist. Here is an example of how to use it:
aCourse.geneds.indexOf(genedParam) > -1
This entire expression evaluates to true if it exists and false if it doesn't.
TEST: Before moving on, test this filter function in main. Reassign your Schedule's courses property to be the result of calling filterByGened passing in uncCourses and a gen ed of your choice. Print your Schedule using the scheduleToString function. You should see only the Courses with the specified gen ed. Note, the courseToString function does not display the gen eds, so you can double check your results by checking the actual CSV.
Part 3.5: Define and export the filterByKeyword function
This function should take an array of Courses and a single keyword (i.e. "computer", "medieval", etc) and return an array of Courses whose description property contains that string. Just like in filterByDepartment, return the unmodified array of courses if the keyword parameter is an empty string.
Hint: Remember the includes method on a string!
let aString = "hello world"; print(aString.includes("ello")); // prints true print(aString.includes("bye")); // prints false
TEST: Before moving on, test this filter function in main. Reassign your Schedule's courses property to be the result of calling filterByKeyword passing in uncCourses and a keyword of your choice. Print your Schedule using the scheduleToString function. You should see only the Courses with the specified keyword in their description.
Part 3.6: Creating the User Interface
Now that we have these filter functions, you need to help the user use them. First comment out your tests except for where you created a schedule and gave its owner property a value. You should have about three lines of code in main: one for csvToArray, another for Schedule declaration, and a third for assignment of owner property.
3.6a) Prompt the user for the number of classes they would like to take and store the result. This will be the size of the final courses array in their Schedule.
3.6b) Create a loop that will run so long as the length of the courses array of the Schedule is less than the number of classes the user indicated they want to take.
3.6c) Inside the loop
Each time the loop runs, the user will be asked to add another Course to their Schedule.
1. First, you should tell the user how many classes they currently have in their Schedule. Print this value with a string such as "You have this many courses: " concatenated before it.
2. Next, create initial values for the filter parameters. Create four variables called dept, courseNum, ge, and keyword, initializing them all to be empty strings except for courseNum, which should be initialized to 0. These are the values that will be passed to the filter functions in case the user elects to not filter by that variable, which is why we put initial check in each filter function.
3. More prompting! For each filter variable (department, course number, ge ned, keyword), ask if the user would like to filter by that variable. If the response is "Y", then prompt for the value they would like to filter by and update the value of the variable (dept, courseNum, ge, keyword) to be this value. (If it is "N", you do not have to do anything because the variable' values will stay at their initialize values that insinuate "I don't want to filter").
4. Use the filter function written in parts 3.2-3.5 to filter the Courses in the following order: first by department, then by number less than X, then by gen ed, and finally by keyword. When you filter by department, you will filter the original uncCourses array; however, when you then call filterByNumberLessThan, you should pass in the result of filtering by department on uncCourses. The same goes for filterByGened and filterByKeyword. Each time, the array will be getting smaller (that is if the user chose to filter by each category).
5. Now, you should print the result of the final filter (filterByKeyword) to the user, so they can see their class options. You should be using a loop and the courseToString function to do this.
6. Prompt the user to see if they wish to add one of the displayed courses to their Schedule.
If they replied "Y", ask them what index the course they want to add was (i.e. the first course displayed at the top of the screen is index 0, the second is 1,...). Then, add this Course to their schedule by storing the result of the addCourseToSchedule (passing in the Course at that index and their Schedule) as their Schedule. You are reassigning their Schedule's value.
Else (if they did not reply "Y"), do nothing because the loop will just loop again.
3.6d) Print the schedule!
When the loop is finished, their Schedule should be full. Print "Here is your schedule:" and then, in another print statement, print the Schedule itself using the scheduleToString function.
Congratulations! You have created a Schedule generator that is faster than Connect Carolina! Feel free to add more filters of your own making to personalize your assignment after you have published and received full credit. How good can you make Connect Carolina?
Part 3.7: Publish your app.
With your program running in your web browser, find the green "Publish App" button. If you are not still logged into introcs.com, you will be prompted to log in, and then your app will be bundled and published to the web!
Part 3.8: Submit for grading
Once the grader is available, to submit for grading, first publish your project as per Part 3.7. Then, go to My101 in the top-right right corner of this page and select "Submit" next to the assignment. From here you should see a button that allows you to submit for grading. Your work will be graded through an automated process. If you receive points off, please select the "Report" link to see which tests did not pass. You can resubmit as many times as you'd like without penalty up until the deadline. We want you to keep working toward full credit on problem sets!