// Test method
 public LinkedList<Integer> getLiveAuctionsIds() throws RemoteException {
   LinkedList<Integer> ll = new LinkedList<Integer>();
   for (AuctionItem t : liveAuctionItems.values()) {
     ll.add(t.getAuctionId());
   }
   return ll;
 }
 @Override
 public LinkedList<String> getExistingAuctions() throws RemoteException {
   LinkedList<String> ll = new LinkedList<String>();
   for (AuctionItem i : liveAuctionItems.values()) {
     ll.add(i.toString());
   }
   return ll;
 }
 @Override
 // Event though the data structure for live auctions is concurrent the method still needs to be
 // synchronized because the creation
 // of auction items concurrently produces auction items with the same id.
 public synchronized boolean registerAuctionItem(
     String name, double min_item_value, Date closing_date, int client) throws RemoteException {
   AuctionItem item = new AuctionItem(name, min_item_value, closing_date, client, this);
   liveAuctionItems.put(item.getAuctionId(), item);
   Thread t = new Thread(item);
   t.start();
   return true;
 }
 @Override
 public boolean moveToExpiredAuctions(int auctionId) {
   if (!liveAuctionItems.containsKey(auctionId)) {
     System.out.println("This auction does not exist\n");
     return false;
   }
   AuctionItem temp;
   if ((temp = liveAuctionItems.remove(auctionId)) != null) {
     System.out.println("Auction successfully removed from list with live auctions\n");
     // Change the auction id to correspond to the index
     expiredAuctions.put(temp.getAuctionId(), temp);
     Thread t = new Thread(temp);
     t.start();
     return true;
   }
   return false;
 }
  @Override
  public void notifyResult(AuctionItem item) {
    if (registeredUsers.containsKey(item.getCreator_id())) {
      RMIClientIntf creator = registeredUsers.get(item.getCreator_id());
      try {
        creator.callBack("Your Auction has produced the following results :\n ");
        creator.callBack(item.getResult());
      } catch (RemoteException e) {
        System.out.println("Creator no longer online\n");
      }
    } else {
      System.out.println("Creator id does not exist\n");
    } // Normally an auction with no creator registered should not exist but since this is a
    // simplified implementation
    // We allow bids on auctions with no creator (usually the initialised ones) for testing
    // purposes.
    if (item.numberOfBids() != 0) {
      System.out.println("Notifying bidders");
      Iterator<Bid> it = item.getBidders();
      while (it.hasNext()) {
        Bid bidder = it.next();
        if (registeredUsers.containsKey(bidder.getUserId())) {
          RMIClientIntf client = registeredUsers.get(bidder.getUserId());
          try {
            client.callBack(item.getResult());
          } catch (RemoteException e) {
            System.out.println("Bidder with id " + bidder.getUserId() + " no longer online\n");
          }

        } else {
          System.out.println("User id does not exist\n");
        }
      }
    }
  }
  public static void main(String args[]) {

    String host = "localhost";
    int port = 1099;

    if (args.length > 0) {
      host = args[0];
      port = Integer.parseInt(args[1]);
    }

    // Initialise auctions from permanent storage
    BufferedReader br = null;
    BufferedWriter bw = null;
    try {
      AuctionServer rmiServer = new AuctionServer();
      Registry reg = LocateRegistry.getRegistry(host, port);
      reg.rebind("auctionServer", rmiServer);
      System.out.println("AuctionServer is ready");
      System.out.println(
          "Select one of the two options :\n1: To save live auctions\n2: To load test auctions that expire in a minute (note you need to start 5 clients first)  ");
      Scanner sc = new Scanner(System.in);
      bw = new BufferedWriter(new FileWriter("auctionsSaved.dat"));
      switch (sc.nextInt()) {
        case 1:
          for (AuctionItem item : rmiServer.liveAuctionItems.values()) {
            bw.write(item.toString() + "\n");
          }
          System.out.println("Saved auctions state.");
          break;
        case 2:
          GenerateAuctions generator = new GenerateAuctions();
          generator.generateList();
          br = new BufferedReader(new FileReader("auctionsBackup.dat"));
          String currentLine;
          while ((currentLine = br.readLine()) != null) {
            String[] array = currentLine.split(",");
            String name = array[0];
            double min_item_value = Double.parseDouble(array[1]);
            Date closing_date = new SimpleDateFormat("d/M/y H:mm:s").parse(array[2]);
            int client = Integer.parseInt(array[3]);
            rmiServer.registerAuctionItem(name, min_item_value, closing_date, client);
          }
          System.out.println("Initialised auctions...");
          break;
      }

    } catch (RemoteException e) {
      System.out.println("Exception in AuctionServer.main " + e);
    } catch (MalformedURLException ue) {
      System.out.println("MalformedURLException in RMIServerImp.main " + ue);
    } catch (IOException e) {
      System.out.println("Could not open file ");
      e.printStackTrace();
    } catch (ParseException e) {
      System.out.println("Unable to parse date");
      e.printStackTrace();
    } finally {
      if (br != null) {
        try {
          br.close();
        } catch (IOException e) {
          System.out.println("Unable to close file.");
        }
      }
      if (bw != null) {
        try {
          bw.close();
        } catch (IOException e) {
          System.out.println("Unable to close file.");
        }
      }
    }
  }