Phone and network
package com.gmail.vhrushyn;
public class Phone {
private int number;
private Network myNetwork;
public Phone(int number) {
super();
this.number = number;
myNetwork = null;
}
public Phone() {
super();
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public void register(Network network) {
int i = network.getI();
network.setI(i + 1);
network.regNum(this.number);
myNetwork = network;
}
public void call(int number) {
int num = 0;
for (int n : myNetwork.getRNumbers()) {
if (n == number) {
num = number;
break;
}
}
if (num != 0) {
System.out.println("Connecting to number: "+number);
System.out.println("... beeeeeeeep, beeeeeeeep, beeeeeeeeep");
}
else {
System.out.println("number "+number+" is not registered in the network");
}
}
}
package com.gmail.vhrushyn;
import java.util.Arrays;
public class Network {
int[] rNumbers = new int[100];
int i = -1;
public Network() {
super();
Arrays.fill(this.rNumbers, 0);
}
public int[] getRNumbers() {
return rNumbers;
}
public void regNum(int number) {
rNumbers[i] = number;
}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
}
package com.gmail.vhrushyn;
public class Main {
public static void main(String[] args) {
Network net = new Network();
System.out.println("Network has been created");
System.out.println("");
// Jack's number
Phone phOne = new Phone(12345);
phOne.register(net);
// Jane's number
Phone phTwo = new Phone(23456);
phTwo.register(net);
// Your number
Phone phThree = new Phone(34567);
phThree.register(net);
System.out.println("you try to call Jack (number: 12345)");
phThree.call(12345);
System.out.println("");
System.out.println("... and now you try to call wrong number");
phThree.call(54321);
}
}