I want to store all the variables values passed in constructor to ArrayList declared in the same class. Please look at the code below:
This is the "Additions" class:
public class Additions {
private ArrayList<Boolean> additions;
private boolean salad;
private boolean tomato;
private boolean cucumber;
private boolean onion;
public Additions(boolean salad, boolean tomato, boolean cucumber, boolean onion) {
this.salad = salad;
this.tomato = tomato;
this.cucumber = cucumber;
this.onion = onion;
}
}
And this is the main class, where I'm declaring my object:
public class Main {
public static void main(String[] args) {
Additions additions = new Additions(true, true, true, true);
}
}
Okay, as I mentioned. My problem is to store all values passed in the constructor above, to this class's field named additions.
The reason I want to do this, is display declared values (eg. [true, true, true, true]) in console.
Thanks for your help in advance!
CodePudding user response:
First solution is you can easily add them to the ArrayList and override toString
public class Additions {
private ArrayList<Boolean> additions;
private boolean salad;
private boolean tomato;
private boolean cucumber;
private boolean onion;
public Additions(boolean salad, boolean tomato, boolean cucumber, boolean onion) {
this.salad = salad;
this.tomato = tomato;
this.cucumber = cucumber;
this.onion = onion;
this.additions = new ArrayList<>();
additions.add(salad);
additions.add(tomato);
additions.add(cucumber);
additions.add(onion);
}
@Override
public String toString() {
return additions.toString();
}
}
Another solution is to replace your boolean variables with a single map so you can add or remove any number of attributes easily without changing the number of variables, any you can check by name if this attributes is enabled or not and also override to string
public class AdditionsMap {
private Map<String, Boolean> attributesMap;
public AdditionsMap(boolean salad, boolean tomato, boolean cucumber, boolean onion) {
attributesMap = new HashMap<>();
attributesMap.put("salad", salad);
attributesMap.put("tomato", tomato);
attributesMap.put("cucumber", cucumber);
attributesMap.put("onion", onion);
}
public boolean isAttributeEnable(String name) {
return attributesMap.getOrDefault(name, false);
}
@Override
public String toString() {
return attributesMap.toString();
}
}
public static void main(String... args) {
Additions additions = new Additions(true, true, true, true);
System.out.println(additions);
AdditionsMap additionsMap = new AdditionsMap(true, true, true, true);
System.out.println(additionsMap);
}
Output will be like
[true, true, true, true]
{salad=true, cucumber=true, onion=true, tomato=true}
CodePudding user response:
If you just want to be able to print [true,true,true,true], you don't need a list. Simply override toString as below.
class Additions {
private boolean salad;
private boolean tomato;
private boolean cucumber;
private boolean onion;
public Additions(boolean salad, boolean tomato, boolean cucumber, boolean onion) {
this.salad = salad;
this.tomato = tomato;
this.cucumber = cucumber;
this.onion = onion;
}
public String toString(){
return String.format("[%s, %s, %s, %s]",
salad, tomato, cucumber,onion);
}
}
public class Main{
public static void main(String[] args) {
Additions a = new Additions(true,false,true,false);
Additions b = new Additions(false,false,true,false);
System.out.println(a);
System.out.println(b);
}
}
CodePudding user response:
record
You can accomplish your goal automatically, with no code, by using the record feature in Java 16 .
With a record, your merely need to specify the name and type of each member field.
record Additions ( boolean salad, boolean tomato, boolean cucumber, boolean onion ) {}
That’s all. By default, in a record the compiler implicitly generates the constructor, getters, equals
& hashCode
, and toString
. The toString
implementation produces text with a label for each member field.
A record is appropriate where the main purpose of your class is to communicate data transparently and immutably.
Additions a = new Additions( true, true, true, true ) ;
System.out.println( a ) ;
If you do not want a report that includes the member field names as labels, add a method to generate your desired text.
String report()
{
String message =
"["
String.join( ", " , this.salad, this.tomato, this.cucumber, this.onion )
"]"
;
return message ;
}