2

I am new to Java, I apologize if I am not using the correct lingo.

A list is loaded from a text file

static List<Media> list=new ArrayList<>();;

public boolean LoadMedia(String path) {
try {
File myObj = new File(path);
Scanner myReader = new Scanner(myObj);
    while (myReader.hasNextLine()) {
    String data = myReader.nextLine();
    String[] str = data.split("-");
    
    if (str[0].equals("EBook")) {
        list.add(new EBook(str[0], Integer.parseInt(str[1]), str[2], Integer.parseInt(str[3]), str[4], Boolean.parseBoolean(str[5])));
    } else if (str[0].equals("MusicCD")) {
        list.add(new MusicCD(str[0], Integer.parseInt(str[1]), str[2], Integer.parseInt(str[3]), str[4], Boolean.parseBoolean(str[5])));
    } else if(str[0].equals("MovieDVD")) {
        list.add(new MovieDVD(str[0], Integer.parseInt(str[1]), str[2], Integer.parseInt(str[3]), str[4], Boolean.parseBoolean(str[5])));

        }
        
    }

I have a Constructor in a class called media

public Media(String name, int id, String title, int year, String chapter, boolean available) {
    this.name = name;
    this.id = id;
    this.title = title;
    this.year = year;
    this.chapter = chapter;
    this.available = available;
}

This gets output to a string

@Override
public String toString() {
        return name + " [id=" + this.id + " title=" + this.title + " chapters=" + this.chapter + " year=" + this.year + " available=" + this.available + "]\n";
}

When a User rents an item:

public void rentMedia(int id) {
    for(Media mediaById : list ) {
        if(mediaById.getId()==id) {
            if(mediaById.isAvailable()) {
                System.out.println("media successfully rented out ");
                
                // some code here to update Boolean from true to false

            } else { 
                System.out.println("Media with id="+id+" is not available for rent ");
            }
        }
    }
}

When the output is loaded again I would like for the output to now say false

I have tired list.set however get the following error:

list.set(5, false);

The method set(int, Media) in the type List<Media> is not applicable for the arguments (int, boolean)

As mentioned by Louis Wasserman and Traian GEICU

list.get(5).setAvailable(false);

Required me to create a setAvailable see below

public void rentMedia(int id) {
    for(Media mediaById : list ) {
        if(mediaById.getId()==id) {
            if(mediaById.isAvailable()) {
                System.out.println("media successfully rented out ");
                System.out.print(mediaById.toString());
                list.get(5).setAvailable(false);
            } else { 
                System.out.println("Media with id="+id+" is not available for rent ");
            }
        }
    }
}

public boolean setAvailable(boolean b) {
    available = false;
    return available;
}

Which threw an error

EBook [id=121 title=Shadow and Bone chapters=20 year=2012 available=true]
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 5 out of bounds for length 2
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:359)
at java.base/java.util.ArrayList.get(ArrayList.java:427)
at Manager.rentMedia(Manager.java:82)
at FinalProject.main(FinalProject.java:91)

Setting the index to something below 2 still did not change the true to false, not that it would but what I was hoping to see was one of the indexes change to false or get an error.

list.get(1).setAvailable(false);

Output:

Enter media id :
121
media successfully rented out 
EBook [id=121 title=Shadow and Bone chapters=20 year=2012 available=true]

Please see screen shot below.

enter image description here

6
  • 1
    Are you looking for list.get(5).setAvailable(false);? Commented Aug 8, 2021 at 15:54
  • list.set(5, false); should be invoked with as instance of Media. list.set(5, new Media("name" ... , false). Now think if is more advantageous to replace and Object with new one just for update one field, or maybe use get to retrieve object and just update the field. Commented Aug 8, 2021 at 16:04
  • @LouisWasserman, I made an edit to the OP, I tried to post as a comment however did not make any sense. Thank you Commented Aug 8, 2021 at 16:13
  • In rent media you should update with an appropriate id : list.get(id).setAvailable(false); also setAvailable should be placed on Media.(related to current record) . Mainly even if is not recommended to update fields directly you could check also with list.get(id).available =false (assume field is public) Commented Aug 8, 2021 at 16:29
  • 1
    Your setAvailable method is broken: It completely ignores its argument, always sets available to false regardless of argument, and then for some bizarre reason returns false. You just want this.available = available;. Commented Aug 8, 2021 at 16:39

1 Answer 1

1

Maybe this could be helpful.

class TestUpdate{
    
    public static void main(String[] args)
    {
        List<Media> l = new ArrayList<Media>();
        l.add(new Media("name", 10, "t1",2020,"C1", true));
        l.add(new Media("name", 20, "t2",2021,"C2", true));
        updateAvailable(l, 10);
        l.forEach(System.out::println);
        
        //update directly on list index 1 _ id=20
        l.get(1).available = false;
        l.forEach(System.out::println);
        
    }
    public static void updateAvailable(List<Media> list,int id)
    {
        for(Media media:list)
        {
            if(media.getId()==id)
            {
                media.setAvailable(false);
            }
        }
    }
    static class Media
    {
        private String name;
        private int id;
        private String title;
        private int year;
        private String chapter;
        public boolean available;

        public Media(String name, int id, String title, int year, 
                     String chapter, boolean available) {
            this.name = name;
            this.id = id;
            this.title = title;
            this.year = year;
            this.chapter = chapter;
            this.available = available;
        }

        public boolean isAvailable() {
            return available;
        }

        public void setAvailable(boolean available) {
            this.available = available;
        }
        
        public int getId() {
            return id;
        }

        
        @Override
        public String toString() {
                return name + " [id=" + this.id + " title=" + this.title +
                       " chapters=" + this.chapter + " year=" + this.year + 
                       " available=" + this.available + "]\n";
        }
    }
}

Output

name [id=10 title=t1 chapters=C1 year=2020 available=false]
name [id=20 title=t2 chapters=C2 year=2021 available=true]

name [id=10 title=t1 chapters=C1 year=2020 available=false]
name [id=20 title=t2 chapters=C2 year=2021 available=false]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.