开发者

How to declare object reference variables and assign them values according to user input.?

开发者 https://www.devze.com 2023-01-26 02:56 出处:网络
I\'m creating a command line Movie Register where the user has different ways to filter movies, directors and actors.

I'm creating a command line Movie Register where the user has different ways to filter movies, directors and actors.

I'm wondering if it's possible to declare some object reference variables and assign them values according to the user input. That is, if the user input is lowercase I'd like to declare and assign value to a Person[] object, and and ArrayList (Persons) object etc, and if the user input is uppercase I'd like to declare and assign value to a Film[] object, ArrayList (Film) etc.

Code:

//String kode is the user input
void find(String kode) {

if (Character.isLowerCase(kode.charAt(0))) { // if the input is a movie code

    ArrayList<Person> utvalg = new ArrayList<Person>();
    Iterator <String> it = personer.keySet().iterator();
    Person obj = new Person();
    HashMap <String,Person> map = Filmregister.personer;
    Person[] arr;

} else { // if the input is a person code

    ArrayList<Film> utvalg = new ArrayList<Film>(); 
    Iterator <String> it = filmer.keySet().iterator();
    Film obj = new Film();
    HashMap <String,Film> map = Filmregister.filmer;
    Film[] arr;

}

while (it.hasNext()) {

    String nokkel = (String) it.next();
    String subnokkel = nokkel.substring(0, kode.trim().length());

    if (subnokkel.equals(kode)) {

    obj = map.get(nokkel);

        utvalg.add(obj);

    } else {
    // do nothing
    } 
}

arr = Utils.sorterFilmerAbc(utvalg);

}

So, javac gives me "cannot find symbol" on all the variables when I reference them outside the if else blocks. I realise that it's because the variables are declared within the if else scope, but I was hoping there's a way to do this so I don't have to have if else statements everytime I want to reference either a Person object or a Film object.

I looked at the possibility to declare the list and maps with regards to Person and then cast the instances to Film objects in the if statement (if the user wants a list of films), but found that this type of casting requires that the classes have an i开发者_开发技巧nheritance relationship, and that is not suitable for these classes.

This is my first post, so if it's too trivial, please advise me.


Just move the variable declarations outside of the if statement blocks? That would include ArrayList utvalg (which is the only one you seem to be using) and any others you need afterwards.


Ultimately, if you want a single declared variable (in this case, arr) to reference collections of two different types, those types have to share a superclass or interface. Of course everything shares Object as a superclass, so you could type it as a Object[], but that's pretty weak.

Often what I see done is to use an interface to tag the relevant classes that might be handled by the shared code. So say that Person and Film each implement an interface FindableByString. Then your find method can do some simple logic based on which type of input it got, and call a generic method to do the bulk of the work:

<T extends FindableByString> ArrayList<T> findInMap( String kode, Map<String,T> map) {

  Iterator <String> it = map.keySet().iterator();
  ArrayList<T> utvalg = new ArrayList<T>();

  while (it.hasNext()) {

    String nokkel = it.next();
    String subnokkel = nokkel.substring(0, kode.trim().length());

    if (subnokkel.equals(kode)) {

      T obj = map.get(nokkel);
      utvalg.add(obj);

    } else {
    // do nothing
    }
  }

  return utvalg; 
}

void find(String kode) {

  HashMap<String,? extends FindableByString> map;
  ArrayList<? extends FindableByString> list;
  FindableByString[] arr;

  if (Character.isLowerCase(kode.charAt(0))) {

    map = personer;
    arr = new Person[1];

  } else {

    map = filmer;
    arr = new Film[1];

  }

 list = findInMap( kode, map );
 arr = list.toArray( arr );

}
0

精彩评论

暂无评论...
验证码 换一张
取 消