javaHopeful
Programmer
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.
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("Object \t\t\tkey\t\tvalue");
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 +"\t");
System.out.print(objKey+"\t");
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("Object \t\t\tkey\t\tvalue");
while(itr.hasNext())
{
Map.Entry thisPair=(Map.Entry) itr.next();
System.out.print(thisPair.getKey() + ":");
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("Enter id to find values for:");
Object o=accts.get((Object)getAcct);
if(o==null)
{ System.out.println("Id not found enter new number");
findObj();
}
else
System.out.println("Values for "+getAcct+"are \n"+o.getValue());
}
public void removeObj (Map m)//Accept user input for map key and remove values associated with the key
{
remAcct=Console.readString("Enter id to find values for:");
Object o=accts.remove((Object)remAcct);
if(o==null)
{ System.out.println("Id not found enter new number");
removeObj();
}
else
System.out.println("Values "+remAcct+"are \n"+o.getValue()+" will be removed.");
}
}