In this lab, you will implement CRUD into ShopV3.0. You will also apply your CRUD knowledge to another domain i.e. start to build a DVD library.
In this practical, you will create a new project called ShopV3.0 in Eclipse and refactor the code so that CRUD is fully implemented for the Product class. This will be the menu you will finally end up with:
Create a new project called ShopV3.0.
Copy the following classes into the project (these are ShopV2.0 classes):
If you are not familiar with the code in these classes, take time now to review it.
Test the new code. In particular:
Test the new code. In particular:
Run the project (as a Java Application).
Test options 1 and 2 to ensure you can add and list products.
Test options 3 and 4; they will do nothing and we will add this code in the subsequent steps.
Continue working with ShopV3.0 in Eclipse. We will now focus on the Don't Repeat Yourself principle and refactor our code so that it is cleaner.
Write a new private, helper method and move this repeated code into it.
Your method should look like this and should return a Product object containing all the details the user typed in:
We will continue with the DRY principle in ShopV3.0 in Eclipse.
Write a new private, helper method and move this repeated code into it.
Your method should look like this and should return an int representing the index the user selected. Note that a -1 is returned if there are no items in the ArrayList:
We will continue with the DRY principle in ShopV3.0 in Eclipse.
Over the next few weeks, we will develop a menu-driven DVDLibrary app (CRUD).
When developing this application, always consider the DRY principle and try to anticipate future changes that may come downstream e.g. you know you are building a CRUD based console driven app, so use your knowledge from the Shop app here.
Here, you will work on Phase 1 of this application; it presents the user with a simple menu for adding and listing DVD titles.
Create a new project in Eclipse called DVDLibraryV1.0.
Within the current project, create a new class called DVD. In this class
Add an instance field, called title, that will store the title of the DVD.
Add a constructor that takes in the title as a parameter and updates the instance field, title (no validation).
Add an accessor and a mutator for the title field.
Add a toString method that formats the object state to a user-friendly string.
Within the current project, create a new class called Library. In this class:
Add an instance field called dvds that can hold an ArrayList of DVDs.
Add a constructor that instantiates the above ArrayList.
Add a method add() that accpets a DVD object as a parameter. This method should add the passed DVD object to the dvds ArrayList.
Add a method listDVDs() that returns a String comprising the index number and the title of each DVD in the dvds ArrayList. If there are no dvds in the ArrayList, the String "No DVDs" should be returned.
Within the current project, create a new class called Driver. In this class:
Add an instance field called library that is of type Library.
Add a constructor that instantiates the above library field.
Create a Scanner object that can be used by all methods in the class.
Add a private method called addDVD() that has a void return type. This method is responsible for asking the user to enter a title for the DVD and adding it to the DVD array list.
Add a private method mainMenu() that has an int return type. This method should display the name of the menu and the menu options (see Figure 1). This method should read the menu option that the user entered and return it.
Add a private method run() that has a void return type. This method is responsible for processing the menu option the user entered and also repeatedly displaying the menu to the user.
Then write a main method that will create an instance of itself (i.e. the Driver class) and call the run method over this instance (see the code below).
public static void main (String[] args)
{
Driver app = new Driver();
app.run();
}
Run the project (as a Java Application).
Test adding a DVD and listing them. Try listing the DVDs when no DVDs have been entered.
Remember, when developing this application, always consider the DRY principle and try to anticipate future changes that may come downstream e.g. you know you are building a CRUD based console driven app, so use your knowledge from the Shop app here.
Create a new project in Eclipse, called DVDLibraryV2.0.
Copy the src java code from DVDLibraryV1.0 into it.
You will now extend the code to allow the user to update and delete DVDs (note that the solution to DVD version 1 is in the solutions tab, should you need it).
In your driver class:
add a third option to the menu: 3) Update a DVD.
in the switch statement, add a call to the updateDVD() method.
create an updateDVD() method that has no return type. This method should:
if DVDs exist in the ArrayList, list the DVDs and ask the user which one they would like to edit.
if the user types in a valid index, ask the user to enter a new title for the DVD.
retrieve the DVD at the specified index and update the title using the title mutator.
In your Library class, add an accessor for the dvs ArrayList:
public ArrayList<DVD> getDVDs()
{
return dvds;
}
Run your code and test this code for the following three scenarios:
No DVDs exist...try updating a DVD. The message "No DVDs" should be displayed.
Create two DVDs...try updating a DVD at index 6. A message similar to "There is no DVD for this index number" should be displayed.
With the two DVDs created...try updating a DVD at index 1. The DVD should be updated.
In your driver class:
add a fourth option to the menu: 4) Delete a DVD.
in the switch statement, add a call to the deleteDVD() method.
create an deleteDVD() method that has no return type. This method should:
if DVDs exist in the ArrayList, list the DVDs and ask the user which one they would like to delete.
if the user types in a valid index, delete the DVD at that index.
Run your code and test this code for the following three scenarios:
No DVDs exist...try deleting a DVD. The message "No DVDs" should be displayed.
Create two DVDs...try deleting a DVD at index 6. A message similar to "There is no DVD for this index number" should be displayed.
With the two DVDs created...try deleting a DVD at index 1. The DVD should be updated.