📄 client.java
字号:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
class Client {
static BufferedReader stdin = new BufferedReader(new InputStreamReader(
System.in));
public static void main(String args[]) throws IOException {
System.out.println("Looking for Meeting server");
try {
String registry = "localhost";
String registration = "rmi://" + registry + "/Meeting";
Remote remoteService = Naming.lookup(registration);
Service service = (Service) remoteService;
while (true) {
printMenu();
int ch = stdin.read();
if (ch == '1') {
spAddUser(service);
} else if (ch == '2') {
spAddMeeting(service);
} else if (ch == '3') {
query(service);
} else if (ch == '4') {
delete(service);
} else if (ch == '5') {
clear(service);
} else if (ch == '6') {
displayAllUser(service);
} else if (ch == '7') {
displayAllMeeting(service);
} else if (ch == '8') {
System.exit(0);
}
}
} catch (NotBoundException e) {
System.out.println("No light bulb server avaliable");
} catch (MalformedURLException e) {
System.out.println("Remote Erro-" + e);
} catch (RemoteException e) {
System.out.println("Erro-" + e);
}
}
public static void spAddUser(Service service) throws IOException {
stdin.readLine();
System.out.print("username: ");
String username = stdin.readLine();
System.out.print("pwd:");
String pwd = stdin.readLine();
if (service.addUser(username, pwd)) {
System.out.println("user is exist");
} else {
System.out.println("register : [" + username + "] [" + pwd + "]");
}
}
public static void spAddMeeting(Service service) throws IOException {
stdin.readLine();
System.out.print("host: ");
String host = stdin.readLine();
System.out.print("pwd:");
String pwd = stdin.readLine();
System.out.print("gest: ");
String gest = stdin.readLine();
System.out.print("startTime: ");
String startTimeStr = stdin.readLine();
Date startTime = StringConvertToDate(startTimeStr);
System.out.print("endTime: ");
String endTimeStr = stdin.readLine();
Date endTime = StringConvertToDate(endTimeStr);
System.out.print("title: ");
String title = stdin.readLine();
System.out.println(service.addMeeting(host, pwd, gest, startTime,
endTime, title));
}
public static void query(Service service) throws IOException {
stdin.readLine();
System.out.print("username: ");
String username = stdin.readLine();
System.out.print("pwd:");
String pwd = stdin.readLine();
System.out.print("startTime: ");
String startTimeStr = stdin.readLine();
Date startTime = StringConvertToDate(startTimeStr);
System.out.print("endTime: ");
String endTimeStr = stdin.readLine();
Date endTime = StringConvertToDate(endTimeStr);
Vector result = service.queryMeeting(username, pwd, startTime, endTime);
System.out.println("The Meeting can Find:");
for (int i = 0; i < result.size(); i++) {
System.out.println(result.get(i).toString());
}
}
public static Date StringConvertToDate(String dateStringToParse) {
SimpleDateFormat bartDateFormat = new SimpleDateFormat("MM-dd-yyyy");
Date date = new Date();
try {
date = bartDateFormat.parse(dateStringToParse);
return date;
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
return date;
}
public static void delete(Service service) throws IOException {
stdin.readLine();
System.out.print("username: ");
String username = stdin.readLine();
System.out.print("pwd:");
String pwd = stdin.readLine();
System.out.print("title: ");
String title = stdin.readLine();
if(service.deleteMeeting(username, pwd, title)){
System.out.println("delete Meeting " + title);
}
}
public static void clear(Service service) throws IOException {
stdin.readLine();
System.out.print("username: ");
String username = stdin.readLine();
System.out.print("pwd:");
String pwd = stdin.readLine();
if (service.clearMeeting(username, pwd)) {
System.out.println("Clear All Meeting of " + username);
}
}
public static void displayAllUser(Service service) throws IOException {
stdin.readLine();
System.out.println(service.allUser());
}
public static void displayAllMeeting(Service service)
throws IOException {
stdin.readLine();
System.out.println(service.allMeeting());
}
public static void printMenu() {
System.out.println("");
System.out.println("1 register user");
System.out.println("2 add meeting");
System.out.println("3 query meeting ");
System.out.println("4 delete meeting ");
System.out.println("5 clear meeting ");
System.out.println("6 display user");
System.out.println("7 display meeting ");
System.out.println("8 exit ");
System.out.print("select your choice:");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -