-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiscounts.java
170 lines (127 loc) · 4.28 KB
/
Discounts.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
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.Collectors;
class Proizvod{
private String name;
private float cena;
private float popust;
private float p;
public Proizvod(float popust, float cena) {
this.popust = popust;
this.cena = cena;
this.p = (Math.abs(cena-popust)/cena)*100;
}
public float getP() {
return p;
}
public Proizvod(String name, int popust, int cena) {
this.name = name;
this.popust = popust;
this.cena = cena;
}
public String getName() {
return name;
}
public float getCena() {
return cena;
}
public float getPopust() {
return popust;
}
}
class Store{
String name;
List<Proizvod> ceni;
public Store(String name, List<Proizvod> ceni) {
this.name = name;
this.ceni = ceni;
}
public String getName() {
return name;
}
public List<Proizvod> getCeni() {
return ceni;
}
public double detAverageDiscount(){
double sum=0.0;
for(int i=0;i<ceni.size();i++)
{
sum+=((ceni.get(i).getCena()-ceni.get(i).getPopust())*100)/ceni.get(i).getCena();
}
return sum/ceni.size();
}
public int getTotalDiscount(){
return (int) ceni.stream().mapToDouble(s->s.getCena()-s.getPopust()).sum();
}
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append(getName());
sb.append('\n' + "Average discount: "+ detAverageDiscount());
sb.append('\n' + "Total discount: " + getTotalDiscount() + '\n');
List<Proizvod> list = new ArrayList<Proizvod>();
for(int i=0;i<ceni.size();i++)
{
int p=0;
list.add(new Proizvod(ceni.get(i).getPopust(),ceni.get(i).getCena()));
}
list= list.stream().sorted(Comparator.comparing(Proizvod::getP).reversed().thenComparing(Proizvod::getName)).collect(Collectors.toList());
for(int i=0;i<list.size();i++)
{
sb.append(list.get(i).getP()+" ");
sb.append(list.get(i).getPopust()+" "+ list.get(i).getCena() + "\n");
}
sb.deleteCharAt(sb.length()-1);
return sb.toString();
}
}
class Discounts{
private Map<String,Store> map;
public Discounts() {
this.map = new HashMap<String, Store>();
}
public int readStores(InputStream inputStream) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String s;
String [] pom;
while((s=br.readLine())!=null)
{
pom=s.split("\\s+");
String name = pom[0];
List<Proizvod> CENI = new ArrayList<>();
for(int i=1;i<pom.length;i++)
{
String [] delovi = pom[i].split(":");
int cena =Integer.parseInt(delovi[1]) ;
int popust = Integer.parseInt(delovi[0]);
CENI.add(new Proizvod(popust,cena));
}
map.putIfAbsent(name,new Store(name,CENI));
}
return map.size();
}
public List<Store> byAverageDiscount(){
List<Store> list = new ArrayList<>();
list =map.values().stream().sorted(Comparator.comparing(Store::detAverageDiscount).reversed().thenComparing(Store::getName))
.limit(3).collect(Collectors.toList());
return list;
}
public List<Store> byTotalDiscount(){
List<Store> list = map.values().stream().sorted(Comparator.comparing(Store::detAverageDiscount).reversed().thenComparing(Store::getName))
.limit(3).collect(Collectors.toList());
return list;
}
}
public class DiscountsTest {
public static void main(String[] args) throws Exception{
Discounts discounts = new Discounts();
int stores = discounts.readStores(System.in);
System.out.println("Stores read: " + stores);
System.out.println("=== By average discount ===");
discounts.byAverageDiscount().forEach(System.out::println);
System.out.println("=== By total discount ===");
discounts.byTotalDiscount().forEach(System.out::println);
}
}