Created
March 5, 2016 12:40
-
-
Save JavaDen/5a64ef7dc994b9cb0bc6 to your computer and use it in GitHub Desktop.
OOP/ Phone
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package phone; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
public class main { | |
public static void main(String[] args) { | |
Network ntw = new Network(); | |
ArrayList<Integer> phones = new ArrayList<>(); | |
ntw.addNumbersListToNetwork( | |
Arrays.asList(new Integer[] { 1, 672346587, 934215678, 674562134, 678903212, 505558489 })); | |
int selfNumber = 671592424; | |
Phone myPhone = new Phone(selfNumber, ntw); | |
int destinationNumber = 542211111; | |
Phone destinationPhone = new Phone(destinationNumber, ntw); | |
myPhone.call(destinationNumber); | |
System.out.println(); | |
System.out.println(); | |
myPhone.call(654434232); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package phone; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class Network { | |
ArrayList<Integer> phoneArray = new ArrayList<Integer>(); | |
public ArrayList<Integer> getPhoneArray() { | |
return phoneArray; | |
} | |
public boolean getReqRegNumber(int reqRegNumber) { | |
return phoneArray.contains(reqRegNumber); | |
} | |
public boolean addNumberToNetwork(int reqRegNumber) { | |
boolean result = this.getReqRegNumber(reqRegNumber); | |
if (!result) { | |
phoneArray.add(reqRegNumber); | |
result = true; | |
} | |
return result; | |
} | |
public boolean addNumbersListToNetwork(List<Integer> phonesList) { | |
return phoneArray.addAll(phonesList); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package phone; | |
public class Phone { | |
private int selfNumber; | |
private Network ntw; | |
private boolean isRegisteredInNetwork = false; | |
public Phone(int selfNumber, Network ntw) { | |
this.ntw = ntw; | |
this.selfNumber = selfNumber; | |
this.checkAndRegisterMeInNetwork(); | |
} | |
private boolean checkAndRegisterMeInNetwork() { | |
if (!this.isRegisteredInNetwork) { | |
this.isRegisteredInNetwork = this.ntw.addNumberToNetwork(this.selfNumber); | |
} | |
return this.isRegisteredInNetwork; | |
} | |
void call(int destinationNumber) { | |
// this.destinationNumber = destinationNumber; | |
if (!this.checkAndRegisterMeInNetwork()) | |
System.out.println("Network not found ..... "); | |
if (ntw.getReqRegNumber(destinationNumber)) { | |
System.out.println("Registration Status Self number : " + ntw.getReqRegNumber(selfNumber) | |
+ "\n Registration Status Dest number : " + ntw.getReqRegNumber(destinationNumber) | |
+ " \nCalling to number : " + destinationNumber); | |
} else { | |
System.out.println("Abonent " + destinationNumber + " out of service..... "); | |
} | |
} | |
public int selfNumber() { | |
return selfNumber; | |
} | |
public boolean connectDestNumID(boolean registStatus) { | |
return registStatus; | |
} | |
public void print() { | |
System.out.println(this.selfNumber); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Lessons1 . Task 4
Создать класс Phone. Наделить его необходимыми свойствами.
Обязательное свойство - номер телефона.
Создать класс Network. Наделить его необходимыми свойствами.
Объект класса Phone должен уметь регистрироваться в сети.
Объект класса Phone должен уметь звонить по номеру на другой Phone. При этом если другой телефон зарегистрирован в сети, то идет дозвон, если нет, то выдается сообщение "Абонент недоступен".