?? todolistinterfaceimpl.java
字號:
import org.omg.CORBA.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import ssd8exercise5creator.*;
import ssd8exercise5.*;
import java.io.*;
import java.util.*;
/**
* class <em>TodolistInterfaceImpl</em> represents the implementation
* of the remote methods defined in <em>TodolistInterface.idl</em>
*
* @author iCarnegie (VP)
* @version 1.0
*/
public class TodolistInterfaceImpl extends _TodolistInterfaceImplBase {
private String username, password;
/*
* The to do list is represented as a Vector of
* object of class ListItem.
*/
private Vector toDoList;
/*
* Keep track of the item ids in this to do list.
*/
int itemId = 0;
/**
* class <em>TodolistInterfaceImpl</em> constructor.
*/
public TodolistInterfaceImpl( String inUserName, String inPassWord ) {
username = inUserName;
password = inPassWord;
toDoList = new Vector();
}
/*
* parseTime parses a string of the form date-time (12/22/2003-12:45)
* and returns a <em>java.util.GregorianCalendar</em> object corresponding
* to the input time string.
* @param input String input time
* @return java.util.GregorianCalendar
*/
private GregorianCalendar parseTime( String inputTime ) {
/*
* Tokenize the start_time into date and time
* based on the '-' character.
*/
StringTokenizer paramTokenizer = new StringTokenizer( inputTime, "-" );
String tempDate = paramTokenizer.nextToken();
/*
* Tokenize the date based upon the '/' character.
*/
StringTokenizer dateTokenizer = new StringTokenizer( tempDate, "/" );
int mon = Integer.parseInt( dateTokenizer.nextToken() );
int day = Integer.parseInt( dateTokenizer.nextToken() );
int year = Integer.parseInt( dateTokenizer.nextToken() );
String tempTime = paramTokenizer.nextToken();
dateTokenizer = new StringTokenizer( tempTime, ":" );
int hour = Integer.parseInt( dateTokenizer.nextToken() );
int min = Integer.parseInt( dateTokenizer.nextToken() );
/*
* Create a new GregorianCalendar based on the parsed out
* start time values.
*/
return new GregorianCalendar( year, mon, day, hour, min );
}
/**
* Authenticate a user.
* @param username String name of user
* @param password String password of user
* @return boolean true if authenticate succeeds, false otherwise
*/
private boolean authenticate (String inUserName, String inPassWord ) {
if ( username.equals(inUserName) && password.equals(inPassWord) ) {
return true;
} else {
return false;
}
}
/**
* Implementation of the add function.
* @param username String username of user
* @param password String password of user
* @param startTime String start time of list item
* @param endTime String end time of item
* @param description String description of the item
* @return boolean indicating success of addition
*/
public boolean add( String username, String password, String startTime, String endTime, String description ) {
/*
* Try to authenticate the user.
*/
if (!authenticate(username, password)) {
return false;
}
GregorianCalendar start = parseTime( startTime );
GregorianCalendar end = parseTime( endTime );
if (end.before(start)) {
return false;
}
/*
* Create a new ListItem to add to the to do list.
*/
ListItem newItem = new ListItem( ++itemId, start, end, description );
/*
* If we have made it to here, then the item must have been acceptable,
* so, add it to the Vector of list items and return true.
*/
toDoList.add( newItem );
return true;
}
/**
* Implementation of query function.
* @param startTime String start time of the interval
* @param endTime String end time of the interval
* @param username String username of the person who added the item
* @param password String password of the person who added the item
* @return String string representation of each list item.
*/
public String query( String name, String password, String startTime, String endTime ) {
if (!authenticate( name, password )) {
return null;
}
StringBuffer matches = new StringBuffer();
GregorianCalendar start = parseTime( startTime );
GregorianCalendar end = parseTime( endTime );
/*
* Enumerate through the vector of items and add the item
* that match to the return vector.
*/
Enumeration items = toDoList.elements();
while( items.hasMoreElements() ) {
/*
* Get the next element from the enumeration.
*/
ListItem tempItem = (ListItem) items.nextElement();
/*
* See if tempItem falls in the given time range
* and was called by the input name or is to be attended by
* the input name. If it was, add it to the return vector.
*/
if ( (tempItem.getItemStart().after(start)) &&
(tempItem.getItemEnd().before(end))) {
matches.append(tempItem.toString() + "\n" );
}
}
return matches.toString();
}
/**
* Implementation of delete function.
* @param username String user name of user
* @param password String password of user
* @param itemId int id of item to delete
* @return boolean indicating success of delete
*/
public boolean delete( String username, String password, int itemId ) {
int index = -1;
boolean found = false;
/*
* Try to authenticate the user.
*/
if (!authenticate(username, password)) {
return false;
}
/*
* Iterate through an enumeration of
* the vector of items and remove the one that
* with id itemId.
*/
Enumeration items = toDoList.elements();
ListItem tempItem = null;
while( items.hasMoreElements() ) {
/*
* Get the next element from the enumeration.
*/
tempItem = (ListItem) items.nextElement();
/*
* Increment the index and see if the item we have
* just gotten from the enumeration matches the id
* we are searching for.
*/
if (tempItem.getId()==itemId) {
found = true;
break;
}
}
/*
* If we have found what we are looking for, remove it and return
* true;
*/
if ( found ) {
toDoList.remove( tempItem );
return true;
}
/*
* If we have made it to here, then the item was not found, so return false.
*/
return false;
}
/**
* Implementation of clear function.
* @param username String user name of user
* @param password String password of user
* @return boolean indicating success of the clear
*/
public boolean clear( String username, String password ) {
/*
* Try to authenticate the user.
*/
if (!authenticate(username, password)) {
return false;
}
/*
* Reset the the to do list to a new
* vector.
*/
toDoList = new Vector();
return true;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -