tags:

views:

245

answers:

3

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?
}
+1  A: 

How to make an ArrayList

Don't reinvent the wheel. Use the built in Collection classes.

Your Inventory should probably simply be an ArrayList<Movie>. (Or, if order doesn't matter, a HashSet<Movie>.)


If you insist on making your own though, you could make use of the Arrays.copyOf. Something like this should do:

class MyArrayList {

    String[] strings = new String[2];
    int size = 0;

    public void add(String str) {
        if (size == strings.length) {
            // Double the size
            strings = Arrays.copyOf(strings, strings.length * 2);
        }

        strings[size++] = str;
    }

    public String get(int i) {
        if (i >= size)
            throw new ArrayIndexOutOfBoundsException();
        return strings[i];
    }

    public void remove(int i) {
        size--;
        for (int j = i; j < size; j++)
            strings[j] = strings[j+1];
    }
}
aioobe
I am aguessing this is homework
NullUserException
I'm simply trying to help him solve the stated problem. It says nothing about a restriction on what classes may be used.
aioobe
@aioobe, the title is "How to make an ArrayList". As in, implement one. I suppose in Java the answer "Don't." is the best, but not for a homework assignment.
Tim Bender
Two problems in remove. 1. Use j to access the array rather than i. 2. you should decrement size before looping. Otherwise you will get an Index Out Of Bounds when size = strings.length.
ILMTitan
good catch ILMTitan, updated my answer. thanks.
aioobe
I used the arraylist but on inventory list it says "array reguired, but myarraylist found. if( list[i].getProductID() == productID ); is the i that it highlights
Lola
You can't access elements in an ArrayList using `list[i]`, you need to use `list.get(i)`.
aioobe
Would I have to write if( list.get(i).getProductID() == productID )
Lola
I have added list.get(i) but no I have an error saying can't find getProductID()==productID
Lola
on what line? could you paste the code on pastebin?
aioobe
Ok, this has me all confused. When I read the above, when you said don't reinvent the wheel just use collection class. What do you mean by that. I'm new to java so everything sounds hard for me right now.
Lola
A: 
//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;

We call constructor with some parameters title and productID, so we can init them only into constructor. Initializing booleans is unnecessary. This used to say that you are using field(not parameter)

// do i need set method for title?

// do i need set method for productID?

Depends. I think, no. You pass them into constructor.

Stas
+1  A: 

Of course the easiest thing to do is just use an ArrayList.

The second easiest thing to do is to create a new Class that implements the List interface, and then use it to wrap a private ArrayList instance.

The third easiest thing to do is to create a new Class that implements the List interface, use it to wrap a private Array and then basically reinvent the (ArrayList) wheel. You'll need to do some homework on generics for that. Good luck!

scottw
Can you show an example of what you are talking about.
Lola
1. ArrayList myList = new ArrayList();
scottw
2. Class MyArrayList implements List { ArrayList innerList;}
scottw
So is this all under one class called MyArrayList. So in order to get an arraylist I would have to do a new class. Would I have to change anything on my other classes that I have.
Lola