-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
255 lines (231 loc) · 9.23 KB
/
Main.java
File metadata and controls
255 lines (231 loc) · 9.23 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static final Map<String, Runnable> COMMANDS;
public static final Map<String, String> OPTIONS;
public static HashMap<String,Double> currencies;
static {
COMMANDS = new HashMap<>();
COMMANDS.put("balance", Main::printBalance);
COMMANDS.put("bal", Main::printBalance);
COMMANDS.put("register", Main::printRegister);
COMMANDS.put("reg", Main::printRegister);
COMMANDS.put("print", Main::printPrint);
OPTIONS = new HashMap<>();
}
public static class Transaction{
public String date="";
public String description="";
public String fromMainAccount="";
public String fromSubAccount="";
public String toMainAccount="";
public String toSubAccount="";
public Double fromAmount=0.0;
public Double toAmount=0.0;
}
public static class transInfo{
public static ArrayList<Transaction> read(File file){
ArrayList<Transaction> t=new ArrayList<Transaction>();
Scanner sc = null;
try {
sc = new Scanner(new FileReader(file));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
while (true) {
String line = sc.nextLine();
if (line.isEmpty()) {
continue;
}
Transaction trans=new Transaction();
//Split line of text like ["Asset:Savings",123.00]
String[] splitLine = line.trim().split("[ \\t]+");
trans.date=splitLine[0];
trans.description=splitLine[1];
line=sc.nextLine();
splitLine = line.trim().split("[ \\t]+");
trans.fromMainAccount=splitLine[0].split(":")[0];
trans.fromSubAccount=splitLine[0].split(":")[1];
if(splitLine.length==1){trans.fromAmount=0.0;}
else{trans.fromAmount=Double.parseDouble(splitLine[1]);}
line=sc.nextLine();
splitLine = line.trim().split("[ \\t]+");
trans.toMainAccount=splitLine[0].split(":")[0];
trans.toSubAccount=splitLine[0].split(":")[1];
if(splitLine.length==1){trans.toAmount=0.0;}
else{trans.toAmount=Double.parseDouble(splitLine[1]);}
t.add(trans);
if(!sc.hasNextLine()){
break;
}
}
return t;
}
}
public static class currency{
public static HashMap<String,Double> money (File file){
HashMap<String,Double> curr=new HashMap<>();
Scanner sc = null;
try {
sc = new Scanner(new FileReader(file));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
while(true) {
String line = sc.nextLine();
if (line.isEmpty()) {
continue;
}
String[] splitLine = line.trim().split("[ \\t]+");
curr.put(splitLine[2],Double.parseDouble(splitLine[3].split("\\$")[1]));
if(!sc.hasNextLine()){
break;
}
}
return curr;
}
}
public static String Path="";
public static String dbPath="prices_db.pricedb";
public static String coin;
public static Boolean sort=false;
public static void main(String[] args) {
// Parse options and arguments from command line
Scanner sc = new Scanner(System.in);
System.out.print("$ LedgerCLI ");
String userInput = sc.nextLine();
String[] inputs=userInput.split(" ");
String command = null;
for (String arg : inputs) {
if (arg.startsWith("--")) {
// Option
int equalsIndex = arg.indexOf('=');
if (equalsIndex == -1) {
// Flag option
OPTIONS.put(arg, null);
} else {
// Value option
String option = arg.substring(0, equalsIndex);
String value = arg.substring(equalsIndex + 1);
OPTIONS.put(option, value);
}
} else {
// Command or argument
if (command == null) {
command = arg;
} else {
System.err.println("Unrecognized argument: " + arg);
System.exit(1);
}
}
}
if (command == null) {
// No command specified
System.err.println("No command specified");
System.exit(1);
}
// Get ledger file
Path = OPTIONS.get("--file");
// Get price database file
coin = OPTIONS.get("--price-db");
if (coin!=null){
File db=new File(dbPath);
currencies=currency.money(db);
}
// Get sort option
sort = OPTIONS.containsKey("--sort");
// Execute command
Runnable commandImpl = COMMANDS.get(command);
if (commandImpl == null) {
// Unrecognized command
System.err.println("Unrecognized command: " + command);
System.exit(1);
}
// Execute command
commandImpl.run();
}
static void printPrint () {
//TODO: Implement printBalance method
if(Path==null){Path= "example.ledger";}
File file = new File(Path);
Scanner sc = null;
try {
sc = new Scanner(new FileReader(file));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
while (sc.hasNextLine()) {
String line = sc.nextLine();
System.out.println(line);
}
}
static void printRegister () {
//TODO: Implement printRegister method
File file = new File("example.ledger");
ArrayList<Transaction> trans=transInfo.read(file);
HashMap<String,Double> accounts=new HashMap<>();
Double total=0.0;
if(coin!=null){
for(int i=0;i<trans.size();++i){
trans.get(i).fromAmount=trans.get(i).fromAmount/currencies.get(coin);
trans.get(i).toAmount=trans.get(i).toAmount/currencies.get(coin);
}
}
for (int i = 0; i < trans.size(); i++) {
total=0.0;
Transaction t = trans.get(i);
System.out.printf("%10s \t", t.date);
System.out.printf("%10s \n", t.description);
System.out.printf("\t\t%25s %10.5f\n", t.fromMainAccount + ":" + t.fromSubAccount, t.fromAmount);
System.out.printf("\t\t%25s %10.5f\n", t.toMainAccount + ":" + t.toSubAccount, t.toAmount);
if(t.fromMainAccount.equals("Assets")){total+=t.fromAmount;}
if (t.toMainAccount.equals("Assets")) {total+=t.toAmount;}
System.out.printf("\t\t%25s %10.5f\n","Total in Checking:",total);
//Main Account: Assets, Expenses etc
}
}
static void printBalance () {
//TODO: Implement printTransactions method
if(Path==null){Path= "example.ledger";}
File file = new File(Path);
ArrayList<Transaction> t=transInfo.read(file);
HashMap<String,Double> accounts=new HashMap<>();
if(coin!=null){
for(int i=0;i<t.size();++i){
t.get(i).fromAmount=t.get(i).fromAmount/currencies.get(coin);
t.get(i).toAmount=t.get(i).toAmount/currencies.get(coin);
}
}
for (int i = 0; i < t.size(); i++) {
Transaction transaction=t.get(i);
//Main Account: Assets, Expenses etc
if(!accounts.containsKey(transaction.fromMainAccount)){
accounts.put(transaction.fromMainAccount,0.0);
}
//Sub account: Car, Groceries etc
if(!accounts.containsKey(transaction.fromSubAccount)){
accounts.put(transaction.fromSubAccount,0.0);
}
accounts.put(transaction.fromMainAccount,accounts.get(transaction.fromMainAccount)+transaction.fromAmount);
accounts.put(transaction.fromSubAccount,accounts.get(transaction.fromSubAccount)+transaction.fromAmount);
//Where the money is going
if(!accounts.containsKey(transaction.toMainAccount)){
accounts.put(transaction.toMainAccount,0.0);
}
//Sub account: Car, Groceries etc
if(!accounts.containsKey(transaction.toSubAccount)){
accounts.put(transaction.toSubAccount,0.0);
}
accounts.put(transaction.toMainAccount,accounts.get(transaction.toMainAccount)+transaction.toAmount);
accounts.put(transaction.toSubAccount,accounts.get(transaction.toSubAccount)+transaction.toAmount);
}
System.out.println("Balance for each Account");
for (String key : accounts.keySet()) {
Double value = accounts.get(key);
System.out.printf("%-15s %15.5f%n",key,value);
}
}
}