I want to just take the data from menSize and its price but it keep printing out the womenSize eventhought it doesn't add anything in it.
Do I need to seperate menSize class and womenSize class or is there any way to fix it:
Here is my Getter and Setter of Shoes class:
private double menSize;
private double price;
private double womenSize;
This is my ShoesController class:
@GetMapping("menshoes") public List getMenShoes() {
List<Shoes> menShoesList = new ArrayList<>(); // Create ArrayList holding men sizes
while (true) {
for (double i = 2; i <= 12; i = 0.5) { // set sizes : 2 , 2.5 ,... until 12
Shoes shoe = new Shoes();
shoe.setMenSize(i);
shoe.setPrice(4); // Set price to $4
menShoesList.add(shoe); // add size to ArrayList
}
return menShoesList;
}
}
@GetMapping("womenshoes")
public List<Shoes> getWomenShoes() {
List<Shoes> womenShoesList = new ArrayList<>(); // Create ArrayList holding women sizes
while (true) {
for (double i = 5; i <= 12; i = 0.5) { // set sizes : 5, 5.5 , ... until 12
Shoes shoe2 = new Shoes();
shoe2.setWomenSize(i);
shoe2.setPrice(4); // Set price to $4
womenShoesList.add(shoe2); // add size to ArrayList
}
return womenShoesList;
}
}
The output when I search for localhost:8080/menshoes are (I just take 3 for examples):
[{"menSize":2.0,"price":"$4.00","womenSize":0.0},{"menSize":2.5,"price":"$4.00","womenSize":0.0},{"menSize":3.0,"price":"$4.00","womenSize":0.0}
CodePudding user response:
I see 2 options: Either having 2 classes, one for each type of shoe or defining the type inside your class and not having two sizes of which one is always empty. Since you have a limited amount of possible types I suggest you use an enum for modeling the type.
public enum ShoeType {
MEN,
WOMEN
}
public class Shoe {
public ShoeType type;
public double size;
public price;
// getter, setter, constructor...
}
CodePudding user response:
Example of this comment: "I would highly recommend only having one [class] type similar to what you suggested"
Default constructor
public class Shoes
{
this(null, 0, 0);
}
Overloaded constructor
public class Shoes(String gender, double size, double price)
{
this.gender = gender;
this.size = size;
this.price = price;
}