import java.util.Scanner;

/**
 * Class that represents an Invoice and provides methods for accessing invoice entries.
 *   @author Dave Reed
 *   @version 1/8/15
 */
public class Invoice implements Comparable<Invoice> {
  private String customerID;
  private String date;
  private int units;
  private double pricePerUnit;
  private String salesPerson;
  
  /**
   * Constructs an invoice with the provided entries.
   *   @param date the date of the invoice
   *   @param customerID the customer ID
   *   @param units the number of units purchased
   *   @param pricePerUnit the price per unit (in dollars)
   *   @param salesPerson the salesperson ID
   */
  public Invoice(String date, String customerID, int units,
		         double pricePerUnit, String salesPerson) {
	  this.date = date;
	  this.customerID = customerID;
	  this.units = units;
	  this.pricePerUnit = pricePerUnit;
	  this.salesPerson = salesPerson;
  }
  
  /**
   * Constructs an invoice, reading entries from the specified input stream.
   *   @param input the input stream for the invoice entries
   */
  public Invoice(Scanner input) {
	  this(input.next(), input.next(), input.nextInt(),
		   input.nextDouble(), input.next());
  }
  
  /**
   * Accessor method for customer ID.
   *   @return the customer ID
   */
  public String getCustomerID() {
	  return this.customerID;
  }
  
  /**
   * Accessor method for invoice date.
   *   @return the date (in the form MM-DD-YYYY)
   */
  public String getDate() {
	  return this.date;
  }
  
  /**
   * Accessor methods for the number of units sold.
   *   @return the number of units
   */
  public int getUnits() {
	  return this.units;
  }
  
  /**
   * Accessor method for the unit price.
   *   @return the price per unit (in dollars)
   */
  public double getPricePerUnit() {
	  return this.pricePerUnit;
  }
  
  /**
   * Accessor method for salesperson ID.
   *   @return the salesperson ID
   */
  public String getSalesPerson() {
	  return this.salesPerson;
  }
  
  /**
   * Converts the invoice into a single String (e.g., for printing).
   *   @return the String representation of the invoice
   */
  public String toString() {
	  return this.getDate() + " " + this.getCustomerID() + " " + 
             this.getUnits() + " " + this.getPricePerUnit() + " " +
			 this.getSalesPerson();
  }
  
  /**
   * Compares the current invoice with the another invoice, based on date.
   *   @return negative # if date of this invoice comes before other's date;
   *           positive # if this date comes after; otherwise, 0
   */
  public int compareTo(Invoice other) {
	  String[] thisParts = this.getDate().split("-");
	  String[] otherParts = other.getDate().split("-");
	  if (!thisParts[2].equals(otherParts[2])) {
		  return Integer.parseInt(thisParts[2]) - Integer.parseInt(otherParts[2]);
	  }
	  else if (!thisParts[0].equals(otherParts[0])) {
		  return Integer.parseInt(thisParts[0]) - Integer.parseInt(otherParts[0]);
	  }
	  else if (!thisParts[1].equals(otherParts[1])) {
		  return Integer.parseInt(thisParts[1]) - Integer.parseInt(otherParts[1]);
	  }
	  else {
		  return (this.getCustomerID()+" "+this.getSalesPerson()).compareTo(other.getCustomerID()+" "+other.getSalesPerson());
	  }
  }
}
