Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How to make an array list of objects in another object?

  1. #1
    Junior Member
    Join Date
    Jan 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to make an array list of objects in another object?

    im making a system in the style of a library where users can borrow different types of items such as books, dvds etc. i need to make an arrylist of loans so that each individual member will hold their own list of loans. im trying to make it so the "media" is referenced in the loan class by whatever the items title is but its not allowing me to create the loan objects.

    **Member Class**
    import java.util.ArrayList;

    public class FullMember extends Member {

    ArrayList<Loan>theLoans = new ArrayList<Loan>();

    public FullMember() {
    /*Loan l;
    l = new Loan(null, "12/2/12");
    theLoans.add(l);
    */
    }

    public FullMember(String newUserName, String newPassWord, String newFirstName, String newLastName) {
    super(newUserName, newPassWord, newFirstName, newLastName);
    setPrivilege("Full");
    }
    public String getMemberDetails() {
    return getId() + " " + getFirstName() + " " + getLastName() + " " + getPrivilege();
    }

    }

    **Loan Class**
    public class Loan {

    private Media title;
    private String loanDate;

    public Loan() {
    loanDate = "not Set";
    // TODO Auto-generated constructor stub
    }
    public Loan(Media Title, String loanDate) {
    this.setloanTitle(Title);
    }
    public Media getloanTitle() {
    return title;
    }
    public void setloanTitle(Media loanTitle) {
    this.title = loanTitle;
    }

    }

    **Media Class**
    public abstract class Media {
    private static int count;
    private int id;
    private String title;
    private double costPrice;
    private String publisher;


    public Media() {
    count = 0;
    id = count;
    title = "not set";
    costPrice = 0.0;
    publisher = "not set";

    }
    public Media(String newTitle, Double newPrice, String newPublisher) {
    count++;
    id = count;
    title = newTitle;
    costPrice = newPrice;
    publisher = newPublisher;

    }
    public int getID() {
    return id;
    }
    public void setID(int newID) {
    this.id = newID;
    }
    public String getTitle() {
    return title;
    }
    public void setTitle(String newTitle) {
    this.title = newTitle;
    }
    public double getCost() {
    return costPrice;
    }
    public void setCost(double cost) {
    this.costPrice = cost;
    }
    public String getPublisher() {
    return publisher;
    }
    public void setPublisher(String newPublisher) {
    this.publisher = newPublisher;
    }
    public String getMediaDetails() {
    return "no media objects should be created";
    }

    }

    My arraylist of members is held in a "Model" class but wastold that members hold thier will be an arraylist of loans inside members. Any help would be greatly appreciated and if question not clear enough will happily try and explain it better. Thanks

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to make an array list of objects in another object?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    its not allowing me to create the loan objects.
    Please copy the full text of any error messages and paste it here so we can see the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2019
    Location
    Belgium
    Posts
    13
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to make an array list of objects in another object?

    you've posted this question on the coderanch too, I've seen someone giving you a full working example
    Last edited by ufotje; January 15th, 2019 at 07:51 AM. Reason: added link

Similar Threads

  1. Struts 2 Select tag array list using objects
    By BimmyJim in forum Web Frameworks
    Replies: 0
    Last Post: November 13th, 2013, 02:13 PM
  2. Largest object in an array of objects
    By Noct in forum Java Theory & Questions
    Replies: 2
    Last Post: May 16th, 2013, 12:49 AM
  3. Array List; How to pass objects into another arraylist
    By Melvrick in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 24th, 2011, 05:55 AM
  4. Conditional statements on Array list objects
    By jaguarpaw in forum Collections and Generics
    Replies: 3
    Last Post: May 10th, 2011, 09:52 AM
  5. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM

Tags for this Thread