{"id":"boolean-logic-brackets","slug":"price-brackets","title":"Challenge: price brackets","text":"Many real calculations work in **brackets**: one rule up to some amount, another rule above it. A long `else if` chain handles them well. Each branch covers one bracket, and because the chain stops at the first true condition, each branch can assume the earlier brackets didn't match.\n\n```java\nint weight = 7;\nint price;\nif (weight <= 2) {\n    price = 5;\n} else if (weight <= 10) {\n    price = 5 + (weight - 2) * 2;\n} else {\n    price = 21 + (weight - 10);\n}\nSystem.out.println(\"Price: \" + price);\n```\n\n```output\nPrice: 15\n```\n\nThe price is set in every branch, so after the chain it always has a value. (`int price;` creates the variable without a value; Java checks that every path gives it one before it's used.)\n\n### The end of part 1\n\nThis step ends the material of the MOOC's first part. Its last section, [Programming in our society](https://java-programming.mooc.fi/part-1/7-programming-in-our-society), is a short read, with no exercises, about how much everyday life (communication, shopping, travel) runs on software, and how programmers are responsible for making it work well for everyone who uses it, including people who aren't used to computers.\n","task":"Shipping is free for orders of 50 euros or more; smaller orders pay 5 euros. Read the order total (whole euros) and print the shipping cost. For the input `30` the program prints:\n\n```\nShipping: 5 euros\n```\n\nFor 50 or more it prints `Shipping: free`.\n","kind":"code","seed":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int total = Integer.valueOf(scanner.nextLine());\n\n    }\n}\n","solution":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int total = Integer.valueOf(scanner.nextLine());\n        if (total >= 50) {\n            System.out.println(\"Shipping: free\");\n        } else {\n            System.out.println(\"Shipping: 5 euros\");\n        }\n    }\n}\n","hints":["Two brackets: 50 or more, and everything below.","`if (total >= 50) { ... } else { ... }`"],"tests":[{"name":"Test 1","stdin":"30\n","expect":"Shipping: 5 euros","hidden":false},{"name":"Test 2","stdin":"50\n","expect":"Shipping: free","hidden":true},{"name":"Test 3","stdin":"49\n","expect":"Shipping: 5 euros","hidden":true}],"require":[],"forbid":[],"more":[{"task":"A delivery service charges by distance (an invented price list):\n\n| Distance | Price |\n|---|---|\n| up to 5 km | 3.0 euros |\n| over 5 km, up to 20 km | 3.0 plus 0.5 for every km over 5 |\n| over 20 km | 10.5 plus 1.0 for every km over 20 |\n\nRead the distance in whole kilometres and print the price. For the input `12` the program prints:\n\n```\nPrice: 6.5 euros\n```\n","kind":"code","seed":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int km = Integer.valueOf(scanner.nextLine());\n\n    }\n}\n","solution":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int km = Integer.valueOf(scanner.nextLine());\n        double price;\n        if (km <= 5) {\n            price = 3.0;\n        } else if (km <= 20) {\n            price = 3.0 + 0.5 * (km - 5);\n        } else {\n            price = 10.5 + 1.0 * (km - 20);\n        }\n        System.out.println(\"Price: \" + price + \" euros\");\n    }\n}\n","hints":["The km over 5 is `km - 5`, so the middle bracket is `3.0 + 0.5 * (km - 5)`.","10.5 is what exactly 20 km costs (3.0 + 0.5 * 15), so the last bracket continues from there."],"tests":[{"name":"Test 1","stdin":"12\n","expect":"Price: 6.5 euros","hidden":false},{"name":"Test 2","stdin":"5\n","expect":"Price: 3.0 euros","hidden":true},{"name":"Test 3","stdin":"20\n","expect":"Price: 10.5 euros","hidden":true},{"name":"Test 4","stdin":"33\n","expect":"Price: 23.5 euros","hidden":true}],"require":[],"forbid":[]},{"task":"A library's late fee (an invented table), for a book returned some whole number of days late:\n\n| Days late | Fee |\n|---|---|\n| 0 | nothing |\n| 1 to 7 | 1 euro per day |\n| 8 to 14 | 7 euros plus 2 per day over 7 |\n| 15 to 30 | 21 euros plus 3 per day over 14 |\n| over 30 | 70 euros |\n\nRead the number of days and print `Nothing to pay` or `Fee: N euros`. For the input `10` the program prints:\n\n```\nFee: 13 euros\n```\n","kind":"code","seed":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int days = Integer.valueOf(scanner.nextLine());\n\n    }\n}\n","solution":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int days = Integer.valueOf(scanner.nextLine());\n        if (days <= 0) {\n            System.out.println(\"Nothing to pay\");\n        } else {\n            int fee;\n            if (days <= 7) {\n                fee = days;\n            } else if (days <= 14) {\n                fee = 7 + 2 * (days - 7);\n            } else if (days <= 30) {\n                fee = 21 + 3 * (days - 14);\n            } else {\n                fee = 70;\n            }\n            System.out.println(\"Fee: \" + fee + \" euros\");\n        }\n    }\n}\n","hints":["Handle 0 first with its own message, then the four fee brackets from the smallest up.","In the 8 to 14 bracket, the days over 7 are `days - 7`: 10 days is 7 + 2 * 3 = 13."],"tests":[{"name":"Test 1","stdin":"10\n","expect":"Fee: 13 euros","hidden":false},{"name":"Test 2","stdin":"0\n","expect":"Nothing to pay","hidden":true},{"name":"Test 3","stdin":"7\n","expect":"Fee: 7 euros","hidden":true},{"name":"Test 4","stdin":"14\n","expect":"Fee: 21 euros","hidden":true},{"name":"Test 5","stdin":"30\n","expect":"Fee: 69 euros","hidden":true},{"name":"Test 6","stdin":"31\n","expect":"Fee: 70 euros","hidden":true}],"require":[],"forbid":[]}]}