目錄結構
// MyRMIClient
package Client;
import InterFace.*;
import java.rmi.Naming;
import java.util.ArrayList;
import java.util.Scanner;
public class MyRMIClient {
public static void Print_Tip(){
System.out.println("選擇操作代號完成操作");
System.out.println("1:add args:book b");
System.out.println("2:queryByID args:int bookID");
System.out.println("3:queryByName args: String name");
System.out.println("4:delete args:int BookID");
System.out.println("5: print args:null");
System.out.println("6: exit");
}
public static void main(String args[]) {
try {
String name = "rmi://127.0.0.1:6600/BookSystem";
BookSystemInfo BookSystem=( BookSystemInfo)Naming.lookup(name);
System.out.println("查找服務端成功");
Scanner input=new Scanner(System.in);
int number = 6;
Print_Tip();
while ((number = input.nextInt())!=6){
switch (number){
case 1:{
System.out.println("輸入ID和名字");
int bookID = input.nextInt();
String bookName = input.next();
Book newbook = new Book(bookName,bookID);
boolean res = BookSystem.add(newbook);
if(res==true){
System.out.println("添加成功");
}
else{
System.out.println("添加失敗");
}
}
break;
case 2:{
System.out.println("輸入ID");
int bookID = input.nextInt();
Book res = BookSystem.queryByID(bookID);
if(res==null){
System.out.println("無該ID號的書籍");
}
else{
System.out.println("書籍信息如下");
res.Print_content();
}
}
break;
case 3:{
System.out.println("輸入name");
String bookName = input.next();
ArrayList<Book> res = BookSystem.queryByName(bookName);
if(res==null){
System.out.println("同名書籍數目爲零");
}
else {
System.out.println("查詢結果如下");
for (Book b : res) {
b.Print_content();
}
}
}
break;
case 4:{
System.out.println("輸入ID");
int bookID = input.nextInt();
boolean res = BookSystem.delete(bookID);
if(res==true){
System.out.println("刪除成功");
}
else{
System.out.println("刪除失敗");
}
}
break;
case 5:{
System.out.println("調用該方法只是在服務端打印書籍列表,客戶端不顯示");
BookSystem.print();
}
break;
}
}
} catch (Exception e) {
System.err.println("??? exception:");
e.printStackTrace();
}
}
}
// Book
package InterFace;
import java.io.Serializable;
public class Book implements Serializable {
private static final long serialVersionUID = 6529685098267757690L;
private String name;
private int BookID;
public Book(String n,int b){
name = n;
BookID = b;
}
public boolean EqualName(String otherName){
return name.equals(otherName);
}
public boolean EqualID(int otherID){
return (BookID==otherID);
}
public void Print_content(){
System.out.println(BookID+": "+name);
}
}
// BookSystem
package InterFace;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.io.Serializable;
import java.util.ArrayList;
public class BookSystem extends UnicastRemoteObject implements BookSystemInfo,Serializable {
private static final long serialVersionUID = 6529685098267757665L;
private ArrayList<Book> list;
public BookSystem() throws RemoteException{
list = new ArrayList<>();
}
public boolean add(Book b) throws RemoteException{
try{
list.add(b);
}catch (Exception e){
return false;
}
return true;
}
public Book queryByID(int BookID) throws RemoteException{
for(Book b :list){
if(b.EqualID(BookID)){
return b;
}
}
return null;
}
public ArrayList<Book> queryByName(String name) throws RemoteException{
ArrayList<Book> res = new ArrayList<Book>();
for(Book b :list){
if(b.EqualName(name)){
res.add(b);
}
}
return res;
}
public boolean delete(int BookID) throws RemoteException{
for(Book b :list){
if(b.EqualID(BookID)){
list.remove(b);
return true;
}
}
return false;
}
public void print(){
System.out.println("書籍列表如下");
for(Book b :list){
b.Print_content();
}
}
}
// BookSystemInfo
package InterFace;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.io.Serializable;
import java.util.ArrayList;
public interface BookSystemInfo extends Remote,Serializable{
boolean add(Book b) throws RemoteException;
Book queryByID(int bookID) throws RemoteException;
ArrayList<Book> queryByName(String name) throws RemoteException;
boolean delete(int BookID) throws RemoteException;
void print() throws RemoteException;
}
// MyRMIServer
package Server;
import java.rmi.registry.LocateRegistry;
import java.rmi.Naming;
import InterFace.*;
public class MyRMIServer {
public static void main(String[] args) throws Exception {
try {
LocateRegistry.createRegistry(6600);
String name ="rmi://127.0.0.1:6600/BookSystem";
BookSystemInfo engine = new BookSystem();
System.out.println("Registering BookSystem Object");
Naming.rebind(name, engine);
} catch (Exception e) {
System.err.println("Exception:" + e);
e.printStackTrace();
}
}
}