📄 client.java
字号:
import java.rmi.*;
import java.io.*;
import javax.naming.*;
/**
* Class Client implentents menu model.
*
* @version 1.0 9 June 2007
* @author Zhao Xiaoyu
*/
public class Client{
/**
* Define a general variable to mark the error state
* and according to the error state to return
* corresponding hint message.
*/
private static int flag = 0;
public static void main(String[] args){
Client c = new Client();
c.run(args);
}
/**
* Implement the client-end menu for user to select.
*/
void run(String[] args){
try{
/**
* This interface represents a naming context, which consists of a
* set of name-to-object bindings.
*/
Context nc = new InitialContext();
/** Declare a RMIIntf object and assign it to null */
RMIIntf r1 = null;
// If the parameter number is not enough exit the program.
if (args.length != 2) {
System.out
.println("\nYou must input remote host ip address and port number!");
System.exit(0);
}
try {
// Retrieves the named object.
r1 = (RMIIntf) nc.lookup("rmi://" + args[0] + ":" + args[1]
+ "/RemoteService");
} catch (NumberFormatException nfe) {
System.out.println("\nInput Parameter Format Error!");
System.exit(0);
}
BufferedReader rd = new BufferedReader(new InputStreamReader(
System.in));
/** Loop to display the main menu. */
while (true) {
// Display the menu item and hint for input.
System.out
.println("1.User Register.\n2.Show Users.\n3.Check Message.\n4.Leave Message.\n5.User Exit");
System.out.print("Please Input Your choice > ");
/** Read in choice. */
String choice = rd.readLine();
/** Declare the function parameters used below. */
String u, p, r, mt;
if (isCorrectNumber(choice)) {
// Parse the choice to int.
int c = Integer.parseInt(choice);
// According to user's choice enter different branchs
switch (c) {
case 1:
System.out.print("Please Input username >> ");
u = rd.readLine();
while(u.equals("")){
System.out.println("Username Can't be null, Please Input Again!");
System.out.print("Please Input username >> ");
u = rd.readLine();
}
System.out.print("Please Input password >> ");
p = rd.readLine();
while(p.equals("")){
System.out.println("Password Can't be null, Please Input Again!");
System.out.print("Please Input password >> ");
p = rd.readLine();
}
System.out.println(r1.register(u, p));
break;
case 2:
System.out.println(r1.showusers());
break;
case 3:
System.out.print("Please Input username >> ");
u = rd.readLine();
while(u.equals("")){
System.out.println("Username Can't be null, Please Input Again!");
System.out.print("Please Input username >> ");
u = rd.readLine();
}
System.out.print("Please Input password >> ");
p = rd.readLine();
while(p.equals("")){
System.out.println("Password Can't be null, Please Input Again!");
System.out.print("Please Input password >> ");
p = rd.readLine();
}
System.out.println(r1.checkmessages(u,p));
break;
case 4:
System.out.print("Please Input Username >> ");
u = rd.readLine();
while(u.equals("")){
System.out.println("Username Can't be null, Please Input Again!");
System.out.print("Please Input username >> ");
u = rd.readLine();
}
System.out.print("Please Input Receiver name >> ");
r = rd.readLine();
while(r.equals("")){
System.out.println("Receivername Can't be null, Please Input Again!");
System.out.print("Please Input Receiver name >> ");
r = rd.readLine();
}
System.out.print("Please Input Message-Text >> ");
mt = rd.readLine();
while(mt.equals("")){
System.out.println("You shouldn't send empty message, Please Input Again!");
System.out.print("Please Input Message-Text >> ");
mt = rd.readLine();
}
System.out.println(r1.leavemessage(u,r,mt));
break;
case 5:
System.out.println("You exit the program successfully!");
System.exit(0);
}
}else {
// Return a variety of error messages.
if (flag == 1)
System.out
.println("\nYou choice must between 1~5!\n");
if (flag == 2)
System.out.println("\nYou must input a number!\n");
continue;
}
}
}catch (RemoteException re) {
System.out.println(re);
} catch (Exception e) {
System.out.println(e);
}
}
/** Implement the functionality of count logic value
*
* @para choice the choice express the user's selecttion
* @return return the judged logic value
*/
public static boolean isCorrectNumber(String choice) {
// Judge whether is input choice'length is longer than one character.
if (choice.length() == 1) {
// Judge whether the input string can be parse into digit.
if (Character.isDigit(choice.charAt(0))) {
int c = Integer.parseInt(choice);
// Judge whether the choice is between the menu option.
if (c > 0 && c < 6)
return true;
else{
flag = 1;
return false;
}
} else{
flag = 2;
return false;
}
} else {
/**
* According to whether the input choice is digit
* return different error flag.
*/
int i = 0;
for (; i < choice.length(); i++) {
if (!Character.isDigit(choice.charAt(i)))
break;
}
if (i == choice.length())
flag = 1;
else
flag = 2;
return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -