From 11a61e74d06617ebcad335e277253dd79257f9e0 Mon Sep 17 00:00:00 2001 From: Jostein Ruen Date: Wed, 14 Aug 2024 13:06:00 +0200 Subject: [PATCH 1/3] Tests written --- domain-model.md | 86 +++++++++++++++++++ .../java/com/booleanuk/core/BasketTest.java | 20 +++++ 2 files changed, 106 insertions(+) create mode 100644 domain-model.md diff --git a/domain-model.md b/domain-model.md new file mode 100644 index 00000000..c4acfb75 --- /dev/null +++ b/domain-model.md @@ -0,0 +1,86 @@ +# Bob's Bagels + +![](./assets/bagels.jpg) + +## Learning Objectives +- Design a domain from user stories +- Use test driven development to build an application + +## Exercise Requirements + +- Use only the Basket class and BasketTest class provided. Later, you'll be building another version of this project using multiple classes together as we learn object-oriented programming. +- You **must** design a domain model before you begin building. Add your model as either a `.md` file or a screenshot +- You **must** use the Red Green Refactor approach to write your code. To demonstrate this, `git commit` after writing your test and commit again after writing the source code to pass it + + +| Classes | Methods | Variables | Scenario | Output | +|---------|-----------------------------|--------------------------|--------------------------------|--------------------------------| +| Basket | | int basketSize | | | +| | | ArrayList basket | | | +| | | | | | +| | addBagel(String bagel) | | Basket is full | return true | +| | | | Basket is not full | return false | +| | removeBagel(String bagel) | | Item exists in basket | Bagel removed from basket list | +| | | | Item doesn't exists in basket | print no bagel in basket | +| | changeCapacity(Int newSize) | | newSize is valid(not negative) | change basketSize variable | +| | | | newSize invalid(negative num) | print invalid size | + + +## User Stories + +``` +1. +As a member of the public, +So I can order a bagel before work, +I'd like to add a specific type of bagel to my basket. +``` + +``` +2. +As a member of the public, +So I can change my order, +I'd like to remove a bagel from my basket. +``` + +``` +3. +As a member of the public, +So that I can not overfill my small bagel basket +I'd like to know when my basket is full when I try adding an item beyond my basket capacity. +``` + +``` +4. +As a Bob's Bagels manager, +So that I can expand my business, +I’d like to change the capacity of baskets. +``` + +``` +5. +As a member of the public +So that I can maintain my sanity +I'd like to know if I try to remove an item that doesn't exist in my basket. +``` + +## Set up instructions +- Fork this repository and clone the forked version to your machine +- Open the root directory of the project in IntelliJ +- Implement the user stories based on the domain model you have developed following the **Red-Green-Refactor** methodology of writing your tests first, writing code to make them pass, and then iterating until the user story is implemented, then refactoring to make your code more elegant. +- Run the tests individually as you go through and then rerun all of the tests at the end to ensure you haven't broken one part of the code when implementing another. + +![](./assets/run-a-test.PNG) + +## Test Output + +When you run a test, it's either going to pass or fail. When it fails, you'll be presented with a big red stream of text. This is called a stack trace and, though intimidating, does contain some useful information. + +One of the core skills of a developer is debugging stack traces like this. The stack trace details in which classes & files the failure happened, and gives you a line number at the end. Most of the lines in the stack trace are irrelevant most of the time, you want to try and identify the files that you're actually working with. + +In the sample screenshot below, we've tried to complete the first step of the exercise but provided an invalid value. Then we run the test associated with it, and we see a big red stack trace, a test failure. + +At the top, we see `expected: <32> but was: <33>`. This means the test expected the value to be 32, but the value the student provided was 33. We can see this in the code snippets at the top of the screenshot. + +In the stack trace itself, we see this line: `at app//com.booleanuk.core.BasketTest.shouldBeAged32(ExerciseTest.java:20)`. This is helpful! This tells us the exact line in the ExerciseTest.java file (line 20) where the failure happened, as well as the method name (shouldBeAged32), helping us to identify where the issue began. This is the kind of thing you need to look for; a relevant file name, method name, class name and line number to give you a good starting point for debugging. + +![](./assets/test-failure.PNG) diff --git a/src/test/java/com/booleanuk/core/BasketTest.java b/src/test/java/com/booleanuk/core/BasketTest.java index e35771b3..dff01508 100644 --- a/src/test/java/com/booleanuk/core/BasketTest.java +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -3,6 +3,26 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import java.util.ArrayList; + class BasketTest { + @Test + public void addBagelTest(){ + Basket basket = new Basket(); + Assertions.assertTrue(basket.addBagel("Gluten Bagel")); + } + + @Test + public void removeBagelTest(){ + Basket basket = new Basket(); + Assertions.assertTrue(basket.removeBagel("Gluten Bagel")); + } + + @Test + public void changeCapacity(){ + Basket basket = new Basket(); + Assertions.assertEquals(basket.getSize(),basket.changeCapacity()); + } + } From 982869d193d9de0057a30803d7c794f565f92234 Mon Sep 17 00:00:00 2001 From: Jostein Ruen Date: Wed, 14 Aug 2024 13:29:51 +0200 Subject: [PATCH 2/3] Tests passing and code done --- src/main/java/com/booleanuk/core/Basket.java | 41 +++++++++++++++++++ .../java/com/booleanuk/core/BasketTest.java | 6 ++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/booleanuk/core/Basket.java b/src/main/java/com/booleanuk/core/Basket.java index df7a20aa..b478b778 100644 --- a/src/main/java/com/booleanuk/core/Basket.java +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -1,5 +1,46 @@ package com.booleanuk.core; +import java.util.ArrayList; + public class Basket { + int basketSize; + ArrayList basket; + + public Basket(){ + this.basketSize = 10; + this.basket = new ArrayList<>(); + } + + public Boolean addBagel(String bagel){ + if (basket.size()>= getBasketSize()){ + System.out.println("BASKET FULL, CANNOT ADD"); + return false; + }else{ + basket.add(bagel); + return true; + } + } + + public boolean removeBagel(String bagel){ + if (!basket.contains(bagel)){ + System.out.println(bagel + " does not exist in the basket"); + return false; + }else{ + return true; + } + } + + public void changeCapacity(int newSize){ + if(newSize >= 0){ //if valid number + this.basketSize = newSize; + }else{ + System.out.println("Invalid basketsize"); + } + } + + + public int getBasketSize(){ + return this.basketSize; + } } diff --git a/src/test/java/com/booleanuk/core/BasketTest.java b/src/test/java/com/booleanuk/core/BasketTest.java index dff01508..00cd403a 100644 --- a/src/test/java/com/booleanuk/core/BasketTest.java +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -11,18 +11,22 @@ class BasketTest { public void addBagelTest(){ Basket basket = new Basket(); Assertions.assertTrue(basket.addBagel("Gluten Bagel")); + Assertions.assertTrue(basket.addBagel("Bagel")); + Assertions.assertTrue(basket.addBagel("non Bagel")); } @Test public void removeBagelTest(){ Basket basket = new Basket(); + basket.addBagel("Gluten Bagel"); Assertions.assertTrue(basket.removeBagel("Gluten Bagel")); } @Test public void changeCapacity(){ Basket basket = new Basket(); - Assertions.assertEquals(basket.getSize(),basket.changeCapacity()); + basket.changeCapacity(50); + Assertions.assertEquals(50, basket.getBasketSize()); } } From 13f5efcb62e7974b1cb0b14c5bd4ab7fcd6c95bc Mon Sep 17 00:00:00 2001 From: Jostein Ruen Date: Wed, 14 Aug 2024 14:15:55 +0200 Subject: [PATCH 3/3] Updated domain model --- domain-model.md | 96 +++++++------------------------------------------ 1 file changed, 12 insertions(+), 84 deletions(-) diff --git a/domain-model.md b/domain-model.md index c4acfb75..bc5cfc64 100644 --- a/domain-model.md +++ b/domain-model.md @@ -1,86 +1,14 @@ -# Bob's Bagels -![](./assets/bagels.jpg) -## Learning Objectives -- Design a domain from user stories -- Use test driven development to build an application - -## Exercise Requirements - -- Use only the Basket class and BasketTest class provided. Later, you'll be building another version of this project using multiple classes together as we learn object-oriented programming. -- You **must** design a domain model before you begin building. Add your model as either a `.md` file or a screenshot -- You **must** use the Red Green Refactor approach to write your code. To demonstrate this, `git commit` after writing your test and commit again after writing the source code to pass it - - -| Classes | Methods | Variables | Scenario | Output | -|---------|-----------------------------|--------------------------|--------------------------------|--------------------------------| -| Basket | | int basketSize | | | -| | | ArrayList basket | | | -| | | | | | -| | addBagel(String bagel) | | Basket is full | return true | -| | | | Basket is not full | return false | -| | removeBagel(String bagel) | | Item exists in basket | Bagel removed from basket list | -| | | | Item doesn't exists in basket | print no bagel in basket | -| | changeCapacity(Int newSize) | | newSize is valid(not negative) | change basketSize variable | -| | | | newSize invalid(negative num) | print invalid size | - - -## User Stories - -``` -1. -As a member of the public, -So I can order a bagel before work, -I'd like to add a specific type of bagel to my basket. -``` - -``` -2. -As a member of the public, -So I can change my order, -I'd like to remove a bagel from my basket. -``` - -``` -3. -As a member of the public, -So that I can not overfill my small bagel basket -I'd like to know when my basket is full when I try adding an item beyond my basket capacity. -``` - -``` -4. -As a Bob's Bagels manager, -So that I can expand my business, -I’d like to change the capacity of baskets. -``` - -``` -5. -As a member of the public -So that I can maintain my sanity -I'd like to know if I try to remove an item that doesn't exist in my basket. -``` - -## Set up instructions -- Fork this repository and clone the forked version to your machine -- Open the root directory of the project in IntelliJ -- Implement the user stories based on the domain model you have developed following the **Red-Green-Refactor** methodology of writing your tests first, writing code to make them pass, and then iterating until the user story is implemented, then refactoring to make your code more elegant. -- Run the tests individually as you go through and then rerun all of the tests at the end to ensure you haven't broken one part of the code when implementing another. - -![](./assets/run-a-test.PNG) - -## Test Output - -When you run a test, it's either going to pass or fail. When it fails, you'll be presented with a big red stream of text. This is called a stack trace and, though intimidating, does contain some useful information. - -One of the core skills of a developer is debugging stack traces like this. The stack trace details in which classes & files the failure happened, and gives you a line number at the end. Most of the lines in the stack trace are irrelevant most of the time, you want to try and identify the files that you're actually working with. - -In the sample screenshot below, we've tried to complete the first step of the exercise but provided an invalid value. Then we run the test associated with it, and we see a big red stack trace, a test failure. - -At the top, we see `expected: <32> but was: <33>`. This means the test expected the value to be 32, but the value the student provided was 33. We can see this in the code snippets at the top of the screenshot. - -In the stack trace itself, we see this line: `at app//com.booleanuk.core.BasketTest.shouldBeAged32(ExerciseTest.java:20)`. This is helpful! This tells us the exact line in the ExerciseTest.java file (line 20) where the failure happened, as well as the method name (shouldBeAged32), helping us to identify where the issue began. This is the kind of thing you need to look for; a relevant file name, method name, class name and line number to give you a good starting point for debugging. - -![](./assets/test-failure.PNG) +| Classes | Methods | Variables | Scenario | Output | User stories | +|---------|-----------------------------|--------------------------|--------------------------------|---------------------------------|--------------| +| Basket | | int basketSize | | | | +| | | ArrayList basket | | | | +| | | | | | | +| | addBagel(String bagel) | | Basket is full | return true | 1 3 | +| | | | Basket is not full | return false | | +| | removeBagel(String bagel) | | Item exists in basket | Bagel removed from basket list | 2 5 | +| | | | Item doesn't exists in basket | print no bagel in basket | | +| | changeCapacity(Int newSize) | | newSize is valid(not negative) | change basketSize variable | 4 | +| | | | newSize invalid(negative num) | print invalid size | | +| | getBasketSize() | | Reads current basketSize | returns integer of maximum size | |