How to create an Arraylist with constructor method with String

Asked

Viewed 166 times

-1

Hello, I would like to know how I could add these variables within this Arraylist.

The program says it is not possible to put String in Publication, I tried casting and did not give.

Thanks in advance.


The question posed by the teacher: In the Academiclibrary class, define a constructor method that takes as parameter values for the attributes name, Description and creationDate. Within this constructor, initialize the corresponding attributes of the class with the values passed and also instantiate an Arraylist called Publications to store the library’s list of publications.


Classe Academiclibrary:

public class AcademicLibrary implements ILibrary{

    //Atributes:
    private String name;
    private String description;
    private String creationDate;
    private ArrayList<Publication> publications;
    private ArrayList<User> users;

    //Operations:
    public String getName() {
        return name;
    }

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

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(String creationDate) {
        this.creationDate = creationDate;
    }

    public ArrayList<Publication> getPublications() {
        return publications;
    }

    public void setPublications(ArrayList<Publication> publications) {
        this.publications = new ArrayList<>();
    }

    public ArrayList<User> getUsers() {
        return users;
    }

    public void setUsers(ArrayList<User> users) {
        this.users = users;
    }

    @Override
    public void addPublication(Publication p) {
        publications.add(p);
    }

    @Override
    public boolean removePublication(long id) {
        publications.remove(id);
        return false;
    }

    @Override
    public ArrayList<Publication> getAllPublication() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public short countPublications() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Publication findPublication(String title) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean hasPublication(String title) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    //Methods:
    public AcademicLibrary(String name, String description, String creationDate) {
        this.name = name;
        this.description = description;
        this.creationDate = creationDate;

        ArrayList<Publication> publication;
        
        publication = new ArrayList<>();

        publication.add(this.name, this.description, this.creationDate); // esse não está funcionando


    }
}

Publication Class:

public abstract class Publication {
    
    //Atributes:
    private String title;
    private short year;
    private byte volume;
    private ArrayList<Author> authors;
    
    //Operations:

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public short getYear() {
        return year;
    }

    public void setYear(short year) {
        this.year = year;
    }

    public byte getVolume() {
        return volume;
    }

    public void setVolume(byte volume) {
        this.volume = volume;
    }

    public ArrayList<Author> getAuthors() {
        return authors;
    }

    public void setAuthors(ArrayList<Author> authors) {
        this.authors = authors;
    }
    
}

Test Class:

public class Tester {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
    
        AcademicLibrary book1 = new AcademicLibrary(input.next(),input.next(),input.next());
        
        System.out.println(book1.getName());
        System.out.println(book1.getDescription());
        System.out.println(book1.getCreationDate());
        
        System.out.println(book1.getPublications()); // não funciona  
    }
}

Error message:

    Picked up _JAVA_OPTIONS: -Xmx512M
X:\UNIJUI\4º Semestre\Linguagem de Programação I\exercicios netbeans\digital-library\src\com\acme\avaliacaoII\AcademicLibrary.java:114: error: no suitable method found for add(String,String,String)
        publication.add(this.name, this.description, this.creationDate); // esse não está funcionando
    method ArrayList.add(Publication,Object[],int) is not applicable
      (argument mismatch; String cannot be converted to Publication)
    method ArrayList.add(Publication) is not applicable
      (actual and formal argument lists differ in length)
    method ArrayList.add(int,Publication) is not applicable
      (actual and formal argument lists differ in length)
1 error
BUILD FAILED (total time: 1 second)

1 answer

0


public AcademicLibrary(String name, String description, String creationDate) {
    this.name = name;
    this.description = description;
    this.creationDate = creationDate;

    ArrayList<Publication> publications = new ArrayList<>();
}

When you create Academiclibrary the list of publications will be empty. To fill in just create a new post and use the add method.

 Publication pub = new Publication();
 //Set atributos...
 publications.add(pub);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.