-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotAmused.java
More file actions
117 lines (97 loc) · 3.29 KB
/
Copy pathNotAmused.java
File metadata and controls
117 lines (97 loc) · 3.29 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
import java.util.*;
public class NotAmused {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int day = 1;
while (sc.hasNextLine()) {
String line = sc.nextLine();
if (line.contains("OPEN")) {
ArrayList<Customer> customers = new ArrayList<>();
while (true) {
line = sc.nextLine();
if (line.contains("CLOSE")) {
Collections.sort(customers);
printDay(day, customers);
break;
} else if (line.contains("ENTER")) {
if(isInList(line.split(" ")[1], Integer.parseInt(line.split(" ")[2]), customers)){
continue;
}
createCustomer(line.split(" ")[1], Integer.parseInt(line.split(" ")[2]), customers);
} else if (line.contains("EXIT")) {
calculateBill(line.split(" ")[1], Integer.parseInt(line.split(" ")[2]), customers);
}
}
}
day++;
}
sc.close();
}
private static boolean isInList(String name, int arrival, ArrayList<Customer> customers) {
for (Customer customer : customers) {
if (customer.getName().equals(name)) {
// update arrival time, so when they exit again it will be calculated
customer.setArrival(arrival);
return true;
}
}
return false;
}
public static void createCustomer(String name, int arrival, ArrayList<Customer> customers) {
Customer c = new Customer(name, arrival);
customers.add(c);
}
public static void calculateBill(String name, int leave, ArrayList<Customer> customers) {
for (Customer customer : customers) {
if (customer.getName().equals(name)) {
customer.setExit(leave);
customer.setBill(customer.getBill() + ((customer.getExit() - customer.getArrival()) * .1));
}
}
}
public static void printDay(int day, ArrayList<Customer> customers) {
System.out.printf("Day %d\n", day);
for (Customer customer : customers) {
System.out.printf("%s $%.2f\n", customer.getName(), customer.getBill());
}
System.out.println();
}
}
class Customer implements Comparable<Customer> {
private String name;
private int arrival;
private int exit;
private double bill;
public Customer(String name, int arrival) {
this.name = name;
this.arrival = arrival;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getArrival() {
return arrival;
}
public void setArrival(int arrival) {
this.arrival = arrival;
}
public int getExit() {
return exit;
}
public void setExit(int exit) {
this.exit = exit;
}
public double getBill() {
return bill;
}
public void setBill(double bill) {
this.bill = bill;
}
@Override
public int compareTo(Customer o) {
return this.getName().compareTo(o.getName());
}
}