Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1.Your user interface must allow the user to enter the month, day, and year. You

ID: 666436 • Letter: 1

Question

1.Your user interface must allow the user to enter the month, day, and year. You can do this using text fields for input, or you can use ComboBoxes if you feel adventurous.

2.The only GUI components which create events that your program needs to handle are the save and retrieve buttons.

3.Dont go overboard making your GUI beautiful! We are just looking for basic functionality here!

4.You must have a separate class which manages the calendar data. You will have a minimum of three classes in your application, a user interface class, the calendar manager class, and a calendar test class. The user interface class creates an instance of the calendar manager in its constructor and stores it in a member variable.

5.The calendar manager must provide methods which support saving a specific calendar entry and retrieving a specific calendar entry. The interfaces must be defined to only pass a single days calendar entry across the interface.

6.The calendar manager must store calendar data into files according to month+year. That is, the calendar entries for December 2011 must all be stored in the same file.

7.The calendar manager must use ObjectInput/OutputStreams to read/write data from/to files. The calendar manager will use an array to store String objects. The position of a String in this array corresponds to the calendar entry for a specific day.

8.The save method of the calendar manager will need to determine if a file exists for the requested month and year. If so, the object from that file must be read into the calendar manager. Otherwise, the calendar manager must create an empty String array. The new entry must be saved to the appropriate days location in the array. The modified array must be saved to the appropriate file.

9.The retrieve method of the calendar manager will need to determine if a file exists for the requested month and year. If not, return an error string indicating that there is no such entry. If the file exists, read the String array from the file and locate the requested days entry. If this entry is null, return an error string indicating that there is no such entry, otherwise return the entry.

Explanation / Answer

CalendarClient.java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

public class CalendarClient extends UnicastRemoteObject implements Serializable, CalendarClientInterface {


protected CalendarClient() throws RemoteException {
  super();
}
  private static CalendarClient _instance;
private static CalendarManagerInterface CalendarManager = null;
private CalendarObjInterface CalendarObj = null;
private static String currentUser = null;
private static Registry registry=null;
private static String initialHost = null;

public void EventAlert(Event e) throws RemoteException{
  System.out.println("Alert from server: you have an upcoming schedule event!!");
  System.out.println(e);
}

public String getUser()
{
  return this.currentUser;
}

public void createCalendar(String argv[]) throws RemoteException, NotBoundException {
  System.out.println("Find right server..");
  findRightServer(argv[1]);
  System.out.println("Create calendar : " + CalendarManager.createCalendar(argv[1]));
}


public void findRightServer(String user) throws RemoteException, NotBoundException
{
  try {
   System.out.println("Old server id is: " + CalendarManager.getServerId());
   System.out.println("Find the right server...");
   CalendarManagerInterface cmi = CalendarManager.assignRightServer(user);
   String host = cmi.getServerIP();
   this.registry = LocateRegistry.getRegistry(host, config.port);
   this.CalendarManager = (CalendarManagerInterface) (registry.lookup("CalendarServerAlias"));
   System.out.println("server id is: " + CalendarManager.getServerId());
   CalendarObj = CalendarManager.ConnectCalendar(this.currentUser);
  } catch (RemoteException e) {
   // TODO Auto-generated catch block
   //e.printStackTrace();
   Log.log("current server is down, back to initial server", Log.demo);
    ReconnectServer();
  } catch (NotBoundException e) {
   // TODO Auto-generated catch block
   //e.printStackTrace();
   Log.log("current server is down, back to initial server", Log.demo);
    ReconnectServer();
  }
    
}

public void ReconnectServer()
{
  try {
   this.registry = LocateRegistry.getRegistry(this.initialHost, config.port);
   this.CalendarManager = (CalendarManagerInterface) (registry.lookup("CalendarServerAlias"));
   if(CalendarManager!=null)
   Log.log("current server is" + this.CalendarManager.getServerId() , Log.demo);
   else
   Log.log("recoonect!" , Log.demo);
    
  } catch (RemoteException e) {
   // TODO Auto-generated catch block
   //e.printStackTrace();
   Log.log("Reconnect server...", Log.demo);
  } catch (NotBoundException e) {
   // TODO Auto-generated catch block
   //e.printStackTrace();
  }  
}

public void connectCalendar(String argv[]) throws RemoteException, NotBoundException {
  if(argv.length < 2) {
   System.out.println("Usage: "connect: <user>" ");
   return;
  }
  
  String user = argv[1].trim();
  System.out.print("Connecting calender for " + user + "...");
  
  findRightServer(user);  
  
  CalendarObj = CalendarManager.ConnectCalendar(user);
  System.out.println("Before fail...Current CalendarManager is "+ CalendarManager.getServerId());
  if(CalendarObj == null) {
   currentUser = null;
   System.out.println(" failed!");
  }
  else {
   //update current user
   currentUser = user;
   System.out.println(" connected!");   
   CalendarManager.registerClient(this, user);
  }
}

/*
* check whether CalendarObj is null or not
*/
public boolean checkCalendarObj() {
  if(CalendarObj == null) {
   System.out.println("CalendarObj not connected, to connect, please connect using "connect: <user>"!");
   return false;
  }
  return true;
}

public void schedule(String line) throws RemoteException, ParseException {
  if(!checkCalendarObj()) return;
  
  Event e;
  if(line.split(";").length==5){
   String[] params = Util.getUserInput(line,5);
   if(params!=null){
    e = Util.parseEvent(params[0],params[1],params[2],params[3],params[4]);
    System.out.println("Schedule... "+CalendarObj.ScheduleEvent(params[4], e));   
   }
   }
  else
  {
   String[] params = Util.getUserInput(line,4);
   if(params!=null){
    e = Util.parseEvent(params[0],params[1],params[2],params[3],currentUser);
    System.out.println("Schedule... "+CalendarObj.ScheduleEvent(currentUser, e));
   }
  }        
}

public void update(String line) throws RemoteException, ParseException {
  if(!checkCalendarObj()) return;
  
  Event e;
  if(line.split(";").length==6){
   String[] params = Util.getUserInput(line,6);
   if(params!=null){
    e = Util.parseEvent(params[1],params[2],params[3],params[4],params[5]);
    if(e==null) return;
    System.out.println("Update... "+CalendarObj.UpdateEvent(params[0], e));
   }
  }
  else
  {
   String[] params = Util.getUserInput(line,5);
   if(params!=null){
    e = Util.parseEvent(params[1],params[2],params[3],params[4],currentUser);
    if(e==null) return;
    System.out.println("Update... "+CalendarObj.UpdateEvent(params[0], e));  
   }
   }        
}
  private void deleteEvent(String line) throws RemoteException {
  // TODO Auto-generated method stub
  String[] params = Util.getUserInput(line,1);
  if(params!=null){
  System.out.println("user input eventId "+params[0]);
  System.out.println("delete... "+CalendarObj.RemoveEvent(params[0]));
  }
}

public void retrieve(String line) throws RemoteException, ParseException {
  
  if(!checkCalendarObj()) return;
  
  String user = null;
  Date startDate = null;
  Date endDate = null;
  SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
  
  if(line.split(";").length==2)
  {
   String[] params = Util.getUserInput(line,2);
   if(params!=null){
   user = this.currentUser;
   startDate = sdf.parse(params[0]);
   endDate = sdf.parse(params[1]);
   }
  }
  else{
   
   String[] params = Util.getUserInput(line,3);
   if(params!=null){
   user = params[0].trim();      
   startDate = sdf.parse(params[1]);
   endDate = sdf.parse(params[2]);
   }
  }
  
  
  if(user!=null)
  {      
   ArrayList<Event> rtn = CalendarObj.RetrieveEvent(user, startDate, endDate);
   if(rtn==null) {
    System.out.println("return event is null");
   }else{
   System.out.println(rtn.toString());
   }
  }
}
  public void list() throws RemoteException, NotBoundException {
  
  try {
   System.out.println("List users who have calendar obj: "+ CalendarManager.list());
  } catch (RemoteException e) {
   // TODO Auto-generated catch block
   //e.printStackTrace();
   ReconnectServer();
   findRightServer(this.currentUser);
   list();
  }
  
}
  public void eventList(String user) throws RemoteException, NotBoundException {
  
  try{
  if(!checkCalendarObj()) return;
  System.out.println( this.currentUser + "'s eventList: "
  + CalendarObj.myEventList(user).toString());
  }catch (RemoteException e) {
   // TODO Auto-generated catch block
   //e.printStackTrace();
   ReconnectServer();
   findRightServer(this.currentUser);
   eventList(user);   
  }
  
}

  public static void main(String argv[]) throws RemoteException{
  
  //Parse the input to get the hostname(the address of server)  

  
  String host = "";  
  if(argv.length==1){
   host = argv[0];
   initialHost = host;
  }else{
   System.out.println("Usage: RMIClient");
   System.exit(1);
  }
  
  System.out.println("Current CalendarManager connect to is: " + host);
  //install a security manager
  System.setSecurityManager(new RMISecurityManager());
  
  //method 1
  //request a reference to the server object
  //comemnt by Huangxin
  //String name = "rmi://"+host+"/RMIServer";
  //System.out.println("Looking up: "+name);
  //String name = "rmi://"+host;
  
  //method 2
  registry = LocateRegistry.getRegistry(host, config.port);
  
  
  try{
   
    CalendarManager = (CalendarManagerInterface) (registry.lookup("CalendarServerAlias"));
   
   System.out.println("CalendarManager connected!");
   
   _instance = new CalendarClient();
   _instance.mainLoop();
   
  }catch(Exception e){
   System.out.println("Exception "+e);
   System.exit(1);
  }
  
  
  
}

public void mainLoop() {
  //Usage
  Util.usageStatement();
  
  Scanner scanner = new Scanner(System.in);
   try{
   while(scanner.hasNextLine())
   {
    String line=scanner.nextLine();
    if(processCommand(line)) break;
   }
  }catch(Exception e){
   System.out.println("Exception "+e);
   System.exit(1);
  }
}
  
  private boolean processCommand(String line) throws ParseException, NotBoundException, RemoteException
{
  
   String[] args = line.split(": ");
   String cmd = args[0];
   if(cmd.equals("exit")) return true;
   else if(cmd.equals("create")) {
    this.createCalendar(args);
    
   }
   else if(cmd.equals("schedule")) {
    this.schedule(line);
   }
   else if(cmd.equals("retrieve")) {
    this.retrieve(line);
   }
   else if(cmd.equals("connect")) {
    this.connectCalendar(args);   
   }
   else if(cmd.equals("list")) {
    this.list();
   }
   else if(cmd.equals("myEventList")) {
    this.eventList(this.currentUser);
   }
   else if(cmd.equals("delete")) {
    this.deleteEvent(line);
   }
   else if(cmd.equals("update")) {
    this.update(line);
   }
   else if(cmd.equals("test")) {
    this.test(args[1]);
   }
   else if(cmd.equals("testNotify"))
   {
    this.testNotify(30);
   }
//  else if(cmd.equals("deleteDatabase")) {
//   this.deleteDatabase();
//  }
   else if(cmd.equals("help")){
    Util.usageStatement();
   }else
   {
    System.out.println("Please type "help: " to see the usage");
   }
   
   System.out.println("------------------------------------");

   return false;
}

private void test(String filename) throws RemoteException, ParseException, NotBoundException {
  // TODO Auto-generated method stub
  Scanner s;
  try {
   s = new Scanner(new File(filename));
   System.out.println("Read test script from file: "+filename);
   while(s.hasNextLine())
   {
    String line=s.nextLine();
    System.out.println("USER INPUT COMMAND "" +line +""");
    Thread.sleep(50);
    if(processCommand(line)) break;
   }
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
}
private void testNotify(int timeLeft) throws ParseException, RemoteException
{
  System.out.println("Test Notify function: event will happen "+timeLeft+" sec later for Bob,Sandy");
  SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
  Calendar cal = Calendar.getInstance();

  cal.add(cal.SECOND,timeLeft);
  String startTime = sdf.format(cal.getTime());
  cal.add(cal.SECOND,timeLeft);
  String endTime = sdf.format(cal.getTime());
  
  String line = "schedule: "+startTime+";"+endTime+";"+"testnotifyEvent"+";group"+";Bob,Sandy";
  schedule(line);
}

}

CalendarManagerInterface.java

import java.net.MalformedURLException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

public interface CalendarManagerInterface extends Remote{

  String createCalendar(String user) throws RemoteException;

  String list() throws RemoteException;

  CalendarObjInterface ConnectCalendar(String user) throws RemoteException;

    void registerClient(CalendarClientInterface cci, String name) throws RemoteException;
  

    boolean hasCalendarObj(String username)throws RemoteException;
   
public String getPredecessorIP() throws RemoteException;


public int getPredecessorId() throws RemoteException;

public void setSuccessor(String succ) throws RemoteException;

public void setPredecessor(String pre) throws RemoteException;

public String getSuccessorIP() throws RemoteException;


public int getSuccessorId() throws RemoteException;


////////SOS and POP
public String getSOSip()throws RemoteException;


public int getSOSid()throws RemoteException;


public String getPOPip()throws RemoteException;


public int getPOPid()throws RemoteException;


public void setSOS(String sosip)throws RemoteException;


public void setPOP(String popip)throws RemoteException;

/**
* @return
*/
int getServerId()throws RemoteException;

/**
* @return
*/
String getServerIP()throws RemoteException;

CalendarManagerInterface getCalendarManagerInterface(String host)throws RemoteException;

/////////////////////////////// Primary and backup //////////////////////////////////
public void copyToNewNode(CalendarManagerInterface newNode) throws RemoteException;

public void addCallBacks(String user, CalendarClientInterface ci) throws RemoteException;

public void addUserCalendars(String user,ArrayList<Event> el) throws RemoteException;

public void AddComingEvents(Event e) throws RemoteException;

public void addBKCallBacks(String user, CalendarClientInterface ci) throws RemoteException;

public void addBKUserCalendars(String user,ArrayList<Event> el) throws RemoteException;

public void AddBKComingEvents(Event e) throws RemoteException;

public void BackupData() throws RemoteException;
public void BackupToPrimary() throws RemoteException;
public void save() throws RemoteException;

public void removeBackupInOldSucc( ) throws RemoteException;

//-----------remove backup event from outdate successor


public void removeBKCallBacks(String user, CalendarClientInterface ci) throws RemoteException;

public void removeBKUserCalendars(String user,ArrayList<Event> el) throws RemoteException;

public void removeBKComingEvents(Event e) throws RemoteException;
//-------load balancing
public void LoadBalancing() throws RemoteException;

////////////////////////////////// Chord /////////////////////////////////////////////////////////

public CalendarManagerInterface find_successor(int id) throws RemoteException;

public CalendarManagerInterface find_predecessor(int id) throws RemoteException;

public CalendarManagerInterface closest_preceding_finger(int id) throws RemoteException;

public void update_others() throws RemoteException;

public void update_finger_table(String s, int i) throws RemoteException;

public void init_finger_table(CalendarManagerInterface other) throws RemoteException;

public void join(CalendarManagerInterface other) throws RemoteException;

public void notify(CalendarManagerInterface other) throws RemoteException;

public void showFingerTabler() throws RemoteException;

public void stabilize() throws RemoteException;

public void leave() throws RemoteException;

public CalendarManagerInterface assignRightServer(String user) throws RemoteException;
public void fix_fingers() throws RemoteException;
public void fix_fingers_byOther() throws RemoteException;

//--------------failure------------------
public boolean isAlive() throws RemoteException;
public boolean checkPredecessor() throws RemoteException;
public boolean checkSuccessor() throws RemoteException;
public void handlePredecessorDown() throws RemoteException;
public void handleSuccessorDown() throws RemoteException;

///////////////////// Distribute schedule Event ////////////////////////////////////
public boolean checkClientAvailable(String user, Event event) throws RemoteException;

public boolean addGroupEvent(String user, Event e)throws RemoteException;


///////////////////////// Two Phase Commit /////////////////////////////////////////////
public boolean canCommit(String user, Event e, CalendarManagerInterface coordinator) throws RemoteException;
public void doFirstAbort(String user, Event e) throws RemoteException;
public boolean doCommit(String user, Event e) throws RemoteException;
}

I cant able to upload FingerTable.java. Order.java, Util.java, Hashing.java, log.java Event.java due to limited character

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote