-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPiecemealVacation.java
More file actions
55 lines (48 loc) · 1.63 KB
/
PiecemealVacation.java
File metadata and controls
55 lines (48 loc) · 1.63 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
package module2.oop.vacation;
import java.util.HashMap;
import java.util.Map;
import java.util.StringJoiner;
/**
* PiecemealVacation
*/
public class PiecemealVacation extends Vacation {
private HashMap<String, Double> items;
/**
* Construct a PiecemealVacation
*
* @param destination The vacation's destination
* @param budget The budget (in dollars) alloted for the vacation
* @param items A data set containing items and their prices.
*/
public PiecemealVacation(String destination, double budget, HashMap<String, Double> items) {
super(destination, budget);
this.items = items;
}
public double getTotalCost() {
double total = 0;
for (double i : items.values()) {
total += i;
}
return total;
}
public void display() {
System.out.println(this.toString());
}
@Override
public String toString() {
StringJoiner joiner = new StringJoiner("\n");
joiner.add("DESTINATION: " + this.getDestination());
joiner.add("BUDGET: " + moneyFormat(getBudget()));
joiner.add("ITEMS:");
// forEach key-value pair in the HashMap
for (Map.Entry<String, Double> entry : this.items.entrySet()) {
String key = entry.getKey();
double val = entry.getValue();
String line = String.format("%-10s %-10s", key, moneyFormat(val));
joiner.add(" • " + line);
};
joiner.add(String.format("%-10s %-10s","TOTAL PRICE: ", moneyFormat(getTotalCost())));
joiner.add(getSurplusString());
return joiner.toString();
}
}