import java.util.List; import java.util.ArrayList; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.NoSuchFileException; /* An instance of this class represents a document. ** The underlying representation is either an ArrayList or a list ** of the kind returned by the Files.readAllLines() method. ** ** Author: R. McCloskey ** Date: October 2026 */ class DocumentViaList implements Document { // instance variables // ------------------ private List linesOfText; // this document's contents // constructor // ----------- /* Establishes this document as containing no lines of text. */ public DocumentViaList() { linesOfText = new ArrayList(100); } /* Initializes this document to include the lines of text in the ** file whose path/name is provided. */ public DocumentViaList(Path path) { try { linesOfText = Files.readAllLines(path); } catch (NoSuchFileException e) { //e.printStackTrace(); System.out.println("*** File " + path + " not found; document initialized to empty ***"); linesOfText = new ArrayList(100); } catch (IOException e) { e.printStackTrace(); } } // observer // -------- /* Returns the number of lines of text in this document. */ public int numLines() { return linesOfText.size(); } /* Returns the line of text identified by the given line number, ** or null if lineNum is not in the range [0 .. numLines()) */ public String getLine(int lineNum) { try { return linesOfText.get(lineNum); } catch (IndexOutOfBoundsException e) { System.out.println("getLine(): Line number " + lineNum + " is out of bounds."); return null; } } /* Displays the portion of this document whose lines are numbered ** in the range [startLine .. endLine). Each line is preceded by ** its line number within square brackets. */ public void display(int startLine, int endLine) { startLine = Math.max(startLine, 0); endLine = Math.min(endLine, numLines()); for (int i = startLine; i < endLine; i++) { System.out.println("[" + i + "]: " + linesOfText.get(i)); } } /* Displays this document from beginning to end. */ public void display() { display(0, numLines()); } // mutators // -------- /* Inserts the given text (as a new line) at the specified position ** within this document. ** pre: 0 <= lineNum <= numLines() */ public void insertLine(int lineNum, String text) { try { linesOfText.add(lineNum, text); } catch (IndexOutOfBoundsException e) { System.out.println("insertLine(): Line number " + lineNum + " is out of bounds."); } } /* Replaces the line at the given position by the given text. ** pre: 0 <= lineNum < numLines() */ public void replaceLine(int lineNum, String text) { try { linesOfText.set(lineNum, text); } catch (IndexOutOfBoundsException e) { System.out.println("replaceLine(): Line number " + lineNum + " is out of bounds."); } } /* Deletes the line of text at the specified position. ** pre: 0 <= lineNum < numLines() */ public void deleteLine(int lineNum) { try { linesOfText.remove(lineNum); } catch (IndexOutOfBoundsException e) { System.out.println("deleteLine(): Line number " + lineNum + " is out of bounds."); } } // Input/Output // ------------ /* Writes this document into the file whose path/name is specified, ** regardless of whether or not a file already exists there. */ public void writeToFile(Path path) throws IOException { Files.write(path, linesOfText); } }