I am new to Java and Im practicing creating classes, objects and getting variables from classes. I created a class Factory which includes 2 string variables and one array of object from different class called Item which includes 2 string variables. In my main program I want to sout variables from Factory class. I managed to print name and address but getItem() obviously won't print getName() string from Item class. I tried overriding with toString in Factory class but it doesn't work with this type of custom class I guess. What can I do in this situation?
Class Factory:
public class Factory {
public static final int MAX_ODABRANIARTIKLI = 3;
String name;
String address;
Item[] items = new Item[MAX_ODABRANIARTIKLI];
public Factory(String name, String address, Item[] items) {
this.name = name;
this.address = address;
this.items = items;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public Item[] getItems() {
return items;
}
public void setName(String name) {
this.name = name;
}
public void setAddress(String address) {
this.address = address;
}
public void setItems(Item[] items) {
this.items = items;
}
}
class Item:
public class Item {
String name;
String category;
}
public Item(String name, String category) {
this.name = name;
this.category = category;
}
public String getName() {
return name;
}
public String getCategory() {
return category;
}
public void setName(String name) {
this.name = name;
}
public void setCategory(String category) {
this.category = category;
}
}
main code:
import hr.java.production.model.Factory;
public class Main {
public static final int MAX_TVORNICA = 2;
public static void main(String[] args) {
Factory[] tvornice = new Factory[MAX_TVORNICA];
System.out.println("Unijeli ste sljedeće tvornice:");
for(Factory factory : tvornice){
ispisTvornica(factory);
}
}
public static void ispisTvornica(Factory factory){
System.out.println("Naziv: " factory.getName());
System.out.println("Web adresa: " factory.getAddress());
System.out.println("Odabrani artikli: " factory.getItems());
}
CodePudding user response:
Override toString() method in Item class which returns name then you can use Arrays.toString(factory.getItems())
CodePudding user response:
Try This
Factory.Java
public class Factory {
public static final int MAX_ODABRANIARTIKLI = 3;
String name;
String address;
Item[] items = new Item[MAX_ODABRANIARTIKLI];
public Factory(String name, String address, Item[] items) {
this.name = name;
this.address = address;
this.items = items;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public Item[] getItems() {
return items;
}
public void setName(String name) {
this.name = name;
}
public void setAddress(String address) {
this.address = address;
}
public void setItems(Item[] items) {
this.items = items;
}
}
Item.java
public class Item {
String name;
String category;
public Item(String name, String category) {
this.name = name;
this.category = category;
}
public String getName() {
return name;
}
public String getCategory() {
return category;
}
public void setName(String name) {
this.name = name;
}
public void setCategory(String category) {
this.category = category;
}
@Override
public String toString() {
return "Item [name=" name "]";
}
}
Main.java
public class Main {
public static final int MAX_TVORNICA = 2;
public static void main(String[] args) {
int i = 0;
Factory[] tvornice = new Factory[MAX_TVORNICA];
System.out.println("Unijeli ste sljedeće tvornice:");
Item[] items = { new Item("Joy", "category_Joy"), new Item("Roy", "category_Roy") };
tvornice[0] = new Factory("Jems", "ABC", items);
tvornice[1] = new Factory("ivuksan", "XYZ", items);
for (Factory factory : tvornice) {
ispisTvornica(factory, i );
}
System.out.println("------------------------------------------------------------------------");
for (Factory factory : tvornice) {
ispisTvornicaAnother(factory);
}
}
public static void ispisTvornica(Factory factory, int i) {
System.out.println("Naziv: " factory.getName());
System.out.println("Web adresa: " factory.getAddress());
System.out.println("Odabrani artikli: " factory.getItems()[i]);
}
public static void ispisTvornicaAnother(Factory factory) {
System.out.println("Naziv: " factory.getName());
System.out.println("Web adresa: " factory.getAddress());
for(Item item : factory.getItems())
System.out.println("Odabrani artikli: " item);
}
}
I define two function:
- ispisTvornica() function print for each iteration 1 by 1 item name present in Item class
- ispisTvornicaAnother() function print for each iteration it print all items name.
Output:-
Unijeli ste sljedeće tvornice:
Naziv: Jems
Web adresa: ABC
Odabrani artikli: Item [name=Joy]
Naziv: ivuksan
Web adresa: XYZ
Odabrani artikli: Item [name=Roy]
------------------------------------------------------------------------
Naziv: Jems
Web adresa: ABC
Odabrani artikli: Item [name=Joy]
Odabrani artikli: Item [name=Roy]
Naziv: ivuksan
Web adresa: XYZ
Odabrani artikli: Item [name=Joy]
Odabrani artikli: Item [name=Roy]