Results 1 to 4 of 4
  1. #1
    lookinginfo is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Post Need some help with this code using hashtable

    Hello,

    have created three classes; Employee, EmployeeTable, ReadInput

    i have attached all three classes. (for some reason i can't attached files)

    Question on add() method. when you run the program and provide the following information it will still add the record. it didn't say INVALIDINPUT.

    EmpClient.main(): client started
    [L]ist| [A]dd | [E]dit | [D]elete | [Q]uit: a
    Adding a new employee...
    Enter a string value for empNo:
    Enter a string value for empName: George Bush
    Enter a float value for salary: 80000
    double empSal = 80000.0
    Employee added:
    , George Bush, 80000.0
    ADDED
    [L]ist| [A]dd | [E]dit | [D]elete | [Q]uit:

    Question on update() method. so ReadInput class is taking input from user emp no. after that, it will search the hashtable and if found then ask for new values. i am not able to achieve that.

    [L]ist| [A]dd | [E]dit | [D]elete | [Q]uit: e
    Editing an employee...
    Enter a string value for empNo: emp02
    No record exists for emp02

    [L]ist| [A]dd | [E]dit | [D]elete | [Q]uit: e
    Editing an employee...
    Enter a string value for empNo: emp-02
    Enter a new string value for empName: Brad Linsle
    Enter a new float value for salary: 55000
    Employee updated:
    emp-02, Brad Linsley, 55000.0

    =========ReadInput Class=======
    Java Code:
    import java.io.IOException;
    import java.util.Scanner;
    
    public class ReadInput {
    
        static Scanner keyboard = new Scanner(System.in);
        
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		
    		System.out.println("EmpClient.main(): client started");
    		EmployeeTable h = new EmployeeTable();
    		int returnValue=-1;
    		while (true) {
    			String action = getInput("[L]ist| [A]dd | [E]dit | [D]elete | [Q]uit: ");
    			if (action.length() == 0 || action.toUpperCase().charAt(0) == 'Q') {
    				System.out.println("EmpClient.main(): client exited");
    				break;
    			}
    			switch (action.toUpperCase().charAt(0)) {
    				case 'L':
    					System.out.println("Listing all employees...");
    					System.out.println(h.size() + " employee(s) found:");
    					System.out.println(h.toString());
    					
    					break;
    				case 'A':
    					//First take three input values from console
    					System.out.println("Adding a new employee...");
    					String empNum= getInput ("Enter a string value for empNo: ");
    					String empName= getInput ("Enter a string value for empName: ");
    					String SalaryStr = getInput ("Enter a float value for salary: ");
    								
    						//double empSal = new Double(SalaryStr);
    						try
    					    {
    					      double empSal = Double.valueOf(SalaryStr.trim()).doubleValue();
    					      System.out.println("double empSal = " + empSal);
    					      Employee record = new Employee(empNum, empName, empSal);
    							System.out.println("Employee added: \n" + record.toString());				
    							returnValue=h.add(record);
    					    }
    					    catch (NumberFormatException nfe)
    					    {
    					      System.out.println("NumberFormatException: " + nfe.getMessage());
    					    }
    						
    						if (returnValue == 0) 
    							System.out.println("INVALIDINPUT");
    						else 
    							
    							if (returnValue == 1) 
    								System.out.println("DUPLICATEKEYERROR");
    							else 
    								if (returnValue == 2) 
    									System.out.println("ADDED");
    								
    																	
    					break;
    				case 'E':
    					System.out.println("Editing an employee...");
    					String employeeNum= getInput ("Enter a string value for empNo: ");
    					String employeeName= getInput ("Enter a new string value for empName: ");
    					String empSalary = getInput ("Enter a new float value for salary: ");
    										
    					//returnValue=h.update(record);
    						if (returnValue == 4) 
    							System.out.println("UPDATED");
    						else 
    							if (returnValue == 5) 
    								System.out.println("RECORDNOTFOUNDERROR");
    			
    					break;
    				case 'D':
    					System.out.println("Deleting an employee...");
    					String empNumb= getInput ("Enter a string value for empNo: ");
    					
    						if (returnValue == 3) 
    							System.out.println("DELETED");
    						
    						else 
    							if (returnValue == 5) 
    								System.out.println("RECORDNOTFOUNDERROR");
    					break;
    				default:
    					System.out.println("Invalid command. Try again.\n");
    			}//end switch
    		}//end while		
    			
    	}
    
    	/**
    	 * After printing a prompt, reads a line of text.
    	 */
    	private static String getInput(String prompt) {
    		System.out.print(prompt);
    		System.out.flush();
    		
    		String input = keyboard.nextLine();
    		
    		return input;
    	}
    	
    }
    ========Employee Class======
    Java Code:
    import java.util.Enumeration;
    
    public class Employee {
    
    	//attributes. Reason for private is that we want to hide from outside world.
    	//attributes only accessible via getter and setter methods
    	private String empNo;
    	private String empName;
    	private double empSal;
    	
    	//constructor
    	public Employee(String empNo, String empName, double empSal) {
    		//super();
    		this.empNo = empNo;
    		this.empName = empName;
    		this.empSal = empSal;
    	}
    
    	//getter and setter methods
    	
    	public String getEmpNo() {
    		return empNo;
    	}
    
    	public void setEmpNo(String empNo) {
    		this.empNo = empNo;
    	}
    
    	public String getEmpName() {
    		return empName;
    	}
    
    	public void setEmpName(String empName) {
    		this.empName = empName;
    	}
    
    	public double getEmpSal() {
    		return empSal;
    	}
    
    	public void setEmpSal(double empSal) {
    		this.empSal = empSal;
    	}
    
    	
    	//Print out all attributes
    
    	@Override
    	public String toString() {
    		return empNo + ", " + empName + ", " + empSal;
    	}
    
    		
    	
    	
    }
    ====EmployeeTable Class=====
    Java Code:
    import java.util.*;
    
    public class EmployeeTable {
    	
    	//attributes
    	private String companyName; // to store company name 
    	private Hashtable table; // to store all the employee records
    	
    	//Constants that the add(), update(), and delete() methods will
    	  //return to show the various the results of the operations
    	  public  static  int INVALIDINPUT=0, DUPLICATEKEYERROR=1, ADDED=2,
    	  	DELETED=3, UPDATED=4, RECORDNOTFOUNDERROR=5;
    
    	  public EmployeeTable() {
    	  		table=new Hashtable();
    	  }
    
    	//Implement the following member methods:
    
    	//Add a record to this table
    	//If row is null or empty, return INVALIDINPUT
    	//If there already exists a record in the table with the same empNo
    	//as the one passed to this method, return DUPLICATEKEYERROR,
    	//otherwise, add it and return ADDED
    	  public int add(Employee record) {
    			//if(record.getEmpNo().toString()=="")
    		//if(record.getEmpNo()=="")
    		  if(record==null)
    				return INVALIDINPUT;
    			else
    			{
    				String key = record.getEmpNo();
    
    				Object obj = table.get(key);
    				
    				if (obj!=null)
    					return DUPLICATEKEYERROR;
    							
    				//if (table.contains(key))
    					//return DUPLICATEKEYERROR;
    				else
    				{
    					table.put(record.getEmpNo(), record);
    					return ADDED;
    				}
    			}
    	  }
    
    	//Update a record in the table
    	//If row is null or empty, return INVALIDINPUT
    	//If there does not exist a record in the table with the same empNo
    	//as the one passed to this method, return RECORDNOTFOUNDERROR,
    	//otherwise, update the record and return UPDATED
    	  public int update(Employee record)
    	  {
    		  if(record==null)
    			return INVALIDINPUT;
    		else
    		{
    			String key = record.getEmpNo();
    
    			Object obj = table.get(key);
    			
    			if (obj!=key)
    				return RECORDNOTFOUNDERROR;
    						
    			else
    			{
    				table.put(record.getEmpNo(), record);
    				return UPDATED;
    			}
    		}
    }
    
    	//Delete a record in the table
    	//If key is null or empty, return INVALIDINPUT
    	//If there does not exist a record in the table with the same empNo
    	//as the key passed to this method, return RECORDNOTFOUNDERROR,
    	//otherwise, delete the record and return DELETED
    	  //public int delete(String key)
    	  //{
    		  //...
    	 // }
    
    
    	//Get a row with the given key
    	//If this key does not exist in the table return null,
    	//otherwise return its corresponding value
    	  //public Employee getRow(String key)
    	  //{
    		  //...
    	  //}
    
    
    	//Overriding the toString() method of the Object class so that
    	//this table can be printed as follows:
    	//emp-03, Brad Linsley, 45000.0
    	//emp-02, Greg Harper, 50000.0
    	//emp-01, Ted Li, 80000.0
    	  public String toString()
    		{
    		  	StringBuffer sb = new StringBuffer();
    			Enumeration e = table.keys();
    			while (e.hasMoreElements())
    			{
    				String key = (String)e.nextElement();
    				Employee obj = (Employee)table.get(key);
    				sb.append(obj);
    				sb.append("\n");
    			}
    			return sb.toString();
    		}
    
    
    	//Return the number of records in this table
    	  public int size()
    	  {
    	  return table.size();  
    	  }
    	}
    Last edited by lookinginfo; 02-19-2013 at 07:18 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Need some help with this code using hashtable

    Please wrap your code in [code] tags [/code] to retain the formatting.
    I, for one, will not bother with unformatted code.

    And can you narrow down your problem?
    People won't run your code in general...
    Please do not ask for code as refusal often offends.

  3. #3
    lookinginfo is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: Need some help with this code using hashtable

    thanks for the suggestion. can someone pls help me on this

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Need some help with this code using hashtable

    For the ADD stuff, where are you validating the input?
    Please do not ask for code as refusal often offends.

Similar Threads

  1. Hashtable question. Getting info from a hashtable
    By sindrem in forum New To Java
    Replies: 4
    Last Post: 01-19-2012, 05:00 PM
  2. hashtable
    By kmm1977 in forum New To Java
    Replies: 4
    Last Post: 01-20-2011, 04:24 PM
  3. hashtable
    By vijayabaskar in forum Java Servlet
    Replies: 0
    Last Post: 04-06-2009, 08:20 AM
  4. Hashtable
    By angelicsign in forum New To Java
    Replies: 6
    Last Post: 02-05-2009, 04:30 PM
  5. Hashtable example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-15-2008, 08:43 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •