0

I have a class Supplement for which I've created an array of supplements. Now I want to pass this object into another class Magazine, where one instance of a magazine has many supplements. So I want to be able to pass this array of supplement objects into my Magazine constructor, and write a method in Magazine that I can use to display the Magazine name, Cost, and all the details in the supplement array it has stored. I think I've managed to successfully pass it as a parameter, but how would I be able to write a method that is able to display this array alongside its relevant magazine?

Supplement.java:

package javaapplication1;

import java.util.Arrays;

public class Supplement {
    private String supplementname;
    private int WeeklySupCost;
    private Supplement[] supplements;

    public void SetSupplementName(String supplementname){
        this.supplementname = supplementname;  
    };

    public void SetWeeklySupCost(int WeeklySupCost){
        this.WeeklySupCost = WeeklySupCost;
    };

    public String GetSupplementName(){
        return supplementname;
    };
    public int GetWeeklyCost(){
        return WeeklySupCost;
    };

    public Supplement(String supplementname, int WeeklySupCost){
        this.supplementname = supplementname;
        this.WeeklySupCost = WeeklySupCost;     
    };

    public void printSupplements(){
        System.out.println("Supplement: " + this.supplementname);
        System.out.println("Weekly Cost: " + this.WeeklySupCost); 
    }
}

Magazine.java:

package javaapplication1;

public class Magazine {
    private String magazinename;
    private int WeeklyCost;
    private Magazine magazineobj;
    private Supplement[] supplements;

    public void SetMagazineName(String magazinename){
        this.magazinename = magazinename;    
    };

    public void SetWeeklyCost(int WeeklyCost){
        this.WeeklyCost = WeeklyCost;
    };

    public String GetMagazineName(){        
        return magazinename;
    };

    public int GetWeeklyCost(){        
        return WeeklyCost;
    };

    public Magazine(String magazinename, int WeeklyCost, Supplement[] supplements){
        this.magazinename = magazinename;
        this.WeeklyCost = WeeklyCost;
        this.supplements = supplements;
    };

    public Magazine(){};
    public void printMagazine(){
        System.out.println("Magazine Name: " + this.magazinename);
        System.out.println("Weekly Cost: " + this.WeeklyCost);
        System.out.println("Supplements: " + supplements[this.supplements]);    
    }
}

Main program:

package javaapplication1;

public class JavaApplication1 {
    public static void main(String[] args) {
        Supplement[] supplements = new Supplement[4];

        supplements[0] = new Supplement("Sports Illustrated Special", 4);
        supplements[1] = new Supplement("Health and Nutrition", 2);
        supplements[2] = new Supplement("Lifestyled", 5);
        supplements[3] = new Supplement("Gamer's Update", 3);

        Magazine magazineobj = new Magazine("The Wheels Special", 35, supplements);
        for(int i = 0; i < 4; i++){
            magazineobj.printMagazine();
            supplements[i].printSupplements();   
        }
        //System.out.println(magazineobj);
    }
}
3
  • why does your supplement class have an Array of supplements as field? and why does your magazine class have a magazine as a field? Also in java after the end of a method there is no semicolon Commented Apr 23, 2020 at 7:03
  • I was using them earlier to try creating a method that takes an object array as a parameter and fills in some details automatically, just forgot to remove it. And not to be rude you are correct, but this does not in any way help me with my problem Commented Apr 23, 2020 at 7:12
  • Sure, this was not meant to solve your problem, just some remarks. If I understand your question correctly, you want to implement a toString() method for both your Magazine and Supplement classes. This method gets called when printing an object to console. Commented Apr 23, 2020 at 7:30

3 Answers 3

1

Rather using printMethod() use toString() method of Object class and override in your class.

public class Magazine {

    @Override
    public String toString() {
        String str = "Magazine Name: " + magazinename + "\nWeekly Cost: " + WeeklyCost + "\nSupplements: ";
        for (Supplement supplement : supplements) {
            str += supplement;
        }
        return str;
    }

//    public void printMagazine(){
//        System.out.println("Magazine Name: " + this.magazinename);
//        System.out.println("Weekly Cost: " + this.WeeklyCost);
//        System.out.println("Supplements: " + supplements[this.supplements]);    
//    }

}

class Supplement {

    @Override
    public String toString() {
        return "Supplement: " + supplementname + "\nWeekly Cost: " + WeeklySupCost;
    };

//    public void printSupplements(){
//        System.out.println("Supplement: " + this.supplementname);
//        System.out.println("Weekly Cost: " + this.WeeklySupCost); 
//    }
}

// In Main
//for(int i = 0; i < 4; i++){
//    magazineobj.printMagazine();
//    supplements[i].printSupplements();   
//}
System.out.println(magazineobj);
Sign up to request clarification or add additional context in comments.

2 Comments

This works perfectly for me! Could you explain the logic behind the for loop in toString under the magazine class?
for (Supplement supplement : supplements) { // Iterate over an Array (Take each element from supplements and put into supplement) str += supplement; // Call toString() method for every element supplement }
1

To print elements of an array, you can use Arrays.toString(arr). Please find the below code and check whether it fulfills your requirement.

Supplement.java

    import java.util.Arrays;
    public class Supplement {
    private String supplementname;
    private int WeeklySupCost;
    private Supplement[] supplements;

    public void SetSupplementName(String supplementname) {
        this.supplementname = supplementname;
    }

    public void SetWeeklySupCost(int WeeklySupCost) {
        this.WeeklySupCost = WeeklySupCost;
    }

    public String GetSupplementName() {
        return supplementname;
    }

    public int GetWeeklyCost() {
        return WeeklySupCost;
    }

    public Supplement(String supplementname, int WeeklySupCost) {
        this.supplementname = supplementname;
        this.WeeklySupCost = WeeklySupCost;
    }

    public void printSupplements() {
        System.out.println("Supplement: " + this.supplementname);
        System.out.println("Weekly Cost: " + this.WeeklySupCost);
        System.out.println("supplements: " + Arrays.toString(this.supplements));
    }
    @Override
    public String toString() {
        return "Supplement{" +
            "supplementname='" + supplementname + '\'' +
            ", WeeklySupCost=" + WeeklySupCost +
            ", supplements=" + Arrays.toString(supplements) +
            '}';
    }
}

Magazine.java:

import java.util.Arrays;
public class Magazine {
    private String magazinename;
    private int WeeklyCost;
    private Magazine magazineobj;
    private Supplement[] supplements;

    public void SetMagazineName(String magazinename) {
        this.magazinename = magazinename;
    }

    public void SetWeeklyCost(int WeeklyCost) {
        this.WeeklyCost = WeeklyCost;
    }

    public String GetMagazineName() {
        return magazinename;
    }

    public int GetWeeklyCost() {
        return WeeklyCost;
    }

    public Magazine(String magazinename, int WeeklyCost, Supplement[] supplements) {
        this.magazinename = magazinename;
        this.WeeklyCost = WeeklyCost;
        this.supplements = supplements;
    }

    public Magazine() {
    }

    public void printMagazine() {
        System.out.println("Magazine Name: " + this.magazinename);
        System.out.println("Weekly Cost: " + this.WeeklyCost);
        System.out.println("Supplements: " + Arrays.toString(this.supplements));
    }

}

Main.java:

public class Main {
    public static void main(String[] args) {
        Supplement[] supplements = new Supplement[4];
        supplements[0] = new Supplement("Sports Illustrated Special", 4);
        supplements[1] = new Supplement("Health and Nutrition", 2);
        supplements[2] = new Supplement("Lifestyled", 5);
        supplements[3] = new Supplement("Gamer's Update", 3);

        Magazine magazineobj = new Magazine("The Wheels Special", 35, supplements);
        for (int i = 0; i < 4; i++) {
            magazineobj.printMagazine();
            supplements[i].toString();
        }
    }
}

You can also use the toString() method to print the values of the above objects.

1 Comment

The variable 'Arrays' could not be found in my magazine class, but after invoking the toString methods to both my classes from the answer below it seems to work
1

Is not much more simple to use ArrayList? Look below:

Supplement class:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Supplement {
    private String supplementname;
    private int WeeklySupCost;
    public Supplement(String supplementname, int weeklySupCost) {
        this.supplementname = supplementname;
        WeeklySupCost = weeklySupCost;
    }
    public String getSupplementname() {
        return supplementname;
    }
    public void setSupplementname(String supplementname) {
        this.supplementname = supplementname;
    }
    public int getWeeklySupCost() {
        return WeeklySupCost;
    }
    public void setWeeklySupCost(int weeklySupCost) {
        WeeklySupCost = weeklySupCost;
    }
    @Override
    public String toString() {
        return "Supplement [supplementname=" + supplementname + ", WeeklySupCost=" + WeeklySupCost + "]";
    }


}

Magazine class:

public class Magazine {
    private String magazinename;
    private int WeeklyCost;
    private List<Supplement> list;
    public Magazine(String magazinename, int weeklyCost, List<Supplement> list) {
        this.magazinename = magazinename;
        WeeklyCost = weeklyCost;
        this.list = list;
    }
    public String getMagazinename() {
        return magazinename;
    }
    public void setMagazinename(String magazinename) {
        this.magazinename = magazinename;
    }
    public int getWeeklyCost() {
        return WeeklyCost;
    }
    public void setWeeklyCost(int weeklyCost) {
        WeeklyCost = weeklyCost;
    }
    public List<Supplement> getList() {
        return list;
    }
    public void setList(List<Supplement> list) {
        this.list = list;
    }
    @Override
    public String toString() {
        return "Magazine [magazinename=" + magazinename + ", WeeklyCost=" + WeeklyCost + ", list=" + list + "]";
    }



}

JavaApplication1 class:

import java.util.ArrayList;
import java.util.List;

public class JavaApplication1 {
    public static void main(String[] args) {
        List<Supplement> supplements = new ArrayList<Supplement>();

        supplements.add(new Supplement("Sports Illustrated Special", 4));
        supplements.add(new Supplement("Health and Nutrition", 2));
        supplements.add(new Supplement("Lifestyled", 5));
        supplements.add(new Supplement("Gamer's Update", 3));

        Magazine magazineobj = new Magazine("The Wheels Special", 35, supplements);

        System.out.println(magazineobj);

        //System.out.println(magazineobj);
    }
}

The output is :

Magazine [magazinename=The Wheels Special, WeeklyCost=35, list=[Supplement [supplementname=Sports Illustrated Special, WeeklySupCost=4], Supplement [supplementname=Health and Nutrition, WeeklySupCost=2], Supplement [supplementname=Lifestyled, WeeklySupCost=5], Supplement [supplementname=Gamer's Update, WeeklySupCost=3]]]

And you can change the toString() method to show as you wish and you reduce the code a lot.

2 Comments

This does seems a little easier to understand, and it works just fine. Thank you so much, now I can apply the same concept for my other classes with similar requirements
I am glad I could help you :)

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.