New to HashMap with objects

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • otorojava
    New Member
    • Jan 2008
    • 8

    #1

    New to HashMap with objects

    Hi,

    I'm trying to learn HashMaps using objects. The string currently returns null or shows an @923e30 error.

    Please advise. Account class and HashMapDemo class

    Code:
    public class Account{
    	
    		String customerAccount;
    		
    	public Account(String customerAccount){ //Constructor
    		this.customerAccount = customerAccount;
    		
    	}
    	
    	public String toString(String customerAccount) {
    
            return ("Customer :" + customerAccount);
    	}
    }
    Code:
    import java.util.*;
    
    public class HashMapDemo {
    
      public static void main(String[] args) {
    
        HashMap hm = new HashMap();
        
    	Account anAcct1 = new Account("123");
    	Account anAcct2 = new Account("456");
    
    	hm.put("100", anAcct1);
    	hm.put("200", anAcct2);
    	
        Set set = hm.entrySet();
    
        Iterator i = set.iterator();
    
        while(i.hasNext()){
          Map.Entry me = (Map.Entry)i.next();
          System.out.println(me.getKey() + " : " + me.getValue() );
        }
    
         System.out.println("Account number : " + hm.get(anAcct1));
        
         System.out.println(hm.get("100").toString()); // Function toString()
    
      }
    }
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Your Accounts toString method is wrong. To successfully override it, Object's toString method takes no arguments:

    [CODE=Java]public class Account{
    private String customerAccount ;

    public Account(String customerAccount ){
    this.customerAc count = customerAccount ;
    }

    public String toString() {
    return customerAccount ;
    }
    }[/CODE]

    Then your demo is easy to do. Note that you can just dump a map by passing it to println:

    [CODE=Java]import java.util.*;

    public class HashMapDemo {
    public static void main(String[] args) {
    Map<String, Account> hm = new HashMap<String, Account>();
    hm.put("100", new Account("123")) ;
    hm.put("200", new Account("456")) ;
    System.out.prin tln(hm);
    System.out.prin tln(hm.get("100 "));
    }
    }[/CODE]

    Collections work best if you use the power of generics. Take the tutorial:

    This collections Java tutorial describes interfaces, implementations, and algorithms in the Java Collections framework

    Comment

    • otorojava
      New Member
      • Jan 2008
      • 8

      #3
      Originally posted by BigDaddyLH
      Your Accounts toString method is wrong. To successfully override it, Object's toString method takes no arguments:

      [CODE=Java]public class Account{
      private String customerAccount ;

      public Account(String customerAccount ){
      this.customerAc count = customerAccount ;
      }

      public String toString() {
      return customerAccount ;
      }
      }[/CODE]

      Then your demo is easy to do. Note that you can just dump a map by passing it to println:

      [CODE=Java]import java.util.*;

      public class HashMapDemo {
      public static void main(String[] args) {
      Map<String, Account> hm = new HashMap<String, Account>();
      hm.put("100", new Account("123")) ;
      hm.put("200", new Account("456")) ;
      System.out.prin tln(hm);
      System.out.prin tln(hm.get("100 "));
      }
      }[/CODE]

      Collections work best if you use the power of generics. Take the tutorial:

      http://java.sun.com/docs/books/tutor...ons/index.html
      Thank you BigDaddyLH.

      How can I work with these arrays? Say I want to add 100 to Jane's account

      Account class
      Code:
              //customerBalance is double
      	public String toString() {
             // return ("Customer :" + customerAccount);
      		 String output = customerAccount + " " +  customerName + " " + customerBalance;
      		 
      		 return output;
      	}
      HashMapDemo class:

      Code:
      	
      
              Account anAcct1 = new Account("12-12-12", "Joe", 600);
      	Account anAcct2 = new Account("45-45-45", "Jane", 500);
      
      	hm.put("100", anAcct1);
      	hm.put("200", anAcct2);
      	
          Set set = hm.entrySet();




      I tried with hm.put("100", (accountBalance +500)); but no luck.

      Any help is greatly appreciated

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Why not define a "deposit" method in class Account? Note this has nothing to do with maps.

        Comment

        • otorojava
          New Member
          • Jan 2008
          • 8

          #5
          Originally posted by BigDaddyLH
          Why not define a "deposit" method in class Account? Note this has nothing to do with maps.
          something like this?

          Account class:
          Code:
          AccountClass:
          	public double doDeposit(){
          
          		Double output = customerBalance + newDeposit;
          		
          		return output;
          	}
          But, how do I get the existing balance from the HashMapDemo class?

          Code:
          HashMapDemo class:
          
          HashMap hm = new HashMap();
          Account anAcct1 = new Account("12-12-12", "Joe", 600);
          hm.put("100", anAcct1);
          Not sure how I can extract 600 from anAcct1. Thank for your help. Am very confused.

          Comment

          • otorojava
            New Member
            • Jan 2008
            • 8

            #6
            Program is to do simple arithmetic, and write/read from file later on.

            So far this is what I have:

            Code:
            //Account class
            
            public class Account{
            	
            		String customerAccount;
            		String customerName;
            		double customerBalance;
            
            	public Account(String customerAccount, String customerName,double customerBalance){ //Constructor
            		this.customerAccount = customerAccount;
            		this.customerName = customerName;	
            		this.customerBalance = customerBalance;
            	}
            	
            	public String toString() {
                   
            String output = customerAccount + " " +  customerName + " " + customerBalance;
            		 
            		 return output;
            	}
            
                
            	public double doDeposit(double newDeposit){
                    //Purpose is to get the existing customer balance and add the new amount through input from joptionpane
            		Double output = customerBalance + newDeposit;
            		
            		return output;
            	}
            Code:
            //HashMapDemo class
            
            import java.util.*;
            
            public class HashMapDemo {
            	
            	int accountBalance = 100;
            	
              public static void main(String[] args) {
            
                HashMap hm = new HashMap();
                
            	Account anAcct1 = new Account("12-12-12", "Joe", 600);
            	Account anAcct2 = new Account("45-45-45", "Jane", 500);
            
                    //Account methods 
                    Account methodAccount = new Account();
            	
            
            	hm.put("100", anAcct1);
            	hm.put("200", anAcct2);
            	
                 System.out.println("Account number : " + hm);  
                 
                 System.out.println("\nMake a new dposit to account");
            
                 [COLOR="Red"]//This is where I am stuck[/COLOR]
                 System.out.println(hm.get("100")+methodAccount.doDeposit(300)); 
            
                 // Function toString()
                 System.out.println("Account number : " + hm);
            
              }
            }
            Thanks in advance

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              1. Note that your deposit method is wrong. When you deposit money in an account in real life, doesn't the balance change?

              2. If you want to be able to get the balance of an account, doesn't that suggest to you that class Account needs a getBalance method?

              Comment

              Working...