前提・実現したいこと
連結リストを用いて、Job_ListというファイルからJob title と pageの二つのオブジェクトを入力して全てのオブジェクトと一緒にまとめを出力するプログラムを作りました。
このプログラムを連結リストではなく
連結リストを使ったキューで作成したいです。
クラスはPrint summaryとjob そしてdriverクラスです。
出来るだけ現状のコードを維持しながら
書き換えしたいのですが、どうしても大幅に変更するようになってしまいます。
該当のソースコード
PrinterクラスのaddJobsに加えてenqueueメソッドを作成して
java
1 2import java.util.LinkedList; 3import java.util.List; 4 5public class Printer { 6 class Job_List { 7 //instances of each data that for input to the file, name is Job_List 8 String title; 9 int pages; 10 //constructor each specified each attributes 11 Job_List(String title, int pages) { 12 this.title = title; 13 this.pages = pages; 14 } 15 } 16 17 //List instance, is empty for input data. 18 private List<Job_List> jobs; 19 private int jobsPerMin; 20 21 ////constructor each specified each attributes 22 Printer(int jobsPerMin) { 23 jobs = new LinkedList<>(); 24 this.jobsPerMin = jobsPerMin; 25 } 26 27 //methods for add job title and pages 28 public void addJobs(String title, int pages) { 29 Job_List newJob = new Job_List(title, pages); 30 jobs.add(newJob); 31 } 32 33 //methods for printing data 34 public PrintSummary printData(int jobCount) { 35 if(jobCount > jobs.size()) 36 jobCount = jobs.size(); 37 38 //if any data are not inputted, return string as below 39 if(jobs.size() == 0) { 40 System.out.println("No jobs present."); 41 return null; 42 } 43 44 //initialize print pages 45 int printPages = 0; 46 47 //return print summary which is the number od jobs,pages and time per min. 48 for(int i=0; i<jobCount; i++) { 49 Job_List job = jobs.get(0); 50 printPages += job.pages; 51 jobs.remove(0); 52 } 53 return new PrintSummary((float) jobCount/jobsPerMin, printPages, jobCount); 54 55} 56} 57
driverクラスのaddjob(title,pages)をenqueue(title,page)に変更すればいいことはわかるのですが
どうしてもうまくいきません。
java
1 2package JobPrinter; 3 4import java.io.File; 5import java.util.Scanner; 6 7public class Driver { 8 //data instances for add or print data in file 9 private static final String ADD_JOBS = "Add"; 10 private static final String PRINT_JOBS = "Print"; 11 12 public static void main(String[] args) { 13 //Set the name of new file 14 String fileName = "Job_List.txt", line, command; 15 String[] commandLine; 16 17 //instances for accumulate each print summary and default value 18 int jobsPerMin = 1; 19 int totalJobs = 0, totalPages = 0; 20 float totalTime = 0; 21 22 try {//Normal process 23 //Create new file, name is above 24 File f = new File(fileName); 25 26 // Create a new Scanner object which will read the data from the 27 // file passed in. To check if there are more line to read from it 28 // check by calling the scanner.hasNextLine() methods. 29 // then, read line one by one till all line is read. 30 31 Scanner sc = new Scanner(f); 32 Printer printer = new Printer(jobsPerMin); 33 PrintSummary summary; 34 35 while(sc.hasNextLine()) { 36 line = sc.nextLine(); 37 commandLine = line.split(" "); 38 command = commandLine[0]; 39 40 //conditional branch 41 switch (command) { 42 // procesing for add jobs 43 case ADD_JOBS: 44 //input job title and pages 45 String title = commandLine[1]; 46 int pages = Integer.parseInt(commandLine[3]); 47 //add these data in printer 48 printer.addJobs(title,pages); 49 //print for all of inputted title and pages 50 System.out.println("Printing: "+title); 51 System.out.println("Number of Pages: "+pages); 52 System.out.println(); 53 break; 54 // processing for print jobs inputted 55 case PRINT_JOBS: 56 int printJobs = Integer.parseInt(commandLine[1]); 57 summary = printer.printData(printJobs); 58 totalJobs += summary.getPrintJobs(); 59 totalPages += summary.getPrintPages(); 60 totalTime += summary.getPrintTime(); 61 62 } 63 } 64 }//Exception handling 65 catch (Exception e) { 66 e.printStackTrace(); 67 } 68 69 //Print summary output 70 System.out.println("Print summary"); 71 System.out.println(totalJobs+" jobs and "+totalPages+ " printed in "+totalTime+" minutes."); 72 } 73} 74
Printsummary class
java
1class PrintSummary { 2 //data instances time,pages and jobs for print last summary 3 private float printTime; 4 private int printPages; 5 private int printJobs; 6 7 //constructor each specified each attributes 8 public PrintSummary(float printTime, int printPages, int printJobs) { 9 this.printTime = printTime; 10 this.printPages = printPages; 11 this.printJobs = printJobs; 12 } 13 14 //setter and getter to access from outside of this class 15 public float getPrintTime() {return printTime;} 16 public void setPrintTime(float printTime) {this.printTime = printTime;} 17 18 public int getPrintPages() {return printPages;} 19 public void setPrintPages(int printPages) {this.printPages = printPages;} 20 21 public int getPrintJobs() {return printJobs;} 22 public void setPrintJobs(int printJobs) {this.printJobs = printJobs;} 23} 24
説明が不十分でしたら申し訳ありませんが
よろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー