Instructor.java

mail@pastecode.io avatar
unknown
java
2 years ago
3.4 kB
4
Indexable
Never
package manos.umdguru;

import org.json.JSONException;
import org.json.JSONObject;
import manos.umdguru.api.planetterp;

public class Instructor {
    private String name;
    private String slug;
    private String type;
    private double average_rating;

    /*
     * The fully qualified constructor for the instructor class
     * @param name the name of the instructor
     * @param slug the slug (unique identifier)
     * @param type the type of instructor
     * @param average_rating the average rating of the instructor
     */
    public Instructor(String name, String slug, String type, double average_rating) {
        this.name = name;
        this.slug = slug;
        this.type = type;
        this.average_rating = average_rating;
    }
    /*
     * The copy constructor for  the instructor class
     * @param o the instructor to copy
     */
    public Instructor(Instructor o){
        this.name = o.name;
        this.slug = o.slug;
        this.type = o.type;
        this.average_rating = o.average_rating;
    }
    /*
     * This constructor is used to get the instructor from the planetterp api
     * @param name the name of the instructor
     */
    public Instructor(String name){
        this(fromJson(planetterp.getProfessor(name, false)));
    }
    /*
     * This method converts a json object to an instructor object
     * @param json the json object to convert
     * @return the instructor object
     */
    public static Instructor fromJson(JSONObject json){
        double rating = 0;
        try{
            rating = json.getDouble("average_rating");
        }catch(JSONException e) {
            rating = 0;
        }
        Instructor instructor = new Instructor(
            json.getString("name"),
            json.getString("slug"),
            json.getString("type"),
            rating
        );
        return instructor;
    }
    /*
     * This method will return a string representation of the instructor object
     * @return the string representation of the instructor object
     */
    public String toString(){
        StringBuffer result = new StringBuffer();
        result.append("Name: " + name + " (" + slug + ") " + " (" + average_rating + ") " + "\n");
        return result.toString();
    }
    public static double getAverageRating(String name){
        return fromJson(planetterp.getProfessor(name, false)).getAverageRating();
    }
    /*
     * @return the name
     */
    public String getName() {
        return name;
    }
    /*
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    /*
     *  @return the slug (unique identifier)
     */
    public String getSlug() {
        return slug;
    }
    /*
     * @param slug the slug to set
     */
    public void setSlug(String slug) {
        this.slug = slug;
    }
    /*
     * @return the type
     */
    public String getType() {
        return type;
    }
    /*
     * @param type the type to set
     */
    public void setType(String type) {
        this.type = type;
    }
    /*
     * @return the average rating
     */
    public double getAverageRating() {
        return average_rating;
    }
    /*
     * @param average_rating the average rating to set
     */
    public void setAverage_rating(double average_rating) {
        this.average_rating = average_rating;
    }

    
}