{"id":"paradigms-object-commands","slug":"object-commands","title":"The command program with a class","text":"Here's the box office again. The state and the rules that guard it move into a class, `Concert`, and the part of `main` that reads commands only calls its methods:\n\n```java\nimport java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        Concert concert = new Concert(200);\n        while (true) {\n            System.out.print(\"> \");\n            String command = scanner.nextLine();\n            if (command.equals(\"quit\")) {\n                System.out.println(\"Box office closed\");\n                break;\n            }\n            if (command.equals(\"sell\")) {\n                int count = Integer.valueOf(scanner.nextLine());\n                if (concert.sell(count)) {\n                    System.out.println(\"Sold! Tickets left: \" + concert.ticketsLeft());\n                } else {\n                    System.out.println(\"Sorry, tickets left: \" + concert.ticketsLeft());\n                }\n            } else if (command.equals(\"refund\")) {\n                int count = Integer.valueOf(scanner.nextLine());\n                if (concert.refund(count)) {\n                    System.out.println(\"Refunded! Tickets left: \" + concert.ticketsLeft());\n                } else {\n                    System.out.println(\"Sorry, tickets sold: \" + concert.getSold());\n                }\n            } else if (command.equals(\"status\")) {\n                System.out.println(\"Sold: \" + concert.getSold() + \", left: \" + concert.ticketsLeft());\n            } else {\n                System.out.println(\"Unknown command: \" + command);\n            }\n        }\n    }\n}\n// ==== Concert.java ====\npublic class Concert {\n    private int seats;\n    private int sold;\n\n    public Concert(int seats) {\n        this.seats = seats;\n        this.sold = 0;\n    }\n\n    public boolean sell(int count) {\n        if (this.sold + count > this.seats) {\n            return false;\n        }\n        this.sold = this.sold + count;\n        return true;\n    }\n\n    public boolean refund(int count) {\n        if (count > this.sold) {\n            return false;\n        }\n        this.sold = this.sold - count;\n        return true;\n    }\n\n    public int getSold() {\n        return this.sold;\n    }\n\n    public int ticketsLeft() {\n        return this.seats - this.sold;\n    }\n}\n```\n\n```input\nsell\n150\nsell\n80\nsell\n50\nrefund\n20\nrefund\n300\nstatus\nquit\n```\n\n```output\n> Sold! Tickets left: 50\n> Sorry, tickets left: 50\n> Sold! Tickets left: 0\n> Refunded! Tickets left: 20\n> Sorry, tickets sold: 180\n> Sold: 180, left: 20\n> Box office closed\n```\n\n- `Concert` keeps the state, `seats` and `sold`, in private instance variables, and its methods keep the rules. `sell` and `refund` return `true` when they did what was asked and `false` when they refused, so the caller knows which message to print. The class never reads or prints anything, as in module 25.\n- The loop in `main` reads the commands and prints the answers. It no longer knows how the limits are checked: it asks `concert.sell(count)` and looks at the answer.\n- The tickets left are worked out in one place, `ticketsLeft()`.\n- `main` isn't much shorter than before. The gain isn't in the number of lines, but in where things are, as the next section shows.\n\n### Why it helps\n\nThe procedural version made three changes hard. Here they are again.\n\n**A second concert.** The hall adds an afternoon show with 120 seats. That's one more object:\n\n```java\nConcert afternoon = new Concert(120);\nConcert evening = new Concert(200);\n```\n\nEach object has its own `seats` and `sold`, and both follow the same rules, written once in the class. In the procedural version, the afternoon show needed two more variables and a copy of every branch. This step's second challenge sells tickets for both shows.\n\n**A new command.** Say the box office adds `one`, which sells a single ticket at the door. In the class version, its branch calls `concert.sell(1)`, and it can't skip the check, because the check is inside `sell`. Every command that sells tickets goes through the same method, so a rule written once holds for all of them.\n\n**A test of the class alone.** The rules can be checked without the command loop, and without typing a single command. Here are two JUnit tests for them (module 26):\n\n```java\n// ==== Concert.java ====\npublic class Concert {\n    private int seats;\n    private int sold;\n\n    public Concert(int seats) {\n        this.seats = seats;\n        this.sold = 0;\n    }\n\n    public boolean sell(int count) {\n        if (this.sold + count > this.seats) {\n            return false;\n        }\n        this.sold = this.sold + count;\n        return true;\n    }\n\n    public boolean refund(int count) {\n        if (count > this.sold) {\n            return false;\n        }\n        this.sold = this.sold - count;\n        return true;\n    }\n\n    public int getSold() {\n        return this.sold;\n    }\n\n    public int ticketsLeft() {\n        return this.seats - this.sold;\n    }\n}\n// ==== ConcertTest.java ====\nimport static org.junit.Assert.assertEquals;\nimport static org.junit.Assert.assertFalse;\n\nimport org.junit.Test;\n\npublic class ConcertTest {\n    @Test\n    public void cannotSellMoreThanTheSeats() {\n        Concert concert = new Concert(10);\n        concert.sell(8);\n        assertFalse(concert.sell(3));\n        assertEquals(2, concert.ticketsLeft());\n    }\n\n    @Test\n    public void cannotRefundMoreThanWereSold() {\n        Concert concert = new Concert(10);\n        concert.sell(4);\n        assertFalse(concert.refund(5));\n        assertEquals(4, concert.getSold());\n    }\n}\n```\n\n```output\nConcertTest: 2 tests\n  cannotRefundMoreThanWereSold: passed\n  cannotSellMoreThanTheSeats: passed\n2 of 2 tests passed\n```\n\nIn the procedural version, the same rules sat inside the loop, between a `nextLine` and a `println`, where no unit test could reach them on their own.\n\n### Both styles in one program\n\nLook at the class version once more. It isn't object-oriented all the way down. The command loop in `main` is a sequence of steps with a loop and ifs, and so is the body of `sell`: check the limit, change the count, return. That's procedural code. What the object adds is the organization: which data belongs together, and which code may change it.\n\nThat's how Java programs are usually written. Objects hold the data and the rules that guard it, and inside the methods, the code mostly runs step by step. A calculation that needs no data of its own often stays a static method, like `Math.max`, which takes two numbers and returns the larger one. And in a bigger program, the command loop would move into a user interface class that takes a Scanner, as in module 25, so that `main` only creates the objects.\n\nThere are other paradigms too. In **functional programming**, for example, a program is built from functions that compute new values from the values they're given, instead of changing variables step by step. Java supports parts of that style as well, with lambdas and streams, which come later in the course (MOOC part 10).\n","task":"Now do the same to the bike-share station of the previous step. `Main.java` is its procedural version, with everything in `main`. Move the state and the rules into the class in `BikeDock.java`, and rewrite the branches of `main` so that they call its methods. The class needs:\n\n- `BikeDock(int slots, int bikes)`: a station with `slots` slots, `bikes` of which hold a bike.\n- `rent(int count)`: if the station has at least `count` bikes, takes them and returns `true`. Otherwise it changes nothing and returns `false`.\n- `bringBack(int count)`: if there are at least `count` free slots, puts the bikes in them and returns `true`. Otherwise it changes nothing and returns `false`.\n- `getBikes()`: returns the number of bikes at the station.\n- `freeSlots()`: returns the number of free slots.\n\nThe class doesn't print anything. `main` creates the station with `new BikeDock(12, 5)` and keeps no count of bikes of its own. The program prints what it did before. For the input shown:\n\n```\n> Enjoy the ride! Bikes here: 2\n> Sorry, bikes here: 2\n> Thanks! Bikes here: 11\n> Sorry, free slots: 1\n> Bikes: 11, free slots: 1\n> Station closed\n```\n","kind":"code","seed":"// ==== BikeDock.java ====\npublic class BikeDock {\n    // Move the state and the rules of the station here.\n}\n\n// ==== Main.java ====\nimport java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int slots = 12;\n        int bikes = 5;\n        while (true) {\n            System.out.print(\"> \");\n            String command = scanner.nextLine();\n            if (command.equals(\"close\")) {\n                System.out.println(\"Station closed\");\n                break;\n            }\n            if (command.equals(\"status\")) {\n                System.out.println(\"Bikes: \" + bikes + \", free slots: \" + (slots - bikes));\n            } else if (command.equals(\"rent\")) {\n                int count = Integer.valueOf(scanner.nextLine());\n                if (count > bikes) {\n                    System.out.println(\"Sorry, bikes here: \" + bikes);\n                } else {\n                    bikes = bikes - count;\n                    System.out.println(\"Enjoy the ride! Bikes here: \" + bikes);\n                }\n            } else if (command.equals(\"return\")) {\n                int count = Integer.valueOf(scanner.nextLine());\n                if (bikes + count > slots) {\n                    System.out.println(\"Sorry, free slots: \" + (slots - bikes));\n                } else {\n                    bikes = bikes + count;\n                    System.out.println(\"Thanks! Bikes here: \" + bikes);\n                }\n            } else {\n                System.out.println(\"Unknown command: \" + command);\n            }\n        }\n    }\n}\n","solution":"// ==== BikeDock.java ====\npublic class BikeDock {\n    // Move the state and the rules of the station here.\n    private int slots;\n    private int bikes;\n\n    public BikeDock(int slots, int bikes) {\n        this.slots = slots;\n        this.bikes = bikes;\n    }\n\n    public boolean rent(int count) {\n        if (count > this.bikes) {\n            return false;\n        }\n        this.bikes = this.bikes - count;\n        return true;\n    }\n\n    public boolean bringBack(int count) {\n        if (count > this.freeSlots()) {\n            return false;\n        }\n        this.bikes = this.bikes + count;\n        return true;\n    }\n\n    public int getBikes() {\n        return this.bikes;\n    }\n\n    public int freeSlots() {\n        return this.slots - this.bikes;\n    }\n}\n\n// ==== Main.java ====\nimport java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        BikeDock dock = new BikeDock(12, 5);\n        while (true) {\n            System.out.print(\"> \");\n            String command = scanner.nextLine();\n            if (command.equals(\"close\")) {\n                System.out.println(\"Station closed\");\n                break;\n            }\n            if (command.equals(\"status\")) {\n                System.out.println(\"Bikes: \" + dock.getBikes() + \", free slots: \" + dock.freeSlots());\n            } else if (command.equals(\"rent\")) {\n                int count = Integer.valueOf(scanner.nextLine());\n                if (dock.rent(count)) {\n                    System.out.println(\"Enjoy the ride! Bikes here: \" + dock.getBikes());\n                } else {\n                    System.out.println(\"Sorry, bikes here: \" + dock.getBikes());\n                }\n            } else if (command.equals(\"return\")) {\n                int count = Integer.valueOf(scanner.nextLine());\n                if (dock.bringBack(count)) {\n                    System.out.println(\"Thanks! Bikes here: \" + dock.getBikes());\n                } else {\n                    System.out.println(\"Sorry, free slots: \" + dock.freeSlots());\n                }\n            } else {\n                System.out.println(\"Unknown command: \" + command);\n            }\n        }\n    }\n}\n","hints":["Do what the box office did: the two local variables `slots` and `bikes` become the private instance variables of BikeDock, set in its constructor. Each check in a branch moves into a method that returns `true` when it did what was asked and `false` when it refused.","In `main`, create `BikeDock dock = new BikeDock(12, 5);` before the loop, and write the `rent` branch as `if (dock.rent(count)) { ... } else { ... }`. Where `main` printed `bikes` or `slots - bikes`, it now prints `dock.getBikes()` or `dock.freeSlots()`."],"tests":[{"name":"Test 1","stdin":"rent\n3\nrent\n4\nreturn\n9\nreturn\n3\nstatus\nclose\n","expect":"> Enjoy the ride! Bikes here: 2\n> Sorry, bikes here: 2\n> Thanks! Bikes here: 11\n> Sorry, free slots: 1\n> Bikes: 11, free slots: 1\n> Station closed","hidden":false},{"name":"Test 2","stdin":"status\nrent\n5\nrent\n1\nreturn\n12\nreturn\n1\nrepair\nstatus\nclose\n","expect":"> Bikes: 5, free slots: 7\n> Enjoy the ride! Bikes here: 0\n> Sorry, bikes here: 0\n> Thanks! Bikes here: 12\n> Sorry, free slots: 0\n> Unknown command: repair\n> Bikes: 12, free slots: 0\n> Station closed","hidden":true},{"name":"Test 3","stdin":"","call":"BikeDock small = new BikeDock(4, 0);\nBikeDock big = new BikeDock(20, 20);\nSystem.out.println(small.rent(1) + \" \" + small.bringBack(4) + \" \" + small.bringBack(1) + \" \" + small.getBikes() + \" \" + small.freeSlots());\nSystem.out.println(big.bringBack(1) + \" \" + big.rent(20) + \" \" + big.rent(1) + \" \" + big.getBikes() + \" \" + big.freeSlots());\n","expect":"false true false 4 0\nfalse true false 0 20","hidden":true}],"require":[{"pattern":"new\\s+BikeDock\\s*\\(\\s*12\\s*,\\s*5\\s*\\)","message":"Create the station in `main` with `new BikeDock(12, 5)`."}],"forbid":[{"pattern":"\\bclass\\s+Main\\b[\\s\\S]*\\bint\\s+(bikes|slots)\\s*=\\s*\\d","message":"`main` shouldn't keep the bikes or the slots in variables of its own any more: they belong in BikeDock."},{"pattern":"\\bclass\\s+BikeDock\\b[\\s\\S]*\\bSystem\\s*\\.\\s*out\\b[\\s\\S]*\\bclass\\s+Main\\b","message":"BikeDock doesn't print. `rent` and `bringBack` return `true` or `false`, and `main` prints the messages."}],"more":[{"task":"The box office now sells tickets for two shows: an afternoon show with 120 seats, and an evening show with 200. `Concert.java` is the class above, unchanged. Write `main`: create one Concert for each show, and read commands, with the prompt `> ` before each one:\n\n- `afternoon`: the next line is a number of tickets. Sell them for the afternoon show, and print `Sold! Tickets left: ` and the tickets left for that show. If they don't fit, print `Sorry, tickets left: ` and the tickets left.\n- `evening`: the same, for the evening show.\n- `status`: print the tickets left for both shows, like `Afternoon: 20 left, evening: 150 left`.\n- `quit`: print `Box office closed` and end the program.\n- Anything else: print `Unknown command: ` and the command.\n\nFor the input shown, the program prints:\n\n```\n> Sold! Tickets left: 20\n> Sold! Tickets left: 150\n> Sorry, tickets left: 20\n> Afternoon: 20 left, evening: 150 left\n> Box office closed\n```\n","kind":"code","seed":"// ==== Main.java ====\nimport java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        // Create the two concerts, then write the command loop.\n    }\n}\n\n// ==== Concert.java ====\npublic class Concert {\n    private int seats;\n    private int sold;\n\n    public Concert(int seats) {\n        this.seats = seats;\n        this.sold = 0;\n    }\n\n    public boolean sell(int count) {\n        if (this.sold + count > this.seats) {\n            return false;\n        }\n        this.sold = this.sold + count;\n        return true;\n    }\n\n    public boolean refund(int count) {\n        if (count > this.sold) {\n            return false;\n        }\n        this.sold = this.sold - count;\n        return true;\n    }\n\n    public int getSold() {\n        return this.sold;\n    }\n\n    public int ticketsLeft() {\n        return this.seats - this.sold;\n    }\n}\n","solution":"// ==== Main.java ====\nimport java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        // Create the two concerts, then write the command loop.\n        Concert afternoon = new Concert(120);\n        Concert evening = new Concert(200);\n        while (true) {\n            System.out.print(\"> \");\n            String command = scanner.nextLine();\n            if (command.equals(\"quit\")) {\n                System.out.println(\"Box office closed\");\n                break;\n            }\n            if (command.equals(\"afternoon\")) {\n                sellTickets(afternoon, Integer.valueOf(scanner.nextLine()));\n            } else if (command.equals(\"evening\")) {\n                sellTickets(evening, Integer.valueOf(scanner.nextLine()));\n            } else if (command.equals(\"status\")) {\n                System.out.println(\"Afternoon: \" + afternoon.ticketsLeft() + \" left, evening: \" + evening.ticketsLeft() + \" left\");\n            } else {\n                System.out.println(\"Unknown command: \" + command);\n            }\n        }\n    }\n\n    public static void sellTickets(Concert concert, int count) {\n        if (concert.sell(count)) {\n            System.out.println(\"Sold! Tickets left: \" + concert.ticketsLeft());\n        } else {\n            System.out.println(\"Sorry, tickets left: \" + concert.ticketsLeft());\n        }\n    }\n}\n\n// ==== Concert.java ====\npublic class Concert {\n    private int seats;\n    private int sold;\n\n    public Concert(int seats) {\n        this.seats = seats;\n        this.sold = 0;\n    }\n\n    public boolean sell(int count) {\n        if (this.sold + count > this.seats) {\n            return false;\n        }\n        this.sold = this.sold + count;\n        return true;\n    }\n\n    public boolean refund(int count) {\n        if (count > this.sold) {\n            return false;\n        }\n        this.sold = this.sold - count;\n        return true;\n    }\n\n    public int getSold() {\n        return this.sold;\n    }\n\n    public int ticketsLeft() {\n        return this.seats - this.sold;\n    }\n}\n","hints":["Create `Concert afternoon = new Concert(120);` and `Concert evening = new Concert(200);` before the loop. The loop is shaped like the box office's, with `afternoon` and `evening` in the place of `sell`.","The answer to a sale is the same for both shows, so a static method in `Main`, such as `sellTickets(Concert concert, int count)`, can print it for either one: both styles in one program. Call it with `afternoon` or with `evening`."],"tests":[{"name":"Test 1","stdin":"afternoon\n100\nevening\n50\nafternoon\n30\nstatus\nquit\n","expect":"> Sold! Tickets left: 20\n> Sold! Tickets left: 150\n> Sorry, tickets left: 20\n> Afternoon: 20 left, evening: 150 left\n> Box office closed","hidden":false},{"name":"Test 2","stdin":"status\nevening\n200\nevening\n1\nafternoon\n120\nstatus\nmorning\nquit\n","expect":"> Afternoon: 120 left, evening: 200 left\n> Sold! Tickets left: 0\n> Sorry, tickets left: 0\n> Sold! Tickets left: 0\n> Afternoon: 0 left, evening: 0 left\n> Unknown command: morning\n> Box office closed","hidden":true},{"name":"Test 3","stdin":"evening\n30\nafternoon\n121\nafternoon\n20\nevening\n171\nstatus\nquit\n","expect":"> Sold! Tickets left: 170\n> Sorry, tickets left: 120\n> Sold! Tickets left: 100\n> Sorry, tickets left: 170\n> Afternoon: 100 left, evening: 170 left\n> Box office closed","hidden":true}],"require":[{"pattern":"new\\s+Concert\\s*\\(\\s*120\\s*\\)","message":"Create the afternoon show with `new Concert(120)`."},{"pattern":"new\\s+Concert\\s*\\(\\s*200\\s*\\)","message":"Create the evening show with `new Concert(200)`."}],"forbid":[]},{"task":"In the procedural version of the station, the only way to try its rules was to run the whole command loop on a list of commands. Now the rules are in `BikeDock`, and a test can check them alone. `BikeDockTest` has one test, for renting. Add a second test, for the other rule: bringing back more bikes than there are free slots changes nothing. For example, make a station of 10 slots with 8 bikes, try to bring back 3, and check that `bringBack` returns `false` and that the station still has 8 bikes.\n\nThe checks run your tests on the BikeDock as it is, where every test should pass, and on BikeDocks with a mistake in `bringBack`, where your new test should fail.\n","kind":"code","seed":"// ==== BikeDockTest.java ====\nimport static org.junit.Assert.assertEquals;\nimport static org.junit.Assert.assertFalse;\nimport static org.junit.Assert.assertTrue;\n\nimport org.junit.Test;\n\npublic class BikeDockTest {\n    @Test\n    public void rentingTakesBikes() {\n        BikeDock dock = new BikeDock(10, 4);\n        assertTrue(dock.rent(3));\n        assertEquals(1, dock.getBikes());\n    }\n\n    // Write a test here for bringing back more bikes than there are free slots.\n}\n\n// ==== BikeDock.java ====\npublic class BikeDock {\n    private int slots;\n    private int bikes;\n\n    public BikeDock(int slots, int bikes) {\n        this.slots = slots;\n        this.bikes = bikes;\n    }\n\n    public boolean rent(int count) {\n        if (count > this.bikes) {\n            return false;\n        }\n        this.bikes = this.bikes - count;\n        return true;\n    }\n\n    public boolean bringBack(int count) {\n        if (count > this.freeSlots()) {\n            return false;\n        }\n        this.bikes = this.bikes + count;\n        return true;\n    }\n\n    public int getBikes() {\n        return this.bikes;\n    }\n\n    public int freeSlots() {\n        return this.slots - this.bikes;\n    }\n}\n","solution":"// ==== BikeDockTest.java ====\nimport static org.junit.Assert.assertEquals;\nimport static org.junit.Assert.assertFalse;\nimport static org.junit.Assert.assertTrue;\n\nimport org.junit.Test;\n\npublic class BikeDockTest {\n    @Test\n    public void rentingTakesBikes() {\n        BikeDock dock = new BikeDock(10, 4);\n        assertTrue(dock.rent(3));\n        assertEquals(1, dock.getBikes());\n    }\n\n    // Write a test here for bringing back more bikes than there are free slots.\n    @Test\n    public void bringingBackNeedsFreeSlots() {\n        BikeDock dock = new BikeDock(10, 8);\n        assertFalse(dock.bringBack(3));\n        assertEquals(8, dock.getBikes());\n    }\n}\n\n// ==== BikeDock.java ====\npublic class BikeDock {\n    private int slots;\n    private int bikes;\n\n    public BikeDock(int slots, int bikes) {\n        this.slots = slots;\n        this.bikes = bikes;\n    }\n\n    public boolean rent(int count) {\n        if (count > this.bikes) {\n            return false;\n        }\n        this.bikes = this.bikes - count;\n        return true;\n    }\n\n    public boolean bringBack(int count) {\n        if (count > this.freeSlots()) {\n            return false;\n        }\n        this.bikes = this.bikes + count;\n        return true;\n    }\n\n    public int getBikes() {\n        return this.bikes;\n    }\n\n    public int freeSlots() {\n        return this.slots - this.bikes;\n    }\n}\n","hints":["A new test is a `public void` method with `@Test` on the line before it. Make its own BikeDock, with `new BikeDock(10, 8)`, so that only 2 slots are free.","`assertFalse(dock.bringBack(3));` checks the answer, and `assertEquals(8, dock.getBikes());` checks that nothing changed. Check both: a mistake could get either one wrong."],"tests":[{"name":"The BikeDock as it is","stdin":"","junit":"BikeDockTest","outcome":"pass","expect":"","hidden":false},{"name":"A BikeDock whose bringBack never checks the free slots","stdin":"","junit":"BikeDockTest","outcome":"fail","replace":{"BikeDock.java":"public class BikeDock {\n    private int slots;\n    private int bikes;\n\n    public BikeDock(int slots, int bikes) {\n        this.slots = slots;\n        this.bikes = bikes;\n    }\n\n    public boolean rent(int count) {\n        if (count > this.bikes) {\n            return false;\n        }\n        this.bikes = this.bikes - count;\n        return true;\n    }\n\n    public boolean bringBack(int count) {\n        this.bikes = this.bikes + count;\n        return true;\n    }\n\n    public int getBikes() {\n        return this.bikes;\n    }\n\n    public int freeSlots() {\n        return this.slots - this.bikes;\n    }\n}\n"},"expect":"","hidden":false},{"name":"A BikeDock whose bringBack says no, but takes the bikes anyway","stdin":"","junit":"BikeDockTest","outcome":"fail","replace":{"BikeDock.java":"public class BikeDock {\n    private int slots;\n    private int bikes;\n\n    public BikeDock(int slots, int bikes) {\n        this.slots = slots;\n        this.bikes = bikes;\n    }\n\n    public boolean rent(int count) {\n        if (count > this.bikes) {\n            return false;\n        }\n        this.bikes = this.bikes - count;\n        return true;\n    }\n\n    public boolean bringBack(int count) {\n        boolean fits = count <= this.freeSlots();\n        this.bikes = this.bikes + count;\n        return fits;\n    }\n\n    public int getBikes() {\n        return this.bikes;\n    }\n\n    public int freeSlots() {\n        return this.slots - this.bikes;\n    }\n}\n"},"expect":"","hidden":true},{"name":"A BikeDock whose bringBack changes nothing, but says yes","stdin":"","junit":"BikeDockTest","outcome":"fail","replace":{"BikeDock.java":"public class BikeDock {\n    private int slots;\n    private int bikes;\n\n    public BikeDock(int slots, int bikes) {\n        this.slots = slots;\n        this.bikes = bikes;\n    }\n\n    public boolean rent(int count) {\n        if (count > this.bikes) {\n            return false;\n        }\n        this.bikes = this.bikes - count;\n        return true;\n    }\n\n    public boolean bringBack(int count) {\n        if (count > this.freeSlots()) {\n            return true;\n        }\n        this.bikes = this.bikes + count;\n        return true;\n    }\n\n    public int getBikes() {\n        return this.bikes;\n    }\n\n    public int freeSlots() {\n        return this.slots - this.bikes;\n    }\n}\n"},"expect":"","hidden":true}],"require":[{"pattern":"@Test\\b","min":2,"message":"Add a second test method, with `@Test` on the line before it."}],"forbid":[]}]}