Solved

JAVA RMI put make object and save it into Hashtable

Posted on 2004-11-01
184 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
Question by:va80
    8 Comments
     
    LVL 13

    Expert Comment

    by:petmagdy
    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
    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
    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
     
    LVL 13

    Accepted Solution

    by:
    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
    sorry getInstance() to be static

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

    Author Comment

    by:va80
    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
    sorry just taking nonesense :)
    I am trying it out now
    0
     
    LVL 13

    Expert Comment

    by:petmagdy
    Venabili,

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Title # Comments Views Activity
    no14 challenge 14 40
    core java. isInteger methods. Finding bug. 10 52
    IT Company 5 38
    json format text only 4 29
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

    846 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

    Need Help in Real-Time?

    Connect with top rated Experts

    5 Experts available now in Live!

    Get 1:1 Help Now