-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBusProb_ArrayList.java
More file actions
191 lines (180 loc) · 5.88 KB
/
BusProb_ArrayList.java
File metadata and controls
191 lines (180 loc) · 5.88 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package com.fresco;
import java.util.*;
class Passanger
{
int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public float getFare() {
return fare;
}
public void setFare(float fare) {
this.fare = fare;
}
float fare;
public Passanger(int id, float fare)
{
this.id=id;
this.fare=fare;
}
}
public class BusProb {
public static void main(String[] args) {
List<String> listOfInputs=new ArrayList<>();
int capacity=10;
int stops=4;
listOfInputs.add("+2501 +2502 +2503 +2504");
listOfInputs.add("-2501 -2504 +2505 +2506 +2507 +2509");
listOfInputs.add("+2501 +2511 -2502 -2505");
listOfInputs.add("+2513 -2507 -2503 -2511 -2509");
String query="1";
System.out.println(output(capacity,stops,listOfInputs,query));
// System.out.println();
}
public static String output(int capacity, int stops, List<String> listOfInputStrings, String query)
{
List<Passanger> details=new ArrayList<Passanger>();
int query1_in=0;
int query1_out=0;
double present=0;
for(String temp:listOfInputStrings) {
String[] temp_str=temp.split(" ");
double in=0;
float capacity1=capacity;
double out=0;
float fare=0;
for(String temp2:temp_str) {
if(temp2.contains("+")) {
in+=1;
}
else {
out+=1;
}
}
query1_in+=in;
query1_out+=out;
present+=in-out;
// System.out.println(present);
if(present<=Math.ceil(capacity1*0.25)) {
fare=(float) (capacity1+capacity1*0.6);
for(String temp2:temp_str) {
if(temp2.contains("+")){
String temp3=temp2.substring(1);
// System.out.println(temp3);
int id=Integer.parseInt(temp3);
Passanger obj=new Passanger(id,fare);
details.add(obj);
}
}
}
else if(present<=Math.ceil(capacity1*0.50)) {
fare=(float) (capacity1+capacity1*0.3);
for(String temp2:temp_str) {
if(temp2.contains("+")){
String temp3=temp2.substring(1);
int id=Integer.parseInt(temp3);
Passanger obj=new Passanger(id,fare);
details.add(obj);
}
}
}
else if(present>Math.ceil(capacity1*0.50)){
fare=(float) capacity1;
for(String temp2:temp_str) {
if(temp2.contains("+")){
String temp3=temp2.substring(1);
int id=Integer.parseInt(temp3);
Passanger obj=new Passanger(id,fare);
details.add(obj);
}
}
}
// System.out.println(details);
}
query=query.replace(" ", "");
String[] query_split=query.split("\\,");
if(query_split[0].equals("1")){
return query1_in+" passengers got on the bus and "+query1_out+" passengers got out of the bus";
}
else if(query_split[0].equals("2")){
int query2_1=0;
int query2_2=0;
int query2_3=0;
float fare1=(float) (capacity+capacity*0.6);
float fare2=(float) (capacity+capacity*0.3);
float fare3=(float) capacity;
for(Passanger p:details) {
if(p.getFare()==fare1) {
query2_1+=1;
}
else if(p.getFare()==fare2) {
query2_2+=1;
}
else {
query2_3+=1;
}
}
return query2_1+" passengers traveled with a fare of "+
fare1+", "+query2_2+" passengers traveled with a fare of "+
fare2+" and "+query2_3+" passengers traveled with a fare of "+
fare3;
}
else if(query_split[0].equals("3")) {
int given_id=Integer.parseInt(query_split[1]);
float total_fare=0;
for(Passanger p:details) {
if(p.getId()==given_id) {
total_fare+=p.getFare();
}
}
return "Passenger "+given_id+" spent a total fare of "+total_fare;
}
else if(query_split[0].equals("4")) {
int given_id=Integer.parseInt(query_split[1]);
int count=0;
for(Passanger p:details) {
if(p.getId()==given_id) {
count+=1;
}
}
return "Passenger "+given_id+" has got on the bus "
+ "for "+count+" times";
}
else if(query_split[0].equals("5")) {
int given_id=Integer.parseInt(query_split[1]);
String status="";
int count_in=0;
int count_out=0;
for(String temp:listOfInputStrings) {
String[] temp_str=temp.split(" ");
for(String temp2:temp_str) {
int temp3=Integer.parseInt(temp2.substring(1));
if(temp3==given_id && temp.charAt(0)=='+') {
count_in+=1;
}
else if(temp3==given_id && temp.charAt(0)=='-') {
count_out+=1;
}
else {
status="not inside ";
}
}
}
System.out.println(count_in);
System.out.println(count_out);
int ispresent=count_in-count_out;
if(ispresent%2==0) {
status="not inside ";
}
else {
status="inside ";
}
return "Passenger "+given_id+" was "+status+"the bus at the end of the trip";
}
return null;
}
}