├── src
├── main
│ └── java
│ │ ├── answers
│ │ ├── AccountType.java
│ │ ├── Calculator.java
│ │ └── Account.java
│ │ └── exercises
│ │ └── Account.java
└── test
│ └── java
│ ├── exercises
│ └── AccountTest.java
│ └── answers
│ └── AccountTest.java
├── pom.xml
└── .gitignore
/src/main/java/answers/AccountType.java:
--------------------------------------------------------------------------------
1 | package answers;
2 |
3 | public enum AccountType {
4 | CHECKING,
5 | SAVINGS
6 | }
7 |
--------------------------------------------------------------------------------
/src/main/java/answers/Calculator.java:
--------------------------------------------------------------------------------
1 | package answers;
2 |
3 | public class Calculator {
4 |
5 | private int total;
6 |
7 | public Calculator() {
8 |
9 | this.total = 0;
10 | }
11 |
12 | public Calculator(int total) {
13 |
14 | this.total = total;
15 | }
16 |
17 | public int getTotal() {
18 |
19 | return this.total;
20 | }
21 |
22 | public void add(int valueToAdd) {
23 |
24 | this.total += valueToAdd;
25 | }
26 |
27 | public void subtract(int valueToSubtract) {
28 |
29 | this.total -= valueToSubtract;
30 | }
31 |
32 | public void multiplyBy(int valueToMultiplyBy) {
33 |
34 | this.total *= valueToMultiplyBy;
35 | }
36 |
37 | public void divide(int valueToDivideBy) throws ArithmeticException {
38 |
39 | this.total /= valueToDivideBy;
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.ontestautomation.javafortesters
8 | javafortesters
9 | 1.0-SNAPSHOT
10 | Training material for Java for Testers
11 |
12 | 21
13 | 21
14 | 5.10.2
15 |
16 |
17 |
18 | org.junit.jupiter
19 | junit-jupiter-engine
20 | ${junit.version}
21 | test
22 |
23 |
24 | org.junit.jupiter
25 | junit-jupiter-params
26 | ${junit.version}
27 | test
28 |
29 |
30 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | target/
2 | pom.xml.tag
3 | pom.xml.releaseBackup
4 | pom.xml.versionsBackup
5 | pom.xml.next
6 | release.properties
7 | dependency-reduced-pom.xml
8 | buildNumber.properties
9 | .mvn/timing.properties
10 |
11 | # Avoid ignoring Maven wrapper jar file (.jar files are usually ignored)
12 | !/.mvn/wrapper/maven-wrapper.jar
13 |
14 |
15 | .metadata
16 | bin/
17 | tmp/
18 | *.tmp
19 | *.bak
20 | *.swp
21 | *~.nib
22 | local.properties
23 | .settings/
24 | .loadpath
25 | .recommenders
26 | .classpath
27 | .project
28 |
29 | # TestNG reporting
30 | test-output/
31 |
32 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
33 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
34 |
35 | .idea/
36 |
37 | # CMake
38 | cmake-build-debug/
39 |
40 | ## File-based project format:
41 | *.iws
42 |
43 | *.iml
44 |
45 | ## Plugin-specific files:
46 |
47 | # IntelliJ
48 | out/
49 |
50 | # mpeltonen/sbt-idea plugin
51 | .idea_modules/
52 |
53 | # JIRA plugin
54 | atlassian-ide-plugin.xml
55 |
56 | # Cursive Clojure plugin
57 | .idea/replstate.xml
58 |
59 | # Crashlytics plugin (for Android Studio and IntelliJ)
60 | com_crashlytics_export_strings.xml
61 | crashlytics.properties
62 | crashlytics-build.properties
63 | fabric.properties
64 |
65 | # Driver binaries
66 | src/test/resources/drivers/
--------------------------------------------------------------------------------
/src/test/java/exercises/AccountTest.java:
--------------------------------------------------------------------------------
1 | package exercises;
2 |
3 | import answers.Account;
4 | import answers.AccountType;
5 | import org.junit.jupiter.api.Assertions;
6 | import org.junit.jupiter.api.Test;
7 |
8 | public class AccountTest {
9 |
10 | @Test
11 | public void deposit10_checkBalance_shouldBe10() {
12 |
13 | // Arrange - create an account using the no-argument constructor
14 | answers.Account account = new answers.Account();
15 |
16 | // Act - deposit 10
17 | account.deposit(10);
18 |
19 | // Assert - check that the balance is 10
20 | // TODO: write the assertion and run the test to see if it passes
21 | }
22 |
23 | @Test
24 | public void deposit10_withdraw20_checkBalance_shouldBeMinus10() {
25 |
26 | // Arrange - create a checking account using the parameterized constructor
27 | // TODO: create the account
28 |
29 | // Act - deposit 10
30 | // TODO: deposit 10 into the account
31 |
32 | // Act - withdraw 20
33 | // TODO: withdraw 20 from the account
34 |
35 | // Assert - check that the balance is -10
36 | // TODO: check that the resulting balance of the account is -10
37 | }
38 |
39 | // ---------------------------------------------------------
40 | // ---------------------------------------------------------
41 |
42 | /* EXCEPTIONS */
43 |
44 | // TODO: write a test that create a new savings account and then immediately tries to
45 | // withdraw 10 from it. This should throw an exception, but does it? Write the test!
46 |
47 | /* PARAMETERIZED TEST */
48 |
49 | // TODO: Write a parameterized test that tests the withdraw function for a savings account
50 | // For all iterations: create a new savings account and deposit 10.
51 | // Iteration 1: withdraw 0, check that balance is 10
52 | // Iteration 2: withdraw 5, check that balance is 5
53 | // Iteration 3, withdraw 10, check that balance is 0
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/src/test/java/answers/AccountTest.java:
--------------------------------------------------------------------------------
1 | package answers;
2 |
3 | import org.junit.jupiter.api.Assertions;
4 | import org.junit.jupiter.api.Test;
5 | import org.junit.jupiter.params.ParameterizedTest;
6 | import org.junit.jupiter.params.provider.CsvSource;
7 |
8 | public class AccountTest {
9 |
10 | @Test
11 | public void deposit10_checkBalance_shouldBe10() {
12 |
13 | // Arrange - create an account using the no-argument constructor
14 | Account account = new Account();
15 |
16 | // Act - deposit 10
17 | account.deposit(10);
18 |
19 | // Assert - check that the balance is 10
20 | Assertions.assertEquals(10, account.getBalance());
21 | }
22 |
23 | @Test
24 | public void deposit10_withdraw20_checkBalance_shouldBeMinus10() {
25 |
26 | // Arrange - create a checking account using the parameterized constructor
27 | Account account = new Account(34567, AccountType.CHECKING);
28 |
29 | // Act - deposit 10
30 | account.deposit(10);
31 |
32 | // Act - withdraw 20
33 | account.withdraw(20);
34 |
35 | // Assert - check that the balance is -10
36 | Assertions.assertEquals(-10, account.getBalance());
37 | }
38 |
39 | // ---------------------------------------------------------
40 | // ---------------------------------------------------------
41 |
42 | /* EXCEPTIONS */
43 |
44 | @Test
45 | public void overdrawFromSavingsAccount_shouldThrowArithmeticException() {
46 |
47 | Account account = new Account(123, AccountType.SAVINGS);
48 |
49 | Assertions.assertThrows(ArithmeticException.class, () -> account.withdraw(10));
50 | }
51 |
52 | @ParameterizedTest(name = "Withdrawing {0} from a savings account with balance 10 should result in balance {1}")
53 | @CsvSource({
54 | "0, 10",
55 | "5, 5",
56 | "10, 0"
57 | })
58 | public void withdrawFromSavinsAccount_checkBalance(int amountToWithdraw, int expectedBalance) {
59 |
60 | Account account = new Account(12345, AccountType.SAVINGS);
61 |
62 | account.deposit(10);
63 |
64 | account.withdraw(amountToWithdraw);
65 |
66 | Assertions.assertEquals(expectedBalance, account.getBalance());
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/src/main/java/answers/Account.java:
--------------------------------------------------------------------------------
1 | package answers;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | public class Account {
7 |
8 | private int balance;
9 | private int accountNumber;
10 | private AccountType accountType;
11 |
12 | public Account(int accountNumber, AccountType accountType) {
13 |
14 | this.balance = 0;
15 | this.accountNumber = accountNumber;
16 | this.accountType = accountType;
17 | }
18 |
19 | public Account() {
20 |
21 | this.balance = 0;
22 | this.accountNumber = 12345;
23 | this.accountType = AccountType.CHECKING;
24 | }
25 |
26 | public void deposit(int amountToDeposit) {
27 |
28 | this.balance += amountToDeposit;
29 | }
30 |
31 | public void withdraw(int amountToWithdraw) {
32 |
33 | if (this.accountType.equals(AccountType.SAVINGS) && amountToWithdraw > this.balance) {
34 | throw new ArithmeticException("You cannot overdraw on a savings account");
35 | }
36 |
37 | this.balance -= amountToWithdraw;
38 | }
39 |
40 | public int getBalance() {
41 |
42 | return this.balance;
43 | }
44 |
45 | public String toString() {
46 |
47 | return String.format("The account with number %d is of type '%s' and has a balance of %d",
48 | this.accountNumber, this.accountType, this.balance);
49 | }
50 |
51 | public void anArrayOfAccounts() {
52 |
53 | Account[] accountArray = new Account[3];
54 |
55 | accountArray[0] = new Account(12345, AccountType.CHECKING);
56 | accountArray[1] = new Account(54321, AccountType.CHECKING);
57 | accountArray[2] = new Account(78789, AccountType.SAVINGS);
58 |
59 | for (Account account : accountArray ) {
60 | System.out.println(account.toString());
61 | }
62 | }
63 |
64 | public void aListOfAccounts() {
65 |
66 | List accountList = new ArrayList();
67 |
68 | accountList.add(new Account(12345, AccountType.CHECKING));
69 | accountList.add(new Account(54321, AccountType.CHECKING));
70 | accountList.add(new Account(78789, AccountType.SAVINGS));
71 |
72 | for(Account account: accountList) {
73 | System.out.println(account.toString());
74 | }
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/src/main/java/exercises/Account.java:
--------------------------------------------------------------------------------
1 | package exercises;
2 |
3 | public class Account {
4 |
5 | /* 1 */
6 |
7 | // Add three properties to this Account class:
8 | // - one that represents the balance (for now, assume that a balance is expressed in whole euros (no cents))
9 | // - one that represents the account number (a number value)
10 | // - one that stores the type of account (either 'checking' or 'savings')
11 | // What data types do these properties have?
12 |
13 |
14 | /* 2 */
15 |
16 | // Add a constructor that enables you to pass
17 | // a value for the properties as arguments
18 |
19 | // Add a no-argument constructor
20 | // Specify a default value for each property
21 | // The account number can be a fixed value for now, such as 12345
22 |
23 |
24 | /* 3 */
25 |
26 | // Add a method getBalance() that returns the account balance
27 | // Do you need parameters?
28 | // What is the return type of the method?
29 |
30 |
31 | // Write a method deposit() that allows you to deposit money (whole euros only, again) into an account
32 | // Do you need parameters?
33 | // What is the return type of the method?
34 |
35 |
36 | // Write a method withdraw() that allows you to withdraw money (whole euros only, again) from an account
37 | // Do you need parameters?
38 | // What is the return type of the method?
39 |
40 |
41 |
42 | // Write a method toString() that returns a description of the account in the form
43 | // The account with number is of type '' and has a balance of
44 | // Use either string concatenation ("" + "") or String.format()
45 | // Do you need parameters?
46 | // What is the return type of the method?
47 |
48 |
49 | /* 4 */
50 |
51 | // Complete the method anArrayOfAccounts() that creates and fills an array of accounts
52 | // Three elements is enough!
53 | // You can use any balance, type and account number you want
54 | public void anArrayOfAccounts() {
55 |
56 | }
57 |
58 | // Write a method that creates and fills a list of accounts
59 | // Three elements is enough!
60 | // You can use any balance, type and account number you want
61 | public void aListOfAccounts() {
62 |
63 | }
64 |
65 | /* 5 */
66 |
67 | // Add an enhanced loop to both methods from section 4
68 | // Iterate over the array and the list and print the result of the toString()
69 | // method to the standard output for each account in the array c.q. the list.
70 |
71 | /* 6 */
72 |
73 | // Create an enumeration called AccountType and add possible values CHECKING or SAVINGS
74 | // Modify your account class so that the account type can only have a value included in this enum.
75 |
76 | /* 7 */
77 |
78 | // Modify the implementation of the withdraw() method so that you can only overdraw on CHECKING accounts
79 | // If you try and overdraw on an account of type SAVINGS, print a message to the standard output.
80 | // DON'T perform the overdraw in this case!
81 |
82 | /* 8 */
83 |
84 | // Modify the implementation of the withdraw() method again, but now so that it
85 | // throws an ArithmeticException when you try and overdraw on a savings account.
86 | }
87 |
--------------------------------------------------------------------------------