我正在教自己Java,我正在尝试构建Java RMI拍卖系统。
我目前有很多课程;
auctionServer - starts the RMI registry, creates localhost service "AuctionService"
auctionInterface - provides a description of remote methods available as provided by auctionImpl
auctionImpl - implements the RMI interface
auctionBuyer - a client to offer options to bid on items
auctionSeller - a client to offer options to list items on the server
我正在努力为用户提供一种在保持服务器运行的同时退出客户端的方法。
这是我的服务器代码;
公共类拍卖服务器{
public auctionServer() {
try {
LocateRegistry.createRegistry(1099);
auctionInterface c = new auctionImpl();
Naming.rebind("rmi://localhost/AuctionService", c);
System.out.println("Server running.....");
}
catch (Exception e) {
System.out.println("Server Error: " + e);
}
}
public static void main(String args[]) {
new auctionServer();
}
}
目前,我在auctionImpl中的卖家客户端有以下方法;
public void sellerChoice(){
System.out.println("---- What would you like to do? ----\n");
System.out.println("1: List an item for auction\n"); /* Main menu interface */
System.out.println("2: Print a list of active items\n");
System.out.println("3: Remove an item from the auction\n");
System.out.println("4: Close the seller client\n");
int menuSelection = scanner.nextInt(); // Create switch case
switch(menuSelection){
case 1: // If user chose option 1
listItem();
break;
case 2: // If user chose option 2
printList();
break;
case 3: // If user chose option 3
removeItem();
break;
case 4:
System.exit(0);
break;
default: // If user chose an option other than 1*2*3
System.out.println("You didn't select 1, 2 or 3!!");
}
}
你可以想象System.exit()不能以我想要的方式工作,因为它不仅结束了客户端,也结束了服务器。所以我很难找到一种方法来实现退出卖家客户端而无需关闭服务器的方法。
我正在努力的另一件事是在创建项目时检查auctionID是否已被使用;
public void listItem(){
ItemInfo createdItem = new ItemInfo();
System.out.println("----Enter the auctionName----");
createdItem.setAuctionName(scanner.next());
System.out.println("----Enter the auctionID----");
createdItem.setAuctionID(scanner.nextInt());
System.out.println("----Enter the item startPrice in pound sterling----");
createdItem.setStartPrice(scanner.nextInt());
System.out.println("----Enter the buyoutPrice in pound sterling----");
createdItem.setBuyoutPrice(scanner.nextInt());
itemSet.add(createdItem);
System.out.println("---- Item successfully listed ----");
System.out.println("---- Press 1 to return to auction main menu ----");
if(scanner.nextInt() == 1){
sellerChoice();
}
}
我尝试编写一个for循环来检查auctionID是否已经存在,但它不会要求用户输入任何东西,而是直接询问起始价格。我试过的循环看起来像这样;
for(ItemInfo info : itemSet){
if(scanner.nextInt() == info.auctionID){
System.out.println("auctionID already exists!");
}
}
我是一个非常新的堆栈溢出和Java,所以如果这个问题太长,请通知我。非常感谢任何帮助。
MichaelGG