#7 Complete - Demetrius

Aberto
demetrm quer mesclar 1 commits de demetrm/Checkbook-OldStyle:master em master

+ 0
- 16
Checkbook/Checkbook.iml Ver arquivo

@@ -1,16 +0,0 @@
1
-<?xml version="1.0" encoding="UTF-8"?>
2
-<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3
-  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
4
-    <output url="file://$MODULE_DIR$/target/classes" />
5
-    <output-test url="file://$MODULE_DIR$/target/test-classes" />
6
-    <content url="file://$MODULE_DIR$">
7
-      <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
8
-      <sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
9
-      <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
10
-      <excludeFolder url="file://$MODULE_DIR$/target" />
11
-    </content>
12
-    <orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
13
-    <orderEntry type="sourceFolder" forTests="false" />
14
-    <orderEntry type="library" name="Maven: junit:junit:4.0" level="project" />
15
-  </component>
16
-</module>

+ 144
- 0
Checkbook/src/main/java/Checkbook.java Ver arquivo

@@ -1,5 +1,149 @@
1
+import java.lang.reflect.ParameterizedType;
2
+import java.time.LocalDate;
3
+import java.util.*;
4
+import java.util.stream.Collectors;
5
+import java.util.stream.Stream;
6
+
1 7
 public class Checkbook {
2 8
 
3 9
     //blind us with your Java eloquence....
4 10
 
11
+    private List<Transaction> debitTransactions;
12
+    private List<Transaction> creditTransactions;
13
+
14
+    Payee owner;
15
+    double cashAmount;
16
+
17
+    Checkbook(){
18
+        new Checkbook(0);
19
+    }
20
+
21
+    Checkbook(double cashAmount){
22
+        new Checkbook(new Payee(),cashAmount);
23
+    }
24
+
25
+    Checkbook(Payee owner, double cashAmount){
26
+        this.owner = owner;
27
+        this.cashAmount = cashAmount;
28
+        debitTransactions = new ArrayList<>();
29
+        creditTransactions = new ArrayList<>();
30
+    }
31
+
32
+    public boolean add(Transaction element){
33
+        if (element.getType() == null) return false;
34
+        if (element.getType().equals(TransactionType.DEBIT))
35
+            return debitTransactions.add(element);
36
+        else return creditTransactions.add(element);
37
+    }
38
+
39
+    public int size(){
40
+        return debitTransactions.size() + creditTransactions.size();
41
+    }
42
+
43
+    public boolean isEmpty(){
44
+        return debitTransactions.isEmpty() && creditTransactions.isEmpty();
45
+    }
46
+
47
+    public boolean contains(Transaction element){
48
+        return debitTransactions.contains(element) || creditTransactions.contains(element);
49
+    }
50
+
51
+    public boolean remove(Transaction element){
52
+        return debitTransactions.remove(element) || creditTransactions.remove(element);
53
+    }
54
+
55
+    public doubleRater iterator(){
56
+        return new doubleRater();
57
+    }
58
+
59
+    public Transaction[] toArray(){
60
+        return Stream.concat(debitTransactions.stream(),creditTransactions.stream()).toArray(Transaction[]::new);
61
+    }
62
+
63
+    public Transaction[] toArray(Transaction[] array){
64
+        List<Transaction> list = Stream.concat(debitTransactions.stream(),creditTransactions.stream())
65
+                .collect(Collectors.toList());
66
+        return list.toArray(array);
67
+    }
68
+
69
+    public double checkbookValue(){
70
+        double sum = debitTransactions.stream().mapToDouble(Transaction::getAmount).sum();
71
+        sum += creditTransactions.stream().mapToDouble(Transaction::getAmount).sum();
72
+        return sum;
73
+    }
74
+
75
+    public Transaction[] getTransactionsForDate(LocalDate aDate){
76
+        return Arrays.stream(toArray()).filter(e -> e.getDate().equals(aDate)).toArray(Transaction[]::new);
77
+    }
78
+
79
+    public Transaction[] getAllDebitTransactions(){
80
+        return debitTransactions.toArray(new Transaction[0]);
81
+    }
82
+
83
+    public Transaction[] getAllCreditTransactions(){
84
+        return creditTransactions.toArray(new Transaction[0]);
85
+    }
86
+
87
+    public boolean containsAll(Collection c){
88
+        return Stream.concat(debitTransactions.stream(),creditTransactions.stream())
89
+                .collect(Collectors.toList()).containsAll(c);
90
+    }
91
+
92
+    public boolean addAll(Collection c){
93
+        for (Object o : c){
94
+            if (!o.getClass().getSimpleName().equals("Transaction"))
95
+                return false;
96
+            add((Transaction) o);
97
+        }
98
+        return true;
99
+    }
100
+
101
+    public boolean removeAll(Collection c){
102
+        int size = size();
103
+        for (Object o : c)
104
+            remove((Transaction) o);
105
+        return size != size();
106
+    }
107
+
108
+    public void clear(){
109
+        debitTransactions.clear();
110
+        creditTransactions.clear();
111
+    }
112
+
113
+    private class doubleRater implements Iterator<Transaction> {
114
+        private final List<Iterator<Transaction>> list;
115
+        private final Iterator<Iterator<Transaction>> it;
116
+        private Iterator<Transaction> current;
117
+
118
+        doubleRater(){
119
+            list = new ArrayList<>();
120
+            list.add(debitTransactions.iterator());
121
+            list.add(creditTransactions.iterator());
122
+            it = list.iterator();
123
+            current = it.next();
124
+        }
125
+
126
+
127
+        @Override
128
+        public boolean hasNext() {
129
+            checkIterator();
130
+            return current.hasNext();
131
+        }
132
+
133
+        @Override
134
+        public Transaction next() {
135
+            checkIterator();
136
+            return current.next();
137
+        }
138
+
139
+        @Override
140
+        public void remove() {
141
+            current.remove();
142
+        }
143
+
144
+        private void checkIterator(){
145
+            if (!current.hasNext() && it.hasNext())
146
+                current = it.next();
147
+        }
148
+    }
5 149
 }

+ 89
- 1
Checkbook/src/main/java/GenericAccount.java Ver arquivo

@@ -1,3 +1,91 @@
1
+import java.lang.reflect.Array;
2
+import java.time.LocalDate;
3
+import java.util.*;
4
+import java.util.stream.Collectors;
5
+import java.util.stream.Stream;
1 6
 
2
-public class GenericAccount {
7
+public class GenericAccount<T extends AccountBook> {
8
+    private List<T> transactions;
9
+
10
+    Payee owner;
11
+    double cashAmount;
12
+
13
+    GenericAccount(){
14
+        new GenericAccount<>(0);
15
+    }
16
+
17
+    GenericAccount(double cashAmount){
18
+        new GenericAccount<>(new Payee(),cashAmount);
19
+    }
20
+
21
+    GenericAccount(Payee owner, double cashAmount){
22
+        this.owner = owner;
23
+        this.cashAmount = cashAmount;
24
+        transactions = new ArrayList<>();
25
+    }
26
+
27
+    public boolean add(T element){
28
+        return transactions.add(element);
29
+    }
30
+
31
+    public int size(){
32
+        return transactions.size();
33
+    }
34
+
35
+    public boolean isEmpty(){
36
+        return transactions.isEmpty();
37
+    }
38
+
39
+    public boolean contains(T element){
40
+        return transactions.contains(element);
41
+    }
42
+
43
+    public boolean remove(T element){
44
+        return transactions.remove(element);
45
+    }
46
+
47
+    public Iterator<T> iterator(){
48
+        return transactions.iterator();
49
+    }
50
+
51
+    public T[] toArray(){
52
+        Class clazz = transactions.get(0).getClass();
53
+        T[] array = (T[]) Array.newInstance(clazz,size());
54
+        for (int i=0; i<array.length; i++)
55
+            array[i] = transactions.get(i);
56
+        return array;
57
+    }
58
+
59
+    public T[] toArray(T[] array){
60
+        return transactions.toArray(array);
61
+    }
62
+
63
+    public double checkbookValue(){
64
+        return transactions.stream().mapToDouble(T::getAmount).sum();
65
+    }
66
+
67
+    public T[] getTransactionsForDate(LocalDate aDate){
68
+        long sum = transactions.stream().filter(e -> e.getDate().equals(aDate)).count();
69
+        Class clazz = transactions.get(0).getClass();
70
+        T[] array = (T[]) Array.newInstance(clazz,(int) sum);
71
+        for (int i=0; i<array.length; i++)
72
+            array[i] = transactions.get(i);
73
+        return array;
74
+    }
75
+
76
+    public boolean containsAll(Collection c){
77
+        return transactions.containsAll(c);
78
+    }
79
+
80
+    public boolean addAll(Collection c){
81
+        return transactions.addAll(c);
82
+    }
83
+
84
+    public boolean removeAll(Collection c){
85
+        return transactions.removeAll(c);
86
+    }
87
+
88
+    public void clear(){
89
+        transactions.clear();
90
+    }
3 91
 }

+ 21
- 2
Checkbook/src/main/java/Transaction.java Ver arquivo

@@ -1,9 +1,10 @@
1 1
 import java.time.LocalDate;
2
+import java.util.Objects;
2 3
 import java.util.concurrent.atomic.AtomicReference;
3 4
 
4
-public class Transaction {
5
+public class Transaction implements AccountBook{
5 6
     private final AtomicReference<Integer> id = new AtomicReference<Integer>();
6
-    private final AtomicReference<LocalDate> date = new AtomicReference<LocalDate>();
7
+    private final AtomicReference<LocalDate> date = new AtomicReference<LocalDate>(LocalDate.now());
7 8
     private String memo;
8 9
     private Payee payee;
9 10
     private TransactionType typee; // Credit (reduces the checkbook), Debit (increases the checkbook)
@@ -50,4 +51,22 @@ public class Transaction {
50 51
         }
51 52
         return (-1.0 * amount);
52 53
     }
54
+
55
+    @Override
56
+    public boolean equals(Object o) {
57
+        if (this == o) return true;
58
+        if (o == null || getClass() != o.getClass()) return false;
59
+        Transaction that = (Transaction) o;
60
+        return Objects.equals(id.get(), that.id.get()) &&
61
+                Objects.equals(date.get(), that.date.get()) &&
62
+                Objects.equals(memo, that.memo) &&
63
+                Objects.equals(payee, that.payee) &&
64
+                typee == that.typee &&
65
+                Objects.equals(amount, that.amount);
66
+    }
67
+
68
+    @Override
69
+    public int hashCode() {
70
+        return Objects.hash(id, date, memo, payee, typee, amount);
71
+    }
53 72
 }

+ 159
- 0
Checkbook/src/test/java/CheckbookTest.java Ver arquivo

@@ -1,12 +1,171 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+import java.time.LocalDate;
5
+import java.util.ArrayList;
6
+import java.util.Iterator;
7
+import java.util.List;
8
+
1 9
 import static org.junit.Assert.*;
2 10
 
3 11
 public class CheckbookTest {
4 12
 
13
+    Checkbook checkbook;
14
+    Payee owner;
15
+    Transaction transaction;
16
+
5 17
     @org.junit.Before
6 18
     public void setUp() throws Exception {
19
+        checkbook = new Checkbook(owner,100);
20
+        transaction = new Transaction("-",owner,TransactionType.DEBIT,1.0);
7 21
     }
8 22
 
9 23
     @org.junit.After
10 24
     public void tearDown() throws Exception {
11 25
     }
26
+
27
+    @Test
28
+    public void testIsEmpty(){
29
+        Assert.assertTrue(checkbook.isEmpty());
30
+    }
31
+
32
+    @Test
33
+    public void testAdd(){
34
+        checkbook.add(transaction);
35
+        Assert.assertEquals(1,checkbook.size());
36
+    }
37
+
38
+    @Test
39
+    public void testContains(){
40
+        checkbook.add(transaction);
41
+        Assert.assertTrue(checkbook.contains(transaction));
42
+    }
43
+
44
+    @Test
45
+    public void testRemove(){
46
+        checkbook.add(transaction);
47
+        Assert.assertTrue(checkbook.remove(transaction));
48
+    }
49
+
50
+    @Test
51
+    public void testIterator(){
52
+        Assert.assertNotNull(checkbook.iterator());
53
+    }
54
+
55
+    @Test
56
+    public void testIteratorIterates(){
57
+        createTransactions();
58
+
59
+        Iterator<Transaction> it = checkbook.iterator();
60
+
61
+        double sum = 0;
62
+        while(it.hasNext())
63
+            sum += it.next().getAmount();
64
+
65
+        Assert.assertEquals(8.0,sum);
66
+    }
67
+
68
+    @Test
69
+    public void testToArray(){
70
+        createTransactions();
71
+
72
+        Transaction[] transactionsArray = checkbook.toArray();
73
+        assertEquals(8, transactionsArray.length);
74
+    }
75
+
76
+    @Test
77
+    public void testToArray2(){
78
+        Transaction[] t = new Transaction[]{transaction};
79
+        createTransactions();
80
+
81
+        Transaction[] transactionsArray = checkbook.toArray(t);
82
+        assertEquals(8, transactionsArray.length);
83
+    }
84
+
85
+    @Test
86
+    public void testCheckbookValue(){
87
+        createTransactions();
88
+        Assert.assertEquals(8.0,checkbook.checkbookValue());
89
+    }
90
+
91
+    @Test
92
+    public void testGetTransactionsForDate(){
93
+        createTransactions();
94
+        Assert.assertEquals(8,checkbook.getTransactionsForDate(LocalDate.of(2018,12,8)).length);
95
+    }
96
+
97
+    @Test
98
+    public void testGetAllDebitTransactions(){
99
+        createTransactions();
100
+        Assert.assertEquals(4,checkbook.getAllDebitTransactions().length);
101
+    }
102
+
103
+    @Test
104
+    public void testGetAllCreditTransactions(){
105
+        createTransactions();
106
+        Assert.assertEquals(4,checkbook.getAllCreditTransactions().length);
107
+    }
108
+
109
+    @Test
110
+    public void testContainsAll(){
111
+        createTransactions();
112
+        List<Transaction> list = new ArrayList<>();
113
+        Transaction t1 = new Transaction("--", owner, TransactionType.DEBIT, 4.0);
114
+        Transaction t2 = new Transaction("--", owner, TransactionType.DEBIT, 4.0);
115
+        list.add(t1);
116
+        list.add(t2);
117
+
118
+        Assert.assertTrue("doesn't contain", checkbook.containsAll(list));
119
+
120
+    }
121
+
122
+    @Test
123
+    public void testAddAll(){
124
+        createTransactions();
125
+        List<Transaction> list = new ArrayList<>();
126
+        Transaction t1 = new Transaction("--", owner, TransactionType.DEBIT, 4.0);
127
+        Transaction t2 = new Transaction("--", owner, TransactionType.DEBIT, 4.0);
128
+        list.add(t1);
129
+        list.add(t2);
130
+
131
+        Assert.assertTrue("doesn't contain", checkbook.addAll(list));
132
+    }
133
+
134
+    @Test
135
+    public void testRemoveAll(){
136
+        createTransactions();
137
+        List<Transaction> list = new ArrayList<>();
138
+        Transaction t1 = new Transaction("--", owner, TransactionType.DEBIT, 4.0);
139
+        Transaction t2 = new Transaction("--", owner, TransactionType.DEBIT, 4.0);
140
+        list.add(t1);
141
+        list.add(t2);
142
+
143
+        Assert.assertTrue(checkbook.removeAll(list));
144
+    }
145
+
146
+    @Test
147
+    public void testClear(){
148
+        createTransactions();
149
+        checkbook.clear();
150
+        Assert.assertEquals(0,checkbook.size());
151
+    }
152
+
153
+    public void createTransactions(){
154
+        owner = new Payee();
155
+        owner.setGivenName("Will");
156
+        owner.setFamilyName("Smith");
157
+
158
+        Payee person = new Payee();
159
+        person.setFamilyName("Jackson");
160
+        person.setGivenName("Jet");
161
+
162
+        for (int i=0; i<4; i++) {
163
+            Transaction t1 = new Transaction("--", owner, TransactionType.DEBIT, 4.0);
164
+            Transaction t2 = new Transaction("--", person, TransactionType.CREDIT, 2.0);
165
+
166
+            checkbook.add(t1);
167
+            checkbook.add(t2);
168
+        }
169
+    }
170
+
12 171
 }

+ 157
- 0
Checkbook/src/test/java/GenericAccountTest.java Ver arquivo

@@ -0,0 +1,157 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+import java.time.LocalDate;
5
+import java.util.ArrayList;
6
+import java.util.Iterator;
7
+import java.util.List;
8
+
9
+import static org.junit.Assert.assertEquals;
10
+
11
+public class GenericAccountTest {
12
+    GenericAccount<Transaction> genAccount;
13
+    Payee owner;
14
+    Transaction transaction;
15
+
16
+    @org.junit.Before
17
+    public void setUp() throws Exception {
18
+        genAccount = new GenericAccount<>(owner,100);
19
+        transaction = new Transaction("-",owner,TransactionType.DEBIT,1.0);
20
+    }
21
+
22
+    @org.junit.After
23
+    public void tearDown() throws Exception {
24
+    }
25
+
26
+    @Test
27
+    public void testIsEmpty(){
28
+        Assert.assertTrue(genAccount.isEmpty());
29
+    }
30
+
31
+    @Test
32
+    public void testAdd(){
33
+        genAccount.add(transaction);
34
+        Assert.assertEquals(1,genAccount.size());
35
+    }
36
+
37
+    @Test
38
+    public void testContains(){
39
+        genAccount.add(transaction);
40
+        Assert.assertTrue(genAccount.contains(transaction));
41
+    }
42
+
43
+    @Test
44
+    public void testRemove(){
45
+        genAccount.add(transaction);
46
+        Assert.assertTrue(genAccount.remove(transaction));
47
+    }
48
+
49
+    @Test
50
+    public void testIterator(){
51
+        Assert.assertNotNull(genAccount.iterator());
52
+    }
53
+
54
+    @Test
55
+    public void testIteratorIterates(){
56
+        createTransactions();
57
+
58
+        Iterator<Transaction> it = genAccount.iterator();
59
+
60
+        double sum = 0;
61
+        while(it.hasNext())
62
+            sum += it.next().getAmount();
63
+
64
+        Assert.assertEquals(8.0,sum);
65
+    }
66
+
67
+    @Test
68
+    public void testToArray(){
69
+        createTransactions();
70
+
71
+        Transaction[] transactionsArray = genAccount.toArray();
72
+        assertEquals(8, transactionsArray.length);
73
+    }
74
+
75
+    @Test
76
+    public void testToArray2(){
77
+        Transaction[] t = new Transaction[]{transaction};
78
+        createTransactions();
79
+
80
+        Transaction[] transactionsArray = genAccount.toArray(t);
81
+        assertEquals(8, transactionsArray.length);
82
+    }
83
+
84
+    @Test
85
+    public void testCheckbookValue(){
86
+        createTransactions();
87
+        Assert.assertEquals(8.0,genAccount.checkbookValue());
88
+    }
89
+
90
+    @Test
91
+    public void testGetTransactionsForDate(){
92
+        createTransactions();
93
+        Assert.assertEquals(8,genAccount.getTransactionsForDate(LocalDate.of(2018,12,8)).length);
94
+    }
95
+
96
+    @Test
97
+    public void testContainsAll(){
98
+        createTransactions();
99
+        List<Transaction> list = new ArrayList<>();
100
+        Transaction t1 = new Transaction("--", owner, TransactionType.DEBIT, 4.0);
101
+        Transaction t2 = new Transaction("--", owner, TransactionType.DEBIT, 4.0);
102
+        list.add(t1);
103
+        list.add(t2);
104
+
105
+        Assert.assertTrue("doesn't contain", genAccount.containsAll(list));
106
+
107
+    }
108
+
109
+    @Test
110
+    public void testAddAll(){
111
+        createTransactions();
112
+        List<Transaction> list = new ArrayList<>();
113
+        Transaction t1 = new Transaction("--", owner, TransactionType.DEBIT, 4.0);
114
+        Transaction t2 = new Transaction("--", owner, TransactionType.DEBIT, 4.0);
115
+        list.add(t1);
116
+        list.add(t2);
117
+
118
+        Assert.assertTrue("doesn't contain", genAccount.addAll(list));
119
+    }
120
+
121
+    @Test
122
+    public void testRemoveAll(){
123
+        createTransactions();
124
+        List<Transaction> list = new ArrayList<>();
125
+        Transaction t1 = new Transaction("--", owner, TransactionType.DEBIT, 4.0);
126
+        Transaction t2 = new Transaction("--", owner, TransactionType.DEBIT, 4.0);
127
+        list.add(t1);
128
+        list.add(t2);
129
+
130
+        Assert.assertTrue(genAccount.removeAll(list));
131
+    }
132
+
133
+    @Test
134
+    public void testClear(){
135
+        createTransactions();
136
+        genAccount.clear();
137
+        Assert.assertEquals(0,genAccount.size());
138
+    }
139
+
140
+    public void createTransactions(){
141
+        owner = new Payee();
142
+        owner.setGivenName("Will");
143
+        owner.setFamilyName("Smith");
144
+
145
+        Payee person = new Payee();
146
+        person.setFamilyName("Jackson");
147
+        person.setGivenName("Jet");
148
+
149
+        for (int i=0; i<4; i++) {
150
+            Transaction t1 = new Transaction("--", owner, TransactionType.DEBIT, 4.0);
151
+            Transaction t2 = new Transaction("--", person, TransactionType.CREDIT, 2.0);
152
+
153
+            genAccount.add(t1);
154
+            genAccount.add(t2);
155
+        }
156
+    }
157
+}