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

ina, MLAn Date Name Final Exam av Winter 2017 Write a program that defines a Pri

ID: 3833144 • Letter: I

Question

ina, MLAn Date Name Final Exam av Winter 2017 Write a program that defines a Print Job structure as follows: 1) An integer job ld 2) A string user name (maximum 25 characters) 2 3 An integer tray (tray will hold the tray number 1 for 8 by 11 paper, number for 8 by 11 paper, and number 3 for 8 paper) is normal, 150% is 4) An integer for paper size (this will hold a percentage: 100% 1.5 times the size) together, all the 5) A string for whether the pages are to be sorted (all the page 1's page 2's stacked (1, 2, 1, 2, 1, 2, etc.) 6) Acharacter for whether the documents should be stapled (Y or N 7) An integer coded for side (221 to be double sided, taking 2 pages and turning it into 1 page, front and back; 121 means to be printed double sided, from a double sided document: 111 means 1 side only to 1 side only or a normal copy) 8) An integer number of copies 9) An integer status (1 means not printed, 0 means printed) Your program will information from the file jobs.txt. Your program will read in an unknown number of jobs into an array (max of 10). The reading of the files may be done in main0, or you may create a function for this. Your program will utilize four functions: display Menu(): Will print the following menu and ask for the user's option. This function will return the choice to main. You must validate the user's entry with a loop. Zoooo ks Copy Menu Press 1 to print the next job Press 2 to see the jobs in the print queue Press 3 to see the job have finished printing Press 0 to quit Enter choice print Job0: Will print the job from the top of the list. This function wil turn the status from 1 (not printed) to 0 (printed) display)ob(): Will display all the jobs in the list still to be printed. finishedJobs0: will display all the jobs that have been printed.

Explanation / Answer

Below is your code in java:

PrintJob.java


public class PrintJob {
   private int jobId;
   private String userName;
   private int tray;
   private int paperSize;
   private String sortOrStack;
   private char isStapled;
   private int sided;
   private int noOfCopies;
   private int status; // 1 means not printed 0 means printed

   public PrintJob() {
       this.status = 1;
   }

   public int getJobId() {
       return jobId;
   }

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

   public String getUserName() {
       return userName;
   }

   public void setUserName(String userName) {
       this.userName = userName;
   }

   public int getTray() {
       return tray;
   }

   public void setTray(int tray) {
       this.tray = tray;
   }

   public int getPaperSize() {
       return paperSize;
   }

   public void setPaperSize(int paperSize) {
       this.paperSize = paperSize;
   }

   public String getSortOrStack() {
       return sortOrStack;
   }

   public void setSortOrStack(String sortOrStack) {
       this.sortOrStack = sortOrStack;
   }

   public char getIsStapled() {
       return isStapled;
   }

   public void setIsStapled(char isStapled) {
       this.isStapled = isStapled;
   }

   public int getSided() {
       return sided;
   }

   public void setSided(int sided) {
       this.sided = sided;
   }

   public int getNoOfCopies() {
       return noOfCopies;
   }

   public void setNoOfCopies(int noOfCopies) {
       this.noOfCopies = noOfCopies;
   }

   public int getStatus() {
       return status;
   }

   public void setStatus(int status) {
       this.status = status;
   }

   //function to print line of print job.
   public String toString() {
       String s = this.getJobId() + "       " + this.getUserName() + "       " + this.getTray() + "       "
               + this.getPaperSize() + "       " + this.getSortOrStack() + "       " + this.getIsStapled() + "       "
               + this.getSided() + "       " + this.getNoOfCopies() + "       ";
  
   return s;
   }
}

PrintDriver.java

import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;

public class PrinterDriver {
   public static void main(String[] args) {
       ArrayList<PrintJob> jobs = new ArrayList<>();
      
       File file = new File("jobs.txt");
      
       try {
           Scanner sc = new Scanner(file);
           String s;
           String[] vals;
           PrintJob p;
   while (sc.hasNextLine()) {
       s = sc.nextLine();
   vals = s.split(" ");
   p = new PrintJob();
   p.setJobId(Integer.parseInt(vals[0]));
   p.setUserName(vals[1]);
   p.setTray(Integer.parseInt(vals[2]));
   p.setPaperSize(Integer.parseInt(vals[3]));
   p.setSortOrStack(vals[4]);
   p.setIsStapled(vals[5].charAt(0));
   p.setSided(Integer.parseInt(vals[6]));
   p.setNoOfCopies(Integer.parseInt(vals[7]));
   jobs.add(p);
   }
   sc.close();
       } catch (Exception e) {
           e.printStackTrace();
       }
      
       Scanner scan = new Scanner(System.in);
       int option = -1;
      
       while(option != 0) {
           displayMenu();
           option = Integer.parseInt(scan.next());
          
           switch(option) {
               case 1:
                   printJob(jobs);
                   break;
               case 2:
                   displayJob(jobs);
                   break;
               case 3:
                   finishedJobs(jobs);
                   break;
               case 0:
                   break;
               default:
                   System.out.println("Please Enter a valid choice.");
                   break;
           }
       }
       scan.close();
   }

   public static void printJob(ArrayList<PrintJob> jobs) {
       System.out.println("Now Printing:");
       //printing top most job here by printing the first element in arraylist which is not yet printed
       for (int i = 0; i < jobs.size(); i++) {
           if (jobs.get(i).getStatus() == 1) {
               System.out.println(jobs.get(i).toString());
               jobs.get(i).setStatus(0);
               break;
           }
       }
   }
   //print the menu
   public static void displayMenu() {
       System.out.println();
       System.out.println("ZooooRocks Copy Menu");
       System.out.println();
       System.out.println("Press 1 to print the next job");
       System.out.println("Press 2 to see the jobs in the next queue");
       System.out.println("Press 3 to see the jobs have finished printing");
       System.out.println("Press 0 to quit");
       System.out.println();
       System.out.print("Enter Choice:");
       System.out.println();
   }
   //print the jobs which are yet to be printed
   public static void displayJob(ArrayList<PrintJob> jobs) {
       System.out.println("The current jobs in the queue:");
       String s = "JobID       User Name   Tray       Paper Size   Sort/Stacked   Stapled       Sided   Number of Copies       ";
      
       System.out.println(s);
       System.out.println("---------------------------------------------------------------------------------------------------------------------");
      
       for (int i = 0; i < jobs.size(); i++) {
           if (jobs.get(i).getStatus() == 1) {
               System.out.println(jobs.get(i).toString());
           }
       }
   }
  
   // to display all the jobs which are already printed
   public static void finishedJobs(ArrayList<PrintJob> jobs) {
       System.out.println("The job which have finished printing:");
       for (int i = 0; i < jobs.size(); i++) {
           if (jobs.get(i).getStatus() == 0) {
               System.out.println(jobs.get(i).toString());
           }
       }
   }
}

Sample Output: -


ZooooRocks Copy Menu

Press 1 to print the next job
Press 2 to see the jobs in the next queue
Press 3 to see the jobs have finished printing
Press 0 to quit

Enter Choice:
2
The current jobs in the queue:
JobID       User Name   Tray       Paper Size   Sort/Stacked   Stapled       Sided   Number of Copies      
---------------------------------------------------------------------------------------------------------------------
100       kimm       1       100       stacked       Y       221       25      
101       mgreen       1       100       stacked       Y       221       20      
102       djmaler       2       100       sorted       N       111       100      
103       brent       3       150       stacked       Y       221       25      
104       kimm       1       100       stacked       Y       221       25      
105       mgreen       1       75       sorted       N       111       1      

ZooooRocks Copy Menu

Press 1 to print the next job
Press 2 to see the jobs in the next queue
Press 3 to see the jobs have finished printing
Press 0 to quit

Enter Choice:
1
Now Printing:
100       kimm       1       100       stacked       Y       221       25      

ZooooRocks Copy Menu

Press 1 to print the next job
Press 2 to see the jobs in the next queue
Press 3 to see the jobs have finished printing
Press 0 to quit

Enter Choice:
2
The current jobs in the queue:
JobID       User Name   Tray       Paper Size   Sort/Stacked   Stapled       Sided   Number of Copies      
---------------------------------------------------------------------------------------------------------------------
101       mgreen       1       100       stacked       Y       221       20      
102       djmaler       2       100       sorted       N       111       100      
103       brent       3       150       stacked       Y       221       25      
104       kimm       1       100       stacked       Y       221       25      
105       mgreen       1       75       sorted       N       111       1      

ZooooRocks Copy Menu

Press 1 to print the next job
Press 2 to see the jobs in the next queue
Press 3 to see the jobs have finished printing
Press 0 to quit

Enter Choice:
1
Now Printing:
101       mgreen       1       100       stacked       Y       221       20      

ZooooRocks Copy Menu

Press 1 to print the next job
Press 2 to see the jobs in the next queue
Press 3 to see the jobs have finished printing
Press 0 to quit

Enter Choice:
2
The current jobs in the queue:
JobID       User Name   Tray       Paper Size   Sort/Stacked   Stapled       Sided   Number of Copies      
---------------------------------------------------------------------------------------------------------------------
102       djmaler       2       100       sorted       N       111       100      
103       brent       3       150       stacked       Y       221       25      
104       kimm       1       100       stacked       Y       221       25      
105       mgreen       1       75       sorted       N       111       1      

ZooooRocks Copy Menu

Press 1 to print the next job
Press 2 to see the jobs in the next queue
Press 3 to see the jobs have finished printing
Press 0 to quit

Enter Choice:
3
The job which have finished printing:
100       kimm       1       100       stacked       Y       221       25      
101       mgreen       1       100       stacked       Y       221       20      

ZooooRocks Copy Menu

Press 1 to print the next job
Press 2 to see the jobs in the next queue
Press 3 to see the jobs have finished printing
Press 0 to quit

Enter Choice:
0