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

1. Overview: Write stubs for any new methods required in the redefined Bibliogra

ID: 3598990 • Letter: 1

Question

1. Overview: Write stubs for any new methods required in the redefined Bibliography class and the methods in the new BiblioFileIO class (view). Then rewrite JUnit test cases for the redefined Bibliography class and write a new JUnit test class to test the BiblioFileIOclass. As before, your JUnittest classes should include tests for the public methods of these classes.

2. Details: Remove any test methods from the earlier JUnit tests that are no longer relevant (e.g., because the methods tested no longer exist or because their functionality has changed substantially — if there are any).

3. Add "modification hints" for those methods in the Bibliography class that will need to be changed, but do not change these methods yet. Enter these "hints" in the method comment block that precedes the methods. Write JUnit tests for these methods that reflects the the methods as they will be once you modify them in Stage 2. These tests should be as complete as you can make them.

****************************************************************************************************************************************************

• Bibliography: This class represents the collection of publications used by your program. It will need an instance variable to represent the collection — this will be built around an ArrayList of Publication objects. This program will function as a Set. It will require the standard methods for a collection (add(), get(), remove(), and size()) as well as methods for handling the standard set operations (intersection(), difference(), union(), and subset()). You may also want to pass through other useful methods from the ArrayList class (such as contains()). This class will require the following public methods:

• public boolean add( Publication pub ) — This method adds or inserts a Publication alphabetically into the Bibliography. This method will return true if the Publication can be added successfully. A Publication can be added successfully if its canAdd() method returns true. If the Publication cannot be added (if its canAdd() method returns false), then the method should return false.

• public boolean delete( int whichOne ) — This method removes from the array the Publication found at the position indicated by whichOne. If the value of whichOne is invalid, then the method should return false. If the value is valid, then the method should return true.

• public Bibliography difference( Bibliography other ) — This method will return a new Bibliography that contains the difference between the current Bibliography and the other Bibliography.

• public Publication get( int whichOne ) — This method will return the Publication object found at the position indicated by whichOne. If the value of whichOne is invalid, then the method should return null.

• public Bibliography intersection( Bibliography other ) — This method will return a new Bibliography that contains the intersection of the current Bibliography and the other Bibliography.

• public int size() — This method returns the size of the collection (the number of Publication objects currently stored).

• public boolean subset( Bibliography other ) — This method will return true if every item in the other Bibliography is also in the current Bibliography.

• public Bibliography union( Bibliography other ) — This method will return a new Bibliography that contains the union of the current Bibliography and the other Bibliography.

****************************************************************************************************************************************************

Explanation / Answer

BiblioControl.java

public class BiblioControl {

    public static final int READER = 0;
    public static final int WRITER = 1;

    private static final String JMU_TITLE = "JMUBiblio Bibliography Manager";
    private static final String CS_TITLE = "CS159 (Fall 2017)";
    private static final String OPERATIONS = "     (A)dd, (E)dit, (D)elete,"
              + " (S)earch, read (L)ist, (N)ext, (B)ack, (Q)uit    ";
      
    private BiblioUserIO view;
    private Bibliography bib;
    private BiblioFileIO file;
    private BiblioFileIO errorsFile;
    private int firstOnPage = 1;
    private int lastOnPage = 16;
    public BiblioControl() {
        view = new BiblioUserIO();
        bib = new Bibliography();
        file = new BiblioFileIO();
        errorsFile = new BiblioFileIO();
    }
    public void start() {
        populateBibFromFile();
        String entry = "";
        String quit = "q";
        while ( !entry.toLowerCase().equals( quit ) ) {
            displayMainPage();
            entry = getInput();
            view.clearScreen();
        }
        populateFileFromBib();
    }
    private void populateBibFromFile() {
        file.open( READER );
        String currentLine = file.readLine();
        while ( currentLine != null ) {
            String[] publication = currentLine.split( "\|" );
            for ( int n = 0; n < publication.length; n++ ) {
                System.out.println( publication[ n ] );
            }
            Publication pub = new Publication( "", "", "", "", -1 );
            if ( publication.length == 5 ) {
                pub.setAuthor( publication[ 0 ] );
                pub.setTitle( publication[ 1 ] );
                pub.setCity( publication[ 2 ] );
                pub.setPublisher( publication[ 3 ] );
                try {
                    int year = Integer.parseInt( publication[ 4 ] );
                    pub.setYear( year );
                } catch ( NumberFormatException e ) {
                    pub.setYear( -1 );
                }
                if ( !pub.canAdd() || !bib.add( pub ) ) {
                    errorsFile.open( WRITER );
                    errorsFile.write( currentLine );
                    errorsFile.close( WRITER );
                }
                System.err.println( bib.get( 0 ) );
            } else {
                errorsFile.open( WRITER );
                errorsFile.write( currentLine );
                errorsFile.close( WRITER );
            }
            currentLine = file.readLine();
        }
        file.close( READER );
    }
    private void displayMainPage() {
        view.centerDisplay( JMU_TITLE );
        view.centerDisplay( CS_TITLE );
        view.display( " " );
        displayList( firstOnPage, lastOnPage );
        displayOperationOptions();
    }
    private void displayList( int first, int last ) {
        for ( int n = first; n <= last; n++ ) {
            if ( n < 10 ) {
                view.display( " " + n + ". " );
            } else {
                view.display( n + ". " );
            }
            if ( bib.get( n - 1 ) != null ) {
                view.display( bib.get( n - 1 ).toString() );
            }
            view.display( " " );
        }
    }
    private void displayOperationOptions() {
        view.display( OPERATIONS );
        displayChooseOperation();
    }
    private void displayChooseOperation() {
        view.display( "    Choose Operationn -> " );
    }
    private String getInput() {
        String input = view.getInput();
        view.clearScreen();
        input = input.toUpperCase();
        switch ( input ) {
            case "A":
                add();
                break;
            case "E":
                edit();
                break;
            case "D":
                delete();
                break;
            case "S":
                search();
                break;
            case "L":
                readList();
                break;
            case "N":
                next();
                break;
            case "B":
                back();
                break;
            case "Q":
                break;
            default:
                displayIncorrectOperationEntry( input );
                break;
        }
        return input;
    }
    private void add() {
        view.centerDisplay( "Add Book" );
        view.display( " " );
        Publication pub = new Publication( "", "", "", "", 0 );
        setAuthor( pub );
        setTitle( pub );
        setCity( pub );
        setPublisher( pub );
        setYear( pub );
        boolean success = false;
        while ( !success ) {
            view.display( "Add (Y/N): " );
            String answer = view.getInput();
            if ( answer.toUpperCase().equals( "Y" ) ) {
                bib.add( pub );
                success = true;
            } else if ( answer.toUpperCase().equals( "N" ) ) {
                success = true;
            } else {
                displayMustBeYOrN();
            }
        }
    }
    private void displayMustBeYOrN() {
        view.display( "Must be 'Y' or 'N'. Please re-enter." );
    }
    private void setAuthor( Publication pub ) {
        view.display( "Author:    " );
        String author = view.getInput();
        while ( !pub.setAuthor( author ) ) {
            displayIncorrectRequiredEntry();
            view.display( "Author:    " );
            author = view.getInput();
        }
    }
    private void displayIncorrectRequiredEntry() {
        view.display( " Required entry. Please re-enter. " );
    }
    private void setTitle( Publication pub ) {
        view.display( "Title:     " );
        String title = view.getInput();
        while ( !pub.setTitle( title ) ) {
            displayIncorrectRequiredEntry();
            view.display( " Title:     " );
            title = view.getInput();
        }
    }
    private void setCity( Publication pub ) {
        view.display( "City:      " );
        String city = view.getInput();
        pub.setCity( city );
    }
    private void setPublisher( Publication pub ) {
        view.display( "Publisher: " );
        String publisher = view.getInput();
        pub.setPublisher( publisher );
    }
    private void setYear( Publication pub ) {
        int yearInt = -1;
        while ( !pub.setYear( yearInt ) ) {
            view.display( "Year:      " );
            String year = view.getInput();
            try {
                yearInt = Integer.parseInt( year );
            } catch ( NumberFormatException e ) {
                yearInt = -1;
            }
            if ( !pub.setYear( yearInt ) ) {
                view.display( " Must be a year between 1450 and 2018. "
                        + "Please re-enter. " );
            }
        }
        view.display( " " );
    }
    private void edit() {
        if ( bib != null ) {
            int pubNumber = -1;
            while ( pubNumber <= 0 || pubNumber > bib.size() ) {
                view.display( "    Edit Publication Number -> " );
                String userEntry = view.getInput();
                try {
                    pubNumber = Integer.parseInt( userEntry );
                } catch ( NumberFormatException e ) {
                    pubNumber = -1;
                    view.display( "     Must be a number between 1 and "
                            + bib.size() + ". Please re-enter." );
                }
            }
            view.clearScreen();
            view.centerDisplay( "Edit Publication" );
            view.display( " " );
            Publication pubToEdit = bib.get( pubNumber - 1 );
            String title = pubToEdit.getTitle();
            if ( title.length() > 60 ) {
                title = title.substring( 0, 60 );
            }
            boolean success = false;
            view.display( "Edit "" + title + ""? (Y/N) -> " );
            String answer = view.getInput();
            while ( !success ) {
                if ( answer.toUpperCase().equals( "Y" ) ) {
                    view.clearScreen();
                    view.centerDisplay( "Edit Publication" );
                    view.display( " " );
                    Publication pubReplacement = new Publication( pubToEdit );
                    resetAuthor( pubToEdit, pubReplacement );
                    resetTitle( pubToEdit, pubReplacement );
                    resetCity( pubToEdit, pubReplacement );
                    resetPublisher( pubToEdit, pubReplacement );
                    resetYear( pubToEdit, pubReplacement );
                    view.display( "Confirm Changes (Y/N): " );
                    String confirm = view.getInput();
                    if ( confirm.toUpperCase().equals( "Y" ) ) {
                        success = true;
                        bib.delete( pubNumber - 1 );
                        bib.add( pubReplacement );
                    } else if ( !confirm.toUpperCase().equals( "N" ) ) {
                        displayMustBeYOrN();
                        view.display( "Confirm Changes (Y/N): " );
                        confirm = view.getInput();
                    } else {
                        success = true;
                    }
                  
                } else if ( !answer.toUpperCase().equals( "N" ) ) {
                    displayMustBeYOrN();
                    view.display( "Edit "" + title + ""? (Y/N) -> " );
                    answer = view.getInput();
                } else {
                    success = true;
                }
            }
        } else {
            view.display( "Nothing to edit. " );
            displayEnterToContinue();
        }
    }
    private void resetAuthor( Publication pubToEdit, Publication pubReplace ) {
        view.display( "Author (" + pubToEdit.getAuthor() + ") " );
        view.display( " New Value -> " );
        String author = view.getInput();
        boolean success = pubReplace.setAuthor( author );
        if ( author.equals( "" ) ) {
            pubReplace.setAuthor( pubToEdit.getAuthor() );
            success = true;
        }
        while ( !success ) {
            displayIncorrectRequiredEntry();
            view.display( "Author (" + pubToEdit.getAuthor() + ") " );
            view.display( " New Value -> " );
            author = view.getInput();
            if ( author.equals( "" ) ) {
                pubReplace.setAuthor( pubToEdit.getAuthor() );
                success = true;
            } else {
                success = pubReplace.setAuthor( author );
            }
        }
    }
    private void resetTitle( Publication pubToEdit, Publication pubReplace ) {
        view.display( "Title (" + pubToEdit.getTitle() + ") " );
        view.display( " New Value -> " );
        String title = view.getInput();
        boolean success = pubReplace.setTitle( title );
        if ( title.equals( "" ) ) {
            pubReplace.setTitle( pubToEdit.getTitle() );
            success = true;
        }
        while ( !success ) {
            displayIncorrectRequiredEntry();
            view.display( "Title (" + pubToEdit.getTitle() + ") " );
            view.display( " New Value -> " );
            title = view.getInput();
            if ( title.equals( "" ) ) {
                pubReplace.setTitle( pubToEdit.getTitle() );
                success = true;
            } else {
                success = !pubReplace.setTitle( title );
            }
        }
    }
    private void resetCity( Publication pubToEdit, Publication pubReplace ) {
        view.display( "City (" + pubToEdit.getCity() + ") " );
        view.display( " New Value -> " );      
        String city = view.getInput();
        if ( !city.equals( "" ) ) {
            pubReplace.setCity( city );
        }
    }
    private void resetPublisher( Publication pubToEdit, Publication
            pubReplace ) {
        view.display( "Publisher (" + pubToEdit.getPublisher() + ") " );
        view.display( " New Value -> " );
        String publisher = view.getInput();
        if ( !publisher.equals( "" ) ) {
            pubReplace.setPublisher( publisher );
        }
    }
    private void resetYear( Publication pubToEdit, Publication pubReplace ) {
        int yearInt = -1;
        while ( !pubReplace.setYear( yearInt ) ) {
            view.display( "Year:      " );
            String year = view.getInput();
            if ( year.equals( "" ) ) {
                yearInt = pubToEdit.getYear();
            } else {
                try {
                    yearInt = Integer.parseInt( year );
                } catch ( NumberFormatException e ) {
                    yearInt = -1;
                }
            }
            if ( !pubReplace.setYear( yearInt ) ) {
                view.display( " Must be a year between 1450 and 2018. "
                        + "Please re-enter. " );
            }
        }
        view.display( " " );
    }
    private void displayFunctionNotAvailable() {
        view.display( "This function is not currently available. " );
        displayEnterToContinue();
    }
    private void displayEnterToContinue() {
        view.pause( "Press <ENTER> to continue . . ." );

    }
    private void delete() {
        if ( bib != null ) {
            int pubNumber = -1;
            while ( pubNumber <= 0 || pubNumber > bib.size() ) {
                view.display( "    Delete Publication Number -> " );
                String userEntry = view.getInput();
                try {
                    pubNumber = Integer.parseInt( userEntry );
                } catch ( NumberFormatException e ) {
                    pubNumber = -1;
                    view.display( "     Must be a number between 1 and "
                            + bib.size() + ". Please re-enter." );
                }
            }
        view.clearScreen();
            view.centerDisplay( "Delete Book" );
            view.display( " " );
            String title = bib.get( pubNumber - 1 ).getTitle();
            if ( title.length() > 60 ) {
                title = title.substring( 0, 60 );
            }
            boolean success = false;
            view.display( "Delete "" + title + ""? (Y/N) -> " );
            String answer = view.getInput();
            while ( !success ) {
                if ( answer.toUpperCase().equals( "Y" ) ) {
                    bib.delete( pubNumber - 1 );
                    success = true;
                } else if ( !answer.toUpperCase().equals( "N" ) ) {
                    displayMustBeYOrN();
                    view.display( "Delete "" + title + ""? (Y/N) -> " );
                    answer = view.getInput();
                } else {
                    success = true;
                }
            }
        } else {
            view.display( "Nothing to delete. " );
            displayEnterToContinue();
        }
    }
    private void search() {
        view.centerDisplay( "Search Book" );
        view.display( " " );
        displayFunctionNotAvailable();
    }
    private void readList() {
        view.centerDisplay( "Reading List" );
        view.display( " " );
        displayFunctionNotAvailable();
    }
  
    private void next() {
        view.clearScreen();
        if ( bib.size() > lastOnPage ) {
            firstOnPage += 16;
            lastOnPage += 16;
        }
        displayMainPage();
    }
    private void back() {
        if ( firstOnPage != 1 ) {
            firstOnPage -= 16;
            lastOnPage -= 16;
        }
    }
    private void displayIncorrectOperationEntry( String input ) {
        view.display( "Incorrect entry (" + input + "). Please re-enter. " );
        displayChooseOperation();
        getInput();
    }
    private void populateFileFromBib() {
        file.open( WRITER );
        for ( int n = 0; n < bib.size(); n++ ) {
            Publication pub = bib.get( n );
            String line = pub.getAuthor() + "|" + pub.getTitle() + "|"
                    + pub.getCity() + "|" + pub.getPublisher() + "|"
                    + pub.getYear() + "|";
            file.write( line );
        }
        file.close( WRITER );
    }
}


BiblioUserIO.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class BiblioUserIO {
  
    private static final int SCREEN_LENGTH = 25;
    private static final int SCREEN_WIDTH = 80;  
  
    private BufferedReader reader;
    public BiblioUserIO() {
        reader = new BufferedReader( new InputStreamReader( System.in ) );
    }
    public void display( String str ) {
        System.out.print( str );
    }
    public String getInput() {
        try {
            return reader.readLine();
        } catch ( IOException e ) {
            return "An error has occured retrieving input.";
        }
  
    }
    public void pause( String message ) {
        System.out.print( message );
        try {
            reader.readLine();
        } catch ( IOException e ) {
            System.out.print( e.getMessage() );
        }      
    }
    public void clearScreen() {
        for ( int n = 0; n < SCREEN_LENGTH; n++ ) {
            System.out.print( " " );
        }
    }
    public void centerDisplay( String str ) {
        int halfLength = str.length() / 2;
        int spaces = ( SCREEN_WIDTH / 2 ) - halfLength;
      
        for ( int n = 0; n < spaces; n++ ) {
            System.out.print( " " );
        }
        display( str + " " );
    }
}

BiblioFileIO.java

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class BiblioFileIO {
    public static final int READER = 0;
    public static final int WRITER = 1;
    private BufferedReader reader;
    private BufferedWriter writer;
    private File file;
    public BiblioFileIO() {
        file = new File( "bibliography.txt" );
    }
    public BiblioFileIO( String fileName ) {
        file = new File( fileName );
    }
    public void close( int which ) {
        try {
            switch ( which ) {
                case READER:
                    if ( reader != null ) {
                        reader.close();
                    }
                    break;
                case WRITER:
                    if ( writer != null ) {
                        writer.close();
                    }
                    break;
            }
        } catch ( IOException e ) {
            System.out.print( e.getMessage() );
        }
    }
    public boolean open( int which ) {
        boolean success = false;
        try {
            if ( which == READER ) {
                if ( file.exists() && file.canRead() ) {
                    reader = new BufferedReader( new FileReader( file ) );
                    success = true;
                }
            } else if ( which == WRITER ) {
                writer = new BufferedWriter( new FileWriter( file ) );
                success = true;
            }
        } catch ( IOException e ) {
            success = false;
        }
        return success;
    }
    public String readLine() {
        String line;
        try {
            line = reader.readLine();
        } catch ( IOException | NullPointerException e ) {
            line = null;
        }
        return line;
    }
    public void write( String line ) {
        try {
            if ( writer != null ) {
                writer.write( line );
                writer.newLine();
            }
        } catch ( IOException e ) {
            System.out.print( e.getMessage() );
        }
    }
    public void deleteFile() {
        file.delete();
    }
}

note: due to limited characters i cant able to post Publication.java and Bibliography.java file