import java.util.ArrayList; /** * A class to maintain an arbitrarily long list of notes. * Notes are numbered for external reference by a human user. * In this version, note numbers start at 0. * * @author David J. Barnes and Michael Kolling. * @version 2008.03.30 */ public class Notebook { // Storage for an arbitrary number of notes. private ArrayList notes; /** * Perform any initialization that is required for the * notebook. */ public Notebook() { notes = new ArrayList(); } /** * Store a new note into the notebook. * @param note The note to be stored. */ public void storeNote(String note) { notes.add(note); } /** * @return The number of notes currently in the notebook. */ public int numberOfNotes() { return notes.size(); } /** * Show a note. * @param noteNumber The number of the note to be shown. */ public void showNote(int noteNumber) { if(noteNumber < numberOfNotes() && noteNumber >= 0) { // This is a valid note number. System.out.println(notes.get(noteNumber)); } } /** * Remove a note from the notebook if it exists. * @param noteNumber The number of the note to be removed. */ public void removeNote(int noteNumber) { if(noteNumber < numberOfNotes() && noteNumber >= 0) { // This is a valid note number. notes.remove(noteNumber); } } /** * List all notes in the notebook. */ public void listNotes() { int index = 0; for(String note : notes) { //index = notes.indexOf(note); System.out.println(index+") "+note); index = index + 1; } } /** * Add a note to the top of the list, and prepend "Urgent:" to it. * @param note The note to be stored. */ public void storeUrgentNote(String note) { notes.add(0, "Urgent: "+note); } /** * Loop through all notes, but only print the urgent ones. Note that * it increments the index counter properly though, even for notes * that aren't urgent. */ public void listUrgentNotes() { int index = 0; for(String note : notes) { if (note.startsWith("Urgent: ")) { System.out.println(index+") "+note); } index = index + 1; // Increment whether we print or not } } /** * Not part of lab. Came up during class after lab... */ public void listUsingWhile() { int index = 0; String note = notes.get(index); while(note.startsWith("Urgent")) { System.out.println(note); index = index + 1; note = notes.get(index); } } /** * Move the specified note to the end of the list. Assumes that * noteNumber is valid. Could use notes.get(noteNumber) to * store the demoted note before removing it, but remove also * _returns_ the removed item. * @param noteNumber The number of the note to be moved to the end */ public void demoteNote(int noteNumber) { String note; note = notes.remove(noteNumber); if (note.startsWith("Urgent: ")) { note = note.substring(8); } notes.add(note); } /** * Exchanges the specified note with the one below it. Assumes * that noteNumber is valid. * @param noteNumber The number of the first note involved in * the swap. */ public void swapNotes(int noteNumber) { String note; note = notes.remove(noteNumber); notes.add(noteNumber+1, note); } /** * Prints the specified number of notes, then stops. This * version uses a for-each loop to work through ALL of the * items in the ArrayList, but only prints the ones whose * index values are less than the desired number of notes. * * @param numToPrint The number of notes to be printed */ public void printFirstFew(int numToPrint) { int index = 0; // Keep a counter as the loop runs for(String note : notes) { // If counter's value is less than number to be printed, // then print the note. if (index < numToPrint) { System.out.println(note); } index = index + 1; // Increment the counter } } /** * Prints the specified number of notes, then stops. This * version uses a for-each loop but leaves the loop (and the * method) once enough items have been printed. * * @param numToPrint The number of notes to be printed */ public void printFirstFew_Return(int numToPrint) { int numPrinted = 0; for(String note : notes) { if (numPrinted == numToPrint) { // Time to leave? return; // Leave the loop and the method } // If we're still here, print note and increment n System.out.println(note); numPrinted = numPrinted + 1; } } /** * Prints the specified number of notes, then stops. This * version uses a while loop that stops after printing the * proper number of list items. * * @param numToPrint The number of notes to be printed */ public void printFirstFew_While(int numToPrint) { int index = 0; while(index < numToPrint) { System.out.println(notes.get(index)); index = index + 1; } } }