Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TreeMap question

Status
Not open for further replies.

javaHopeful

Programmer
Joined
Jan 30, 2003
Messages
1
Location
US
I have perused this site frequently for advice. I am currently trying to learn java data structures. I hope someone can look at this and tell me what I'm doing wrong. When I try to compile the code I receive substantial errors, when I fix 6 I get 10 more. Currently I am receiving some of the following errors.
When I try to pass my treemap to my methods I get incompatible type, then why I try to cast it as an object it tells me explicit cast needed. How can I pass my treemap to my methods?

I'm trying to figure this problem.

Code:
public class Employee 
{
    // Fields
    private String Name;
    private String ID;
    private double balance;


   // Three-parameter constructor
    public Employee(String n, String id, double bal)
    {		Name=n;
		ID=id;
		balance=bal;
	}
    
    public void setCustomerName(String s)
    {
		Name = s;
	}


	public void setID(String t)
	{
		ID = t;
	}


	public void setBalance(double bal)
	{
		balance = bal;
    	 }

   	 public String getName()
   	 {
      	  return Name;
    	}

   	 public String getID()
  	{
        return ID;
   	 }



    public double getBalance()
    {
        return balance;
    }

	

    public void printBalance()
    { System.out.println("\nBalance: " + balance );  }

    public String toString()
    {
		return "\nEmployee: " + Name + "  ID: " + ID + "  Balance: " + balance;
    }
	

}// end class


   import java.util.*;

public class EmployeeMap 
{	
	String getAcct;
	String remAcct;
	
	public static void main(String[]args)
{
	Object[] accts=new Object [6];//Create array of emp objects
	accts[0]=new Employee("Smith","1",1000);
	accts[1]=new Employee("Walters","2",3000);
	accts[2]=new Employee("Martin","3",6000);
	accts[3]=new Employee("Mann","4",8000);
	accts[4]=new Employee("Jenkins","5",7000);
	accts[5]=new Employee("Williams","6",9000);
	
	Map account=new TreeMap();//create treemap

	
		for(int i=0; i<accts.length; i++)//add  objects to treemap
		account.put(((Employee)accts[i]).getID(),accts[i]);


	Set acctSize=account.entrySet();

	System.out.println(&quot;Object \t\t\tkey\t\tvalue&quot;);

	for(Iterator j=acctSize.iterator(); j.hasNext();)//print objects from treemap
	{
	Map.Entry me=(Map.Entry)j.next();
	Object objKey=me.getKey();
	Object objValue=me.getValue();
	System.out.print(me +&quot;\t&quot;);
	System.out.print(objKey+&quot;\t&quot;);
	System.out.println(objValue);
	}
		findObj((Object)accts);
		removeObj((Object)accts);


}	

	

	public void printMap(String acct,Map m) //demonstrate  printMap method
	{
	Set entries=m.entrySet();
	Iterator itr=entries.iterator();
	
	System.out.println(&quot;Object \t\t\tkey\t\tvalue&quot;);

		while(itr.hasNext())
		{
		Map.Entry thisPair=(Map.Entry) itr.next();
		System.out.print(thisPair.getKey() + &quot;:&quot;);
		System.out.println(thisPair.getValue());
		}
	}
	
 	public void findObj (Map m)//Accept user input for map key and return values associated with the key
	{
		getAcct=Console.readString(&quot;Enter id to find values for:&quot;);
		Object o=accts.get((Object)getAcct);
			if(o==null)
		  	{	System.out.println(&quot;Id not found enter new number&quot;);		
				findObj();
			}
			else
				System.out.println(&quot;Values for &quot;+getAcct+&quot;are \n&quot;+o.getValue());
			
	
	}
	


	public void removeObj (Map m)//Accept user input for map key and remove values associated with the key
	{
		remAcct=Console.readString(&quot;Enter id to find values for:&quot;);
		Object o=accts.remove((Object)remAcct);
			if(o==null)
		  	{	System.out.println(&quot;Id not found enter new number&quot;);		
				removeObj();
			}
			else
				System.out.println(&quot;Values &quot;+remAcct+&quot;are \n&quot;+o.getValue()+&quot; will be removed.&quot;);
			
	
	}
	
	



}
 
There are still a lot of errors in your code!!

The main problem is that printMap, findObj and removeObj are non-static functions, those functions can only be applied on objects of the class EmployeeMap, while there arent any EmployeeMaps constructed.
I recommend a structure like this:
(there's also no need to make String getAcct and String remAcct members of EmployeeMap)

class EmployeeMap
{
Map account;

EmployeeMap()
{
account=new TreeMap();
}

public static void main(String[]args)
{
EmployeeMap mymap=new EmployeeMap();
mymap.findObj();
//....
}

public void printMap() {
//....
}

public void findObj () {
// ....
Object o=account.get((Object)getAcct);
// ....
}



public void removeObj ()
{
// ....
Object o=account.remove((Object)remAcct);
// ....
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top