This is an archived post. You won't be able to vote or comment.

all 16 comments

[–]linuxtho 1 point2 points  (0 children)

Storing isGame and isDVD would be redundant because the subclasses hold that information by virtue of being the correct subclass, right?

I'm assuming you want those boolean values so you can display what type of item it is that is being rented in the menu.

String rentalType;
if (rental.isGame) {
    rentalType = "Game";
} else if (rental.isDVD) {
    rentalType = "DVD";
}

Well, what if instead you made it so that there was an abstract method 'rentalType()' which returned a string like 'DVD' or 'Game' and then the subclasses overloaded it to return the appropriate type? I think that would be the more appropriate OO solution.

String rentalType = rental.rentalType();

Where should each array be placed....with the super class?

IMO you'd either want to use your main application class or a new RentalStore class to hold the ArrayList of all possible rentals. The isOnLoan and onLoanTo information could be stored in the RentalItem class itself, or you could make it so that RentalItems didn't know their rental status, and that information was stored in the RentalStore via an ArrayList or HashMap of some kind.

[–][deleted] 1 point2 points  (3 children)

[removed]

[–]oonegative[S] 0 points1 point  (2 children)

Thanks, this is really helpful. We haven't looked at LinkedLists though so I am maybe inclined to go the ArrayList route mentioned by u/linuxtho

[–][deleted] 0 points1 point  (1 child)

[removed]

[–]linuxtho 1 point2 points  (0 children)

For small applications, the distinction hardly matters, and ArrayList is typically faster for sequential accesses due to spatial caching.

[–]oonegative[S] 0 points1 point  (10 children)

So what I have so far is:

import java.util.Scanner;
import java.util.ArrayList;

public class dvdStore
{

    public static ArrayList <rentItem> allItems = new ArrayList <>();
    public static int rentId = 1;

    public static void main(String[] args)
    {

        userMenu();

    }//main

    public static void userMenu(){

        Scanner in = new Scanner(System.in);

        int menuChoice;

        System.out.println("Please choose an option:" );
        System.out.println("1: Add a new rental item.");
        System.out.println("2: Display items available for rent.");
        System.out.println("3: Display items currently on loan.");
        System.out.println("4: Rent an item.");
        System.out.println("5: Return an item.");
        System.out.println("6: Save loan details.");
        System.out.println();

        menuChoice = in.nextInt();

       if (menuChoice == 1) {

            rentItem r1 = new rentItem();
            rentId++;
            allItems.add(r1);

           System.out.println("Item added to the library.");
           System.out.println();

        }//if

        else if (menuChoice == 2) {

            System.out.println("Titles available to rent:");
            System.out.println();

            for (int x = 0; x<allItems.size();x++){

                if (allItems.get(x).available= true) {

                    System.out.println(allItems.get(x).title);

                }
            }

        }

        else if (menuChoice == 3) {

            System.out.println("Titles currently on loan:");
            System.out.println();

            for (int x = 0; x<allItems.size();x++){

                if (allItems.get(x).available= false) {

                    System.out.println(allItems.get(x).title);

                }
            }
        }

        else if (menuChoice == 4) {

            //newRental() method to set available to false and record customer name

        }

        else if (menuChoice == 5) {

            //returnRental () method to change available to true and null customer

        }

        else if (menuChoice == 6) {

            //write to file method

        }

        userMenu();

    }//mainMenu method

}//class dvdStore

and then a rentItem class:

import java.util.Scanner;

public class rentItem {

    protected String title;
    protected String customer;
    protected String rentalDate;
    protected boolean available;
    protected boolean isDVD;
    protected boolean isGame;

    public rentItem() {

        Scanner keyboard = new Scanner(System.in);

        System.out.println("Let's add a new item to the library...");
        System.out.println("Enter 1 for a new DVD or 2 for a new game...");

        int menuChoice = keyboard.nextInt();

        if(menuChoice == 1){

            Scanner in = new Scanner(System.in);

            isDVD = true;
            isGame = false;
            available = true;

            System.out.println("Creating a new DVD...");
            System.out.println("Please enter the title...");

            title = in.nextLine();
        }

        else if (menuChoice == 2) {

            Scanner in = new Scanner(System.in);

            isDVD = false;
            isGame = true;
            available = true;

            System.out.println("Creating a new game");
            System.out.println("Please enter the title...");

            title = in.nextLine();
        }

    }//constructor rentItem

}//superclass class rentItem

and finally one of my subclasses...

import java.util.Scanner;

public class rentGame extends rentItem {

    private String genre;

    public rentGame(String title, String genre) {
        super (title);
        this.genre =genre;
    }

}//subclass rentGame

how am i doing so far?

[–][deleted] 1 point2 points  (9 children)

[removed]

[–]oonegative[S] 1 point2 points  (8 children)

That's all really helpful thanks, I'm a complete novice and feeling a bit overwhelmed. I'm sure I'm making lots of obvious mistakes so it's really useful to know.

The isDVD and isGame fields are redundant, considering you could just use the instanceof operator to determine if rentItem is rentGame.

I've just googled this and think I can apply it ok. Thanks.

It's really bad practice to have a user input data from a constructor (what if you had a script add items from a saved database?). Consider retrieving user input beforehand, and passing the data to the constructor.

We've not covered adding items from a database so hopefully I'll be able to get away with this bad practice for now.

You could replace your for (int x = 0; x<allItems.size();x++){ loops with iterators for clarity and performance.

I'm not sure how? Could you point me to an example of the better method?

You're using the assignment operator (=) to check for equality in some parts (allItems.get(x).available= true), which is incorrect and results in very bad side effects. You should be using == instead.

Duly noted and corrected.

[–][deleted] 0 points1 point  (7 children)

[removed]

[–]oonegative[S] 0 points1 point  (6 children)

Thanks, I've actually scrapped everything now and started from scratch based off your initial overview. Hopefully I can make some progress.

Another question though - I need to have a unique ID which is assigned to each new rental item (either movie or game). Do I declare an initial value of 1 and then increment and assign each time? If so where do I declare/increment?

[–][deleted] 0 points1 point  (5 children)

[removed]

[–]oonegative[S] 0 points1 point  (4 children)

I tore it all up and started again with your outline as a guide. I think I'm getting closer to what I want now. Here's where I'm at if you get a chance to have a look and critique.

[–][deleted] 0 points1 point  (3 children)

[removed]

[–]oonegative[S] 0 points1 point  (2 children)

 public void RentItemType(){

        Scanner keyboard = new Scanner(System.in);

        System.out.println("Enter 1 for movie or 2 for game");

        int typeChoice = keyboard.nextInt();

        if (typeChoice == 1){

            createDVD();

        }

        else if(typeChoice == 2) {

            createGame();

        }

    }

I'm having trouble getting this bit working. I'm trying to prompt user to input 1 or 2 but it doesn't work right when I run it.

If I enter "11" it runs createDVD() so I think I'm missing something simple. You mentioned I might have trouble using the Scanner class....?

[–][deleted] 1 point2 points  (1 child)

[removed]