import javax.swing.*; public class Postage { public static void main(String[] args) { runTests(); String input = JOptionPane.showInputDialog("Weight:"); double weight = Double.parseDouble(input); String output = getPostage(weight); JOptionPane.showMessageDialog(null, output); } private static String getPostage(double weight) { String output; if (weight <= 0.0) { output = "Weight must be positive!"; } else if (weight <= 20.0) { output = "Postage is 5.50 kronor."; } else if (weight <= 100.0) { output = "Postage is 11.00 kronor."; } else if (weight <= 250.0) { output = "Postage is 22.00 kronor."; } else if (weight <= 500.0) { output = "Postage is 33.00 kronor."; } else { output = "Too heavy: use a packet."; } return output; } public static void runTests() { testPostage(10.0, "Postage is 5.50 kronor."); testPostage(20.0, "Postage is 5.50 kronor."); testPostage(20.5, "Postage is 11.00 kronor."); testPostage(150, "Postage is 22.00 kronor."); testPostage(300, "Postage is 33.00 kronor."); testPostage(-10, "Weight must be positive!"); testPostage(Double.MAX_VALUE, "Too heavy: use a packet."); testPostage(Double.POSITIVE_INFINITY, "Too heavy: use a packet."); for (int i = 0; i < 100; i++) { double weight = Math.random() * 1000; String output = getPostage(weight); System.out.print(". "); } } private static void testPostage(double weight, String expected) { String actual = getPostage(weight); if (expected.equals(actual)) { System.out.print(". "); } else { System.out.println("Test failed!"); System.out.println("Input: " + weight); System.out.println("Expected output:" + expected); System.out.println("Actual output:" + actual); } } }