-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest2004_4.java
More file actions
105 lines (86 loc) · 2.52 KB
/
Copy pathTest2004_4.java
File metadata and controls
105 lines (86 loc) · 2.52 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
package org.line;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
/*
* snapshots 원본
* transactions 기록
* 이름, 숫자 구하기
* */
public class Test2004_4 {
public static void main(String[] args) {
String[][] snapshots = { { "ACCOUNT1", "100" }, { "ACCOUNT2", "150" }
,{ "AACOUNT0", "150" }};
String[][] transactions = {
{"1", "SAVE", "ACCOUNT2", "100"},
{"2", "WITHDRAW", "ACCOUNT1", "50"},
{"1", "SAVE", "ACCOUNT2", "100"},
{"4", "SAVE", "ACCOUNT3", "500"},
{"3", "WITHDRAW", "ACCOUNT2", "30"}};
solution(snapshots,transactions);
}
static List<info> list = new ArrayList<>();
public static String[][] solution(String[][] snapshots, String[][] transactions) {
for (int i = 0; i < snapshots.length; i++) {
list.add(new info(snapshots[i][0], Long.parseLong(snapshots[i][1])));
}
boolean[] visited = new boolean[transactions.length + 1];
for (int i = 0; i < transactions.length; i++) {
int id = Integer.parseInt(transactions[i][0]);
String type = transactions[i][1]; // save, withdraw
String accountName = transactions[i][2];
long accountPrice = Long.parseLong(transactions[i][3]);
if(visited[id] == true)
continue;
visited[id] = true;
int idx = find(accountName);
if(idx == -1) {
list.add(new info(accountName, accountPrice));
}else {
if(type.equals("SAVE"))
list.get(idx).savePrice(accountPrice);
else
list.get(idx).withPrice(accountPrice);
}
}
list.sort((info1,info2) ->
info1.name.compareTo(info2.name));
String[][] answer = new String[list.size()][2];
for(int i=0; i<list.size(); i++) {
answer[i][0] = list.get(i).name;
answer[i][0] = list.get(i).price+"";
System.out.println(list.get(i).name+", "+list.get(i).price);
}
return answer;
}
private static int find(String accountName) {
for(int i=0; i<list.size(); i++) {
if(list.get(i).name.equals(accountName)) {
return i;
}
}
return -1;
}
}
class info implements Comparable<info>{
String name;
long price;
info(String name, long price) {
this.name = name;
this.price = price;
}
long savePrice(long p) {
return price += p;
}
long withPrice(long p) {
return price -= p;
}
public int compare(info arg0, info arg1) {
return arg0.name.compareTo(arg1.name);
}
@Override
public int compareTo(info arg0) {
// TODO Auto-generated method stub
return this.name.compareTo(arg0.name);
}
}