CourseUtilities.java

  1. package edu.ucsb.cs156.courses.utilities;

  2. /** static utility methods for dealing with courses */
  3. public class CourseUtilities {

  4.   // Utility class; this allows jacoco to be satisified that constructors are covered.
  5.   private CourseUtilities() {}

  6.   /**
  7.    * Given a subject area and course number, return a course id that is formatted similarly to the
  8.    * precise way that course numbers are formatted in UCSB's GOLD system.
  9.    *
  10.    * <p>That format has the subject area left justified in an 8 character field, followed by the
  11.    * course number right justified in a 3 character field, followed by the suffix (if any) left
  12.    * justified in a 2 character field.
  13.    *
  14.    * <p>However, we use this function to query rtora's CSV files with grade data, at
  15.    * https://github.com/rtora/UCSB_Grades/ in which this course id is trimmed, such that there are
  16.    * never ending spaces.
  17.    *
  18.    * <p>Therefore, we intentionally also trim the result course id in this function to properly
  19.    * query the CSV files. For example, CMPSC 130A would typically be written:
  20.    *
  21.    * <p>"CMPSC 130A "
  22.    *
  23.    * <p>but we return "CMPSC 130A" to query the CSV file.
  24.    *
  25.    * @param subjectArea subject area, such as CMPSC
  26.    * @param courseNumber course number, such as 130A
  27.    * @return formatted course number
  28.    */
  29.   public static String makeFormattedCourseId(String subjectArea, String courseNumber) {
  30.     String[] nums = courseNumber.split("[a-zA-Z]+");
  31.     String[] suffs = courseNumber.split("[0-9]+");
  32.     String result = "";
  33.     if (suffs.length < 2) { // no suffix
  34.       result =
  35.           String.format("%-8s", subjectArea) // 'CMPSC '
  36.               + String.format("%3s", nums[0]) // ' 8'
  37.       ;
  38.     } else {
  39.       result =
  40.           String.format("%-8s", subjectArea) // 'CMPSC '
  41.               + String.format("%3s", nums[0]) // ' 8'
  42.               + String.format("%-2s", suffs[1]) // 'A '
  43.       ;
  44.     }
  45.     return result.trim();
  46.   }
  47. }