-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMobilePhone.java
More file actions
104 lines (86 loc) · 1.99 KB
/
MobilePhone.java
File metadata and controls
104 lines (86 loc) · 1.99 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
interface MobilePhoneInterface{
public Integer number();
public Boolean status();
public void switchOn();
public void switchOff();
public Exchange location();
}
public class MobilePhone{
/*
A class for representing mobile phones.
*/
Integer id;
Boolean phoneStatusOn;
Exchange baseStation;
public MobilePhone(int number){
/*
Constructor to create a mobile phone.
Unique identifier for a mobile phone is an integer.
*/
id = number;
phoneStatusOn = false;
}
public Integer number(){
// Returns the id of the mobile phone
return id;
}
public Boolean status(){
// Returns the status of the phone, i.e. switched on or switched of
return phoneStatusOn;
}
public void switchOn(){
// Changes the status to switched on
if(!phoneStatusOn){
phoneStatusOn = true;
}
}
public void switchOff(){
// Changes the status to switched of
if(phoneStatusOn){
phoneStatusOn = false;
}
}
public Exchange location() throws MobilePhoneSwitchedOffException{
/*
Returns the base station with which
the phone is registered if the phone is
switched on and an exception if the phone is off.
*/
if(status()){
return baseStation;
}
else {
String err = "Error - Mobile phone with identifier "+this.number()+" is currently switched off";
throw new MobilePhoneSwitchedOffException(err);
}
}
public void setBaseStation (Exchange base){
// Set Base Station for current exchange
baseStation = base;
}
public String toString(){
/*
Returns the number of Mobile Phone
In the form of a string
*/
if(phoneStatusOn){
return String.valueOf(id);
}
else{
return "";
}
}
public boolean equals(MobilePhone o) {
/*
Two mobile phones are the same
if their numbers are the same
*/
if (o == this) {
return true;
}
if (!(o instanceof MobilePhone)) {
return false;
}
return o.number() == id;
}
}