Created
August 15, 2020 11:41
-
-
Save kryvoboker/86fe5de9bcaa7c49da9a2f513fe73d55 to your computer and use it in GitHub Desktop.
HomeWork7(Generics and Collections)(4 funny)
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
public class Human { | |
private String name; | |
public Human(String name) { | |
this.name = name; | |
} | |
public Human() { | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
@Override | |
public String toString() { | |
return "Human{" + "name=" + name + '}'; | |
} | |
} |
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
public class Main { | |
public static void main(String[] args) { | |
Turn t = new Turn(); | |
Human hOne = new Human("Sheldon"); | |
Human hTwo = new Human("Leonard"); | |
Human hThree = new Human("Volovitc"); | |
Human hFour = new Human("Kutrapalli"); | |
Human hFive = new Human("Penny"); | |
t.setHumanInList(hOne); | |
t.setHumanInList(hTwo); | |
t.setHumanInList(hThree); | |
t.setHumanInList(hFour); | |
t.setHumanInList(hFive); | |
System.out.println(t); | |
System.out.println(); | |
t.drinkCola(2); | |
System.out.println(t); | |
} | |
} |
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
import java.io.IOException; | |
import java.util.ArrayDeque; | |
public class Turn { | |
private ArrayDeque<Human> humansList = new ArrayDeque<>(); | |
public Turn() { | |
} | |
public Turn(ArrayDeque<Human> humansList) { | |
this.humansList = humansList; | |
} | |
public ArrayDeque<Human> getHumansList() { | |
return humansList; | |
} | |
public void setHumansList(ArrayDeque<Human> humansList) { | |
this.humansList = humansList; | |
} | |
public void setHumanInList(Human h) { | |
humansList.add(h); | |
} | |
public void drinkCola(int count) { | |
try { | |
for (int i = 1; i <= count; i += 1) { | |
if (humansList.iterator().next() == humansList.getLast()) { | |
throw new IOException(); | |
} else if (humansList.iterator().next() != humansList.getLast()) { | |
for (int j = 0; j < 2; j += 1) { | |
humansList.addLast(humansList.iterator().next()); | |
} | |
humansList.remove(humansList.iterator().next()); | |
} | |
} | |
} catch (IOException ex) { | |
System.out.println(humansList.getLast() + " уже выпил стаканчик волшебный колы :))"); | |
} | |
} | |
@Override | |
public String toString() { | |
return "Turn{" + "humansList=" + humansList + '}'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment