tags:

views:

92

answers:

0

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.

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();
}

}

  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  );

  }

}

}