I have a homework assignemnt that I have been working with for the past few days and I just keep on going circles and circles thinking that I got the codes right but something wrong always comes up. These are the requirments
In assignment 2, you will use the modified solution of the assignment 1. In the current version, the database is created when the program starts and it is not possible to make any change later (e.g., add other movies or delete some). You will add this functionality.
In the new scenario, the program first asks if the user is a customer or the owner of the store. If the user is a customer, the program will ask what the customer wants to do (this is already implemented in the current version, please check the program).
When the owner (or administrator) mode is selected, the owner can add or delete movies. Please remember that when you run the program there will be 8 movies already in the database (so you need to write the proper code before the user interface starts).
. . .
netFlix.add( "Prince of Persia", 140);
netFlix.add( "Clash of Titans", 223);
netFlix.add( "Avatar", 353);
netFlix.add( "Inception", 460);
netFlix.add( "Resident Evil", 105);
netFlix.add( "Devil", 624);
netFlix.add( "Memento", 117);
netFlix.add( "D2: The Mighty Ducks", 508);
netFlix.add( "The Lord of the Rings",910);
netFlix.add( "The Uninvited", 120);
. . .
Since the owner mode is selected, the owner can try to add/delete/display movies. You should check if:
• The owner tries to add an existing record.
• The owner tries to delete non-existing record.
In the modified assignment 1 solution, there is class called Inventory.java. An array was employed to implement the data structure of your database there. . . .
public class Inventory
{
private Movie[] list;
. . .
In this project, you will implement your own class MyArrayList (please read Java ArrayList API and lecture notes) using arrays, and then you will use an object of the class MyArrayList to implement the data structure of your database: . . .
public class Inventory
{
private MyArrayList list;
. . .
Please remember that the predefined class ArrayList is similar to arrays but it can grow and shrink in size. Please notice that your assignment WILL NOT BE ACCEPTED if your MyArrayList class does NOT grow or shrink when IT REQUIRES. The predefined class ArrayList is formed from a template class, the template class notation ArrayList is used to create objects. But, your class MyArrayList WILL NOT BE A TEMPLATE CLASS! Therefore, we will use the ordinary notation to create an object of the class MyArrayList. And we will always store objects of the class Movie in the MyArrayList object. Please go over the all member methods of the predefined class ArrayList, and try to figure out which of those you should implement for your own class MyArrayList (make sure that you have getSize() or size() and getCapacity() or capacity() methods. 5. Grow and Shrink, How or When? You should first understand what kind of array you need to use. The initial capacity of the array MUST BE 2
"An array of objects of the class Movie"
i. An array of Movie references
ii. Each reference in the array points to a Movie object.
When we want to add another Movie object, the member method add of the class MyArrayList first should check if all the capacity is used or not. Since there is no more space, you should create a new array with 4 elements (next time 8, then 16 and so forth).
Then, you should copy all the elements (i.e., the memory addresses of the objects, DO NOT TRY TO CREATE NEW OBJECTS!!!) of the first array to the new array. Copying refers assigning elements of the first array to the elements of the new array.
If the owner wants to delete one of the objects in the array, one should call the member function remove of the class MyArrayList.
When you delete a record if size=capacity/2, you need to create a new array and copy the elements (this is shrinking!!!). For example, if the capacity of the current array is 8 and there are 4 empty spaces, you need to create a new array with 4 elements.
I have several classes the first class is the main class:
public class Main {
public static void main(String[] args)
{
// Create the database
Inventory netFlix = new Inventory(8);
netFlix.add( "Prince of Persia", 140);
netFlix.add( "Clash of Titans", 223);
netFlix.add( "Avatar", 353);
netFlix.add( "Inception", 460);
netFlix.add( "Resident Evil", 105);
netFlix.add( "Devil", 624);
netFlix.add( "Memento", 117);
netFlix.add( "D2: The Mighty Ducks", 508);
netFlix.add( "The Lord of the Rings",910);
netFlix.add( "The Uninvited", 120);
Menu application = new Menu(netFlix);
application.run();
}
}
The second class is the inventory class
public class Inventory
{
private Movie[] list;
private int numberOfRecords;
public Inventory(int size)
{
list = new Movie[size];
numberOfRecords = 0;
}
public boolean add(String title, int productID)
{
if( numberOfRecords == list.length )
{
System.out.println("Not enough space! Not added: " + title);
return false;
}
else
{
list[numberOfRecords] = new Movie(title,productID );
numberOfRecords++;
return true;
}
}
public Movie getMovie(int index)
{
return list[index];
}
public int getIndex(int productID)
{
int index = -1;
for(int i=0; i<numberOfRecords; i++)
{
if( list[i].getProductID() == productID )
{
index = i;
}
}
return index;
}
public void print()
{
String status;
System.out.println("Code Title Status");
System.out.println("---- ------------------------- --------");
for(int i=0; i<numberOfRecords; i++)
{
status ="Available";
if( list[i].getIsReserved())
{
status = "Reserved";
}
else if( list[i].getIsRented())
{
status = "Rented";
}
// why there is minus(-)
System.out.printf("%3d. %-25s %-9s\n", list[i].getProductID(), list[i].getTitle(),status );
}
}
}
The third class is menu class:
import java.util.Scanner
public class Menu
{
private Inventory database;
private char menuItem;
private Scanner input;
public Menu(Inventory database)
{
this.database = database;
menuItem = 'N';
input = new Scanner(System.in);
}
private void showMenu()
{
System.out.println();
System.out.println("------------------");
System.out.println("Display Movies : D");
System.out.println("Rent a Movie : R");
System.out.println("Reserve a Movie: S");
System.out.println("Exit : E");
System.out.println("------------------");
System.out.println();
System.out.print("Please make your selection: ");
}
private void rentMovie(int productID)
{
int index = database.getIndex(productID);
if( index == -1)
{
System.out.println("There is not such a code.");
}
else
{
if( database.getMovie(index).getIsRented())
{
System.out.println("You cannot rent " + database.getMovie(index).getTitle() + ". It is already rented.");
}
else
{
database.getMovie(index).setIsRented(true);
System.out.println("Please take your movie.");
}
}
}
private void reserveMovie(int productID)
{
int index = database.getIndex(productID);
if( index == -1)
{
System.out.println("There is not such a code.");
}
else
{
if( database.getMovie(index).getIsReserved() )
{
System.out.println("You cannot reserve " + database.getMovie(index).getTitle() + ". It is already reserved.");
}
else
{
if( database.getMovie(index).getIsRented())
{
database.getMovie(index).setIsReserved(true);
System.out.println( database.getMovie(index).getTitle() + " is reserved for you." );
}
else
{
System.out.println( database.getMovie(index).getTitle() + " is available. You can rent it if you like.");
}
}
}
}
public void run()
{
while(true)
{
switch(menuItem)
{
case 'N':
showMenu();
menuItem = input.next().charAt(0);
break;
case 'D':
database.print();
showMenu();
menuItem = input.next().charAt(0);
break;
case 'R':
System.out.print("Please enter product code:");
rentMovie( input.nextInt() );
showMenu();
menuItem = input.next().charAt(0);
break;
case 'S':
System.out.print("Please enter product code:");
reserveMovie( input.nextInt() );
showMenu();
menuItem = input.next().charAt(0);
break;
case 'E':
System.out.print("Program terminated.");
System.exit(0);
break;
default :
showMenu();
menuItem = input.next().charAt(0);
}
}
}
}
The fourth class is movie class:
public class Movie
{
//why we dont initialize here
private String title;
private int productID;
private boolean isReserved;
private boolean isRented;
// how we use constructor
public Movie(String title, int productID)
{
// why there is this here
this.title = title;
this.productID = productID;
isReserved = false;
isRented = false;
}
public void setIsReserved(boolean isReserved)
{
this.isReserved = isReserved;
}
public void setIsRented(boolean isRented)
{
this.isRented = isRented;
}
public boolean getIsReserved()
{
return isReserved;
}
public boolean getIsRented()
{
return isRented;
}
public String getTitle()
{
return title;
}
// do i need set method for title?
public int getProductID()
{
return productID;
}
// do i need set method for productID?
}