1) the code pasted below can be used to answer the
following:
the output should be as follows: (your program will have
more items)
here is the code that you will need
public class Item
{
private String title;
private int playingTime;
private boolean gotIt;
private String comment;
/**
* Initialise the fields of the item.
* @param theTitle The title of this item.
* @param time The running time of this
item.
*/
public Item(String theTitle, int time)
{
title = theTitle;
playingTime = time;
gotIt = false;
comment = "<no comment>";
}
/**
* Enter a comment for this item.
* @param comment The comment to be
entered.
*/
public void setComment(String comment)
{
this.comment = comment;
}
/**
* @return The comment for this item.
*/
public String getComment()
{
return comment;
}
public String getTitle()
{
return title;
}
/**
* Set the flag indicating whether we own this
item.
* @param ownIt true if we own the item, false
otherwise.
*/
public void setOwn(boolean ownIt)
{
gotIt = ownIt;
}
/**
* @return Information whether we own a copy of
this item.
*/
public boolean getOwn()
{
return gotIt;
}
/**
* Print details of this item to the text
terminal.
*/
public void print()
{
System.out.print(title + " (" +
playingTime + " mins)");
if(gotIt) {
System.out.println("*");
} else {
System.out.println();
}
System.out.println(" " +
comment);
}
}
public class Database
{
private Item [] items;
private int numItems;
private final int MAX = 20;
/* Construct an empty Database. */
public Database()
{
items = new Item [MAX];
numItems = 0;
}
/**
* Add an item to the database.
* @param theItem The item to be added.
*/
public void addItem(Item theItem)
{
items[numItems] = theItem;
numItems++;
}
/**
* Print a list of all currently stored items to
the
* text terminal.
*/
public void list()
{
for(int i = 0; i< numItems; i++)
{
items.print();
System.out.println();
// empty line between items
}
}
/**
* This method returns the Item object that has that
given title.
*
* @param title to be searched
* @return an item
*/
public Item findItem(String title) {
for (int i = 0; i < numItems; i++)
{
if(items.getTitle().equals(title)) {
return
items;
}
}
return null;
}
}
import java.util.Scanner;
public class MyDatabase
{
public static void main (String [] args)
{
Database myCollection = new
Database();
CD item1 = new CD ("Stronger",
"Kelly Clarkson", 17, 68);
Item item2 = new DVD ("Pirates of the
Caribbean: On Stranger Tides",
"Rob Marshall",
137);
DVD item3 = new DVD ("E.T.: The
Extra-Terrestrial", "Steven Spielberg",
115);
Item item4 = new CD ("Thriller",
"Michael JackSon", 9, 54);
Item item5 = new CD ("Let It Be",
"Beatles", 11, 47);
myCollection.addItem(item1);
myCollection.addItem(item2);
myCollection.addItem(item3);
myCollection.addItem(item4);
myCollection.addItem(item5);
myCollection.list();
item4.setOwn(true);
item4.setComment("Favorite track: Human
Nature");
item3.setOwn(true);
item3.setComment("Favorite movie
myCollection.list();
Scanner scan = new
Scanner(System.in);
}
}
public class DVD extends Item
{
private String director;
/**
* Constructor for objects of class DVD
* @param theTitle The title of this DVD.
* @param theDirector The director of this
DVD.
* @param time The running time of the main
feature.
*/
public DVD(String theTitle, String theDirector, int
time)
{
super(theTitle, time);
director = theDirector;
}
/**
* @return The director for this DVD.
*/
public String getDirector()
{
return director;
}
/**
* Print details of this DVD to the text
terminal.
*/
public String toString()
{
return super.toString() + "DVD:" + "
director: " + director;
}
}
public class CD extends Item
{
private String artist;
private int numberOfTracks;
/**
* Initialize the CD.
* @param theTitle The title of the CD.
* @param theArtist The artist of the CD.
* @param tracks The number of tracks on the
CD.
* @param time The playing time of the CD.
*/
public CD(String theTitle, String theArtist, int
tracks, int time)
{
super(theTitle, time);
artist = theArtist;
numberOfTracks = tracks;
}
/**
* @return The artist for this CD.
*/
public String getArtist()
{
return artist;
}
/**
* @return The number of tracks on this
CD.
*/
public int getNumberOfTracks()
{
return numberOfTracks;
}
/**
* Print details of this CD to the text
terminal.
*/
public String toString()
{
return super.toString() + "CD:"+ "
" + artist + " tracks: " +
numberOfTracks;
}
}
2) In Database: define a private/helper method called findIndex – that is given a title of an Item and returns the index where the item is found, or returns -1 if the item is not found. This method is similar to findItem, except that it returns the index, not the Item. 3) Add a method named removeItem in Database that is given a String - a title of an Item - and a. removes the Item object that has that given title from the array b. Returns the object removed. c. Hint: you will call findIndex to get the index of the item in the array. Call that index i. If i is not -1 (i.e. the item with the title is in the array), then you shift all the Items in the array at indices higher than i down by one index. 4) Test this removeItem method, by writing code in the main method that a. removes the 1st Item by calling removeItem with the appropriate title and prints the return value b. Prints the entire collection after removing this item c. Removes the last item by calling removeſtem with the appropriate title and prints the return value d. Prints the entire collection after removing both these items
PART B: 1) Define a new class called DBManager a. The instance variables for this class: i. String which is the name of the Manger ii. a Database object; iii. a Scanner object - to read input from the keyboard. Initialize all these instance variables in the constructor of DBManager. b. a method with the following header: public void process Transactions() This method interacts with the user, getting input from the keyboard, allowing the user to add an item into the database, find an item, delete an item. It also has an option to display all the items. See the sample run I have provided to help you write this method. Write "helper” – private methods in this class, to process the add, find, delete and display options and call them in process Transactions. Do not write one huge method which has ALL the code. 2) Define another class called MyCatalog: This is the class which has the following main method- DO NOT ADD CHANGE THIS CODE except use your name instead of mine below public class MyCatalog { public static void main(String[] args) { DBManager manager = new DBManager("Namita"); manager.process Transactions(); }
Please enter: "a" to add an item, "f" to find an item, "d" to delete an item, "s" to show all the items in the catalog, or "q" to quit. a Enter the item type that you wish to added to the catalog: (CD, DVD) DVD Enter the title of the DVD: E.T. Enter the director of the DVD: Steven Spielberg Enter the playing time of the DVD: 115 Please enter: "a" to add an item, "f" to find an item, "d" to delete an item, "s" to show all the items in the catalog, or "q" to quit. s Showing all Items... CD: Let It Be (108 mins) <no comment> Beatles tracks: 5 DVD: E.T. (115 mins) <no comment> director: Steven Spielberg