Last active
August 29, 2015 14:07
-
-
Save OlgaKulikova/e86fa9493f849ee3c072 to your computer and use it in GitHub Desktop.
The project Phones2
This file contains hidden or 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 IPhone extends Phone { | |
public IPhone() { | |
} | |
public IPhone(String number) { | |
this.number = number; | |
} | |
@Override | |
public void call(String number) { | |
System.out.println(this.number + " is calling " + number); | |
super.call(number); | |
} | |
@Override | |
public void answer() { | |
System.out.println("IPhone is listening."); | |
} | |
} |
This file contains hidden or 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 IPhone5 extends IPhone { | |
public IPhone5(String number) { | |
this.number = number; | |
} | |
@Override | |
public void answer() { | |
System.out.println("IPhone5 is listening."); | |
} | |
} |
This file contains hidden or 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 MainClass { | |
public static void main(String[] args) { | |
PhonesList phl= new PhonesList(); | |
Phone nokia = new Nokia3310("111-11-11"); | |
phl.add(nokia); | |
Phone iphone = new IPhone("222-22-22"); | |
phl.add(iphone); | |
Phone iphone5 = new IPhone5("333-33-33"); | |
phl.add(iphone5); | |
Phone sumsung = new SumsungS4("444-44-44"); | |
phl.add(sumsung); | |
nokia.call("222-22-22"); | |
iphone.call("333-33-33"); | |
iphone5.call("444-44-44"); | |
sumsung.call("111-11-11"); | |
} | |
} | |
This file contains hidden or 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 Nokia3310 extends Phone { | |
public Nokia3310(String number) { | |
this.number = number; | |
} | |
@Override | |
public void call(String number) { | |
System.out.println(this.number + " is calling " + number); | |
super.call(number); | |
} | |
@Override | |
public void answer() { | |
System.out.println("Nokia3310 is listening."); | |
} | |
} | |
This file contains hidden or 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 abstract class Phone { | |
protected String number; | |
public String getNumber() { | |
return number; | |
} | |
public void call(String number) { | |
PhonesList.find(number).answer(); | |
} | |
public abstract void answer(); | |
} |
This file contains hidden or 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; | |
/* | |
Написать код для связи телефонов между собой. | |
У каждого телефона будет свой номер. | |
При вызове call(x) телефон должен найти собеседника по номеру x | |
из всех доступных в данный момент телефонов | |
(из всех созданных объектов типа Phone) и вызвать его метод answer(). | |
*/ | |
public class PhonesList { | |
private static Phone[] list = new Phone[100]; | |
private static int p = 0; | |
public void add(Phone ph) { | |
list[p++] = ph; | |
} | |
public static Phone find(String number) { | |
for (int i = 0; i < p; i++) { | |
if (list[i].getNumber().equals(number)) | |
return list[i]; | |
} | |
return list[-1]; | |
} | |
} |
This file contains hidden or 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 SumsungS4 extends Phone { | |
public SumsungS4(String number) { | |
this.number = number; | |
} | |
@Override | |
public void call(String number) { | |
System.out.println(this.number + " is calling " + number); | |
super.call(number); | |
} | |
@Override | |
public void answer() { | |
System.out.println("SumsungS4 is listening."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment