-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.java
More file actions
242 lines (140 loc) · 6.41 KB
/
Main.java
File metadata and controls
242 lines (140 loc) · 6.41 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
class Tariff {
static Tariff[] tariffPlans = new Tariff[4];
private int id;
private String code = new String();
private String name = new String();
private double rate;
private String otherDetails = new String();
static void loadTariffs() {
createObjectAndLoadTariff( 1 , "1" , "Local calls O/N" , 1 , "NIL" );
createObjectAndLoadTariff( 2 , "2" , "other state" , 3 , "valdity-28 days" );
createObjectAndLoadTariff( 3 , "3" , "other nations" , 5 , "NIL" );
createObjectAndLoadTariff( 4 , "4" , "local and S/N" , 0.2 , "valdity-7 days" );
}
static void createObjectAndLoadTariff(int lId, String lCode, String lName, double lRate, String lOtherDetails) {
Tariff object = new Tariff();
object.id = lId;
object.code = lCode;
object.name = lName;
object.rate = lRate;
object.otherDetails = lOtherDetails;
tariffPlans[ lId - 1 ] = object;
}
private double computeCallCost(CustomerPhoneCall customerPhoneCallObject) {
long callStartTime, callEndTime, callDuration;
int callType;
callStartTime = customerPhoneCallObject.getCallStartTime().getTime();
callEndTime = customerPhoneCallObject.getCallEndTime().getTime();
callType = customerPhoneCallObject.getCallType();
callDuration = calculateCallDuration( callStartTime , callEndTime );
switch ( callType ) {
case 1:
return ( tariffPlans[ 0 ].rate * callDuration );
case 2:
return ( tariffPlans[ 1 ].rate * callDuration );
case 3:
return ( tariffPlans[ 2 ].rate * callDuration );
case 4:
return ( tariffPlans[ 3 ].rate * callDuration );
}
return 0;
}
long calculateCallDuration(long lCallStartTime, long lCallEndTime) {
long diff = lCallEndTime - lCallStartTime;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
long diffDays = diff / (24 * 60 * 60 * 1000);
if ( diffHours != 0 )
diffMinutes += ( diffHours * 60 );
if ( diffDays != 0 )
diffMinutes += ( diffHours * 1440 );
return diffMinutes;
}
public String generateCallMessage(CustomerPhoneCall customerPhoneCallObject) {
double cost;
long callType;
String tariffName = new String();
double tariffRate;
String callMessage = new String();
callType = customerPhoneCallObject.getCallType();
cost = computeCallCost( customerPhoneCallObject );
tariffName = tariffPlans[ (int)callType - 1 ].name;
tariffRate = tariffPlans[ (int)callType - 1 ].rate;
callMessage = "Dear customer, your last call cost is " + cost + "\n" + "You have been charged under the tariff named " + tariffName + "\n" + "Your tariff rate is " + tariffRate + "Rs/min";
return callMessage;
}
}
class CustomerPhoneCall {
private String customerNumber = new String();
private String calledNumber = new String();
private Date callStartTime;
private Date callEndTime;
private int callType;
void setCustomerNumber(String custNum) {
customerNumber = custNum;
}
void setCalledNumber(String calledNum) {
calledNumber = calledNum;
}
void setCallStartTime(String startTime) {
try {
callStartTime = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").parse( startTime );
} catch (Exception e) {
e.printStackTrace();
}
}
void setCallEndTime(String endTime) {
try {
callEndTime = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").parse( endTime );
} catch (Exception e) {
e.printStackTrace();
}
}
void setCallType(int type) {
callType = type;
}
String getCustomerNumber() {
return customerNumber;
}
String getCalledNumber() {
return calledNumber;
}
Date getCallStartTime() {
return callStartTime;
}
Date getCallEndTime() {
return callEndTime;
}
int getCallType() {
return callType;
}
}
class Main {
public static void main(String args[]) {
CustomerPhoneCall customerPhoneCallObject = new CustomerPhoneCall();
obtainCallDetails( customerPhoneCallObject );
Tariff tariffObject = new Tariff();
tariffObject.loadTariffs();
System.out.print( tariffObject.generateCallMessage( customerPhoneCallObject ) );
}
static void obtainCallDetails(CustomerPhoneCall customerPhoneCallObject) {
Scanner scanner = new Scanner( System.in );
System.out.println( "Enter customer phone number :" );
customerPhoneCallObject.setCustomerNumber( scanner.nextLine() );
System.out.println( "Enter the number called :" );
customerPhoneCallObject.setCalledNumber( scanner.nextLine() );
System.out.println( "Enter the call start time(dd-MM-yyyy HH:mm:ss) :" );
customerPhoneCallObject.setCallStartTime( scanner.nextLine() );
System.out.println( "Enter the call end time(dd-MM-yyyy HH:mm:ss) :" );
customerPhoneCallObject.setCallEndTime( scanner.nextLine() );
System.out.println( "Choose call type :" );
System.out.println( "1 Local calls O/N 1.0Rs/min" );
System.out.println( "2 other state 3.0Rs/min" );
System.out.println( "3 other nations 5.0Rs/min" );
System.out.println( "4 local and S/N 0.2Rs/min" );
customerPhoneCallObject.setCallType( scanner.nextInt() );
}
}