2016-10-10 8 views
0

私の質問は、ハッシュマップ値オブジェクトを "アンラップ"するにはどうすればいいですか?値のHashMapオブジェクト

ハッシュマップの作成、保存、読み込みができます。私は人口を行っているし、給料を計算する従業員を選択しています。

Map<Integer, Employee> hm = new HashMap<>(); 

hID++; 
    System.out.println("Enter employee first name: "); 
    String nameF = sc.next(); 

    System.out.println("Enter employee last name"); 
    String nameL = sc.next(); 

    hourly = new HourlyEmployee(); 
    Employee hEmp = new Employee(hourly, nameF, nameL); 

    System.out.println(hEmp.getName()); 

    hm.put(hID, hEmp); 

/* 
save() 
exit 
run 
load() 
select employee, enter ID 
loops to find match 
*/ 

Set<Map.Entry<Integer, Employee>> set = hm.entrySet(); 

    do 
    { 
    System.out.println("Enter the empoyee ID"); 
    int id = sc.nextInt(); 

     if (id >= 1000 && id < 1999) 
     { 
      System.out.println("***** HOURLY *****"); 

      for (Map.Entry<Integer, Employee> entry : set) 
     { 
     if (entry.getKey().equals(id)) 
     { 
      // this is where I run into the issues. all I get 
      // with the value is the hash location. I can't 
      // figure out how to unwrap into the hourly class 
      // as the object, then edit the values then wrap again. 
      System.out.println(entry.getValue()); 
      // I understand this is not how you get what I 
      // need. But this does give me: 
      // 
      // "[email protected]0"  

     } 

} 

コードコメントを参照してください。

+0

ません奇妙な「ラッピング」が値に起こっています。 *どんな*従業員は、あなたがtoStringメソッドを書いていないので、HashMapの値のように印刷します。あなたと同じようにオブジェクトを使用してください。 – user2357112

+0

私が言うことは、HourlyEmployeeクラスがEmployeeの一部であり、継承がないことです。 HourlyEmployeeメソッドにアクセスできるようにしたい – Chris

+0

HashMapが関与していない場合は、何をしても構いません。 (また、あなたのクラスの構成は意味をなさない。) – user2357112

答えて

0

私は解決策が見つかりました:*部分コード

public void selectEmployee() { 
    Set<Map.Entry<Integer, Employee>> set = hm.entrySet(); 

    do { 
     System.out.println("Enter the empoyee ID"); 

     int id = sc.nextInt(); 

     if (id >= 1000 && id < 1999) { 
      System.out.println("***** HOURLY *****"); 

      for (Map.Entry<Integer, Employee> entry : set) { 
       if (entry.getKey().equals(id)) { 

        int key = entry.getKey(); 
        Employee hEmp = entry.getValue(); 

        hEmp.getHourlyEmp().getGrossPay(); 

       } 

      } 
+0

私はこれを知っている必要があります持っています。私は思考以上のものでした。 – Chris

関連する問題