Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with this side project. Any help would be appreciated. Working in Java. Attached at the bottom are two template files. I need

I need help with this side project. Any help would be appreciated. Working in Java. Attached at the bottom are two template files. I need the two class templates filled in according to the instructions provided.

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

shelvingsystem class...................

image text in transcribed

Shelving System UML Diagram: (This is different from the Assignment 3 diagram.) ShelvingSystem shelves: ArrayList + ShelvingSystem( ) + getNumberOfShelf Items(): int + addItem( Shelfitem item ): boolean + addItem Shelf Item item, int position): boolean + removeItem( String itemName ): Shelf Item + findItem( String itemName ): Shelf Item + displayShelves(): void + loadData( String filename): boolean + saveData( String filename ): boolean Note: As a reminder, the game's title is really the item name from the super class. A specific example is this: GAME###Super Mario Bros. 3###NES###4###true Setup the Class ShelvingSystem: First, update ShelvingSystem so that it agrees to implement the Shelving Operations interface and the DataFileOperations interface by adding implements ShelvingOperations, DataFileOperations" on to the end of the class signature line. Note: When you complete this, NetBeans will indicate that there are problems with the Shelving System class. This is expected, because all abstract methods must be implemented from the interfaces specified and at this point they are missing. These errors will go away over time. You can add method stubs to get rid of the errors while working on the program. Reminder: A method stub is a minimal framework for the method that does not actually accomplish the required task yet. Generally it will be the method signature with an empty method body, except for a possible incorrect retum value to make things temporarily work. Setting Up Method Stubs: Start by creating the method stubs to get a minimal framework setup for your program. If this is accomplished successfully, the program will run, although it will not actually do anything yet. For each method in the UML diagram, create the method signature and minimal body. NetBeans will help somewhat with the "insert code feature, however it will not quite be enough for the abstract interface methods that you will be implementing. Here is a method stub for the first addItem method to get you started: @Override public boolean addItem(ShelfItem item) { boolean wasAddSuccessful = false; // TODO - Complete this method return wasAddSuccessful; } To make this a little easier, you can go into the Shelving Operations and DataFileOperations files and copy and paste the method signatures. Then setup a body with a temporary return value, if applicable. If a method is returning an object, returning null is a valid temporary choice. Tip: You could also add a print statement to your method stub, indicating that the method is not complete yet. This can be helpful when you start running the program to test things out. private ArrayList shelves; The ArrayList will hold the items we are adding to the shelves. Unlike with the array used in assignment three, there is no longer a need to keep track of the number of items stored as it is being taken care of by the ArrayList object. The ArrayList is also going to handle all expansion and shifting operations needed. Constructor: The Shelving System class has only one constructor. public ShelvingSystem () The default constructor ensures that all data member values are initialized as follows. The shelves ArrayList is instantiated as a new object. It is not given a starting size. Getters & Setters: The ShelvingSystem class will have only one getter and no setters. The single getter for the class will return the current size of the shelves ArrayList. Implemented Shelving Operations Methods (Overridden): Once again, there are five abstract methods in the ShelvingOperations interface that the ShelvingSystem class must implement (this will get rid of the errors that started when Shelving System agreed to implement the Shelving Operations interface earlier). public boolean addItem (Shelf Item item) This addItem method will add a new item to the end of the shelves ArrayList. If the addition is successful, return true and false otherwise. public boolean addItem(Shelf Item item, int position) This addItem method will add a new item at a specific position to the shelves ArrayList. If the addition is successful, return true and false otherwise. (If the position is invalid, the ArrayList add method will throw an exception and this is the case when the additem method returns false.) public Shelf Item removeItem(String itemName) The removeItem method will attempt to remove an item based on the string name provided. It must be an exact match for the remove to occur. If there are duplicates, only the first item that matches will be removed. If the item was removed, the method will return a reference to the item, otherwise it will return null. Unlike the solution in Assignment 3, the ArrayList remove method will slide the contents after the removed item over to the left. This is how people typically handle removals on a physical bookshelf. This is an O(n) method. 1. Call the findItem method to try locating the item 2. If the item was found (i.e., it is not null) 3. Remove the item found from the shelves ArrayList 4. Return the removed Shelfltem object or null if it was not located public Shelf Item findItem(String itemName) The findItem method will attempt to find an item based on the string name provided. It must be an exact match for the item to be found. If there are duplicates, only the first item that matches will be located. If the item was found, the method will return a reference to the item, otherwise it will return null. This is performing linear/sequential search and it is an O(n) method. 1. Keep searching until the first match for the item name is found or the loop index reaches the size of the shelves ArrayList 2. If the item name matches the value in the ArrayList at the loop index 3. Store the found object in a temporary variable 4. Return the found ShelfItem object or null if it was not located public void displayShelves() The displayShelves method will display the current state of the Shelving System in a format similar to this (your data will likely be different): ** Displaying All Shelf Contents ** Shelf 1 Contents: 1. BOOK: Old Man's War by John Scalzi 2. BOOK: The Last Colony by John Scalzi 3. BOOK: The Ghost Brigades by John Scalzi 4. GAME: Mario Odyssey for Switch 5. GAME: God of War for PS4 Shelf 2 Contents: 6. BOOK : Neverwhere by Neil Gaiman Each shelf' will indicate what number it is (starting at one) and what contents it contains. The items on the shelves will be numbered starting at one and continuing until the size of the shelves ArrayList is reached. Each shelf item listing will be indented five spaces. The item number will be listed followed by the toString of the actual Shelfltem stored on the shelves. As there is no way to know how much extra space is contained in an ArrayList object, there is now no need to display empty locations. public boolean loadData(String filename) This method will load the data contained in the specified filename into the ShelvingSystem object. This is the algorithm for the method: 1. Create a File object based on the filename parameter 2. Try 3. Create a Scanner object opening with the File object as the parameter 4. Catch FileNotFoundException 5. Display an appropriate error message indicating what file could not be opened 6. Return false 7. While the file has a line to read 8. Read the line 9. Split the line based on the ###" delimiter value (stored in variable line Values) 10. Check if the line Values array has the correct number of items (five) 11. Check if the first item in line Values is BOOK 12. Output a line starting with LOADING BOOK: " followed by the second element in line Values (the book title) 13. Create a new Book object with the correct values from the split line 14. Call the addItem() method with the new Book object 15. Check if the first item in line Values is GAME 16. Output a line starting with LOADING GAME: " followed by the second element in line Values (the game title) 17. Create a new Game object with the correct values from the split line 18. Call the addItem() method with the new Game object 19. Else if not a book or a game 20. Output this error message (it is two lines, second slightly indented): ERROR: Illegal Item Type! Item Type Ignored: 21. Else if lineValues array is the incorrect size (not five) 22. Output this error message (it is two lines, second slightly indented): ERROR: Illegal Line Format ! Line Ignored: 23. Return true if the values were loaded from the file, false if there were issues opening the file or nothing could be read because the file was empty public boolean saveData(String filename) This method will save the current state (the shelf items stored in the ArrayList) of the ShelvingSystem object to a file specified by filename. This method overwrites the data already contained in the file. This is the algorithm for the method: 1. Create a File object based on the filename parameter 2. Try 3. Create a Print Writer object with the File object as the parameter 4. Catch general Exception 5. Display an appropriate error message indicating what file could not be created 6. Return false 7. For each value in the shelves ArrayList (hint: use a for-each loop) 8. Output a line starting with "SAVING" followed by the toString value of the Shelfltem 9. Write the string returned from the toFileFormat() method to the file 10. Close the Print Writer object to finish writing to the file 11. Return true if the data was successfully written to the file or false if there were problems creating the file Note: The output from the load and save methods is really just for debugging purposes. Normally it would not be displayed in the final version of a program, although it might be generated and written to a file or there might be a way to trigger the output. Note: All of these methods and all overridden or implemented methods) need to be preceded with the @Override flag right before the method signature. Important Warning: Do not change the file path format in the main class to use Windows-like formatting. This will cause the program to not work correctly on Mac or Linux systems. import java.io. PrintWriundException; import java.util.ArrayList; import java.io.File; import import java.util.Scanner; // TODO - Complete this class according to the assignment specifications public class ShelvingSystem { /* The size of a shelf. (Just used for displaying now) */ private static final int SHELF_SIZE = 5; /* The ArrayList that holds all of the shelves items */ private ArrayList shelves; // TODO - Complete this class according to the assignment specifications } Shelving System UML Diagram: (This is different from the Assignment 3 diagram.) ShelvingSystem shelves: ArrayList + ShelvingSystem( ) + getNumberOfShelf Items(): int + addItem( Shelfitem item ): boolean + addItem Shelf Item item, int position): boolean + removeItem( String itemName ): Shelf Item + findItem( String itemName ): Shelf Item + displayShelves(): void + loadData( String filename): boolean + saveData( String filename ): boolean Note: As a reminder, the game's title is really the item name from the super class. A specific example is this: GAME###Super Mario Bros. 3###NES###4###true Setup the Class ShelvingSystem: First, update ShelvingSystem so that it agrees to implement the Shelving Operations interface and the DataFileOperations interface by adding implements ShelvingOperations, DataFileOperations" on to the end of the class signature line. Note: When you complete this, NetBeans will indicate that there are problems with the Shelving System class. This is expected, because all abstract methods must be implemented from the interfaces specified and at this point they are missing. These errors will go away over time. You can add method stubs to get rid of the errors while working on the program. Reminder: A method stub is a minimal framework for the method that does not actually accomplish the required task yet. Generally it will be the method signature with an empty method body, except for a possible incorrect retum value to make things temporarily work. Setting Up Method Stubs: Start by creating the method stubs to get a minimal framework setup for your program. If this is accomplished successfully, the program will run, although it will not actually do anything yet. For each method in the UML diagram, create the method signature and minimal body. NetBeans will help somewhat with the "insert code feature, however it will not quite be enough for the abstract interface methods that you will be implementing. Here is a method stub for the first addItem method to get you started: @Override public boolean addItem(ShelfItem item) { boolean wasAddSuccessful = false; // TODO - Complete this method return wasAddSuccessful; } To make this a little easier, you can go into the Shelving Operations and DataFileOperations files and copy and paste the method signatures. Then setup a body with a temporary return value, if applicable. If a method is returning an object, returning null is a valid temporary choice. Tip: You could also add a print statement to your method stub, indicating that the method is not complete yet. This can be helpful when you start running the program to test things out. private ArrayList shelves; The ArrayList will hold the items we are adding to the shelves. Unlike with the array used in assignment three, there is no longer a need to keep track of the number of items stored as it is being taken care of by the ArrayList object. The ArrayList is also going to handle all expansion and shifting operations needed. Constructor: The Shelving System class has only one constructor. public ShelvingSystem () The default constructor ensures that all data member values are initialized as follows. The shelves ArrayList is instantiated as a new object. It is not given a starting size. Getters & Setters: The ShelvingSystem class will have only one getter and no setters. The single getter for the class will return the current size of the shelves ArrayList. Implemented Shelving Operations Methods (Overridden): Once again, there are five abstract methods in the ShelvingOperations interface that the ShelvingSystem class must implement (this will get rid of the errors that started when Shelving System agreed to implement the Shelving Operations interface earlier). public boolean addItem (Shelf Item item) This addItem method will add a new item to the end of the shelves ArrayList. If the addition is successful, return true and false otherwise. public boolean addItem(Shelf Item item, int position) This addItem method will add a new item at a specific position to the shelves ArrayList. If the addition is successful, return true and false otherwise. (If the position is invalid, the ArrayList add method will throw an exception and this is the case when the additem method returns false.) public Shelf Item removeItem(String itemName) The removeItem method will attempt to remove an item based on the string name provided. It must be an exact match for the remove to occur. If there are duplicates, only the first item that matches will be removed. If the item was removed, the method will return a reference to the item, otherwise it will return null. Unlike the solution in Assignment 3, the ArrayList remove method will slide the contents after the removed item over to the left. This is how people typically handle removals on a physical bookshelf. This is an O(n) method. 1. Call the findItem method to try locating the item 2. If the item was found (i.e., it is not null) 3. Remove the item found from the shelves ArrayList 4. Return the removed Shelfltem object or null if it was not located public Shelf Item findItem(String itemName) The findItem method will attempt to find an item based on the string name provided. It must be an exact match for the item to be found. If there are duplicates, only the first item that matches will be located. If the item was found, the method will return a reference to the item, otherwise it will return null. This is performing linear/sequential search and it is an O(n) method. 1. Keep searching until the first match for the item name is found or the loop index reaches the size of the shelves ArrayList 2. If the item name matches the value in the ArrayList at the loop index 3. Store the found object in a temporary variable 4. Return the found ShelfItem object or null if it was not located public void displayShelves() The displayShelves method will display the current state of the Shelving System in a format similar to this (your data will likely be different): ** Displaying All Shelf Contents ** Shelf 1 Contents: 1. BOOK: Old Man's War by John Scalzi 2. BOOK: The Last Colony by John Scalzi 3. BOOK: The Ghost Brigades by John Scalzi 4. GAME: Mario Odyssey for Switch 5. GAME: God of War for PS4 Shelf 2 Contents: 6. BOOK : Neverwhere by Neil Gaiman Each shelf' will indicate what number it is (starting at one) and what contents it contains. The items on the shelves will be numbered starting at one and continuing until the size of the shelves ArrayList is reached. Each shelf item listing will be indented five spaces. The item number will be listed followed by the toString of the actual Shelfltem stored on the shelves. As there is no way to know how much extra space is contained in an ArrayList object, there is now no need to display empty locations. public boolean loadData(String filename) This method will load the data contained in the specified filename into the ShelvingSystem object. This is the algorithm for the method: 1. Create a File object based on the filename parameter 2. Try 3. Create a Scanner object opening with the File object as the parameter 4. Catch FileNotFoundException 5. Display an appropriate error message indicating what file could not be opened 6. Return false 7. While the file has a line to read 8. Read the line 9. Split the line based on the ###" delimiter value (stored in variable line Values) 10. Check if the line Values array has the correct number of items (five) 11. Check if the first item in line Values is BOOK 12. Output a line starting with LOADING BOOK: " followed by the second element in line Values (the book title) 13. Create a new Book object with the correct values from the split line 14. Call the addItem() method with the new Book object 15. Check if the first item in line Values is GAME 16. Output a line starting with LOADING GAME: " followed by the second element in line Values (the game title) 17. Create a new Game object with the correct values from the split line 18. Call the addItem() method with the new Game object 19. Else if not a book or a game 20. Output this error message (it is two lines, second slightly indented): ERROR: Illegal Item Type! Item Type Ignored: 21. Else if lineValues array is the incorrect size (not five) 22. Output this error message (it is two lines, second slightly indented): ERROR: Illegal Line Format ! Line Ignored: 23. Return true if the values were loaded from the file, false if there were issues opening the file or nothing could be read because the file was empty public boolean saveData(String filename) This method will save the current state (the shelf items stored in the ArrayList) of the ShelvingSystem object to a file specified by filename. This method overwrites the data already contained in the file. This is the algorithm for the method: 1. Create a File object based on the filename parameter 2. Try 3. Create a Print Writer object with the File object as the parameter 4. Catch general Exception 5. Display an appropriate error message indicating what file could not be created 6. Return false 7. For each value in the shelves ArrayList (hint: use a for-each loop) 8. Output a line starting with "SAVING" followed by the toString value of the Shelfltem 9. Write the string returned from the toFileFormat() method to the file 10. Close the Print Writer object to finish writing to the file 11. Return true if the data was successfully written to the file or false if there were problems creating the file Note: The output from the load and save methods is really just for debugging purposes. Normally it would not be displayed in the final version of a program, although it might be generated and written to a file or there might be a way to trigger the output. Note: All of these methods and all overridden or implemented methods) need to be preceded with the @Override flag right before the method signature. Important Warning: Do not change the file path format in the main class to use Windows-like formatting. This will cause the program to not work correctly on Mac or Linux systems. import java.io. PrintWriundException; import java.util.ArrayList; import java.io.File; import import java.util.Scanner; // TODO - Complete this class according to the assignment specifications public class ShelvingSystem { /* The size of a shelf. (Just used for displaying now) */ private static final int SHELF_SIZE = 5; /* The ArrayList that holds all of the shelves items */ private ArrayList shelves; // TODO - Complete this class according to the assignment specifications }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Master The Art Of Data Storytelling With Visualizations

Authors: Alexander N Donovan

1st Edition

B0CNMD9QRD, 979-8867864248

More Books

Students also viewed these Databases questions

Question

5. Identify and describe nine social and cultural identities.

Answered: 1 week ago

Question

2. Define identity.

Answered: 1 week ago

Question

4. Describe phases of majority identity development.

Answered: 1 week ago