📄 client.java
字号:
import org.omg.CORBA.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import java.io.*;
import ssd8exercise5creator.*;
import ssd8exercise5.*;
/**
* class <em>Client</em> represents a distributed to do list system
* client implemented in Corba.
*
* @author iCarnegie, inc (VP)
* @version 1.0
*/
public class Client {
public static void main( String [] args ) {
int choice = 0, meetingid = 0;
String username, password, recipientname, message, start_time, end_time, otheruser, description,
queryReturn;
/*
* Setup to read from the keyboard.
*/
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
/*
* Create an instance of the message center interface.
*/
TodolistInterface toDoList;
TodolistCreatorInterface toDoListCreator;
try {
/*
* Create and initialize the orb.
*/
ORB orb = ORB.init( args, null );
/*
* Get the root naming context.
*/
org.omg.CORBA.Object objectReference = orb.resolve_initial_references( "NameService" );
NamingContext namingContextReference = NamingContextHelper.narrow( objectReference );
/*
* Find the object named TodolistCreator.
*/
NameComponent nc = new NameComponent("TodolistCreator","");
NameComponent path[] = { nc };
toDoListCreator = TodolistCreatorInterfaceHelper.narrow(namingContextReference.resolve(path));
/*
* Loop forever while displaying the menu and performing
* operations for the user.
*/
while ( true ) {
System.out.println( "********** Distributed To Do List ************ " );
System.out.println( "(1) Register a new user" );
System.out.println( "(2) Clear a user's to do list" );
System.out.println( "(3) Delete a to do list item" );
System.out.println( "(4) Add an item to the to do list" );
System.out.println( "(5) Query items from the to do list" );
System.out.println( "(6) Exit" );
System.out.println( "**********************************************" );
System.out.println( "Enter choice: " );
choice = Integer.parseInt( keyboard.readLine() );
/*
* Register a member. Print an error message if the
* registration fails or a success message
* if the registration succeeds.
*/
if (choice==1) {
System.out.println( "Enter username: " );
username = keyboard.readLine();
System.out.println( "Enter password: " );
password = keyboard.readLine();
if (!toDoListCreator.register(username,password)) {
System.out.println( "Trouble registering!" );
} else {
System.out.println( "Registration Successful!" );
}
}
/*
* Clear a to do list. Get the username and password. Print a
* success message if the clear succeeds or a failure message
* if the clear fails.
*/
if (choice==2) {
System.out.println( "Enter username: " );
username = keyboard.readLine();
System.out.println( "Enter password: " );
password = keyboard.readLine();
nc = new NameComponent(username,"");
NameComponent path2[] = { nc };
toDoList = TodolistInterfaceHelper.narrow(namingContextReference.resolve(path2));
if ( toDoList.clear( username, password ) ) {
System.out.println( " To Do List for " + username + " has been successfully cleared." );
} else {
System.out.println( "Trouble clearing to do list for " + username + "!" );
System.out.println( "Try a different username/password!" );
}
}
/*
* Attempt to delete a meeting. The user must enter
* the username, password, and meeting id. Print a
* success message if the delete succeeds and a failure
* notice if the deletion fails.
*/
if ( choice==3 ) {
System.out.println( "Enter username: " );
username = keyboard.readLine();
System.out.println( "Enter password: " );
password = keyboard.readLine();
System.out.println( "Enter id of to do list item to delete: " );
meetingid = Integer.parseInt( keyboard.readLine() );
nc = new NameComponent(username,"");
NameComponent path2[] = { nc };
toDoList = TodolistInterfaceHelper.narrow(namingContextReference.resolve(path2));
if ( toDoList.delete(username, password, meetingid ) ) {
System.out.println( "List item successfully deleted!" );
} else {
System.out.println( "Could not delete list item!" );
System.out.println( "Try a different username/password/list item id!" );
}
}
/*
* Add an item to the to-do list. Print a success
* message if the addition succeeds. The user must enter his username, password,
* start time, end time, and item description.
*/
if (choice==4) {
System.out.println( "Enter username: " );
username = keyboard.readLine();
System.out.println( "Enter password: " );
password = keyboard.readLine();
System.out.println( "Enter the start time" );
System.out.println( "For example, 12/22/2003-12:45" );
start_time = keyboard.readLine();
System.out.println( "Enter the end time" );
System.out.println( "For example, 12/22/2003-12:45" );
end_time = keyboard.readLine();
System.out.println( "Enter the to do list item description" );
description = keyboard.readLine();
nc = new NameComponent(username,"");
NameComponent path2[] = { nc };
toDoList = TodolistInterfaceHelper.narrow(namingContextReference.resolve(path2));
if ( toDoList.add( username, password, start_time, end_time, description ) ) {
System.out.println( "To do list item added successfully!" );
} else {
System.out.println( "Failed to add list item!" );
}
}
/*
* Query a meeting. User must enter username, password, start time,
* and end time. Print an error message if there were no meetings matching
* the criteria. Otherwise print the meetings matching the criteria.
*/
if (choice==5) {
System.out.println( "Enter username: " );
username = keyboard.readLine();
System.out.println( "Enter password: " );
password = keyboard.readLine();
System.out.println( "Enter the start time" );
System.out.println( "For example, 12/22/2003-12:45" );
start_time = keyboard.readLine();
System.out.println( "Enter the end time" );
System.out.println( "For example, 12/22/2003-12:45" );
end_time = keyboard.readLine();
nc = new NameComponent(username,"");
NameComponent path2[] = { nc };
toDoList = TodolistInterfaceHelper.narrow(namingContextReference.resolve(path2));
queryReturn = toDoList.query( username, password, start_time, end_time );
if (queryReturn==null || queryReturn.length()==0) {
System.out.println( "No to do list items matched criteria!" );
} else {
System.out.println( "To do list items matching criteria: " );
System.out.println( queryReturn );
}
}
/*
* Exit.
*/
if (choice==6) {
System.exit(0);
}
}
} catch( Exception e ) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -