HW06Homework 06 – Watching SportsTopics: polymorphism, exceptions, file I/OProblem DescriptionPlease make sure to read all parts of this document carefully.You and your friends are big fans...

I have attached the file with questions


HW06 Homework 06 – Watching Sports Topics: polymorphism, exceptions, file I/O Problem Description Please make sure to read all parts of this document carefully. You and your friends are big fans of sports and decide to go looking for some sports games to attend. As you look for sports games, you become overwhelmed by how many there are to choose from. To help you decide, you create a database that stores information about the sports games you have bought tickets for and will watch. Solution Description You will need to complete and turn in 6 classes: SportsGame.java, BasketballGame.java, FootballGame.java, InvalidTicketException.java, Tickets.java, and Driver.java. Notes: 1. All variables should be inaccessible from other classes and must require an instance to be accessed through, unless specified otherwise. 2. All methods should be accessible from everywhere and must require an instance to be accessed through, unless specified otherwise. 3. Use constructor and method chaining whenever possible! Ensure that your constructor chaining helps you reduce code reusage! 4. When throwing exceptions, provide descriptive and specific messages. 5. Reuse code when possible and helper methods are optional. 6. Make sure to add all Javadoc comments to your methods and classes! SportsGame.java This abstract class represents a sports game in general. Variables: • String venue – the location of the sports game • String startTime – the time that the game began and will be represented as “HH:MM” o Example: 13:31 • String startDate – the date that the game happens and will be represented as “MM-DD YYYY" (month-day-year) o Example: 04-05-2023 • int score1 – the score of the first team • int score2 – the score of the second team • int seatsLeft – the number of remaining seats for viewing the game Constructor(s): • A constructor that takes in venue, startTime, startDate, score1, score2, and seatsLeft. o If the passed in value for venue, startTime, or startDate is blank or null, throw an IllegalArgumentException with a useful message. o If the passed in value for score1, score2, or seatsLeft is negative, throw an IllegalArgumentException with a useful message. Methods: • toString o This method should properly override Object’s toString method and return the following String on one line, without spaces after commas, and without the curly braces: “{venue},{startTime},{startDate},{score1},{score2}, {seatsLeft}” • equals o This method should properly override Object’s equals method. o Two SportsGame objects are equal if they have the same venue, startTime, startDate, score1, score2, and seatsLeft. • Any necessary getter and setter methods (emphasis on necessary – don’t write any that have no use). BasketballGame.java This class describes a basketball game, and it is a concrete implementation of SportsGame. Variables: • String league – the name of the league that the basketball teams are a part of Constructor(s): • A constructor that takes in venue, startTime, startDate, score1, score2, seatsLeft, and league. • If the passed in value for league is blank or null, throw an IllegalArgumentException with a useful message. Methods: • toString o This method should properly override SportsGame’s toString method and return the following String on one line, without spaces after commas, and without the curly braces: “BasketballGame,{venue},{startTime},{startDate}, {score1},{score2},{seatsLeft},{league}” • equals o This method should properly override SportsGame’s equals method. o Two BasketballGames are equal if they have the same venue, startTime, startDate, score1, score2, seatsLeft, and league. • Reuse as much code as possible! FootballGame.java This class describes a football game, and it is a concrete implementation of SportsGame. Variables: • String singer – the name of the singer performing at halftime Constructor(s): • A constructor that takes in venue, startTime, startDate, score1, score2, seatsLeft, and singer. • If the passed in value for singer is blank or null, throw an IllegalArgumentException with a useful message. Methods: • toString o This method should properly override SportsGame’s toString method and return the following String on one line, without spaces after commas, and without the curly braces: “FootballGame,{venue},{startTime},{startDate},{score1 }, {score2},{seatsLeft},{singer}” • equals o Should properly override SportsGame’s equals() method. o Two FootballGames are equal if they have the same venue, startTime, startDate, score1, score2, seatsLeft, and singer. • Reuse as much code as possible! InvalidTicketException.java This class describes a checked exception. Note that this class should be a child of the most generic checked exception class in the java.lang package. Constructor(s): • A constructor that takes in a String representing the exception’s message. • A no-argument constructor that has default message “Invalid ticket”. Tickets.java This class holds various public static methods that will allow you to read from and write to the database. The database is represented by a comma-separated value (CSV) file. Please refer to the clarification thread on Ed Discussion for a sample file. Methods: • retrieveGames() o Takes in a String representing the path name of the file to read from. o Throws a FileNotFoundException with a useful message if the passed in path name is blank or null, or the path name is valid, but not to a file. o Returns an ArrayList of SportsGame objects created from the tokens. o Any exceptions thrown by this method should be propagated to another method. o Each line of the file will contain information about sports games. ▪ File line tokens will be in the following format on their own line, without the curly braces: {GameType},{venue},{startTime},{startDate},{score1}, {score2},{seatsLeft},{league/singer} ▪ Iterate through the file and create a SportsGame object for each token. • Note: There can be any number of lines in the file, but lines will not be empty. ▪ Add each SportsGame object to an ArrayList. ▪ If the game type in the token is not BasketballGame or FootballGame, throw an InvalidTicketException with a useful message. You may assume other tokens in the line will contain valid data. • For example, an exception should be thrown given the following token: “SoccerGame,MBS,13:31,04-05-2023,3,0,6,MLS” • processInfo o This is a private helper method that will be used to process a line of text from a CSV into a BasketballGame or FootballGame object. o Takes in a String representing the line of info to process. o Returns a SportsGame object that is either a BasketballGame or FootballGame. o If the type of game is not BasketballGame or FootballGame, throw an InvalidTicketException with a useful message. o IMPORTANT: You may not use a second Scanner object to process this line, as it will break the autograder. o HINT: The String split() method can be used to split a String with a given delimiter into an array of tokens. Think about what character you can split the line apart with to separate the information. • purchaseTickets o Takes in a String representing the path name of the file to write to and an ArrayList of SportsGame objects. o If the passed in path name is blank or null, throw an IllegalArgumentException with a useful message. o If no file with the inputted path name exists, create a file with the given name. o Any exceptions thrown by this method should be propagated to another method. o Iterate through the ArrayList and write each SportsGame object to a new line. o If a SportsGame has no remaining seats, do not write it to the file. o Note: If the file exists and there is already information contained in the given file, it should not be overwritten. Instead, you should add onto the end of the existing file. o To implement this, you should follow a read-modify-write design pattern. This pattern can be used to change the contents of a file in the following three steps: ▪ reading data from a file into an intermediate structure (e.g. an ArrayList) ▪ performing any necessary modifications to that data in the intermediate structure ▪ writing the data in the intermediate structure back out to the same file This allows us to use familiar tools like Scanner and PrintWriter to change the contents of a file without modifying the contents of the file directly (which is much more difficult). • findTickets o Takes in a String representing the path name of the file to read from and a SportsGame object. o Throws a FileNotFoundException with a useful message if the passed in path name is blank or null, or the path name is valid, but not to a file. o Returns an ArrayList of Integer objects. o Iterate through the file and find all occurrences of the SportsGame object. ▪ When you find an occurrence of the inputted SportsGame, add its corresponding line number to the ArrayList. The first line of a file is line 0. o If the inputted SportsGame object is not found, throw an InvalidTicketException with a useful message. o Example: If the SportsGame object is found on lines 1, 3, and 9, return an ArrayList containing Integers 1, 3, and 9. o Reuse as much code as possible! • attendGame o Takes in a String representing the path name of the file to write to and a SportsGame object. o Throws a FileNotFoundException with a useful message if the passed in path name is blank or null, or the path name is valid, but not to a file. o Iterate through the file and find all instances of the SportsGame object. o Remove each occurrence of the SportsGame from the file, keeping the lines in the file contiguous (I.e., there are no gaps between the lines in the file). ▪ Hint: How can you use some of the methods you have written to reuse as much code as possible? o If the inputted SportsGame object is not found, throw an InvalidTicketException with a useful message. Driver.java This class will be used to test your code. Methods: • main o Create two BasketballGame and two FootballGame objects. o Write the objects into a file called “TestTickets.csv” using purchaseTickets. ▪ Note: It is recommended to open the CSV file using a text editor rather a spreadsheet program when viewing or editing the file. o Create another BasketballGame object and add it to “TestTickets.csv”. o Read the CSV file into an ArrayList using retrieveGames and print each object to a new line. o Call attendGame on a SportsGame currently in the CSV file to remove each occurrence from the file. o Note: This is to help you get started with testing your code, and it is not comprehensive. It is strongly recommended that you create more test cases. Checkstyle You must run checkstyle on your submission (To learn more about Checkstyle, check out cs1331-style guide.pdf under CheckStyle Resources in the Modules section of Canvas.) The Checkstyle cap for this assignment is 35 points. This means there is a maximum point deduction of 35. If you don't have Checkstyle yet, download it from Canvas -> Modules -> CheckStyle Resources -> checkstyle-8.28.jar. Place it in the same folder as the files you want to run Checkstyle on. Run checkstyle on your code like so: $ java -jar checkstyle-8.28.jar yourFileName.java Starting audit... Audit done. The message above means there were no Checkstyle errors. If you had any errors, they would show up above this message, and the number at the end would be the points we would take off (limited by the checkstyle cap mentioned in the Rubric section). In future homeworks we will be increasing this cap, so get into the habit of fixing these style errors early! Additionally, you must Javadoc your code. Run the following to only check your Javadocs: $ java -jar checkstyle-8.28.jar -j yourFileName.java Run the following to check both Javadocs and Checkstyle: $ java -jar checkstyle-8.28.jar -a yourFileName.java For additional help with Checkstyle see the CS 1331 Style Guide. Turn-In Procedure Submission To submit, upload the files listed below to the corresponding assignment on Gradescope: • SportsGame.java • BasketballGame.java • FootballGame.java • InvalidTicketException.java • Tickets.java • Driver.java Make sure you see the message stating the assignment was submitted successfully. From this point, Gradescope will run a basic autograder on your submission as discussed in the next section. Any autograder test are provided as a courtesy to help “sanity check” your work and you may not see all the test cases used to grade your work. You are responsible for thoroughly testing your submission on your own to ensure you have fulfilled the requirements of this assignment. If you have questions about the requirements given, reach out to a TA or Professor via Piazza for clarification. You can submit as many times as you want before the deadline, so feel free to resubmit as you make substantial progress on the homework. We will only grade your latest submission. Be sure to submit every file each time you resubmit. Gradescope Autograder If an autograder is enabled for this assignment, you may be able to see the results of a few basic test cases on your code. Typically, tests will correspond to a rubric item, and the score returned represents the performance of your code on those rubric items only. If you fail a test, you can look at the output to determine what went wrong and resubmit once you have fixed the issue. The Gradescope tests serve two main purposes: • Prevent upload mistakes (e.g. non-compiling code) • Provide basic formatting and usage validation In other words, the test cases on Gradescope are by no means comprehensive. Be sure to thoroughly test your code by
Mar 31, 2023
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here