Finish the WeightView by using a JComboBox.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 11 Jan 2019 17:29:53 +0000 (18:29 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 11 Jan 2019 17:29:53 +0000 (18:29 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/Size.java
src/SizeView.java
src/Weight.java
src/WeightView.java

index a0e7d780900685c817fd46e26a46ffb5303614c5..a61f24cb79769b34c3a8e8b502542621232a81ed 100644 (file)
@@ -31,7 +31,7 @@ public class Size implements Comparable<Size> {
     }
 
     private boolean validateSize(int size) {
-        return (size >= min && size <= max);
+        return (size >= getMin() && size <= getMax());
     }
 
     @Override
index 781f397c58acaeae55e273c56b1bc0ff375ca1e8..e9e16f62812d2276591c26b67bdf53ad5ceb0b56 100644 (file)
@@ -8,7 +8,7 @@ public class SizeView extends JPanel {
     SizeView(Size sizeObj) {
         setSizeObj(sizeObj);
         this.label = new JLabel();
-        this.label.setText("Size");
+        this.label.setText("Size (meters)");
         SpinnerNumberModel numberModel = new SpinnerNumberModel((this.getSizeObj().getMax() - this.getSizeObj().getMin()) / 2,
                 this.getSizeObj().getMin(),
                 this.getSizeObj().getMax(),
@@ -16,7 +16,6 @@ public class SizeView extends JPanel {
         this.spinner = new JSpinner(numberModel);
         if (this.getSizeObj().getSize() != 0)
             this.spinner.setValue(this.getSizeObj().getSize());
-
         add(label);
         add(spinner);
     }
index 5e33327fac7ec77eac23a14ec0aa46a6b4e65342..4b67611af0eef2f07bfa7437e498d9418298af7c 100644 (file)
@@ -1,4 +1,7 @@
 public class Weight implements Comparable<Weight> {
+    private int min = 2;
+    private int max = 600;
+    private int defaultWeight = 72;
     private int weight;
 
     Weight() {
@@ -8,6 +11,18 @@ public class Weight implements Comparable<Weight> {
         setWeight(weight);
     }
 
+    public int getMin() {
+        return min;
+    }
+
+    public int getMax() {
+        return max;
+    }
+
+    public int getDefaultWeight() {
+        return defaultWeight;
+    }
+
     public int getWeight() {
         return weight;
     }
@@ -16,12 +31,21 @@ public class Weight implements Comparable<Weight> {
         if (validateWeight(weight)) {
             this.weight = weight;
         } else {
-            throw new IllegalArgumentException("Weight cannot be negative or zero");
+            throw new IllegalArgumentException("Weight must be between " + this.getMin() + " and " + this.getMax());
         }
     }
 
     private boolean validateWeight(int weight) {
-        return (weight > 0);
+        return (weight >= getMin() && weight <= getMax());
+    }
+
+    public Integer[] getValuesArray() {
+        int arrayLength = this.getMax() - this.getMin() + 1;
+        Integer[] intArray = new Integer[arrayLength];
+        for (int i = 0; i < intArray.length; i++) {
+            intArray[i] = this.getMin() + i;
+        }
+        return intArray;
     }
 
     @Override
index 9e7ef20d8c0744377cef894240f2e82f6ce0f60e..fd6d68cb40d0e974ca32b226fb3d80cb6611a9a7 100644 (file)
@@ -3,12 +3,19 @@ import javax.swing.*;
 public class WeightView extends JPanel {
     private Weight weightObj;
     private JLabel label;
+    private JComboBox<Integer> comboBox;
 
     WeightView(Weight weightObj) {
         setWeightObj(weightObj);
         this.label = new JLabel();
-        this.label.setText("Weight");
+        this.label.setText("Weight (kilograms)");
+        this.comboBox = new JComboBox<>(this.getWeightObj().getValuesArray());
+        if (this.getWeightObj().getWeight() != 0)
+            this.comboBox.setSelectedItem(this.getWeightObj().getWeight());
+        else
+            this.comboBox.setSelectedItem(this.getWeightObj().getDefaultWeight());
         add(label);
+        add(comboBox);
     }
 
     public Weight getWeightObj() {