Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In our software we never want to have sections of code that are duplicated. We also want to avoid sections of code that are
In our software we never want to have sections of code that are duplicated. We also want to avoid sections of code that are nearly duplicated - the same with slight variations. This is one of the primary rules in programming: no duplicate code. You may have noticed that in the Library assignment we break this rule, even though we have used inheritance. In this assignment, we remove duplicate code that we have added to the project by using the principles of polymorphism. We are going to remove nearly duplicate code involving books and videos. Requirements Here are the requirements for the project. In Class Library, we have two separate arrays for books and videos. Change these two arrays to be a single array of Loanable[]. Make the size of the array be 10. . Create a new method addToLibrary(Loanable loanable) to add a new Loanable to Loanable[]. Update addBookToLibrary() to create a new book and then call addToLibrary(). Make a similar change for video. Create a new method called find Loanable() that finds a Loanable by title and returns it. Delete findBook(), findVideo(). Create a new method checkout(String title) that finds a Loanable and checks it out. Delete checkoutBook(), checkoutVideo(). Create toString() in Class Book and Video. Change printinventory() to use loanables[] and the toString() methods. Remove the code that totals pages and minutes. When I made these changes, it reduced the size of Library by about 40 lines, making it 33% smaller than it was. Testing Here is a test program: public static void main (String arg[]) { Library library = new Library(); library.addBookToLibrary ("Moby Dick", 540); library.addBookToLibrary ("The Crow", 112) ; library.addBookToLibrary ("Coding", 412); } library.addBookToLibrary library.addBookToLibrary library.addBookToLibrary library.addVideoToLibrary library.addVideoToLibrary library.addVideoToLibrary ("Metaverse", 200); ("Purple", 180); ("Summer", 301); ("Blade Runner", 190); ("Inception", 230); ("The Tiger and Me", 200); library.print Inventory (); library.checkout ("Orange"); library.checkout ("Purple"); library.checkout ("Coding"); library.checkout ("Purple"); library.checkout ("Inception"); library.printItemsOnLoan (); Output Inventory: Book 'Moby Dick' Number of pages: 540 Book 'The Crow' Number of pages: 112 Book 'Coding' Number of pages: 412 Book 'Metaverse' Number of pages: 200 Book 'Purple' Number of pages: 180 Book 'Summer' Number of pages: 301 Video 'Blade Runner' Number of minutes: 190 Video 'Inception' Number of minutes: 230 Video 'The Tiger and Me' Number of minutes: 200 Couldn't find book Orange You can check out Purple You can check out Coding Purple is already checked out. You can check out Inception Loaned: Book 'Moby Dick' Number of pages: 540 Book 'The Crow' Number of pages: 112 Book 'Metaverse' Number of pages: 200 Book 'Purple' Number of pages: 180 Book 'Summer' Number of pages: 301 Video 'Blade Runner' Number of minutes: 190 Video 'Inception' Number of minutes: 230 Video 'The Tiger and Me' Number of minutes: 200 Number of items on loan: 9
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Here is a basic outline of the changes you need to make to the existing code 1 Change arrays to a si...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started