简体   繁体   中英

How do I use contains to search through a custom object ArrayList for a particular string?

I'm completely brand new to programming (started yesterday...) and Java so excuse any stupid mistakes and really awful code (I have no clue how to order/format). I've been given a task to make an inventory of videos and I want to be able to search through the inventory to check if a particular video is there.

I know I can use contains to do this but I can't get it to work with my custom objects ArrayList (videos) and I want it to search through all the data (each InventoryRow below). I've overridden equals and HashCode but it still won't work - whenever I try to run the code it will always tell me it can't find the video even if the video is there. (FYI I use contains towards the end of my code under the rent and check functions)

I'd really appreciate any help as I've been googling all day to no avail. Also if this can't be done or another method would be better please let me know! Thanks.

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

class InventoryRow {
@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result
            + ((availability == null) ? 0 : availability.hashCode());
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    result = prime * result
            + ((returndate == null) ? 0 : returndate.hashCode());
    result = prime * result + ((type == null) ? 0 : type.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    InventoryRow other = (InventoryRow) obj;
    if (availability == null) {
        if (other.availability != null)
            return false;
    } else if (!availability.equals(other.availability))
        return false;
    if (name == null) {
        if (other.name != null)
            return false;
    } else if (!name.equals(other.name))
        return false;
    if (returndate == null) {
        if (other.returndate != null)
            return false;
    } else if (!returndate.equals(other.returndate))
        return false;
    if (type == null) {
        if (other.type != null)
            return false;
    } else if (!type.equals(other.type))
        return false;
    return true;
}

private String name;
private String type;
private Character availability;
private String returndate;

public InventoryRow(String name, String type, Character availability,
        String returndate) {
    this.name = name;
    this.type = type;
    this.availability = availability;
    this.returndate = returndate;
}

public String getReturndate() {
    return returndate;
}

public void setReturndate(String returndate) {
    this.returndate = returndate;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public Character getAvailability() {
    return availability;
}

public void setAvailability(Character availability) {
    this.availability = availability;
}

public String toString() {
    return name + "   " + type + "   " + availability + "   " + returndate;
}
}

public class InventorySort {

public static void main(String[] args) {
    List<InventoryRow> videos = new ArrayList<InventoryRow>();

    videos.add(new InventoryRow("Casablanca", "Old", 'Y', "1 January 2015"));
    videos.add(new InventoryRow("Jurassic Park", "Regular", 'N',
            "1 January 2015"));
    videos.add(new InventoryRow("2012", "Regular", 'Y', "1 January 2015"));
    videos.add(new InventoryRow("Ant-Man", "New", 'Y', "1 January 2015"));

    // Another ArrayList because I can't seem to search through the first
    // one?
    /*ArrayList<String> names = new ArrayList<String>();
    names.add("Casablanca");
    names.add("Jurassic Park");
    names.add("2012");
    names.add("Ant-Man");*/

    Scanner input = new Scanner(System.in);

    // Output the prompt
    System.out.println("What do you want to do?");

    // Wait for the user to enter a line of text
    String line = input.nextLine();

    // List, rent and check functions
    // List function
    if (line.equals("l")) {
        // Sort function
        Collections.sort(videos, new Comparator<InventoryRow>() {
            public int compare(InventoryRow o1, InventoryRow o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
        for (InventoryRow inventory : videos) {
            System.out.println(inventory);
        }
        // Rent function
    } else if (line.equals("r")) {
        System.out.println("Which video would you like to rent?");
        String line2 = input.nextLine();
        // Search through ArrayList
        if (videos.contains(line2)) {
            System.out.println("Video available to rent!");
        } else {
            System.out.println("Video unavailable to rent.");
        }
        // Check function
    } else if (line.equals("c")) {
        System.out
                .println("Which video would you like to check is in the inventory?");
        String line3 = input.nextLine();
        // Search through ArrayList
        if (videos.contains(line3)) {
            System.out.println("Video found!");
        } else {
            System.out
                    .println("Video not found. Please see the inventory below.");
            Collections.sort(videos, new Comparator<InventoryRow>() {
                public int compare(InventoryRow o1, InventoryRow o2) {
                    return o1.getName().compareTo(o2.getName());
                }
            });
            for (InventoryRow inventory : videos) {
                System.out.println(inventory);
            }
        }
        // If anything else is entered
    } else {
        System.out
                .println("The only options are to list (l), rent (r) or check (c).");
    }

}
}

You can use contains. But, for the first day of programming, it might be more understandable to simply iterate over your inventory, comparing the input string with the video name:

    boolean foundIt = false;
    for (InventoryRow ir : videos) {
        if (line3.equals(ir.getName())) {
            foundIt = true;
            break;
        }
    }
    if (foundIt) {
        System.out.println("Video found!");

Alternative to @kilo answer, you could implement equals and hashcode method only on the name of video class and check it in the following way.

String line3 = input.nextLine();
        // Search through ArrayList
        if (videos.contains(new Video(line3, null, null, null))) {
            System.out.println("Video found!");
        } 

This will return contains = true only if the name matches.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM