From 936ca07ad25b466317952c7a2faaa7d6eaa6d9ba Mon Sep 17 00:00:00 2001 From: SowinskiBraeden Date: Wed, 12 Nov 2025 20:40:25 -0800 Subject: [PATCH] complete lab 10 --- .../ca/bcit/comp2522/lab10/BookStore.java | 322 ++++++++++++++++++ src/code/ca/bcit/comp2522/lab10/Novel.java | 97 ++++++ .../ca/bcit/comp2522/lab10/Validator.java | 27 ++ 3 files changed, 446 insertions(+) create mode 100644 src/code/ca/bcit/comp2522/lab10/BookStore.java create mode 100644 src/code/ca/bcit/comp2522/lab10/Novel.java create mode 100644 src/code/ca/bcit/comp2522/lab10/Validator.java diff --git a/src/code/ca/bcit/comp2522/lab10/BookStore.java b/src/code/ca/bcit/comp2522/lab10/BookStore.java new file mode 100644 index 0000000..2821722 --- /dev/null +++ b/src/code/ca/bcit/comp2522/lab10/BookStore.java @@ -0,0 +1,322 @@ +package ca.bcit.comp2522.lab10; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * BookStore to demonstrate Lists, ArrayLists, Sets, + * Maps, HashMaps and Iterators. + * + * @author Braeden Sowinski + * @author Trishaan Shetty + * @author Nico Agostini + * @author Calvin Arifianto + * @version 1.0.0 + */ +public class BookStore +{ + private static final int NO_NOVELS = 0; + private static final int DECADE_RANGE = 9; + private static final int DEFAULT_LONGEST_VALUE = 0; + private static final int TO_PERCENTAGE = 100; + + private final String bookStoreName; + private final List novels; + + /** + * BookStore constructor creates a bookstore, and tests + * hash maps and iterators + * @param bookStoreName - The bookStoreName to call bookstore + */ + public BookStore(final String bookStoreName) + { + Validator.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)); + } + + /** + * printAllTitles prints all the novels in the bookstore + */ + public void printAllTitles() + { + this.novels.stream() + .map(Novel::getTitle) + .map(String::toUpperCase) + .forEach(System.out::println); + + } + + /** + * printBoolTitle of all books containing the input string + * @param title - The title that book matches or contains + */ + public void printBookTitle(final String title) + { + this.novels.stream() + .map(Novel::getTitle) + .filter(s -> s.toUpperCase().contains(title.toUpperCase())) + .forEach(System.out::println); + } + + /** + * printTitlesInAlphaOrder takes all novels and + * prints them in alphabetical order + */ + public void printTitlesInAlphaOrder() + { + this.novels.stream() + .map(Novel::getTitle) + .sorted() + .forEach(System.out::println); + } + + /** + * printGroupByDecade prints any books within a given decade + * e.g. 2000-2009 + * @param decade - The decade to find books published within + */ + public void printGroupByDecade(final int decade) + { + this.novels.stream() + .filter(n -> n.getYearPublished() >= decade && + n.getYearPublished() <= decade + DECADE_RANGE) + .map(Novel::getTitle) + .forEach(System.out::println); + } + + /** + * getLongest book title out of all novels + */ + public void getLongest() + { + this.novels.stream() + .max(Comparator.comparingInt(n -> n.getTitle().length())) + .ifPresent(System.out::println); + } + + /** + * isThereABookWrittenIn checks if any books were written in + * a given year + * @param year - The year to check for books + * @return if any books were published in a given year + */ + public boolean isThereABookWrittenIn(final int year) + { + return this.novels.stream() + .anyMatch(n -> n.getYearPublished() == year); + } + + /** + * howManyBooksContain a given word + * @param word - The word to check how many books contain it + * @return the number of books containing the word + */ + public int howManyBooksContain(final String word) + { + return (int) this.novels.stream() + .filter(n -> n.getTitle() + .toLowerCase() + .contains(word.toLowerCase()) + ) + .count(); + } + + /** + * whichPercentWrittenBetween gives the percent + * of books from all books that were written + * between two given years + * @param first - The first year book can be checked in + * @param last - The 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 + ) { + return this.novels.stream() + .filter(n -> n.getYearPublished() >= first && + n.getYearPublished() <= last) + .count() / (float) this.novels.size() * TO_PERCENTAGE; + } + + /** + * getOldestBook by year published + * @return the oldest book by year published + */ + public Novel getOldestBook() + { + return this.novels.stream() + .min(Comparator.comparingInt(Novel::getYearPublished)) + .get(); + } + + /** + * getBooksThisLength returns a list of books that match + * a given title length + * @param titleLength - The title length to compare books to + * @return list of books matching given title length + */ + public List getBooksThisLength(final int titleLength) + { + return this.novels.stream() + .filter(n -> n.getTitle().length() == titleLength) + .toList(); + } + + /** + * main program entry to test methods of BookStore + * @param args - The arguments from 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/lab10/Novel.java b/src/code/ca/bcit/comp2522/lab10/Novel.java new file mode 100644 index 0000000..dc38fb8 --- /dev/null +++ b/src/code/ca/bcit/comp2522/lab10/Novel.java @@ -0,0 +1,97 @@ +package ca.bcit.comp2522.lab10; + +/** + * Novel class stores basic information about a novel + * and has useful methods for sorting alphabetically. + * + * @author Braeden Sowinski + * @author Nico Agostini + * @author Trishaan Shetty + * @author Calvin Arifianto + * @version 1.0.0 + */ +public class Novel + implements Comparable +{ + private final String title; + private final String authorName; + private final int yearPublished; + + /** + * Novel constructor + * @param title - The title of novel + * @param authorName - The authorName of the novel + * @param yearPublished - The year published of the novel + */ + public Novel(final String title, + final String authorName, + final int yearPublished) + { + Validator.validateString(title); + Validator.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 year published + */ + public int getYearPublished() + { + return this.yearPublished; + } + + /** + * compareTo to sort novels based on title + * @param novel - The novel object 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(); + } +} diff --git a/src/code/ca/bcit/comp2522/lab10/Validator.java b/src/code/ca/bcit/comp2522/lab10/Validator.java new file mode 100644 index 0000000..32e1a48 --- /dev/null +++ b/src/code/ca/bcit/comp2522/lab10/Validator.java @@ -0,0 +1,27 @@ +package ca.bcit.comp2522.lab10; + +/** + * Validator is a helper class to validate inputs + * + * @author Braeden Sowinski + * @author Trishaan Shetty + * @author Nico Agostini + * @author Calvin Arifianto + * @version 1.0.0 + */ +public class Validator +{ + /** + * validateString ensures string input is not null or empty + * @param str - The String to validate + * @throws IllegalArgumentException if invalid string + */ + public static void validateString(final String str) + throws IllegalArgumentException + { + if (str == null || str.isEmpty()) + { + throw new IllegalArgumentException("String cannot be null or empty"); + } + } +}