I would like to make a constructor with an optional parameter, to avoid giving in Strings all the time, because I need to use the constructor like a thousand times, so I would like to call methods instead for some parameters, like color, with the same 10 options every time.
I dont want to constantly type "silver", " ancient silver", "antique bronze", "rose gold" etc with "", and having to fully type it out, i would love to have the parameter preconditioned with methods, that change my String color according to which method I just called in.
Here is the constructor in the Charms class:
public Charms(String charmName, //here is the place for the color// , String charmMaterial, double bulkPrice, int piecesPerBulk) {
this.charmName = charmName;
this.charmColor = //here is the place for the methods where I want to have the options//;
this.charmMaterial = charmMaterial;
this.bulkPrice = bulkPrice;
this.piecesPerBulk = piecesPerBulk;
this.pricePerPiece = bulkPrice / piecesPerBulk;
}
Here are some of the methods I would love to make work in the Optionc class:
private String color;
public void silverColor(String color) {
this.color = "silver";
}
public void ancientSilverColor(String color) {
this.color = "ancient silver";
}
public void antiqueBronzeColor (String color){
this.color = "antique bronze";
}
public void roseGoldColor(String color) {
this.color = "rose gold";
}
So when I call it in the Components class:
Charms sewingCharms = new Charms("sewingCharms", //here i just start to call whichever method I want for color and it runs smoothly//, "metal", 785.69, 20);
I was trying to figure out whether I need a switch for the methods to call them in one place or an if-else wrapped in a contructor or another method, it just can't seem to work for me and now my brain is just flooded.
Thanks for any help, kind critics are welcome, please do not humiliate me, I'm still learning and trying.
I would suggest to create an enum like this:
public enum Color {
SILVER("silver"),
ANCIENT_SILVER("ancient silver"),
ANTIQUE_BRONZE("antique bronze"),
ROSE_GOLD("rose gold");
private final String displayName;
Color(String displayName) {
this.displayName = displayName;
}
public String getDisplayName() {
return displayName;
}
}
and Charms
class:
public class Charms {
private String charmName;
private String charmColor;
private String charmMaterial;
private double bulkPrice;
private int piecesPerBulk;
private double pricePerPiece;
public Charms(String charmName, Color charmColor, String charmMaterial, double bulkPrice, int piecesPerBulk) {
this.charmName = charmName;
this.charmColor = charmColor.getDisplayName(); // Use getDisplayName method from enum
this.charmMaterial = charmMaterial;
this.bulkPrice = bulkPrice;
this.piecesPerBulk = piecesPerBulk;
this.pricePerPiece = bulkPrice / piecesPerBulk;
}
}
or simple like this:
public class Charms {
private String charmName;
private Color charmColor; // enum here
private String charmMaterial;
private double bulkPrice;
private int piecesPerBulk;
private double pricePerPiece;
public Charms(String charmName, Color charmColor, String charmMaterial, double bulkPrice, int piecesPerBulk) {
this.charmName = charmName;
this.charmColor = charmColor; // simple assign to enum field
this.charmMaterial = charmMaterial;
this.bulkPrice = bulkPrice;
this.piecesPerBulk = piecesPerBulk;
this.pricePerPiece = bulkPrice / piecesPerBulk;
}
}
Usage:
Charms sewingCharms = new Charms("sewingCharms", Color.SILVER, "metal", 785.69, 20);