/**
 * NOTE: It is not clear to me if the author of the original
 * labtest was looking for a solution that used aggregation or
 * composition. The solution here is for composition, but a
 * solution that used aggregation would be acceptable for this
 * term's labtest.---Burton
 */

import java.util.Random;

/**
 * Creates a random computer. The size of the monitor is randomly chosen
 * and is either 17, 19 or 21. The processor is also randomly chosen and
 * is either Intel or AMD.
 * 
 * @author CSE1030Z
 *
 */
public class Computer {
	public static final String AMD = "AMD";
	public static final String INTEL = "Intel";
	
	private String processor;
	private Monitor monitor;
	
	/**
	 * Creates a computer from a given processor name and monitor.
	 * 
	 * @param processor the processor name
	 * @param monitor the monitor
	 * @pre. processor is "AMD" or "Intel"
	 * 
	 */
	private Computer(String processor, Monitor monitor)
	{
		this.setProcessor(processor);
		this.setMonitor(monitor);
	}
	
	/**
	 * Creates a random computer. The size of the monitor is randomly chosen
	 * and is either 17, 19 or 21. The processor is also randomly chosen and
	 * is either Intel or AMD. 
	 * 
	 * @return a random computer.
	 * 
	 */
	public static Computer getRandom() 
	{
		String processor = null;
		Random gen = new Random();
		if(gen.nextBoolean()) 
		{
			processor = Computer.AMD;
		}
		else 
		{
			processor = Computer.INTEL;
		}
		return new Computer(processor, Monitor.getRandom());
	}
	
	/**
	 * Returns the processor of this computer. 
	 * 
	 * @return the processor of this computer.
	 */
	public String getProcessor() 
	{
		return processor;
	}
	
	/**
	 * Sets the processor of this computer to the given processor.
	 * The processor is only set if the given processor is either Intel or AMD.
	 * 
	 * @param processor the new processor of this computer. 
	 * @return true if processor is either Intel or AMD, false otherwise.
	 */
	public boolean setProcessor(String processor) 
	{
	    boolean ok = false;
		if (processor.equals(Computer.AMD) || processor.equals(Computer.INTEL))
		{
		    this.processor = processor;
		    ok = true;
		}
		return ok;
	}
	
	/**
	 * Returns the monitor of this computer. 
	 * 
	 * @return the monitor of this computer.
	 */
	public Monitor getMonitor() 
	{
		return new Monitor(this.getMonitor().getSize());
	}
	
	/**
	 * Sets the monitor of this computer to the given monitor.
	 * 
	 * @param monitor the new monitor of this computer.
	 * @pre. monitor != null
	 * 
	 */
	public void setMonitor(Monitor monitor) 
	{
		this.monitor = new Monitor(monitor.getSize());
	}

	/**
	 * Tests if the given object is the same as this computer. Two computers
	 * are the same if they have the same monitor and processor.
	 * 
	 * @param obj an object. 
	 * @return true if the given object is the same as this computer, false otherwise.
	 */
	@Override
	public boolean equals(Object obj) 
	{
		boolean eq = false;
		if(this == obj)
		{
			eq = true;
		}
		else if(obj != null && this.getClass() == obj.getClass())
		{
			Computer other = (Computer) obj;
			eq = this.getMonitor().equals(other.getMonitor()) &&
			     this.getProcessor().equals(other.getProcessor());
		}
		return eq;
	}

	/**
	 * Returns a string representation of this computer. This
	 * representation consists of "Computer (monitor [x inches], processor y)" 
	 * where x is the size of the monitor of this computer and y is the processor 
	 * of this computer. For example, the string "Computer (monitor [17 inches], 
	 * processor Intel)" represents a computer with a 17 inch screen and an 
	 * Intel processor.
	 * 
	 * @return a string representation of this computer.
	 */
	@Override
	public String toString() 
	{
		return "Computer (" + this.monitor.toString() + 
			", processor " + this.processor + ")";
	}
	
	
}
