-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathUVa00187_TransactionProcessing.java
113 lines (97 loc) · 2.69 KB
/
UVa00187_TransactionProcessing.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package uva;
import java.io.*;
import java.util.*;
public class UVa00187_TransactionProcessing {
static class TransactionLine {
String seq;
String acc;
double val;
public TransactionLine(String seq, String acc, double val) {
this.seq = seq;
this.acc = acc;
this.val = val;
}
}
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
HashMap<String, String> accounts = new HashMap<String, String>();
while (true) {
String acc = in.nextLine();
if (acc.startsWith("000"))
break;
String number = acc.substring(0, 3);
String name = acc.substring(3);
accounts.put(number, name);
}
ArrayList<TransactionLine> lines = new ArrayList<TransactionLine>();
String prev = "";
while (true) {
String line = in.nextLine();
if (line.startsWith("000"))
break;
String seq = line.substring(0, 3);
String acc = line.substring(3, 6);
double val = Double.parseDouble(line.substring(6).trim()) / 100.0;
if (!seq.equals(prev)) {
double sum = 0.0;
for (TransactionLine l : lines) {
sum += l.val;
}
if (sum != 0.0) {
System.out.println("*** Transaction " + lines.get(0).seq + " is out of balance ***");
for (TransactionLine l : lines) {
StringBuilder sb = new StringBuilder();
sb.append(l.acc + " ");
sb.append(accounts.get(l.acc));
for (int i = 0; i < 30 - accounts.get(l.acc).length(); ++i)
sb.append(" ");
sb.append(" ");
sb.append(String.format(Locale.ENGLISH, "%10.2f", l.val));
System.out.println(sb);
}
System.out.println("999 Out of Balance " + String.format(Locale.ENGLISH, "%10.2f", Math.abs(sum)));
System.out.println();
}
lines.clear();
prev = seq;
}
lines.add(new TransactionLine(seq, acc, val));
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
try {
while (tokenizer == null || !tokenizer.hasMoreTokens())
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
return tokenizer.nextToken();
}
public String nextLine() {
String line = null;
try {
tokenizer = null;
line = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return line;
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}