Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

JAVA RMI put make object and save it into Hashtable

Posted on 2004-11-01
11
Medium Priority
?
192 Views
Last Modified: 2012-06-21
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
      }



}
0
Comment
Question by:va80
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
11 Comments
 
LVL 13

Expert Comment

by:petmagdy
ID: 12467747
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
 

Author Comment

by:va80
ID: 12468476
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
 

Author Comment

by:va80
ID: 12468526
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 13

Accepted Solution

by:
petmagdy earned 1600 total points
ID: 12468570
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
 
LVL 13

Expert Comment

by:petmagdy
ID: 12468579
sorry getInstance() to be static

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

Author Comment

by:va80
ID: 12469055
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
 

Author Comment

by:va80
ID: 12469152
sorry just taking nonesense :)
I am trying it out now
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12791815
Venabili,

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question