/** Student.java
    a simple class representing a Student at a university
    basic constructors, accessors, and mutators and toString()
    a demo class
    @author cosc 2325 class
    @version 1.0
*/

import java.util.*;
import java.text.DecimalFormat;

public class Student implements Comparable
{
  private int id_;
  private String name_;
  private double gpa_;


  /**
    creates Student with default values of 0, 0.0, XXX in
    id_, gpa and name fields
  */
  public Student() {
    this.setId(0);
    this.setGpa(0.0);
    this.setName("XXX");
  }

/** returns the name of this student
    @return name of student
*/
  public String getName() {
    return this.name_; 
  }
/** returns the id_ of this student
    @return id_ of student
*/
  public int getId() {
    return this.id_;
  }
/** returns the gpa of this student
    @return gpa of student
*/
  public double getGpa() {
    return this.gpa_;
  }
  /** sets name to given argument
   @param name value to set this name to
  */
  public void setName(String name) {
    this.name_ = name.toUpperCase().trim();
  }
  /** sets id_ to given argument
      does not modify this id if id is invalid
   @param theId_ value to set id_ to
  */  public void setId(int id) {
    if (id >= 0 && id <= 9999)
      this.id_ = id;
      }
  /** sets GPA to given argument, sets it
      to 0.0 if the argument given is less than 0.
   @param gpa value to set this gpa to
  */
  public void setGpa(double gpa) {
    if (gpa >= 0.0)
      this.gpa_= gpa;
    else
      this.gpa_=0.0;
  }
  /** creates a readable formatted string of all fields of this Student
    @return - readable text string
  */
  public String toString() {
    DecimalFormat fmt = new DecimalFormat("##.0");
    String temp = "Id : " + this.getId()+
      " Name: " + this.getName() +
      " GPA: " + fmt.format(this.getGpa());
    return temp;
  }

  /** attempts to read the next Student from the given
     Scanner object, returns true on success false if 
     not able to read correctly or wrong format.
     Assumes format of input is 1 piece of data per line,
     student id_ is on first line, student name on 2nd line,
     and student gpa is on third line of input.
    @param  in the object to read from (created and all ready to be read)
    @return  true on success, false on failure to read
  */

  public boolean readFromScanner(Scanner in) 
  {
      int theId;
      double theGpa;
      String theName;
      String temp;
      // read the first line of input from the Scanner
      try {
      temp = in.nextLine().trim();
      }catch(NoSuchElementException e) {
          System.out.println("Found end of data");
	  return false; // no more data
      }
      if(temp.equals("")) return false; // empty line
     try {
       theId = Integer.parseInt(temp);
       this.setId(theId);
     } catch(NumberFormatException e) {
       System.out.println("Error in attempting to read student id_ number");
       return false;
     }
     // now try to read the name
     try {
     temp = in.nextLine().trim();
     }catch(NoSuchElementException e) {
	 return false;
     }
     if(temp.equals("")) return false;
     this.setName(temp);
     // now try to read the gpa as a string
     try {
       temp = in.nextLine();
     }catch (NoSuchElementException e) {
	 return false;
     }
      
     if(temp.equals("")) return false;
     try {
       theGpa=Double.parseDouble(temp);
       this.setGpa(theGpa);
     } catch(NumberFormatException e) {
       System.out.println("Error in attempting to read student gpa");
       return false;
     }
     return true; // success if we get this far
   }

  /** the expected method for the Comparable interface
   * @param - o  the object we are comparing this Student to, should be a Student 
   * @return the result of comparing this Student id_ to the other
   *         Student's id_
   */

  public int compareTo(Object object) {
    Student s1 = (Student) object;
    return (this.id_ - s1.getId());
  }

}
