Selaa lähdekoodia

final product

yauhenip 8 vuotta sitten
vanhempi
commit
d0b88bf9b4

+ 8
- 0
pom.xml Näytä tiedosto

@@ -7,6 +7,14 @@
7 7
     <groupId>com.zipcodewilmington</groupId>
8 8
     <artifactId>bankaccountlab</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <dependencies>
11
+        <dependency>
12
+            <groupId>org.junit.jupiter</groupId>
13
+            <artifactId>junit-jupiter-api</artifactId>
14
+            <version>5.2.0</version>
15
+            <scope>test</scope>
16
+        </dependency>
17
+    </dependencies>
10 18
 
11 19
 
12 20
 </project>

+ 105
- 0
src/main/java/com/zipcodewilmington/bankaccountlab/Atm.java Näytä tiedosto

@@ -0,0 +1,105 @@
1
+package com.zipcodewilmington.bankaccountlab;
2
+
3
+import java.util.Scanner;
4
+
5
+public class Atm {
6
+    private Scanner scanner = new Scanner(System.in);
7
+    private int accountIndex;
8
+    private int recipientIndex;
9
+    private String command;
10
+
11
+    BankAccount[] accounts;
12
+
13
+    public Atm(BankAccount[] accounts) {
14
+       this.accounts = accounts;
15
+    }
16
+
17
+    public void setCommand() {
18
+        System.out.println(showCommandMenu());
19
+        this.command = scanner.next();
20
+    }
21
+
22
+    public void setAccountIndex() {
23
+        System.out.println(showAccounts());
24
+        accountIndex = scanner.nextInt();
25
+    }
26
+
27
+    public void setRecipientIndex() {
28
+        System.out.println(showAccounts());
29
+        recipientIndex = scanner.nextInt();
30
+    }
31
+
32
+    public BankAccount getAccount(int index) {
33
+        return this.accounts[index - 1];
34
+    }
35
+
36
+    public String showAccounts() {
37
+        String listOfAccounts = "";
38
+        for (int i=0; i<this.accounts.length; i++) {
39
+            listOfAccounts = listOfAccounts.concat(i+1 + ". " + this.accounts[i].getName() + "\n");
40
+        }
41
+        return listOfAccounts;
42
+    }
43
+
44
+    public String showCommandMenu() {
45
+        return "You're working with " + getAccount(accountIndex).getName() + "\n" +
46
+                "Please choose an operation:\n" +
47
+                "1. Display balance\n" +
48
+                "2. Deposit money\n" +
49
+                "3. Withdraw money\n" +
50
+                "4. Transfer money\n" +
51
+                "5. Choose another account\n" +
52
+                "6. Exit";
53
+    }
54
+
55
+    public int getInt(String message) {
56
+        System.out.println(message);
57
+        return scanner.nextInt();
58
+    }
59
+
60
+    public String executeCommand() {
61
+        int amount;
62
+        String output = "";
63
+        boolean flag = true;
64
+        while (flag) {
65
+            switch (command) {
66
+                case "1":
67
+                    output = getAccount(accountIndex).getBalance();
68
+                    flag = !flag;
69
+                    break;
70
+                case "2":
71
+                    amount = getInt("How much would you like to deposit?");
72
+                    output = getAccount(accountIndex).deposit(amount);
73
+                    flag = !flag;
74
+                    break;
75
+                case "3":
76
+                    amount = getInt("How much would you like to withdraw?");
77
+                    output = getAccount(accountIndex).withdraw(amount);
78
+                    flag = !flag;
79
+                    break;
80
+                case "4":
81
+                    System.out.println("Choose a recipient account from a list:");
82
+                    setRecipientIndex();
83
+                    amount = getInt("How much would you like to transfer?");
84
+                    output = (getAccount(accountIndex).transfer(getAccount(recipientIndex), amount));
85
+                    flag = !flag;
86
+                    break;
87
+                case "5":
88
+                    System.out.println("Choose another account:");
89
+                    setAccountIndex();
90
+                    setCommand();
91
+                    break;
92
+                case "6":
93
+                    System.out.println("Thank you, and goodbye");
94
+                    output = "exit";
95
+                    flag = !flag;
96
+                    break;
97
+                default:
98
+                    output = "Wrong input, please try again";
99
+                    flag = !flag;
100
+                    break;
101
+            }
102
+        }
103
+        return output;
104
+    }
105
+}

+ 46
- 4
src/main/java/com/zipcodewilmington/bankaccountlab/BankAccount.java Näytä tiedosto

@@ -1,7 +1,49 @@
1 1
 package com.zipcodewilmington.bankaccountlab;
2 2
 
3
-/**
4
- * Created by leon on 1/10/18.
5
- */
6
-public class BankAccount {
3
+public abstract class BankAccount {
4
+
5
+    double balance;
6
+    String name;
7
+
8
+    public BankAccount() {
9
+    }
10
+
11
+    public BankAccount(double balance, String name) {
12
+        this.balance = balance;
13
+        this.name = name;
14
+    }
15
+
16
+    public String getBalance() {
17
+        return String.format("%s %.2f", "Available balance is", this.balance);
18
+    }
19
+
20
+    public String getName() {
21
+        return this.name;
22
+    }
23
+
24
+    public String withdraw(double amount) {
25
+        if (this.balance >= amount) {
26
+            this.balance -= amount;
27
+            return "Please collect money";
28
+        } else {
29
+            return "Insufficient balance";
30
+        }
31
+    }
32
+
33
+    public String deposit(double amount) {
34
+        this.balance += amount;
35
+        return "Money has been successfully deposited";
36
+    }
37
+
38
+    public String transfer(BankAccount recipient, double amount) {
39
+        if (this.balance >= amount) {
40
+            this.withdraw(amount);
41
+            recipient.deposit(amount);
42
+            return "Money has been successfully transferred";
43
+        } else {
44
+            return "Insufficient balance";
45
+        }
46
+    }
47
+
48
+
7 49
 }

+ 8
- 0
src/main/java/com/zipcodewilmington/bankaccountlab/BusinessAccount.java Näytä tiedosto

@@ -0,0 +1,8 @@
1
+package com.zipcodewilmington.bankaccountlab;
2
+
3
+public class BusinessAccount extends BankAccount{
4
+
5
+    public BusinessAccount(double balance, String name) {
6
+        super(balance, name);
7
+    }
8
+}

+ 8
- 0
src/main/java/com/zipcodewilmington/bankaccountlab/CheckingAccount.java Näytä tiedosto

@@ -0,0 +1,8 @@
1
+package com.zipcodewilmington.bankaccountlab;
2
+
3
+public class CheckingAccount extends BankAccount {
4
+
5
+   public CheckingAccount(double balance, String name) {
6
+        super(balance, name);
7
+    }
8
+}

+ 18
- 3
src/main/java/com/zipcodewilmington/bankaccountlab/MainApplication.java Näytä tiedosto

@@ -1,7 +1,22 @@
1 1
 package com.zipcodewilmington.bankaccountlab;
2 2
 
3
-/**
4
- * Created by leon on 1/10/18.
5
- */
6 3
 public class MainApplication {
4
+    public static void main(String args[]) {
5
+        BankAccount ca  = new CheckingAccount(1090.36, "Checking Account");
6
+        BankAccount sa = new SavingsAccount(8675.07, "Savings Account");
7
+        BankAccount ba = new BusinessAccount(10090.88, "Business Account");
8
+        BankAccount[] accounts = {ca, sa, ba};
9
+        Atm atm = new Atm(accounts);
10
+        String message;
11
+
12
+        System.out.println("Welcome to your ATM!\nPlease choose an account:");
13
+        atm.setAccountIndex();
14
+
15
+        while (true) {
16
+            atm.setCommand();
17
+            message = atm.executeCommand();
18
+            if (message.equals("exit")) break;
19
+            else System.out.println(message);
20
+        }
21
+    }
7 22
 }

+ 8
- 0
src/main/java/com/zipcodewilmington/bankaccountlab/SavingsAccount.java Näytä tiedosto

@@ -0,0 +1,8 @@
1
+package com.zipcodewilmington.bankaccountlab;
2
+
3
+public class SavingsAccount extends BankAccount {
4
+
5
+    public SavingsAccount(double balance, String name) {
6
+        super(balance, name);
7
+    }
8
+}

+ 30
- 0
src/test/java/com/zipcodewilmington/bankaccountlab/AtmTest.java Näytä tiedosto

@@ -0,0 +1,30 @@
1
+package com.zipcodewilmington.bankaccountlab;
2
+
3
+import org.junit.jupiter.api.Test;
4
+
5
+import static org.junit.jupiter.api.Assertions.assertEquals;
6
+
7
+public class AtmTest {
8
+
9
+    BankAccount ca  = new CheckingAccount(1090.36, "Checking Account");
10
+    BankAccount sa = new SavingsAccount(8675.07, "Savings Account");
11
+    BankAccount ba = new BusinessAccount(10090.88, "Business Account");
12
+    BankAccount[] accounts = {ca, sa, ba};
13
+    Atm atm = new Atm(accounts);
14
+
15
+    @Test
16
+    public void showAccountsTest() {
17
+        String expected = "1. Checking Account\n" +
18
+                "2. Savings Account\n" +
19
+                "3. Business Account\n";
20
+        String actual = atm.showAccounts();
21
+        assertEquals(expected, actual);
22
+    }
23
+
24
+    @Test
25
+    public void executeCommandTest() {
26
+        BankAccount expected = sa;
27
+        BankAccount actual = atm.getAccount(2);
28
+        assertEquals(expected, actual);
29
+    }
30
+}

+ 58
- 3
src/test/java/com/zipcodewilmington/bankaccountlab/BankAccountTest.java Näytä tiedosto

@@ -1,7 +1,62 @@
1 1
 package com.zipcodewilmington.bankaccountlab;
2 2
 
3
-/**
4
- * Created by leon on 1/10/18.
5
- */
3
+import org.junit.jupiter.api.Test;
4
+
5
+import static org.junit.jupiter.api.Assertions.assertEquals;
6
+
6 7
 public class BankAccountTest {
8
+
9
+    CheckingAccount ca = new CheckingAccount(920.00, "Checking");
10
+    SavingsAccount sa = new SavingsAccount(80.90, "Savings");
11
+
12
+    @Test
13
+    public void withdrawTestNormal() {
14
+        String expected = "Please collect money";
15
+        String actual = ca.withdraw(80);
16
+        assertEquals(expected, actual);
17
+    }
18
+
19
+    @Test
20
+    public void withdrawTestNotEnoughMoney() {
21
+        String expected = "Insufficient balance";
22
+        String actual = ca.withdraw(920.20);
23
+        assertEquals(expected, actual);
24
+    }
25
+
26
+    @Test
27
+    public void depositTestNormal() {
28
+        String expected = "Money has been successfully deposited";
29
+        String actual = ca.deposit(80);
30
+        assertEquals(expected, actual);
31
+    }
32
+
33
+    @Test
34
+    public void getBalanceTest() {
35
+        String expected = "Available balance is 990.56";
36
+        ca.deposit(70.56);
37
+        String actual = ca.getBalance();
38
+        assertEquals(expected, actual);
39
+    }
40
+
41
+    @Test
42
+    public void transferTestNormal() {
43
+        String expected = "Money has been successfully transferred";
44
+        String actual = ca.transfer(sa,480);
45
+        assertEquals(expected, actual);
46
+    }
47
+
48
+    @Test
49
+    public void transferTestNormal2() {
50
+        String expected = "Available balance is 120.00";
51
+        ca.transfer(sa,800);
52
+        String actual = ca.getBalance();
53
+        assertEquals(expected, actual);
54
+    }
55
+
56
+    @Test
57
+    public void transferTestNotEnoughMoney() {
58
+        String expected = "Insufficient balance";
59
+        String actual = sa.transfer(ca, 480);
60
+        assertEquals(expected, actual);
61
+    }
7 62
 }