Question
Java Problem package library.service.classes;//set up the package import library.service.classes.BookGenre; public class BookRecord{ int recordNo; String title; String []authors; BookGenre genre; String tag; int pages; static
Java Problem
package library.service.classes;//set up the package
import library.service.classes.BookGenre;
public class BookRecord{
int recordNo;
String title;
String []authors;
BookGenre genre;
String tag;
int pages;
static int cnt=10000;
/on-default constructor
public BookRecord(String title, String genre, String [] name, String tag, int numPages){
this.setTitle(title);
this.setGenre(genre);
this.setAuthors(name);
this.setRecordNo();
this.setTag(tag);
this.setPages(numPages);
}
//accessors
public String getTitle(){
return this.title;
}
public BookGenre getGenre(){
return this.genre;
}
public String [] getAuthors(){
String []ret = new String[this.authors.length];
for(int i=0;i ret[i]=this.authors[i]; } return ret; } public String getAuthorList(){ String ret=""; for(int i=0;i ret=ret + " " +this.authors[i]; } return ret; } public int getRecordNo(){ return this.recordNo; } public String getTag() { return this.tag; } public int getNumPages() { return this.pages; } //mutators public void setRecordNo(){ this.recordNo=BookRecord.cnt++; } public void setTitle(String title){ this.title=title; } public void setGenre(String genre){ this.genre=BookGenre.valueOf(genre); } public void setAuthors(String []authorList){ this.authors = new String[authorList.length]; for(int i=0;i this.authors[i]=authorList[i]; } } public void setTag(String tag){ this.tag = tag; } public void setPages(int numPages) { this.pages = numPages; } //toString method public String toString(){ String str=""; str = str + "=================================== "; str = str + "Tag: " + this.getTag() + " "; str = str + "Record No:" + this.getRecordNo() + " "; str = str + "Title:" + this.getTitle() + " "; str = str + "Genre: " + this.getGenre() + " "; str = str + "Authors: " + this.getAuthorList() + " "; str = str + "No. of Pages: " + this.getNumPages() + " "; str = str + "=================================== "; return str; } //toEquals method public boolean equals(BookRecord aRecord){//remember you should not compare the record no if(!title.equals(aRecord.title)) return false; if(!this.authorEqual(aRecord)) return false; if(!genre.equals(aRecord.genre)) return false; if(pages != aRecord.pages) return false; if(!tag.equals(aRecord.tag)) return false; return true; } private boolean authorEqual(BookRecord aRecord){ for(int i=0;i if(!this.authors[i].equals(aRecord.authors[i])){ //assume the authors will always be provided in the same order return false; } } return true; } } package library.service.classes; public enum BookGenre {GENRE_HISTORY,GENRE_SCIENCE,GENRE_ENGINEERING,GENRE_LITERATURE}; package library.client.classes; import java.util.Scanner; import java.io.BufferedReader; import java.io.File; import java.io.*; import java.io.IOException; import library.service.classes.BookGenre; import library.service.classes.BookRecord; class library{ BookRecord []books; //array of objects int noRecords=0; /o of records; it is not the size of the array public void resize(int resizeFactor){//library to expand the array //initiatilize a new array with larger size and then copy the variables to int initSize = this.noRecords; BookRecord [] newArray = new BookRecord[initSize+resizeFactor]; //copy the object over for(int i=0;i newArray[i]=this.books[i]; } this.books=newArray; System.out.println("Resized the array from " + initSize + " to " + this.books.length); } public void searchByGenre(BookGenre genre){ //loop through the book records and list the genre for(int i=0;i if(this.books[i].getGenre()==genre){ System.out.println(this.books[i].toString()); } } } public void searchTag(String [] tags){ int length = tags.length; for (int i = 0; i for (int j = 0; j if (tags[i].compareTo(tags[j]) > 0) { String tag = tags[i]; tags[i] = tags[j]; tags[j] = tag; } } } } public int tsearch(String tag, String [] tags, int a, int b) { if (b return -1; } if (b -a == 1) { return tags[a].equals(tag) ? a : -1; } int pivot = (a+b)/2; if (tag.compareTo(tags[pivot]) return tsearch (tag, tags, 0, pivot); } else if (tag.compareTo(tags[pivot]) > 0) { return tsearch (tag, tags, pivot, b); } return pivot; } public boolean searchForDuplicate(BookRecord aRecord){ //loop through the library and find duplicates //return true if duplicate found //else return false if(this.noRecords==0) return false; for(int i=0;i if(this.books[i].equals(aRecord)) return true; } return false; } public void print(){//list the library for(int i=0;i System.out.println(this.books[i].toString()); } } library(){ this.books=new BookRecord[5];//intial size is 5 } public BookRecord [] sortsString (BookRecord [] myArray, int noRecord) { } public BookRecord[ ] sortPages(BookRecord [] myArray, int noRecords) { } public static void main(String []args) throws IOException {//instantiate the library //arg[0]: text file //arg[1]: resize factor int resizeFactor = Integer.parseInt(args[1]); library myLib = new library(); //read the the files from text files String []authors; BookRecord aRecord; Scanner scan; String str; BufferedReader br = new BufferedReader (new InputStreamReader(System.in)); String [] tags = new String[8]; for (int i = 0; i tags[i] = br.readLine(); } int index = tsearch (tag, tags, 0, tags.length); if (index System.out.println("No match found"); } else { System.out.println(); } try { File myFile=new File(args[0]); scan=new Scanner(myFile);//each line has the format title:genre:author-name-1,author-name-2..authorname-m while(scan.hasNextLine()){ str=scan.nextLine(); String []tok=str.split(":"); authors=tok[2].split(","); aRecord = new BookRecord(tok[0],tok[1],authors); //check for duplicate records if (!myLib.searchForDuplicate(aRecord)){ //create a BookRecord object and load it on the array myLib.books[myLib.noRecords] = aRecord; myLib.noRecords++; //System.out.println("No of records: " + myLib.noRecords); myLib.books[myLib.tags] = aRecord; } else{ System.out.println("Found a duplicate"); System.out.println(aRecord.toString()); } //check if the array needs to resize if(myLib.books.length == myLib.noRecords){ /eed to add additional space myLib.resize(resizeFactor); } } scan.close(); }catch(IOException ioe){ System.out.println("The file can not be read"); } //User interactive part String option1, option2; scan = new Scanner(System.in); while(true){ System.out.println("Select an option:"); System.out.println("Type \"S\" to list books of a genre"); System.out.println("Type \"P\" to print out all the book recors"); System.out.println("Type \"T\" to search for a record with specific tag"); System.out.println("Type \"Q\" to Quit"); option1=scan.nextLine(); switch (option1) { case "S": System.out.println("Type a genre. The genres are:"); for (BookGenre d : BookGenre.values()) { System.out.println(d); } option2=scan.nextLine(); //assume the use will type in a valid genre myLib.searchByGenre(BookGenre.valueOf(option2)); break; case "P": myLib.print(); break; case "T": case "Q": System.out.println("Quitting program"); System.exit(0); default: System.out.println("Wrong option! Try again"); break; } } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started