Java Bank示例

时间:2012-07-09 06:40:26

标签: java hashmap

package nimit.bank;

import java.util.HashMap;


public class MDB {

public final static int entityCustomer=1;
public static HashMap<Object, Object> hmapCustomer = new HashMap<Object, Object>();
public final static int entityAccount=2;
public static HashMap<Object, Object> hmapAccount = new HashMap<Object, Object>();
public final static int entityBank=3;
public static HashMap<Object, Object> hmapBank = new HashMap<Object, Object>();

public static Object get(int entity,Object key) {
Object obj=null;
switch(entity){
case entityCustomer: obj=hmapCustomer.get(key);
break;
case entityAccount: obj=hmapAccount.get(key);
break;
case entityBank: obj=hmapBank.get(key);
break;
}
return obj   
}   
public static void put(int entity, Object key,Object obj ){       
switch(entity){
case entityCustomer: hmapCustomer.put(key,obj);
break;
case entityAccount: hmapAccount.put(key, obj);
break;
case entityBank: hmapBank.put(key,obj);
break;
}
  }
}

这是一个银行示例,我使用散列图来存储数据。我已经创建了一个Customer.java类,如下所示。

package nimit.bank;

public class Customer {
    private String name;
    private String address;
    private long phone_no;

    public String getName() {
        return name;
    }

    public String getAddress() {
        return address;
    }

    public long getPhone_No() {
        return phone_no;
    }

    public Customer(String _Name, String _Address, long _phone) {
        name = _Name;
        address = _Address;
        phone_no = _phone;
    }
}

我创建了一个BankController.java类来输入数据并检索它。

package nimit.bank;

public class BankController {

    public static void main(String[] args) {
        Customer C = new Customer("nimit", "Jane Street", 26711610);
        Object key = 54321;
        MDB.put(MDB.entityCustomer, key, C);

        System.out.println(MDB.get(MDB.entityCustomer, key));
    }
}

我的疑问是, System.out.println(MDB.get(MDB.entityCustomer,key)); 给出null作为输出。我无法理解。我将密钥作为对象类型传递。

0 个答案:

没有答案