1

I am trying to find a way to insert all the info pertaining to the student or the faculty into some sort of String Collections(JAVA). example, maps , hashtables arraylists etc My main has the below methods which cover all name ,city..... getters setters and Tostring() a. I read multiple posts relevant to my question,but non suffices my needs.

public void studentStuff(){

        student.nameAge();
        student.addressparamaters();
        System.out.println(student.toSTring());

    }

public void facultyStuff(){

        faculty.nameAge();
        faculty.addressparamaters();
        System.out.println(faculty.toSTring());

Running my Main I get prompted for input in the following order;

Please Enter First Name
Mike
Please Enter Last Name
Fontain
Please Enter Age
22
Please Enter The House Number :
15
Please Enter The StreetName :
Mackay Place
Please Enter The City Name :
Long Island
Please Enter The House Zip Code:
null
Please a valid ZIP code format
11209

Out Put

 First Name: Mike
 Last Name :   Fontain
 Age :   22.0
 HOUSE NUMBER: 15
 Street Name :   Mackay
 City :   Place
 State:   Long
 Zip:   11209
Community
  • 1
  • 1
  • FYI, I am not sure if the technology even exist to achieve my requirements . please advise either way. Doesn't hurt to ask:) –  Feb 06 '14 at 21:23

3 Answers3

2

In Java, the inherent concept of a Class accomplishes this for you, as long as you decide on a key member of the class.

class Student extends Person
{
    private int student_id; //if unique, good candidate for a key
    ....
    private int age,weight...;
}

And you can store a collection of students (or students and faculty if they extend a base class) in any sort of data structure, such as ArrayList etc.

List<Person> mylist = new ArrayList<Person>();
mylist.add(new Student(1234));

You can add methods to Student, such as overriding toString() to print out any important information.

If you wanted an O(1) access time to fetch a student based on their key, you could implement something like

Map<Integer,Person> mymap = new HashMap<Integer,Person>();
mymap.put(student1.getStudentID(),student1);
C.B.
  • 8,096
  • 5
  • 20
  • 34
  • The structure of my code utilized Inheritance and Composition clas Person{} >> class student extends person{} etc. How is this going to work. Am I only storing the Student_ID inside my map OR Hashtable. Can you please elaborate –  Feb 06 '14 at 21:30
  • Right , I do have an Interface toString(){} that is implemented by all sub-classes. I am probably misunderstanding collections... i WAS THINKING THAT EVERYTHING(all values) actually get stored inside the Arraylist or any other sort of Collections .... –  Feb 06 '14 at 21:35
  • @user3267108 every class extends Object, so you can always override the toString() method to make it print whatever you want. – C.B. Feb 06 '14 at 21:37
  • "Vote Up requires 15 reputation" I owe u –  Feb 06 '14 at 21:45
2
public class Student {
   Map<String> params = new HashMap<>()
   public Student setParameter(String paramName, String paramValue) {
      params.add(paramName, paramValue);
      return this;
   }

   public getParam(String paramName) {
      String value = param.get(paramName);
      if(value == null) value == "undef";
      return value;
   }
}

Do you mean something like that? You can store any parameter and retieve it knowing it's name.

Observer
  • 710
  • 10
  • 14
  • That can be very useful.I Will def make that come into play somewhere in my code. –  Feb 06 '14 at 21:40
0

You have two types of classes: Faculty and Student. But you want to add them to the same collection. So either both must implement same interface or extend same class. If you have common functionalities between the two classes go for extending a base class, else stick with implementing an interface.

The identification number for a person will be unique. So use a Map with key as the identification number and value as the interface or base class common for student and faculty. To learn basics about Maps refer http://preciselyconcise.com/java/collections/h_map.php . This link also gives you an idea of which map you will need to use in which situation.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Sai Sunder
  • 1,001
  • 1
  • 11
  • 16