JAVA RMI put make object and save it into Hashtable

I am making RMI program which is supposed to a "test" object called "pruefung" in an hashtable.  The program runs in class Called "PruefungsverwaltungsClient" and uses an Class called "Pruefung"  I am a little confused where I am supposed to have the Hashtable but I am supposed to make the object "Pruefung" in an method called PruefungAnlegen(Pruefung pruefung) throws RemoteException;  and save it in hashtable The interface is called PruefungsVerwaltung. I tried to save the object in Hashtable at the top of the Client Class "PruefungsverwaltungsClient" but my Eclipse always wants me to make the Variables Static.   My idea is to make a function inside the class Pruefung which saves it in Hashtable.  can someone help me make the object and save it in Hashtable please  but here is the code:

Class Pruefung:

public class Pruefung implements Serializable {



      String pruefungsName;
      String beginn;
      String ende;
      String ort;
      String pruefer;
    Student student;
      
    public Pruefung(String pruefungsName, String beginn, String ende, String ort, String pruefer)  {
    this.pruefungsName = pruefungsName;
    this.beginn = beginn;
    this.ende = ende;
    this.ort = ort;
    this.pruefer = pruefer;
   }
   void setPruefungsName(String pruefungsName) {
         this.pruefungsName=pruefungsName;
   }
   
   void setBeginn(String beginn) {
      this.beginn=beginn;
   }
   
   void setEnde(String ende) {
      this.ende=ende;
   }
   
   void setOrt(String ort) {
      this.ort=ort;
   }
   
   void setPruefer(String pruefer) {
      this.pruefer=pruefer;
   }
 
  public String getBeginn() {
        return beginn;
  }
 
  public String getEnde() {
      return ende;
  }
 
  public String getOrt() {
      return ort;
  }
 
  public String getPruefer() {
      return pruefer;
  }
  public String getPruefungsname() {
      return pruefungsName;
  }

//this was the idea about the hashtable!!!!!!!!!!!!!!!!!!!!!!
  Hashtable Hash = new Hashtable(); //byr til Hashtable
  public boolean Add(Pruefung ny)  {
        String ID = ny.getPruefungsname();
        if(Hash.containsKey(ID)){
              return false;
        }
        else {
              Hash.put(ID,ny);
              return true;
        }
                             

  }

   
}

********************************************************
Client class PruefungsVerwaltungsClient:

import java.net.InetAddress;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.util.Hashtable;
import java.io.*;

/*
 * Created on 27.10.2004
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */

/**
 * @author Valdimar1
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class PruefungsVerwaltungsClient  {
      //Eclipse always wants to make there variables Static :(
      public static String test = null;
      public  static String pruefungsName;
      public static String beginn = null;
      public static String ende = null;
      public static String ort = null;
       public static String pruefer = null;
      public static Pruefung p;
       public void pruefungAnlegen(Pruefung p) {
             p = new Pruefung(pruefungsName, beginn, ende, ort, pruefer);
             p.Add(p);

             test = p.getOrt();

            
       }


      public static void main(String[] args) {
            String registryServer = null;

            // determine name of registryServer where remote object is registered
            // use a Security Manager
            //String message = "Blank", webServer = "";

            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  //opens up standard input
            if(System.getSecurityManager() == null) {
                                      System.setSecurityManager(new RMISecurityManager());
                  }
            if (args.length == 1) {
                        // get registryServer from command line arg
                        registryServer = args[0];
            }
            else {
                        try {
                                    // use local host as registryServer
                                    System.out.println("Using local host as registryServer.");            
                                    registryServer = InetAddress.getLocalHost().getHostName();
                        }
                        catch (java.net.UnknownHostException ex) {
                                    System.out.println("Failed to determine name of local host.");
                                    System.exit(1);
                        }
            }

            PruefungsVerwaltung obj = null;
            try {

                  System.out.println("lookup PruefungsverwaltungServer on: " + registryServer);      
                  // perform lookup in rmiregistry on registryServer for remote
                  // object "Hello-Server"
                  // cast reference to remote interface Hello

                  obj = (PruefungsVerwaltung)Naming.lookup("//" + registryServer + "/" + "PruefungsverwaltungServer");
                  //setja her afganginn
                  //skrifa i variablana
                  System.out.println("*****************************************************************");
                  System.out.println("*****************************************************************");
                  System.out.println("********************* Pruefungsverwaltung ***********************");
                  System.out.println("******************** Valdimar Jon Audunsson *********************");
                  System.out.println("*********************** Matrnr: 9914436 *************************");
                  System.out.println("*****************************************************************");
                  System.out.println("*****************************************************************");
                  
                  String choice1 = null;
                  int choice = 0;
                  char choice2 = 0;
                  char yourchoice;
                  do {
                  yourchoice = 0;
                  System.out.println("Choose 'a' to make new Test ");
                  System.out.println("Choose 'q' to quit ");
                  System.out.println("Choose a character: ");
                  yourchoice = (char)System.in.read();
                  //System.out.println("Choose an operation: ");
            //choice1 = br.readLine();
                  //choice = Integer.parseInt(choice1); //change String to int
                  
                  switch (yourchoice){
                        case 'a': {
                        
                                  System.out.println("Namen der Pruefung: ");
                                  pruefungsName = br.readLine();
                                  pruefungsName = br.readLine();
                  
                                  System.out.println("Beginn der Pruefung: ");
                                  beginn = br.readLine();
                  
                                  System.out.println("Ende der Pruefung: ");
                                  ende = br.readLine();
                  
                                  System.out.println("Ort der Pruefung: ");
                                  ort = br.readLine();
                  
                                  System.out.println("Pruefer: ");
                                   pruefer = br.readLine();

                                       obj.PruefungAnlegen (p);
                                   //obj.PruefungAnlegen (new Pruefung(pruefungsName, beginn, ende, ort, pruefer));
                              System.out.println(test);
                             //p.Add(p);
                             
                                   } break;
                        case 'q': {
                                       System.out.println("Thank you for your visit ");
                                   } break;
                        default: {System.out.println("Wrong number!! Choose again "); } break;
                        
                  }
                  }while(yourchoice != 'q');
                  
            } catch (Exception e) {
                  System.out.println("PruefungsverwaltungServer exception: " + e.getMessage());
                  e.printStackTrace();
            }
            //Her getur komid lika eitthvad
      }



}
va80Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
petmagdyConnect With a Mentor Commented:
ok the Java Complier requires that any class member field (like test ) used from a static method (main method) to be a static member field so this is normal

In ur case the PruefungsVerwaltungsClient is to be designed as singletone so u can call it from ur main method, so here is the suggested modifications to
PruefungsVerwaltungsClient:

1- add a member method like this:

Hashtable tests;

2- Add a member method of itself (Yes do it!!)
protected static PruefungsVerwaltungsClient instance=null;


3- make the constructor private and add ur member methods initializations specially

tests = new Hashtable();
instance = this;

3- make a method like this:

public PruefungsVerwaltungsClient getInstance()
{
  if(instance == null)
  {
    instance = new PruefungsVerwaltungsClient();
  }
  return instance;
}

4- u now can add a method to add tests like this

public void addTest(String key, Pruefung value)
{
  tests.put(key, value);
}

5- now from ur main method do this:

PruefungsVerwaltungsClient theClass = PruefungsVerwaltungsClient.getInstance();
theClass.addTest(...);



0
 
petmagdyCommented:
Frankly I don't understand ur requirment and I think their is no one who can do, what is ur requirment Exactly?
U r talking about a hashtable and inserting objects into it, What objects to insert based on what condition and ? what do u want to achieve?
0
 
va80Author Commented:
Sorry about that. ill try to make it more understandable....
1. Class Pruefung (it means Test) saves information about Tests (where it is, what it is called, when it begins, ends and where it takes place).  

2.Class PruefungsVerwaltungsClient the main program where the user writes the information about the test and and presses enter and then the function PruefungAnlegen (means make new Test) is supposed to make an object of Class "Pruefung" (Test).  now when many tests are being registered many objects of Class "pruefung" are made and I wanted to put these objects into a Hashtable.  Later on I am supposed to make Class "Student" and these are supposed to register for Tests (Class Pruefung) and these objects of Class Students are also to be Saved in Hashtable.  No Database is to be made.  This is a very simple program... I hope this helps something..when not i will try to explane it more...please response
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
va80Author Commented:
I tried to make the Hashtable inside the Class "Pruefung" but I am not sure where and how i am supposed to implement the hashtable and how to put the objects of Class "pruefung" in there
0
 
petmagdyCommented:
sorry getInstance() to be static

public static PruefungsVerwaltungsClient getInstance()
{
  if(instance == null)
  {
    instance = new PruefungsVerwaltungsClient();
  }
  return instance;
}
0
 
va80Author Commented:
ok another thing.. in main program where I write the info to be saved in Class "pruefung" for example:                               System.out.println("Namen der Pruefung: ");
                               pruefungsName = br.readLine();
 my compiler wants the Variables to be static such as this "pruefungsName".  Doesnt that mean that this variables cant be changed???
0
 
va80Author Commented:
sorry just taking nonesense :)
I am trying it out now
0
 
petmagdyCommented:
Venabili,

I thinked I have helped quite good here, I deserve some points
0
All Courses

From novice to tech pro — start learning today.