From 2bb2aa17b6533b2e8d2fc97997ed76bc38b6931d Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 11 Jan 2019 18:29:53 +0100 Subject: [PATCH] Finish the WeightView by using a JComboBox. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/Size.java | 2 +- src/SizeView.java | 3 +-- src/Weight.java | 28 ++++++++++++++++++++++++++-- src/WeightView.java | 9 ++++++++- 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/Size.java b/src/Size.java index a0e7d78..a61f24c 100644 --- a/src/Size.java +++ b/src/Size.java @@ -31,7 +31,7 @@ public class Size implements Comparable { } private boolean validateSize(int size) { - return (size >= min && size <= max); + return (size >= getMin() && size <= getMax()); } @Override diff --git a/src/SizeView.java b/src/SizeView.java index 781f397..e9e16f6 100644 --- a/src/SizeView.java +++ b/src/SizeView.java @@ -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); } diff --git a/src/Weight.java b/src/Weight.java index 5e33327..4b67611 100644 --- a/src/Weight.java +++ b/src/Weight.java @@ -1,4 +1,7 @@ public class Weight implements Comparable { + 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 { 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 { 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 diff --git a/src/WeightView.java b/src/WeightView.java index 9e7ef20..fd6d68c 100644 --- a/src/WeightView.java +++ b/src/WeightView.java @@ -3,12 +3,19 @@ import javax.swing.*; public class WeightView extends JPanel { private Weight weightObj; private JLabel label; + private JComboBox 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() { -- 2.34.1