Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that uses one of the Map classes to implement a contact list. Co

ID: 3920385 • Letter: W

Question

Write a program that uses one of the Map classes to implement a contact list. Contact information should include frst name, last name, company name, phone number and an email address. You can assume a combination of frst and last name are unique. The list should be stored on your computer’s fle system. The program should prompt the user for the name of the fle here the information is stored, and allo the user to add a contact from the list, delete a contact from the list, and display the entire contact list (all feldss in a reasonably readable format in last name/frst name order.

A print queue is a list of documents, called jobs, aiting to be printed. Each job is assigned an integer job number and an estimated print time, in seconds. If a job arrives and the printer is currently printing another job, the job aits in the print queue. This type of aiting line is called a frsttintfrsttout (FIFOs or frsttcometfrsttserve (FCF)s aiting line model or queue. The Java LinkedList class is a pretty good class for implementing a aiting line model like this in code.

Write a program that uses a LinkedList object to store a list of print jobs in a FIFO queue. We ill assume that each job has a unique job number that is assigned sequentially, and that the print time is randomly distributed bet een 10 seconds and 1000 seconds. These
t o data elements should be defned as part of a Job class. The program should prompt the user to either add a job to the queue or to quit. When a user adds a job, the program assigns a job number and randomly generates a print time for the job. When the user quits, the program ill display the jobs (i.e., the job id and print times in the queue, in the order in hich they ere added.

For this exercise, you can use the java.util.Random class to generate random numbers, as follows:

Random rnGenerator = new Random( rnSeed ); // creates a Random object

printTime = rnGenerator.nextInt(991) + 10; // next int in range 10-1000

The frst statement above instantiates a Random object calledrnGenerator. The constructor argument rnSeed is called a random number seed. It is of type long, and is used to help ensure that the same sequence of random numbers is generated each time the program is run... hich is helpful for test purposes. The nextInt()method returns a random integer in the range 0 to 999.

Explanation / Answer

//ContactBook Program

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.util.HashMap;

import java.util.Map;

import java.util.Scanner;

import java.util.TreeMap;

public class ContactMap

{

     public static void main(String args[]) throws IOException

     {

          Scanner input=new Scanner(System.in);

          //Create a TreeMap , to store contact object with last name as the key

          Map<String, Contact> contactsMap = new TreeMap<String, Contact>();

          //Prompt the user to enter the contacts file name

          System.out.print("Enter the contacts file name: ");

          //Read the file name

          String contactsFile=input.nextLine();

          //Open the file

          BufferedReader in = new BufferedReader(new FileReader(contactsFile));

          String line = "";

          //Read all contacts into tree map

        while ((line = in.readLine()) != null)

        {

            String columns[] = line.split(" ");

            contactsMap.put(columns[1].trim(), new Contact(columns[0].trim(),columns[1].trim(),columns[2].trim(),columns[3].trim()));

        }

        //Run the menu

        while(true)

        {

        int choice;

        //Print the menu

        System.out.println("1. Add a contact to the list");

        System.out.println("2. Delete a contact from the list");

        System.out.println("3. Display the entire contact list.");

        System.out.println("4.Exit");

        System.out.print("Enter your choice: ");

        //Read the choice

        choice=input.nextInt();

        switch(choice)

        {

              case 1:

                   //Read new contact information

                   System.out.print("Enter the first name: ");

                   input.nextLine();

                   String fName=input.nextLine();

                  System.out.print("Enter the last name: ");

                   String lName=input.nextLine();

                   System.out.print("Enter the phone number: ");

                   String phoneNumber=input.nextLine();

                   System.out.print("Enter the email-id: ");

                   String eId=input.nextLine();

                   //Call the addToList method

                   addToList(contactsMap,new Contact(fName,lName,phoneNumber,eId));

                        break;

              case 2:

                   //Prompt the user to enter the last name

                   System.out.print("Enter the last name to remove from the contacts: ");

                   input.nextLine();

                   //Read the last name

                   String phNo= input.nextLine();

                   //Call the deleteFromList method

                   deleteFromList(contactsMap,phNo);

                        break;

              case 3:

                   //Call displayContacts to print all contacts

                   displyContacts(contactsMap);

                   input.nextLine();

                        break;

              case 4:

                   //Exit from the program

                   System.exit(0);

              default: System.out.println("Enter a valid choice");

        }

        }

     }

     //Adds the new Contact to the treemap.

     static void addToList(Map<String, Contact> contactsMap,Contact newContact )

     {

          contactsMap.put(newContact.getLName(),newContact);

     }

     //deletes a contact from the list by last name

     static void deleteFromList(Map<String, Contact> contactsMap,String pNo)

     {

          if(contactsMap.get(pNo)!=null)

          {

              System.out.println("Contact : "+contactsMap.get(pNo)+ " with phone number "+pNo+" is removed");

              contactsMap.remove(pNo);

          }

          else

              System.out.println("Contact does not exist with phone number "+pNo);

     }

     //Display all contacts

     static void displyContacts(Map<String, Contact> contactsMap)

     {

          for (String key : contactsMap.keySet())

          {

            System.out.println(contactsMap.get(key));

        }

     }

}
-------------------------------------------------------------------------------------------------------------------
//Job Program

public class Job {

   private int jobId;
   private int jobTime;

   //Default constructor
   public Job() {
    
   }

   //Parameterized constructor
   public Job(int jobId, int jobTime) {
       this.jobId = jobId;
       this.jobTime = jobTime;
   }

   public int getJobId() {
       return jobId;
   }

   public void setJobId(int jobId) {
       this.jobId = jobId;
   }

   public int getJobTime() {
       return jobTime;
   }

   public void setJobTime(int jobTime) {
       this.jobTime = jobTime;
   }

   @Override
   public String toString() {
       return "Job ID : "+jobId+ ", Job Time : "+jobTime;
   }

}


//Program name FIFO.java

import java.util.LinkedList;
import java.util.Random;
import java.util.Scanner;


public class FIFO {

   public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);
       Random rnGenerator = new Random();
       LinkedList<Job> jobs = new LinkedList<Job>();
       String jobInput;
    
       while(true) {
        
           System.out.println("Add a job id to the queue or enter quit to stop");
        
           jobInput = sc.next();
           if(jobInput.equalsIgnoreCase("quit")) {
               break;
           } else {
            
               int jobId = Integer.parseInt(jobInput);
               int jobTime = rnGenerator.nextInt( 1000 ) + 10;
            
               //Create a job object
               Job job = new Job(jobId,jobTime);
            
               //Add to job list
               jobs.add(job);
           }
       }
    
       //Display all the jobs
       System.out.println("All jobs present in queue are: ");
       System.out.println(jobs);
    
       //Processing jobs
       while(!jobs.isEmpty()) {
           System.out.println(" Job Processing started.");
           System.out.println(jobs.removeFirst());
           System.out.println("Job Processing completed.");
       }
    
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote