diff --git a/.gitignore b/.gitignore index 143acc8..41a25f1 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ *.class /.idea/* +/src/code/ca/bcit/comp2522/testing/* diff --git a/src/code/ca/bcit/comp2522/lab05/BookStore.java b/src/code/ca/bcit/comp2522/lab05/BookStore.java new file mode 100644 index 0000000..bbb6cd1 --- /dev/null +++ b/src/code/ca/bcit/comp2522/lab05/BookStore.java @@ -0,0 +1,406 @@ +package ca.bcit.comp2522.lab05; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.List; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; + +/** + * BookStore to demonstrate Lists, ArrayLists, Sets, + * Maps, HashMaps and Iterators. + * + * @author Braeden Sowinski + * @author Trishaan Shetty + * @author Nico Agostini + * @version 1.0.0 + */ +public class BookStore { + private static final int DECADE_RANGE = 9; + + private final String bookStoreName; + private final List novels; + + /** + * validateString ensures string input is not null or empty + * @param str to validate + * @throws IllegalArgumentException if invalid string + */ + private static void validateString(final String str) + throws IllegalArgumentException + { + if (str == null || str.isEmpty()) { + throw new IllegalArgumentException("String cannot be null or empty"); + } + } + + /** + * BookStore constructor creats a bookstore, and tests + * hash maps and iterators + * @param bookStoreName to call bookstore + */ + public BookStore( + final String bookStoreName + ) { + validateString(bookStoreName); + + this.bookStoreName = bookStoreName; + this.novels = new ArrayList<>(); + + novels.add(new Novel("The Adventures of Augie March", "Saul Bellow", 1953)); + novels.add(new Novel("All the King’s Men", "Robert Penn Warren", 1946)); + novels.add(new Novel("American Pastoral", "Philip Roth", 1997)); + novels.add(new Novel("An American Tragedy", "Theodore Dreiser", 1925)); + novels.add(new Novel("Animal Farm", "George Orwell", 1946)); + novels.add(new Novel("Appointment in Samarra", "John O'Hara", 1934)); + novels.add(new Novel("Are You There God? It's Me, Margaret.", "Judy Blume", 1970)); + novels.add(new Novel("The Assistant", "Bernard Malamud", 1957)); + novels.add(new Novel("At Swim-Two-Birds", "Flann O'Brien", 1938)); + novels.add(new Novel("Atonement", "Ian McEwan", 2002)); + novels.add(new Novel("Beloved", "Toni Morrison", 1987)); + novels.add(new Novel("The Berlin Stories", "Christopher Isherwood", 1946)); + novels.add(new Novel("The Big Sleep", "Raymond Chandler", 1939)); + novels.add(new Novel("The Blind Assassin", "Margaret Atwood", 2000)); + novels.add(new Novel("Blood Meridian", "Cormac McCarthy", 1986)); + novels.add(new Novel("Brideshead Revisited", "Evelyn Waugh", 1946)); + novels.add(new Novel("The Bridge of San Luis Rey", "Thornton Wilder", 1927)); + novels.add(new Novel("Call It Sleep", "Henry Roth", 1935)); + novels.add(new Novel("Catch-22", "Joseph Heller", 1961)); + novels.add(new Novel("The Catcher in the Rye", "J.D. Salinger", 1951)); + novels.add(new Novel("A Clockwork Orange", "Anthony Burgess", 1963)); + novels.add(new Novel("The Confessions of Nat Turner", "William Styron", 1967)); + novels.add(new Novel("The Corrections", "Jonathan Franzen", 2001)); + novels.add(new Novel("The Crying of Lot 49", "Thomas Pynchon", 1966)); + novels.add(new Novel("A Dance to the Music of Time", "Anthony Powell", 1951)); + novels.add(new Novel("The Day of the Locust", "Nathanael West", 1939)); + novels.add(new Novel("Death Comes for the Archbishop", "Willa Cather", 1927)); + novels.add(new Novel("A Death in the Family", "James Agee", 1958)); + novels.add(new Novel("The Death of the Heart", "Elizabeth Bowen", 1958)); + novels.add(new Novel("Deliverance", "James Dickey", 1970)); + novels.add(new Novel("Dog Soldiers", "Robert Stone", 1974)); + novels.add(new Novel("Falconer", "John Cheever", 1977)); + novels.add(new Novel("The French Lieutenant's Woman", "John Fowles", 1969)); + novels.add(new Novel("The Golden Notebook", "Doris Lessing", 1962)); + novels.add(new Novel("Go Tell It on the Mountain", "James Baldwin", 1953)); + novels.add(new Novel("Gone with the Wind", "Margaret Mitchell", 1936)); + novels.add(new Novel("The Grapes of Wrath", "John Steinbeck", 1939)); + novels.add(new Novel("Gravity's Rainbow", "Thomas Pynchon", 1973)); + novels.add(new Novel("The Great Gatsby", "F. Scott Fitzgerald", 1925)); + novels.add(new Novel("A Handful of Dust", "Evelyn Waugh", 1934)); + novels.add(new Novel("The Heart Is a Lonely Hunter", "Carson McCullers", 1940)); + novels.add(new Novel("The Heart of the Matter", "Graham Greene", 1948)); + novels.add(new Novel("Herzog", "Saul Bellow", 1964)); + novels.add(new Novel("Housekeeping", "Marilynne Robinson", 1981)); + novels.add(new Novel("A House for Mr. Biswas", "V.S. Naipaul", 1962)); + novels.add(new Novel("I, Claudius", "Robert Graves", 1934)); + novels.add(new Novel("Infinite Jest", "David Foster Wallace", 1996)); + novels.add(new Novel("Invisible Man", "Ralph Ellison", 1952)); + novels.add(new Novel("Light in August", "William Faulkner", 1932)); + novels.add(new Novel("The Lion, The Witch and the Wardrobe", "C.S. Lewis", 1950)); + novels.add(new Novel("Lolita", "Vladimir Nabokov", 1955)); + novels.add(new Novel("Lord of the Flies", "William Golding", 1954)); + novels.add(new Novel("The Lord of the Rings", "J.R.R. Tolkien", 1954)); + novels.add(new Novel("Loving", "Henry Green", 1945)); + novels.add(new Novel("Lucky Jim", "Kingsley Amis", 1954)); + novels.add(new Novel("The Man Who Loved Children", "Christina Stead", 1940)); + novels.add(new Novel("Midnight's Children", "Salman Rushdie", 1981)); + novels.add(new Novel("Money", "Martin Amis", 1984)); + novels.add(new Novel("The Moviegoer", "Walker Percy", 1961)); + novels.add(new Novel("Mrs. Dalloway", "Virginia Woolf", 1925)); + novels.add(new Novel("Naked Lunch", "William Burroughs", 1959)); + novels.add(new Novel("Native Son", "Richard Wright", 1940)); + novels.add(new Novel("Neuromancer", "William Gibson", 1984)); + novels.add(new Novel("Never Let Me Go", "Kazuo Ishiguro", 2005)); + novels.add(new Novel("1984", "George Orwell", 1948)); + novels.add(new Novel("On the Road", "Jack Kerouac", 1957)); + novels.add(new Novel("One Flew Over the Cuckoo's Nest", "Ken Kesey", 1962)); + novels.add(new Novel("The Painted Bird", "Jerzy Kosinski", 1965)); + novels.add(new Novel("Pale Fire", "Vladimir Nabokov", 1962)); + novels.add(new Novel("A Passage to India", "E.M. Forster", 1924)); + novels.add(new Novel("Play It as It Lays", "Joan Didion", 1970)); + novels.add(new Novel("Portnoy's Complaint", "Philip Roth", 1969)); + novels.add(new Novel("Possession", "A.S. Byatt", 1990)); + novels.add(new Novel("The Power and the Glory", "Graham Greene", 1939)); + novels.add(new Novel("The Prime of Miss Jean Brodie", "Muriel Spark", 1961)); + novels.add(new Novel("Rabbit, Run", "John Updike", 1960)); + novels.add(new Novel("Ragtime", "E.L. Doctorow", 1975)); + novels.add(new Novel("The Recognitions", "William Gaddis", 1955)); + novels.add(new Novel("Red Harvest", "Dashiell Hammett", 1929)); + novels.add(new Novel("Revolutionary Road", "Richard Yates", 1961)); + novels.add(new Novel("The Sheltering Sky", "Paul Bowles", 1949)); + novels.add(new Novel("Slaughterhouse-Five", "Kurt Vonnegut", 1969)); + novels.add(new Novel("Snow Crash", "Neal Stephenson", 1992)); + novels.add(new Novel("The Sot-Weed Factor", "John Barth", 1960)); + novels.add(new Novel("The Sound and the Fury", "William Faulkner", 1929)); + novels.add(new Novel("The Sportswriter", "Richard Ford", 1986)); + novels.add(new Novel("The Spy Who Came in from the Cold", "John le Carré", 1964)); + novels.add(new Novel("The Sun Also Rises", "Ernest Hemingway", 1926)); + novels.add(new Novel("Their Eyes Were Watching God", "Zora Neale Hurston", 1937)); + novels.add(new Novel("Things Fall Apart", "Chinua Achebe", 1959)); + novels.add(new Novel("To Kill a Mockingbird", "Harper Lee", 1960)); + novels.add(new Novel("To the Lighthouse", "Virginia Woolf", 1929)); + novels.add(new Novel("Tropic of Cancer", "Henry Miller", 1934)); + novels.add(new Novel("Ubik", "Philip K. Dick", 1969)); + novels.add(new Novel("Under the Net", "Iris Murdoch", 1954)); + novels.add(new Novel("Under the Volcano", "Malcolm Lowry", 1947)); + novels.add(new Novel("Watchmen", "Alan Moore and Dave Gibbons", 1986)); + novels.add(new Novel("White Noise", "Don DeLillo", 1985)); + novels.add(new Novel("White Teeth", "Zadie Smith", 2000)); + novels.add(new Novel("Wide Sargasso Sea", "Jean Rhys", 1966)); + + final Map novelsMap; + final Set keySet; + final List keyList; + final Iterator iter; + + novelsMap = new HashMap(); + + for (final Novel novel : this.novels) { + novelsMap.put(novel.getTitle(), novel); + } + + keySet = novelsMap.keySet(); + keyList = new ArrayList<>(keySet); + Collections.sort(keyList); + + iter = keyList.iterator(); + + while (iter.hasNext()) + { + final String key = iter.next(); + if (key.toLowerCase().contains("the")) { + novelsMap.remove(key); + } + } + + System.out.println("Sorted and cleaned novels:"); + for (final Novel novel : novelsMap.values()) { + System.out.println(novel); + } + } + + /** + * printAllTitles prints all the novels in the bookstore + */ + public void printAllTitles() + { + for (final Novel novel : this.novels) { + System.out.println(novel.getTitle().toUpperCase()); + } + } + + /** + * printBoolTitle of all books containing the input string + * @param title that book matches or contains + */ + public void printBookTitle(final String title) + { + for (final Novel novel : this.novels) { + if (novel.getTitle().toLowerCase().contains(title.toLowerCase())) { + System.out.println(novel.getTitle()); + } + } + } + + /** + * printTitlesInAlphaOrder takes all novels and + * prints them in alphabetical order + */ + public void printTitlesInAlphaOrder() + { + Collections.sort(this.novels); + + for (final Novel novel : this.novels) { + System.out.println(novel.getTitle()); + } + } + + /** + * printGroupByDecade prints any books within a given decade + * e.g. 2000-2009 + * @param decade to find books published in + */ + public void printGroupByDecade(final int decade) + { + for (final Novel novel : this.novels) { + if (novel.getYearPublished() >= decade && novel.getYearPublished() <= decade + DECADE_RANGE) { + System.out.println(novel.getTitle()); + } + } + } + + /** + * getLongest book title out of all novels + */ + public void getLongest() + { + String title; + int longest; + + longest = 0; + title = ""; + + for (final Novel novel : this.novels) { + if (novel.getTitle().length() > longest) { + longest = novel.getTitle().length(); + title = novel.getTitle(); + } + } + + System.out.println(title); + } + + /** + * isThereABookWrittenIn checks if any books were written in + * a given year + * @param year to check for books + * @return if any books were published in a given year + */ + public boolean isThereABookWrittenIn(final int year) + { + for (final Novel novel : this.novels) { + if (novel.getYearPublished() == year) { + return true; + } + } + return false; + } + + /** + * howManyBooksContain a given word + * @param word to check how many books contain it + * @return the number of books containing the word + */ + public int howManyBooksContain(final String word) + { + int count; + + count = 0; + + for (final Novel novel : this.novels) { + if (novel.getTitle().toLowerCase().contains(word.toLowerCase())) { + count++; + } + } + + return count; + } + + /** + * whichPercentWrittenBetween gives the percent + * of books from all books that were written + * between two given years + * @param first year book can be checked in + * @param last year book can be checked in + * @return the percent of books written between first year and last year + */ + public float whichPercentWrittenBetween( + final int first, + final int last + ) { + int inRange; + + inRange = 0; + + for (final Novel novel : this.novels) { + if (novel.getYearPublished() >= first && novel.getYearPublished() <= last) { + inRange++; + } + } + + return ((float) inRange / (float) this.novels.size()) * 100; + } + + /** + * getOldestBook by year published + * @return the oldest book by year published + */ + public Novel getOldestBook() + { + Novel oldest; + + oldest = null; + + for (final Novel novel : this.novels) { + if (oldest == null) { + oldest = novel; + } else { + if (novel.getYearPublished() < oldest.getYearPublished()) { + oldest = novel; + } + } + } + + return oldest; + } + + /** + * getBooksThisLength returns a list of books that match + * a given title length + * @param titleLength to compare books to + * @return list of books matching given title length + */ + public List getBooksThisLength(final int titleLength) { + final List booksOfLength; + + booksOfLength = new ArrayList<>(); + + for (final Novel novel : this.novels) { + if (novel.getTitle().length() == titleLength) { + booksOfLength.add(novel); + } + } + + return booksOfLength; + } + + /** + * main program entry to test methods of BookStore + * @param args form cli + */ + public static void main(final String[] args) + { + final BookStore bookstore; + final Novel oldest; + final List fifteenCharTitles; + + bookstore = new BookStore("Classic Novels Collection"); + + System.out.println("All Titles in UPPERCASE:"); + bookstore.printAllTitles(); + + System.out.println("\nBook Titles Containing 'the':"); + bookstore.printBookTitle("the"); + + System.out.println("\nAll Titles in Alphabetical Order:"); + bookstore.printTitlesInAlphaOrder(); + + System.out.println("\nBooks from the 2000s:"); + bookstore.printGroupByDecade(2000); + + System.out.println("\nLongest Book Title:"); + bookstore.getLongest(); + + System.out.println("\nIs there a book written in 1950?"); + System.out.println(bookstore.isThereABookWrittenIn(1950)); + + System.out.println("\nHow many books contain 'heart'?"); + System.out.println(bookstore.howManyBooksContain("heart")); + + System.out.println("\nPercentage of books written between 1940 and 1950:"); + System.out.println(bookstore.whichPercentWrittenBetween(1940, 1950) + "%"); + + System.out.println("\nOldest book:"); + oldest = bookstore.getOldestBook(); + + System.out.println(oldest.getTitle() + " by " + oldest.getAuthorName() + ", " + + oldest.getYearPublished()); + + System.out.println("\nBooks with titles 15 characters long:"); + fifteenCharTitles = bookstore.getBooksThisLength(15); + + fifteenCharTitles.forEach(novel -> System.out.println(novel.getTitle())); + } +} diff --git a/src/code/ca/bcit/comp2522/lab05/Novel.java b/src/code/ca/bcit/comp2522/lab05/Novel.java new file mode 100644 index 0000000..01c7e9f --- /dev/null +++ b/src/code/ca/bcit/comp2522/lab05/Novel.java @@ -0,0 +1,96 @@ +package ca.bcit.comp2522.lab05; + +/** + * Novel class stores basic information about a novel + * and has useful methods for sorting alphabetically. + * + * @author Braeden Sowinski + * @author Nico Agostini + * @author Trishaan Shetty + */ +public class Novel implements Comparable { + private final String title; + private final String authorName; + private final int yearPublished; + + /** + * validateString ensures given string is not null or empty + * @param str to validate + * @throws IllegalArgumentException if invlalid string + */ + private static void validateString(final String str) + throws IllegalArgumentException + { + if (str == null || str.isEmpty()) { + throw new IllegalArgumentException("String cannot be null or empty"); + } + } + + /** + * Novel constructor + * @param title of novel + * @param authorName of the novel + * @param yearPublished of the novel + */ + public Novel( + final String title, + final String authorName, + final int yearPublished + ) { + validateString(title); + validateString(authorName); + + this.title = title; + this.authorName = authorName; + this.yearPublished = yearPublished; + } + + /** + * getTitle of the novel + * @return title of the novel + */ + public String getTitle() { return this.title; } + + /** + * getAuthorName of the novel + * @return author name of the novel + */ + public String getAuthorName() { return this.authorName; } + + /** + * getYearPublished of the novel + * @return novel yearh published + */ + public int getYearPublished() { return this.yearPublished; } + + /** + * compareTo to sort novels based on title + * @param novel to compare to + * @return number value of comparison, i.e. >1, 0, <1 + */ + @Override + public int compareTo(final Novel novel) { + return this.title.compareTo(novel.getTitle()); + } + + /** + * toString gives a neat string representation + * of the novel + * @return the string representation of novel attributes + */ + @Override + public String toString() { + final StringBuilder builder; + builder = new StringBuilder(); + + builder.append("\""); + builder.append(this.title); + builder.append("\""); + builder.append(" by "); + builder.append(this.authorName); + builder.append(", "); + builder.append(yearPublished); + + return builder.toString(); + } +}