Search code examples
javaarraylistsortedlist

how to create multiple instances of a class?


I have a class Borrower which consists of a name and then it also stores an arraylist full of items that the person has borrowed.

I want to be able to create multiple instances of this class from my main and then be able to access them to view the items they have borrowed.

I am having trouble understanding how to create multiple instances. I just keep running into the issue of overwriting the the Borrowed class.

So in the code below if I create newBorrower("Tim") and then addItem("Wrench"), and then go to create newBorrower("john") then I overwrite newBorrower.

I want to be able to create multiple instances of Borrower based on user input?

I've tried saving the entire Borrower class. I'm not sure if that would work, because it will not sort so I can't add multiple names or I get an error.

Borrower Class

public class Borrower 
{

    protected String name;
    protected String item;

    ArrayList<String> itemList = new ArrayList<String>();

    public Borrower()
    { 
    }

    public Borrower(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return name;
    }

    public void addItem(String item)
    {
        this.item = item;
        itemList.add(item);
    }

Main Class

public class WhoBorrowedIt 
{

    public static void main(String[] args) 
    {
        ArraySortedList<String> borrowersList = new ArraySortedList<String>();
        Borrower newBorrower = new Borrower();
        Borrower otherBorrower = new Borrower();

        Scanner inName = new Scanner(System.in);
        Scanner inItem = new Scanner(System.in);

        String item;
        String name;

        String menu;
        int option;

        menu = "Make a selection: " + "\n" 
                + "1. Add Borrower" + "\n"
                + "2. Add Item Borrowed" + "\n"
                + "3. Remove Item Returned" + "\n"
                + "4. View Borrowers" + "\n"
                + "5. Exit";

        do
        {
            Scanner in = new Scanner(System.in);
            System.out.println(menu);
            option = in.nextInt();

            switch(option)
            {
                case 1: //create borrower
                {
                    System.out.println("Enter Name");
                    name = inName.nextLine();
                    newBorrower = new Borrower(name);
                    borrowersList.add(newBorrower.getName());
                    break;
                }

                case 2: //add items
                {
                    System.out.println("Enter item");
                    item = inItem.nextLine();
                    System.out.println("Who is borrowing");
                    name = inName.nextLine();
                    if(borrowersList.contains(name))
                    {
                        newBorrower.addItem(item);
                    }
                    else
                    {
                        newBorrower = new Borrower(name);
                        borrowersList.add(newBorrower.getName());
                        newBorrower.addItem(item);
                    }
                }
        }while(option != 5)
}
}

Solution

  • Use a Map like this

    if (borrowersMap.containsKey(name))
    {
        borrowersMap.get(name).addItem(item)
    }
    else
    {
        newBorrower = new Borrower(name);
        borrowersMap.put(newBorrower.getName(), newBorrower);
        newBorrower.addItem(item);
    }
    

    where borrowersMap is a HashMap

    Map<String, Borrower> borrowersMap = new HashMap<String, Borrower>();