import java.io.*;
import java.net.*;



/**
 * A simple stub for a remote database of Course objects
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class CourseDatabaseStub implements CourseDatabase
{
    private ObjectOutputStream    out;
    private ObjectInputStream     in;
    private Socket                s;


    /**
     * Constructor
     */
    public CourseDatabaseStub(String host, int port)
    {
       try 
       {
          s = new Socket(host, port);
	    
          out = new ObjectOutputStream(s.getOutputStream());
          in  = new ObjectInputStream(s.getInputStream());
       } 
       catch (IOException ioe) 
       {
          ioe.printStackTrace();
       }
    }

    /**
     * Add a Course
     *
     * @param course   The Course to add
     */
    public void add(Course course)
    {
       try 
       {
          out.writeObject("add");
          out.writeObject(course);
          out.flush();
       } 
       catch (IOException ioe) 
       {
          ioe.printStackTrace();
       }
    }

    /**
     * Get a Course
     *
     * @param course   The Course to add
     */
    public Course get(String department, int number)
    {
       Course       course;

       course = null;

       try 
       {
          out.writeObject("get");
          out.writeObject(department);
          out.writeInt(number);
          out.flush();
	    
          course = (Course)in.readObject();
       } 
       catch (IOException ioe) 
       {
          ioe.printStackTrace();
       } 
       catch (ClassNotFoundException cnfe) 
       {
          cnfe.printStackTrace();
       }
	
       return course;
    }

    /**
     * Remove a Course
     *
     * @param department   The department code
     * @param number       The course number
     */
    public void remove(String department, int number)
    {
       try 
       {
          out.writeObject("remove");
          out.writeObject(department);
          out.writeInt(number);
          out.flush();
       } 
       catch (IOException ioe) 
       {
          ioe.printStackTrace();
       }
    }

}
