Skip to content

Commit a5f4e19

Browse files
1. flat cost calculator basic setup
1 parent c1e4d76 commit a5f4e19

File tree

8 files changed

+155
-3
lines changed

8 files changed

+155
-3
lines changed

flat-finder/.classpath

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
4+
<classpathentry kind="src" path="src"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

flat-finder/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/

flat-finder/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>flat-finder</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.8
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.8
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.itp.flatfinder.client;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import com.itp.flatfinder.model.Flat;
7+
import com.itp.flatfinder.util.FlatCostCalculator;
8+
9+
public class FlatApplication {
10+
11+
public static void main(String[] args) {
12+
13+
List<Flat> flats = new ArrayList<>();
14+
flats.add(new Flat("A", 10000, 2, 15, 500));
15+
flats.add(new Flat("B", 12000, 1, 15, 100));
16+
flats.add(new Flat("C", 11000, 4, 20, 1500));
17+
18+
FlatCostCalculator.calculateTotalCost(flats, 10, 5, 20);
19+
20+
flats.forEach(f-> {
21+
22+
System.out.println(f.getName()+"\t"+f.getTotalCost());
23+
});
24+
25+
}
26+
27+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.itp.flatfinder.model;
2+
3+
public class Flat {
4+
5+
private String name;
6+
private int rentPerMonth;
7+
private int distance;
8+
private int travelTime;
9+
private int locationAdvantage;
10+
private int totalCost;
11+
12+
public Flat(String name, int rentPerMonth, int distance, int travelTime, int locationAdvantage) {
13+
super();
14+
this.name = name;
15+
this.rentPerMonth = rentPerMonth;
16+
this.distance = distance;
17+
this.travelTime = travelTime;
18+
this.locationAdvantage = locationAdvantage;
19+
}
20+
21+
public int getTotalCost() {
22+
return totalCost;
23+
}
24+
25+
public void setTotalCost(int totalCost) {
26+
this.totalCost = totalCost;
27+
}
28+
29+
public String getName() {
30+
return name;
31+
}
32+
33+
public void setName(String name) {
34+
this.name = name;
35+
}
36+
37+
public int getRentPerMonth() {
38+
return rentPerMonth;
39+
}
40+
41+
public void setRentPerMonth(int rentPerMonth) {
42+
this.rentPerMonth = rentPerMonth;
43+
}
44+
45+
public int getDistance() {
46+
return distance;
47+
}
48+
49+
public void setDistance(int distance) {
50+
this.distance = distance;
51+
}
52+
53+
public int getTravelTime() {
54+
return travelTime;
55+
}
56+
57+
public void setTravelTime(int travelTime) {
58+
this.travelTime = travelTime;
59+
}
60+
61+
public int getLocationAdvantage() {
62+
return locationAdvantage;
63+
}
64+
65+
public void setLocationAdvantage(int locationAdvantage) {
66+
this.locationAdvantage = locationAdvantage;
67+
}
68+
69+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.itp.flatfinder.util;
2+
3+
import java.util.List;
4+
5+
import com.itp.flatfinder.model.Flat;
6+
7+
public class FlatCostCalculator {
8+
9+
// Call by Value & Call by Ref in java.
10+
public static void calculateTotalCost(List<Flat> flats, int distanceCost,
11+
int travelCost, int totalWorkingDays) {
12+
flats.parallelStream().forEach(f->{
13+
int totalCost = 0;
14+
totalCost += f.getRentPerMonth();
15+
totalCost += (f.getDistance() * distanceCost) * totalWorkingDays;
16+
totalCost += (f.getTravelTime() * travelCost) * totalWorkingDays;
17+
totalCost -= f.getLocationAdvantage();
18+
f.setTotalCost(totalCost);
19+
});
20+
}
21+
}

movie-service/src/com/itp/movie/service/MovieService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.itp.movie.service;
22

33
import java.time.LocalDate;
4-
import java.util.Arrays;
54
import java.util.Collections;
65
import java.util.Comparator;
76
import java.util.HashMap;
@@ -16,7 +15,7 @@
1615
public class MovieService {
1716

1817
// Movie In memory database
19-
private Map<String, List<Movie>> moviesMap = new HashMap();
18+
private Map<String, List<Movie>> moviesMap = new HashMap<>();
2019

2120
public void addMovie(String category, Movie movie) {
2221
// store movie in the memory
@@ -38,7 +37,8 @@ public List<Movie> getAllMovies(String category) {
3837
* m1.getRating(); } });
3938
*/
4039

41-
Collections.sort(moviesMap.get(category), (m1, m2) -> m2.getRating() - m1.getRating());
40+
Collections.sort(moviesMap.get(category),
41+
(m1, m2) -> m2.getRating() - m1.getRating());
4242

4343
return moviesMap.get(category);
4444
}

0 commit comments

Comments
 (0)