Objective Multiple Dictionaries for database 1 points Use UA

Objective: Multiple Dictionaries for database (1 points)

Use UALdictionary.java to create two Dictionaries and allows user to retrieve the employee record by Key of either dictionary. The employee.txt contains data records of all employee.

Dictionary one will use SSN field which is the fourth field of the data field on the record as Key.

Dictionary two will use Name field which is the first field of the data field on the record as Key.

Sam,123 Main Line,555-0469,123-45-6789,2423.07
Carla,456 Off Line,555-0101,987-65-4321,1246.15
Woody,789 Off Rocker,555-0000,010-20-3040,1169.23
Diane,678 Fifth Ave,555-0690,958-47-3625,10.55
Norm,987 Suds Blvd.,555-8374,456-78-9000,11.2
Cliff,321 Duds Lane,555-7282,621-12-1234,12.0
Tom,2631 Main Blv,423-1155,524-332-6654,10.0
Kristen,443 Norfolk str,765-9457,010-332-1111,20.0

Solution

//Tested on Eclipse in ubuntu,Linux

/*******************UALdictionary.java****************/


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class UALdictionary {

   Scanner input = new Scanner(System.in);
   Map<String, Employee> dictionary1 = new HashMap<>();
   Map<String, Employee> dictionary2 = new HashMap<>();

   public void readFile() {

       /* Please provide the file path */
       File file = new File(\"/home/anshu/chegg/AnshuChegg/src/employee.txt\");
       try {
           /* FileReader and BufferedReader for reading file */
           FileReader fr = new FileReader(file);
           BufferedReader br = new BufferedReader(fr);

           String line = \"\";
           /*
           * Reading file line by line using buffered reader as we read one
           * line we are splitting line by comm and setting values into emp
           * object
           */
           while ((line = br.readLine()) != null) {
               Employee emp = new Employee();
               String empData[] = line.split(\",\");
               emp.setName(empData[0]);
               emp.setAddress(empData[1]);
               emp.setPhoneNumber(empData[2]);
               emp.setSsn(empData[3]);
               emp.setSalary(Double.parseDouble(empData[4]));

               /*
               * Inserting data into first dictionary and second dictionary in
               * first dictionary key is ssn in second dictionary key is name
               */
               dictionary1.put(empData[3], emp);
               dictionary2.put(empData[0], emp);

           }

       } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }

   }

   /*
   * This method is used for searching employee data into both dictionary if
   * record found then it will print employee data otherwise it will print No
   * record found
   */
   public void printData() {

       System.out.println(\"Please Enter name or ssn for searching employee data\");
       String key = input.nextLine();
       if (dictionary1.get(key) != null) {
           Employee emp = dictionary1.get(key);
           System.out.println(\"Employee Name: \" + emp.getName() + \"\ Employee Address: \" + emp.getAddress()
                   + \"\ Employee Phone Number: \" + emp.getPhoneNumber() + \"\ Employee SSN: \" + emp.getSsn()
                   + \"\ Employee Salary: \" + emp.getSalary());
       } else if (dictionary2.get(key) != null) {
           Employee emp = dictionary2.get(key);
           System.out.println(\"Employee Name: \" + emp.getName() + \"\ Employee Address: \" + emp.getAddress()
                   + \"\ Employee Phone Number: \" + emp.getPhoneNumber() + \"\ Employee SSN: \" + emp.getSsn()
                   + \"\ Employee Salary: \" + emp.getSalary());
       } else {
           System.out.println(\"Employee does not Exist\");
       }

   }

   public static void main(String args[]) {

       /* Creating object of UALDictionary class */
       UALdictionary uaLdictionary = new UALdictionary();
       /* Calling method for reading file */
       uaLdictionary.readFile();
       /* Calling PrintData method for printing employee data */
       uaLdictionary.printData();

   }

}

/******************Employee.java*************/

public class Employee {
   /* Employee Record variable declarations */
   private String name;
   private String address;
   private String phoneNumber;
   private String ssn;
   private double salary;

   /*
   * Getter and setter method of employees for setting employee information
   * and retrieving employee information
   */
   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public String getAddress() {
       return address;
   }

   public void setAddress(String address) {
       this.address = address;
   }

   public String getPhoneNumber() {
       return phoneNumber;
   }

   public void setPhoneNumber(String phoneNumber) {
       this.phoneNumber = phoneNumber;
   }

   public String getSsn() {
       return ssn;
   }

   public void setSsn(String ssn) {
       this.ssn = ssn;
   }

   public double getSalary() {
       return salary;
   }

   public void setSalary(double salary) {
       this.salary = salary;
   }

}


/*******************employee.txt************/

Sam,123 Main Line,555-0469,123-45-6789,2423.07
Carla,456 Off Line,555-0101,987-65-4321,1246.15
Woody,789 Off Rocker,555-0000,010-20-3040,1169.23
Diane,678 Fifth Ave,555-0690,958-47-3625,10.55
Norm,987 Suds Blvd.,555-8374,456-78-9000,11.2
Cliff,321 Duds Lane,555-7282,621-12-1234,12.0
Tom,2631 Main Blv,423-1155,524-332-6654,10.0
Kristen,443 Norfolk str,765-9457,010-332-1111,20.0

/**************output*******************/

search by name

Please Enter name or ssn for searching employee data
Sam
Employee Name: Sam
Employee Address: 123 Main Line
Employee Phone Number: 555-0469
Employee SSN: 123-45-6789
Employee Salary: 2423.07

orsearch by ssn

Please Enter name or ssn for searching employee data
987-65-4321
Employee Name: Carla
Employee Address: 456 Off Line
Employee Phone Number: 555-0101
Employee SSN: 987-65-4321
Employee Salary: 1246.15

Thanks a lot

Objective: Multiple Dictionaries for database (1 points) Use UALdictionary.java to create two Dictionaries and allows user to retrieve the employee record by Ke
Objective: Multiple Dictionaries for database (1 points) Use UALdictionary.java to create two Dictionaries and allows user to retrieve the employee record by Ke
Objective: Multiple Dictionaries for database (1 points) Use UALdictionary.java to create two Dictionaries and allows user to retrieve the employee record by Ke
Objective: Multiple Dictionaries for database (1 points) Use UALdictionary.java to create two Dictionaries and allows user to retrieve the employee record by Ke

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site