Skip to content

Commit 63c9cb0

Browse files
author
Sarah Akinkunmi
committed
update
1 parent 079748d commit 63c9cb0

File tree

35 files changed

+86
-25
lines changed

35 files changed

+86
-25
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

out/production/deitel-exercise/chapter15/filematchingwithmultipletransactions/files/log.txt

Whitespace-only changes.

out/production/deitel-exercise/chapter15/filematchingwithmultipletransactions/files/newmast.txt

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
100 Alan Jones 348.17
2+
300 Mary Smith 27.19
3+
500 Sam Sharp 0.00
4+
700 Suzy Green -14.22
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
100 27.14
2+
300 62.11
3+
400 100.56
4+
900 82.17
5+
300 83.89
6+
700 80.78
7+
700 1.53
Binary file not shown.
Binary file not shown.

src/chapter15/filematchingwithmultipletransactions/Account.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
1-
package chapter15.filematching;
1+
package chapter15.filematchingwithmultipletransactions;
22

33
import java.io.Serializable;
44

5+
/*
6+
15.5 (File Matching with Multiple Transactions) It’s possible (and actually common) to have several
7+
transaction records with the same record key. This situation occurs, for example, when a customer makes
8+
several purchases and cash payments during a business period. Rewrite your accounts receivable
9+
file-matching program from Exercise 15.4 to provide for the possibility of handling several transaction
10+
records with the same record key. Modify the test data of CreateData.java to include the additional
11+
transaction records in Fig. 15.16.
12+
13+
Account number Dollar amount
14+
300 83.89
15+
700 80.78
16+
700 1.53
17+
18+
Fig. 15.16 | Additional transaction records
19+
*/
20+
521
public class Account implements Serializable {
622
private int account;
723
private String firstName;

src/chapter15/filematchingwithmultipletransactions/CreateAccountFile.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package chapter15.filematching;
1+
package chapter15.filematchingwithmultipletransactions;
22

33
import java.io.FileNotFoundException;
44
import java.util.Formatter;
@@ -12,7 +12,7 @@ public class CreateAccountFile {
1212

1313
public static void openFile() {
1414
try {
15-
output = new Formatter("oldmast.txt");
15+
output = new Formatter("src\\chapter15\\filematchingwithmultipletransactions\\files\\oldmast.txt");
1616
} catch (SecurityException e) {
1717
System.err.println("Write permission denied. Terminating.");
1818
System.exit(1);

src/chapter15/filematchingwithmultipletransactions/CreateTrFile.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package chapter15.filematching;
1+
package chapter15.filematchingwithmultipletransactions;
22

33
import java.io.FileNotFoundException;
44
import java.util.Formatter;
@@ -12,7 +12,7 @@ public class CreateTrFile {
1212

1313
public static void openFile() {
1414
try {
15-
output = new Formatter("trans.txt");
15+
output = new Formatter("src\\chapter15\\filematchingwithmultipletransactions\\files\\trans.txt");
1616
} catch (SecurityException e) {
1717
System.err.println("Write permission denied. Terminating.");
1818
System.exit(1);

src/chapter15/filematchingwithmultipletransactions/FileMatch.java

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
package chapter15.filematching;
1+
package chapter15.filematchingwithmultipletransactions;
22

33
import java.io.FileNotFoundException;
44
import java.io.IOException;
55
import java.nio.file.Files;
66
import java.nio.file.Path;
77
import java.nio.file.Paths;
8-
import java.util.Formatter;
9-
import java.util.NoSuchElementException;
10-
import java.util.Scanner;
8+
import java.util.*;
119

1210
public class FileMatch {
1311
private static Scanner scannerA;
@@ -19,11 +17,12 @@ public class FileMatch {
1917
private static String[] fullName;
2018
private static double[] accBal;
2119
private static double[] trAmnt;
20+
private static ArrayList<Double> trAmnt2 = new ArrayList<>();
2221
private static int lines = 0;
2322
private static int lines2 = 0;
2423

2524
public static void openAccountFile() {
26-
Path path = Paths.get("oldmast.txt");
25+
Path path = Paths.get("src\\chapter15\\filematchingwithmultipletransactions\\files\\oldmast.txt");
2726

2827
try {
2928
scannerA = new Scanner(path);
@@ -38,7 +37,7 @@ public static void openAccountFile() {
3837
}
3938

4039
public static void openTransactionRecordFile() {
41-
Path path = Paths.get("trans.txt");
40+
Path path = Paths.get("src\\chapter15\\filematchingwithmultipletransactions\\files\\trans.txt");
4241
try {
4342
scannerB = new Scanner(path);
4443
lines2 = (int) Files.lines(path).count();
@@ -64,6 +63,20 @@ public static void readAccountRecords() {
6463
}
6564
}
6665

66+
/**
67+
* for sorted arrays with elements having same values
68+
* @param arr - the array we want to eliminate duplicate values from
69+
* @return arr - with no duplicate elements
70+
*/
71+
public static int[] removeDuplicates(int[] arr) {
72+
LinkedHashSet<Integer> set = new LinkedHashSet<>();
73+
for (int j : arr) {
74+
set.add(j);
75+
}
76+
77+
return set.stream().mapToInt(Number::intValue).toArray();
78+
}
79+
6780
public static void readTrRecords() {
6881
try {
6982
while (scannerB.hasNext()) {
@@ -79,19 +92,40 @@ public static void readTrRecords() {
7992

8093
public static void compare() {
8194
try {
82-
output = new Formatter("newmast.txt");
83-
output2 = new Formatter("log.txt");
95+
output = new Formatter("src\\chapter15\\filematchingwithmultipletransactions\\files\\newmast.txt");
96+
output2 = new Formatter("src\\chapter15\\filematchingwithmultipletransactions\\files\\log.txt");
97+
98+
System.out.println();
99+
int[] trAccNum2 = removeDuplicates(trAccNum);
100+
Arrays.sort(trAccNum2);
101+
// for(int x : trAccNum2) System.out.println(x);
102+
103+
for (int i = 0; i < trAccNum.length; i++) {
104+
trAmnt2.add(trAmnt[i]);
105+
for (int j = i + 1; j < trAccNum.length; j++) {
106+
if(trAccNum[j] == trAccNum[i]) {
107+
trAmnt2.remove(trAmnt[i]);
108+
trAmnt2.add(trAmnt[i] + trAmnt[i++]);
109+
} else {
110+
trAmnt2.add(trAmnt[i]);
111+
}
112+
}
113+
}
114+
115+
for (double x : trAmnt2) {
116+
System.out.print(x + " ");
117+
}
84118

85119
if(lines == lines2) {
86120
for (int i = 0; i < lines; i++) {
87121
for (int j = 0; j < lines2; j++) {
88-
if (accNum[i] == trAccNum[j]) {
89-
output.format("%d %s %.2f%n", accNum[i], fullName[i], accBal[i] + trAmnt[i]);
122+
if (accNum[i] == trAccNum2[i]) {
123+
output.format("%d %s %.2f%n", accNum[i], fullName[i], accBal[i] += trAmnt2.get(i));
90124
break;
91-
} else if (accNum[i] != trAccNum[i]) {
125+
} else if (accNum[i] != trAccNum2[i]) {
92126
output.format("%d %s %.2f%n", accNum[i], fullName[i], accBal[i]);
93127
output2.format("%s %d%n", "Unmatched transaction record for account number",
94-
trAccNum[i]);
128+
trAccNum2[i]);
95129
break;
96130
}
97131
}

src/chapter15/filematchingwithmultipletransactions/TransactionRecord.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package chapter15.filematching;
1+
package chapter15.filematchingwithmultipletransactions;
22

33
public class TransactionRecord {
44
private int accountNumber;
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +0,0 @@
1-
Unmatched transaction record for account number 400
2-
Unmatched transaction record for account number 900
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +0,0 @@
1-
100 Alan Jones 375.31
2-
300 Mary Smith 89.30
3-
500 Sam Sharp 0.00
4-
700 Suzy Green -14.22

src/chapter15/filematchingwithmultipletransactions/files/trans.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
300 62.11
33
400 100.56
44
900 82.17
5+
300 83.89
6+
700 80.78
7+
700 1.53
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
package hackerranksolutions.thirtydaysofcode.day20;public class Day20 {
1+
package hackerranksolutions.thirtydaysofcode.day20;
2+
3+
public class Day20 {
4+
25
}

0 commit comments

Comments
 (0)