From 150bcb47e21a865858fcd9d6a50b3aa7a4d53d9c Mon Sep 17 00:00:00 2001 From: rrudy Date: Thu, 26 Jul 2018 23:20:27 -0500 Subject: [PATCH] =?UTF-8?q?Se=20agregan=20las=20pr=C3=A1cticas=206=20-=201?= =?UTF-8?q?4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 36 ++++ src/main/java/ralvhe/.gitignore | 4 + .../src/soccer/Game.java | 16 ++ .../src/soccer/Goal.java | 16 ++ .../src/soccer/League.java | 50 ++++++ .../src/soccer/Player.java | 14 ++ .../src/soccer/Team.java | 15 ++ .../src/soccer/Game.java | 19 ++ .../src/soccer/Goal.java | 19 ++ .../src/soccer/League.java | 70 ++++++++ .../src/soccer/Player.java | 17 ++ .../src/soccer/Team.java | 18 ++ .../src/soccer/Game.java | 19 ++ .../src/soccer/Goal.java | 19 ++ .../src/soccer/League.java | 77 ++++++++ .../src/soccer/Player.java | 17 ++ .../src/soccer/Team.java | 18 ++ .../08-Methods_Practice1/src/soccer/Game.java | 19 ++ .../08-Methods_Practice1/src/soccer/Goal.java | 19 ++ .../src/soccer/League.java | 79 ++++++++ .../src/soccer/Player.java | 17 ++ .../08-Methods_Practice1/src/soccer/Team.java | 18 ++ .../08-Methods_Practice2/src/soccer/Game.java | 40 +++++ .../08-Methods_Practice2/src/soccer/Goal.java | 19 ++ .../src/soccer/League.java | 77 ++++++++ .../src/soccer/Player.java | 17 ++ .../08-Methods_Practice2/src/soccer/Team.java | 18 ++ .../src/utility/GameUtils.java | 61 +++++++ .../08-Methods_Practice3/src/soccer/Game.java | 46 +++++ .../08-Methods_Practice3/src/soccer/Goal.java | 19 ++ .../src/soccer/League.java | 70 ++++++++ .../src/soccer/Player.java | 17 ++ .../08-Methods_Practice3/src/soccer/Team.java | 18 ++ .../src/utility/GameUtils.java | 66 +++++++ .../src/soccer/Game.java | 87 +++++++++ .../src/soccer/Goal.java | 61 +++++++ .../src/soccer/League.java | 70 ++++++++ .../src/soccer/Player.java | 26 +++ .../src/soccer/Team.java | 46 +++++ .../src/utility/GameUtils.java | 41 +++++ .../src/soccer/Game.java | 91 ++++++++++ .../src/soccer/Goal.java | 61 +++++++ .../src/soccer/League.java | 68 +++++++ .../src/soccer/Player.java | 39 ++++ .../src/soccer/Team.java | 57 ++++++ .../src/utility/GameUtils.java | 41 +++++ .../src/soccer/Game.java | 124 +++++++++++++ .../src/soccer/Goal.java | 61 +++++++ .../src/soccer/League.java | 75 ++++++++ .../src/soccer/Player.java | 37 ++++ .../src/soccer/Team.java | 73 ++++++++ .../src/utility/GameUtils.java | 41 +++++ .../src/soccer/Game.java | 123 +++++++++++++ .../src/soccer/Goal.java | 61 +++++++ .../src/soccer/League.java | 84 +++++++++ .../src/soccer/Player.java | 37 ++++ .../src/soccer/Team.java | 90 ++++++++++ .../src/utility/GameUtils.java | 41 +++++ .../src/soccer/Game.java | 121 +++++++++++++ .../src/soccer/Goal.java | 61 +++++++ .../src/soccer/League.java | 81 +++++++++ .../src/soccer/Player.java | 37 ++++ .../src/soccer/Team.java | 95 ++++++++++ .../src/utility/GameUtils.java | 41 +++++ .../src/utility/PlayerDatabase.java | 76 ++++++++ .../src/soccer/Game.java | 136 ++++++++++++++ .../src/soccer/Goal.java | 61 +++++++ .../src/soccer/League.java | 103 +++++++++++ .../src/soccer/Player.java | 37 ++++ .../src/soccer/Team.java | 95 ++++++++++ .../src/utility/GameUtils.java | 41 +++++ .../src/utility/PlayerDatabase.java | 80 +++++++++ .../src/soccer/Game.java | 163 +++++++++++++++++ .../src/soccer/Goal.java | 61 +++++++ .../src/soccer/League.java | 109 +++++++++++ .../src/soccer/Player.java | 37 ++++ .../src/soccer/Team.java | 95 ++++++++++ .../src/utility/PlayerDatabase.java | 80 +++++++++ .../src/soccer/Game.java | 165 +++++++++++++++++ .../src/soccer/GameEvent.java | 59 ++++++ .../src/soccer/Goal.java | 21 +++ .../src/soccer/League.java | 106 +++++++++++ .../src/soccer/Player.java | 37 ++++ .../src/soccer/Possession.java | 14 ++ .../src/soccer/Team.java | 95 ++++++++++ .../src/utility/PlayerDatabase.java | 80 +++++++++ .../src/soccer/Game.java | 167 +++++++++++++++++ .../src/soccer/GameEvent.java | 62 +++++++ .../src/soccer/Goal.java | 19 ++ .../src/soccer/League.java | 106 +++++++++++ .../src/soccer/Player.java | 37 ++++ .../src/soccer/Possession.java | 19 ++ .../src/soccer/Team.java | 95 ++++++++++ .../src/utility/PlayerDatabase.java | 80 +++++++++ .../src/soccer/Game.java | 167 +++++++++++++++++ .../src/soccer/GameEvent.java | 62 +++++++ .../src/soccer/Goal.java | 18 ++ .../src/soccer/League.java | 106 +++++++++++ .../src/soccer/Player.java | 37 ++++ .../src/soccer/Possession.java | 19 ++ .../src/soccer/Team.java | 110 ++++++++++++ .../src/utility/PlayerDatabase.java | 80 +++++++++ .../src/soccer/Game.java | 170 ++++++++++++++++++ .../src/soccer/GameEvent.java | 62 +++++++ .../src/soccer/Goal.java | 18 ++ .../src/soccer/League.java | 113 ++++++++++++ .../src/soccer/Player.java | 50 ++++++ .../src/soccer/Possession.java | 19 ++ .../src/soccer/Team.java | 107 +++++++++++ .../src/utility/PlayerDatabase.java | 80 +++++++++ .../src/soccer/Game.java | 168 +++++++++++++++++ .../src/soccer/GameEvent.java | 62 +++++++ .../src/soccer/Goal.java | 18 ++ .../src/soccer/League.java | 124 +++++++++++++ .../src/soccer/Player.java | 49 +++++ .../src/soccer/Possession.java | 19 ++ .../src/soccer/Team.java | 107 +++++++++++ .../src/utility/PlayerDatabase.java | 84 +++++++++ .../src/utility/PlayerDatabaseException.java | 20 +++ 119 files changed, 7240 insertions(+) create mode 100644 .gitignore create mode 100644 src/main/java/ralvhe/.gitignore create mode 100644 src/main/java/ralvhe/practices/06-ObjectsClasses_Practice1/src/soccer/Game.java create mode 100644 src/main/java/ralvhe/practices/06-ObjectsClasses_Practice1/src/soccer/Goal.java create mode 100644 src/main/java/ralvhe/practices/06-ObjectsClasses_Practice1/src/soccer/League.java create mode 100644 src/main/java/ralvhe/practices/06-ObjectsClasses_Practice1/src/soccer/Player.java create mode 100644 src/main/java/ralvhe/practices/06-ObjectsClasses_Practice1/src/soccer/Team.java create mode 100644 src/main/java/ralvhe/practices/06-ObjectsClasses_Practice2/06-ObjectsClasses_Practice2/src/soccer/Game.java create mode 100644 src/main/java/ralvhe/practices/06-ObjectsClasses_Practice2/06-ObjectsClasses_Practice2/src/soccer/Goal.java create mode 100644 src/main/java/ralvhe/practices/06-ObjectsClasses_Practice2/06-ObjectsClasses_Practice2/src/soccer/League.java create mode 100644 src/main/java/ralvhe/practices/06-ObjectsClasses_Practice2/06-ObjectsClasses_Practice2/src/soccer/Player.java create mode 100644 src/main/java/ralvhe/practices/06-ObjectsClasses_Practice2/06-ObjectsClasses_Practice2/src/soccer/Team.java create mode 100644 src/main/java/ralvhe/practices/07-ManipulateFormat_Practice1/src/soccer/Game.java create mode 100644 src/main/java/ralvhe/practices/07-ManipulateFormat_Practice1/src/soccer/Goal.java create mode 100644 src/main/java/ralvhe/practices/07-ManipulateFormat_Practice1/src/soccer/League.java create mode 100644 src/main/java/ralvhe/practices/07-ManipulateFormat_Practice1/src/soccer/Player.java create mode 100644 src/main/java/ralvhe/practices/07-ManipulateFormat_Practice1/src/soccer/Team.java create mode 100644 src/main/java/ralvhe/practices/08-Methods_Practice1/src/soccer/Game.java create mode 100644 src/main/java/ralvhe/practices/08-Methods_Practice1/src/soccer/Goal.java create mode 100644 src/main/java/ralvhe/practices/08-Methods_Practice1/src/soccer/League.java create mode 100644 src/main/java/ralvhe/practices/08-Methods_Practice1/src/soccer/Player.java create mode 100644 src/main/java/ralvhe/practices/08-Methods_Practice1/src/soccer/Team.java create mode 100644 src/main/java/ralvhe/practices/08-Methods_Practice2/src/soccer/Game.java create mode 100644 src/main/java/ralvhe/practices/08-Methods_Practice2/src/soccer/Goal.java create mode 100644 src/main/java/ralvhe/practices/08-Methods_Practice2/src/soccer/League.java create mode 100644 src/main/java/ralvhe/practices/08-Methods_Practice2/src/soccer/Player.java create mode 100644 src/main/java/ralvhe/practices/08-Methods_Practice2/src/soccer/Team.java create mode 100644 src/main/java/ralvhe/practices/08-Methods_Practice2/src/utility/GameUtils.java create mode 100644 src/main/java/ralvhe/practices/08-Methods_Practice3/src/soccer/Game.java create mode 100644 src/main/java/ralvhe/practices/08-Methods_Practice3/src/soccer/Goal.java create mode 100644 src/main/java/ralvhe/practices/08-Methods_Practice3/src/soccer/League.java create mode 100644 src/main/java/ralvhe/practices/08-Methods_Practice3/src/soccer/Player.java create mode 100644 src/main/java/ralvhe/practices/08-Methods_Practice3/src/soccer/Team.java create mode 100644 src/main/java/ralvhe/practices/08-Methods_Practice3/src/utility/GameUtils.java create mode 100644 src/main/java/ralvhe/practices/09-EncapConstructors_Practice1/src/soccer/Game.java create mode 100644 src/main/java/ralvhe/practices/09-EncapConstructors_Practice1/src/soccer/Goal.java create mode 100644 src/main/java/ralvhe/practices/09-EncapConstructors_Practice1/src/soccer/League.java create mode 100644 src/main/java/ralvhe/practices/09-EncapConstructors_Practice1/src/soccer/Player.java create mode 100644 src/main/java/ralvhe/practices/09-EncapConstructors_Practice1/src/soccer/Team.java create mode 100644 src/main/java/ralvhe/practices/09-EncapConstructors_Practice1/src/utility/GameUtils.java create mode 100644 src/main/java/ralvhe/practices/09-EncapConstructors_Practice2/src/soccer/Game.java create mode 100644 src/main/java/ralvhe/practices/09-EncapConstructors_Practice2/src/soccer/Goal.java create mode 100644 src/main/java/ralvhe/practices/09-EncapConstructors_Practice2/src/soccer/League.java create mode 100644 src/main/java/ralvhe/practices/09-EncapConstructors_Practice2/src/soccer/Player.java create mode 100644 src/main/java/ralvhe/practices/09-EncapConstructors_Practice2/src/soccer/Team.java create mode 100644 src/main/java/ralvhe/practices/09-EncapConstructors_Practice2/src/utility/GameUtils.java create mode 100644 src/main/java/ralvhe/practices/10-Conditions_Practice1/src/soccer/Game.java create mode 100644 src/main/java/ralvhe/practices/10-Conditions_Practice1/src/soccer/Goal.java create mode 100644 src/main/java/ralvhe/practices/10-Conditions_Practice1/src/soccer/League.java create mode 100644 src/main/java/ralvhe/practices/10-Conditions_Practice1/src/soccer/Player.java create mode 100644 src/main/java/ralvhe/practices/10-Conditions_Practice1/src/soccer/Team.java create mode 100644 src/main/java/ralvhe/practices/10-Conditions_Practice1/src/utility/GameUtils.java create mode 100644 src/main/java/ralvhe/practices/10-Conditions_Practice2/src/soccer/Game.java create mode 100644 src/main/java/ralvhe/practices/10-Conditions_Practice2/src/soccer/Goal.java create mode 100644 src/main/java/ralvhe/practices/10-Conditions_Practice2/src/soccer/League.java create mode 100644 src/main/java/ralvhe/practices/10-Conditions_Practice2/src/soccer/Player.java create mode 100644 src/main/java/ralvhe/practices/10-Conditions_Practice2/src/soccer/Team.java create mode 100644 src/main/java/ralvhe/practices/10-Conditions_Practice2/src/utility/GameUtils.java create mode 100644 src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice1/src/soccer/Game.java create mode 100644 src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice1/src/soccer/Goal.java create mode 100644 src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice1/src/soccer/League.java create mode 100644 src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice1/src/soccer/Player.java create mode 100644 src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice1/src/soccer/Team.java create mode 100644 src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice1/src/utility/GameUtils.java create mode 100644 src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice1/src/utility/PlayerDatabase.java create mode 100644 src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice2/src/soccer/Game.java create mode 100644 src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice2/src/soccer/Goal.java create mode 100644 src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice2/src/soccer/League.java create mode 100644 src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice2/src/soccer/Player.java create mode 100644 src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice2/src/soccer/Team.java create mode 100644 src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice2/src/utility/GameUtils.java create mode 100644 src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice2/src/utility/PlayerDatabase.java create mode 100644 src/main/java/ralvhe/practices/12-UsingInheritance_Practice1/src/soccer/Game.java create mode 100644 src/main/java/ralvhe/practices/12-UsingInheritance_Practice1/src/soccer/Goal.java create mode 100644 src/main/java/ralvhe/practices/12-UsingInheritance_Practice1/src/soccer/League.java create mode 100644 src/main/java/ralvhe/practices/12-UsingInheritance_Practice1/src/soccer/Player.java create mode 100644 src/main/java/ralvhe/practices/12-UsingInheritance_Practice1/src/soccer/Team.java create mode 100644 src/main/java/ralvhe/practices/12-UsingInheritance_Practice1/src/utility/PlayerDatabase.java create mode 100644 src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/src/soccer/Game.java create mode 100644 src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/src/soccer/GameEvent.java create mode 100644 src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/src/soccer/Goal.java create mode 100644 src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/src/soccer/League.java create mode 100644 src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/src/soccer/Player.java create mode 100644 src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/src/soccer/Possession.java create mode 100644 src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/src/soccer/Team.java create mode 100644 src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/src/utility/PlayerDatabase.java create mode 100644 src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/src/soccer/Game.java create mode 100644 src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/src/soccer/GameEvent.java create mode 100644 src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/src/soccer/Goal.java create mode 100644 src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/src/soccer/League.java create mode 100644 src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/src/soccer/Player.java create mode 100644 src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/src/soccer/Possession.java create mode 100644 src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/src/soccer/Team.java create mode 100644 src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/src/utility/PlayerDatabase.java create mode 100644 src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/src/soccer/Game.java create mode 100644 src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/src/soccer/GameEvent.java create mode 100644 src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/src/soccer/Goal.java create mode 100644 src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/src/soccer/League.java create mode 100644 src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/src/soccer/Player.java create mode 100644 src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/src/soccer/Possession.java create mode 100644 src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/src/soccer/Team.java create mode 100644 src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/src/utility/PlayerDatabase.java create mode 100644 src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/src/soccer/Game.java create mode 100644 src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/src/soccer/GameEvent.java create mode 100644 src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/src/soccer/Goal.java create mode 100644 src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/src/soccer/League.java create mode 100644 src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/src/soccer/Player.java create mode 100644 src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/src/soccer/Possession.java create mode 100644 src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/src/soccer/Team.java create mode 100644 src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/src/utility/PlayerDatabase.java create mode 100644 src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/soccer/Game.java create mode 100644 src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/soccer/GameEvent.java create mode 100644 src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/soccer/Goal.java create mode 100644 src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/soccer/League.java create mode 100644 src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/soccer/Player.java create mode 100644 src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/soccer/Possession.java create mode 100644 src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/soccer/Team.java create mode 100644 src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/utility/PlayerDatabase.java create mode 100644 src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/utility/PlayerDatabaseException.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e77e2fc --- /dev/null +++ b/.gitignore @@ -0,0 +1,36 @@ +/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice1/build/ +/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice1/nbproject/private/ +/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice2/06-ObjectsClasses_Practice2/build/ +/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice2/06-ObjectsClasses_Practice2/nbproject/private/ +/src/main/java/ralvhe/practices/07-ManipulateFormat_Practice1/build/ +/src/main/java/ralvhe/practices/07-ManipulateFormat_Practice1/nbproject/private/ +/src/main/java/ralvhe/practices/08-Methods_Practice1/build/ +/src/main/java/ralvhe/practices/08-Methods_Practice1/nbproject/private/ +/src/main/java/ralvhe/practices/08-Methods_Practice2/build/ +/src/main/java/ralvhe/practices/08-Methods_Practice2/nbproject/private/ +/src/main/java/ralvhe/practices/08-Methods_Practice3/build/ +/src/main/java/ralvhe/practices/08-Methods_Practice3/nbproject/private/ +/src/main/java/ralvhe/practices/09-EncapConstructors_Practice1/build/ +/src/main/java/ralvhe/practices/09-EncapConstructors_Practice1/nbproject/private/ +/src/main/java/ralvhe/practices/09-EncapConstructors_Practice2/build/ +/src/main/java/ralvhe/practices/09-EncapConstructors_Practice2/nbproject/private/ +/src/main/java/ralvhe/practices/10-Conditions_Practice1/build/ +/src/main/java/ralvhe/practices/10-Conditions_Practice1/nbproject/private/ +/src/main/java/ralvhe/practices/10-Conditions_Practice2/build/ +/src/main/java/ralvhe/practices/10-Conditions_Practice2/nbproject/private/ +/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice1/build/ +/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice1/nbproject/private/ +/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice2/build/ +/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice2/nbproject/private/ +/src/main/java/ralvhe/practices/12-UsingInheritance_Practice1/build/ +/src/main/java/ralvhe/practices/12-UsingInheritance_Practice1/nbproject/private/ +/src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/build/ +/src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/nbproject/private/ +/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/build/ +/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/nbproject/private/ +/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/build/ +/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/nbproject/private/ +/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/build/ +/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/nbproject/private/ +/src/main/java/ralvhe/practices/14-Exceptions_Practice1/build/ +/src/main/java/ralvhe/practices/14-Exceptions_Practice1/nbproject/private/ \ No newline at end of file diff --git a/src/main/java/ralvhe/.gitignore b/src/main/java/ralvhe/.gitignore new file mode 100644 index 0000000..d6213e3 --- /dev/null +++ b/src/main/java/ralvhe/.gitignore @@ -0,0 +1,4 @@ +*.class +*.xml +*.properties +*.mf diff --git a/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice1/src/soccer/Game.java b/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice1/src/soccer/Game.java new file mode 100644 index 0000000..dc14d41 --- /dev/null +++ b/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice1/src/soccer/Game.java @@ -0,0 +1,16 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package soccer; + +/** + * + * @author ralhe + */ +public class Game { + public Team homeTeam; + public Team awayTeam; + public Goal[] goals; +} diff --git a/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice1/src/soccer/Goal.java b/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice1/src/soccer/Goal.java new file mode 100644 index 0000000..abf35de --- /dev/null +++ b/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice1/src/soccer/Goal.java @@ -0,0 +1,16 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package soccer; + +/** + * + * @author ralhe + */ +public class Goal { + public Team theTeam; + public Player thePlayer; + public double theTime; +} diff --git a/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice1/src/soccer/League.java b/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice1/src/soccer/League.java new file mode 100644 index 0000000..d87e669 --- /dev/null +++ b/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice1/src/soccer/League.java @@ -0,0 +1,50 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package soccer; + +/** + * + * @author ralhe + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + // TODO code application logic here + Player player1 = new Player(); + player1.playerName = "George Eliot"; + Player player2 = new Player(); + player2.playerName = "Graham Greene"; + Player player3 = new Player(); + player3.playerName = "Geoffrey Chaucer"; + Player[] thePlayers = {player1,player2,player3}; + Team team1 = new Team(); + team1.teamName = "The Greens"; + team1.playerArray = thePlayers; + player1.playerName = "Robert Service"; + + Team team2 = new Team(); + team2.teamName = "The Reds"; + team2.playerArray = new Player[3]; + team2.playerArray[0] = new Player(); + team2.playerArray[0].playerName = "Robert Service"; + team2.playerArray[1] = new Player(); + team2.playerArray[1].playerName = "Robbie Burns"; + team2.playerArray[2] = new Player(); + team2.playerArray[2].playerName = "Rafael Sabatini"; + + for(Player thePlayer:team1.playerArray){ + System.out.println(thePlayer.playerName); + } + + for(Player thePlayer : team2.playerArray){ + System.out.println(thePlayer.playerName); + } + } + +} diff --git a/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice1/src/soccer/Player.java b/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice1/src/soccer/Player.java new file mode 100644 index 0000000..93c1ef8 --- /dev/null +++ b/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice1/src/soccer/Player.java @@ -0,0 +1,14 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package soccer; + +/** + * + * @author ralhe + */ +public class Player { + public String playerName; +} diff --git a/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice1/src/soccer/Team.java b/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice1/src/soccer/Team.java new file mode 100644 index 0000000..ffad9cc --- /dev/null +++ b/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice1/src/soccer/Team.java @@ -0,0 +1,15 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package soccer; + +/** + * + * @author ralhe + */ +public class Team { + public String teamName; + public Player[] playerArray; +} diff --git a/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice2/06-ObjectsClasses_Practice2/src/soccer/Game.java b/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice2/06-ObjectsClasses_Practice2/src/soccer/Game.java new file mode 100644 index 0000000..f9732f7 --- /dev/null +++ b/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice2/06-ObjectsClasses_Practice2/src/soccer/Game.java @@ -0,0 +1,19 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Game { + + public Team homeTeam; + public Team awayTeam; + public Goal[] goals; + +} diff --git a/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice2/06-ObjectsClasses_Practice2/src/soccer/Goal.java b/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice2/06-ObjectsClasses_Practice2/src/soccer/Goal.java new file mode 100644 index 0000000..144a3d0 --- /dev/null +++ b/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice2/06-ObjectsClasses_Practice2/src/soccer/Goal.java @@ -0,0 +1,19 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Goal { + + public Team theTeam; + public Player thePlayer; + public double theTime; + +} diff --git a/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice2/06-ObjectsClasses_Practice2/src/soccer/League.java b/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice2/06-ObjectsClasses_Practice2/src/soccer/League.java new file mode 100644 index 0000000..f7c38ec --- /dev/null +++ b/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice2/06-ObjectsClasses_Practice2/src/soccer/League.java @@ -0,0 +1,70 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + // Create team1 + Player player1 = new Player(); + player1.playerName = "George Eliot"; + Player player2 = new Player(); + player2.playerName = "Graham Greene"; + Player player3 = new Player(); + player3.playerName = "Geoffrey Chaucer"; + Player[] thePlayers = {player1, player2, player3 }; + + Team team1 = new Team(); + team1.teamName = "The Greens"; + team1.playerArray = thePlayers; + + + // Create team2 + Team team2 = new Team(); + team2.teamName = "The Reds"; + team2.playerArray = new Player[3]; + team2.playerArray[0] = new Player(); + team2.playerArray[0].playerName = "Robert Service"; + team2.playerArray[1] = new Player(); + team2.playerArray[1].playerName = "Robbie Burns"; + team2.playerArray[2] = new Player(); + team2.playerArray[2].playerName = "Rafael Sabatini"; + + Game currGame = new Game(); + currGame.awayTeam = team1; + currGame.homeTeam = team2; + + Goal goal1 = new Goal(); + goal1.thePlayer = currGame.homeTeam.playerArray[2]; + goal1.theTeam = currGame.homeTeam; + goal1.theTime = 55; + + Goal[] theGoals = {goal1}; + currGame.goals = theGoals; + + System.out.println("Goal scored after " + currGame.goals[0].theTime + " mins by " + currGame.goals[0].thePlayer.playerName + " of " +currGame.goals[0].theTeam.teamName); + + /* Practice 6-2. Create a Game here */ + + /* Practice 6-2. Create a Goal object here */ + + /* Practice 6-2. Put Goal object in a Goal array and assign Goal array to goals attribute of Game object */ + + /* Practice 6-2. Print out the score of the Game */ + + + + } +} diff --git a/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice2/06-ObjectsClasses_Practice2/src/soccer/Player.java b/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice2/06-ObjectsClasses_Practice2/src/soccer/Player.java new file mode 100644 index 0000000..07670b0 --- /dev/null +++ b/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice2/06-ObjectsClasses_Practice2/src/soccer/Player.java @@ -0,0 +1,17 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Player { + + public String playerName; + +} diff --git a/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice2/06-ObjectsClasses_Practice2/src/soccer/Team.java b/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice2/06-ObjectsClasses_Practice2/src/soccer/Team.java new file mode 100644 index 0000000..247f229 --- /dev/null +++ b/src/main/java/ralvhe/practices/06-ObjectsClasses_Practice2/06-ObjectsClasses_Practice2/src/soccer/Team.java @@ -0,0 +1,18 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Team { + + public String teamName; + public Player[] playerArray; + +} diff --git a/src/main/java/ralvhe/practices/07-ManipulateFormat_Practice1/src/soccer/Game.java b/src/main/java/ralvhe/practices/07-ManipulateFormat_Practice1/src/soccer/Game.java new file mode 100644 index 0000000..f9732f7 --- /dev/null +++ b/src/main/java/ralvhe/practices/07-ManipulateFormat_Practice1/src/soccer/Game.java @@ -0,0 +1,19 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Game { + + public Team homeTeam; + public Team awayTeam; + public Goal[] goals; + +} diff --git a/src/main/java/ralvhe/practices/07-ManipulateFormat_Practice1/src/soccer/Goal.java b/src/main/java/ralvhe/practices/07-ManipulateFormat_Practice1/src/soccer/Goal.java new file mode 100644 index 0000000..144a3d0 --- /dev/null +++ b/src/main/java/ralvhe/practices/07-ManipulateFormat_Practice1/src/soccer/Goal.java @@ -0,0 +1,19 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Goal { + + public Team theTeam; + public Player thePlayer; + public double theTime; + +} diff --git a/src/main/java/ralvhe/practices/07-ManipulateFormat_Practice1/src/soccer/League.java b/src/main/java/ralvhe/practices/07-ManipulateFormat_Practice1/src/soccer/League.java new file mode 100644 index 0000000..44ea3bb --- /dev/null +++ b/src/main/java/ralvhe/practices/07-ManipulateFormat_Practice1/src/soccer/League.java @@ -0,0 +1,77 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + // Create team1 + Player player1 = new Player(); + player1.playerName = "George Eliot"; + Player player2 = new Player(); + player2.playerName = "Graham Greene"; + Player player3 = new Player(); + player3.playerName = "Geoffrey Chaucer"; + Player[] thePlayers = {player1, player2, player3 }; + + Team team1 = new Team(); + team1.teamName = "The Greens"; + team1.playerArray = thePlayers; + + // Create team2 + Team team2 = new Team(); + team2.teamName = "The Reds"; + team2.playerArray = new Player[3]; + team2.playerArray[0] = new Player(); + team2.playerArray[0].playerName = "Robert Service"; + team2.playerArray[1] = new Player(); + team2.playerArray[1].playerName = "Robbie Burns"; + team2.playerArray[2] = new Player(); + team2.playerArray[2].playerName = "Rafael Sabatini"; + + Game currGame = new Game(); + currGame.homeTeam = team1; + currGame.awayTeam = team2; + Goal goal1 = new Goal(); + goal1.thePlayer = currGame.homeTeam.playerArray[2]; + goal1.theTeam = currGame.homeTeam; + goal1.theTime = 55; + Goal[] theGoals = {goal1}; + currGame.goals = theGoals; + + System.out.println("Goal scored after " + + currGame.goals[0].theTime + " mins by " + + currGame.goals[0].thePlayer.playerName + " of " + + currGame.goals[0].theTeam.teamName); + + /* Practice 7-1. Add code for finding a player within team2 here */ + + for(Player thePlayer: team2.playerArray){ + if(thePlayer.playerName.matches(".*Sab.*")){ + System.out.println("Found "+thePlayer.playerName); + System.out.println("Last name is "+thePlayer.playerName.split(" ")[1]); + } + } + StringBuilder familyNameFirst = new StringBuilder(); + for(Player thePlayer: team1.playerArray){ + String name[] = thePlayer.playerName.split(" "); + familyNameFirst.append(name[1]); + familyNameFirst.append(", "); + familyNameFirst.append(name[0]); + System.out.println(familyNameFirst); + familyNameFirst.delete(0, familyNameFirst.length()); + } + } +} diff --git a/src/main/java/ralvhe/practices/07-ManipulateFormat_Practice1/src/soccer/Player.java b/src/main/java/ralvhe/practices/07-ManipulateFormat_Practice1/src/soccer/Player.java new file mode 100644 index 0000000..07670b0 --- /dev/null +++ b/src/main/java/ralvhe/practices/07-ManipulateFormat_Practice1/src/soccer/Player.java @@ -0,0 +1,17 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Player { + + public String playerName; + +} diff --git a/src/main/java/ralvhe/practices/07-ManipulateFormat_Practice1/src/soccer/Team.java b/src/main/java/ralvhe/practices/07-ManipulateFormat_Practice1/src/soccer/Team.java new file mode 100644 index 0000000..247f229 --- /dev/null +++ b/src/main/java/ralvhe/practices/07-ManipulateFormat_Practice1/src/soccer/Team.java @@ -0,0 +1,18 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Team { + + public String teamName; + public Player[] playerArray; + +} diff --git a/src/main/java/ralvhe/practices/08-Methods_Practice1/src/soccer/Game.java b/src/main/java/ralvhe/practices/08-Methods_Practice1/src/soccer/Game.java new file mode 100644 index 0000000..f9732f7 --- /dev/null +++ b/src/main/java/ralvhe/practices/08-Methods_Practice1/src/soccer/Game.java @@ -0,0 +1,19 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Game { + + public Team homeTeam; + public Team awayTeam; + public Goal[] goals; + +} diff --git a/src/main/java/ralvhe/practices/08-Methods_Practice1/src/soccer/Goal.java b/src/main/java/ralvhe/practices/08-Methods_Practice1/src/soccer/Goal.java new file mode 100644 index 0000000..144a3d0 --- /dev/null +++ b/src/main/java/ralvhe/practices/08-Methods_Practice1/src/soccer/Goal.java @@ -0,0 +1,19 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Goal { + + public Team theTeam; + public Player thePlayer; + public double theTime; + +} diff --git a/src/main/java/ralvhe/practices/08-Methods_Practice1/src/soccer/League.java b/src/main/java/ralvhe/practices/08-Methods_Practice1/src/soccer/League.java new file mode 100644 index 0000000..cd3e4f3 --- /dev/null +++ b/src/main/java/ralvhe/practices/08-Methods_Practice1/src/soccer/League.java @@ -0,0 +1,79 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package soccer; + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + /* Practice 8-1. Start of code that builds teams */ + /* Practice 8-1. End of code that builds teams */ + /* Practice 8-1. Delete next three lines */ + Team[] theTeams = createTeams(); + Game[] theGames = createGames(theTeams); + + Game currGame = theGames[0]; + + Goal goal1 = new Goal(); + goal1.thePlayer = currGame.homeTeam.playerArray[2]; + goal1.theTeam = currGame.homeTeam; + goal1.theTime = 55; + Goal[] theGoals = {goal1}; + currGame.goals = theGoals; + + System.out.println("Goal scored after " + + currGame.goals[0].theTime + " mins by " + + currGame.goals[0].thePlayer.playerName + " of " + + currGame.goals[0].theTeam.teamName); + + } + + /* Practice 8-1. Add new static method createTeams() here */ + public static Team[] createTeams() { + // Create team1 + Player player1 = new Player(); + player1.playerName = "George Eliot"; + Player player2 = new Player(); + player2.playerName = "Graham Greene"; + Player player3 = new Player(); + player3.playerName = "Geoffrey Chaucer"; + Player[] thePlayers = {player1, player2, player3}; + + Team team1 = new Team(); + team1.teamName = "The Greens"; + team1.playerArray = thePlayers; + + // Create team2 + Team team2 = new Team(); + team2.teamName = "The Reds"; + team2.playerArray = new Player[3]; + team2.playerArray[0] = new Player(); + team2.playerArray[0].playerName = "Robert Service"; + team2.playerArray[1] = new Player(); + team2.playerArray[1].playerName = "Robbie Burns"; + team2.playerArray[2] = new Player(); + team2.playerArray[2].playerName = "Rafael Sabatini"; + Team[] theTeams = {team1, team2}; + return theTeams; + } + + /* Practice 8-1. Add new static method createGames() here */ + public static Game[] createGames(Team[] theTeams) { + Game theGame = new Game(); + theGame.homeTeam = theTeams[0]; + theGame.awayTeam = theTeams[1]; + Game[] theGames = {theGame}; + return theGames; + } + +} diff --git a/src/main/java/ralvhe/practices/08-Methods_Practice1/src/soccer/Player.java b/src/main/java/ralvhe/practices/08-Methods_Practice1/src/soccer/Player.java new file mode 100644 index 0000000..07670b0 --- /dev/null +++ b/src/main/java/ralvhe/practices/08-Methods_Practice1/src/soccer/Player.java @@ -0,0 +1,17 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Player { + + public String playerName; + +} diff --git a/src/main/java/ralvhe/practices/08-Methods_Practice1/src/soccer/Team.java b/src/main/java/ralvhe/practices/08-Methods_Practice1/src/soccer/Team.java new file mode 100644 index 0000000..247f229 --- /dev/null +++ b/src/main/java/ralvhe/practices/08-Methods_Practice1/src/soccer/Team.java @@ -0,0 +1,18 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Team { + + public String teamName; + public Player[] playerArray; + +} diff --git a/src/main/java/ralvhe/practices/08-Methods_Practice2/src/soccer/Game.java b/src/main/java/ralvhe/practices/08-Methods_Practice2/src/soccer/Game.java new file mode 100644 index 0000000..6cc5c0f --- /dev/null +++ b/src/main/java/ralvhe/practices/08-Methods_Practice2/src/soccer/Game.java @@ -0,0 +1,40 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +import utility.GameUtils; + +/** + * + * @author Administrator + */ +public class Game { + + public Team homeTeam; + public Team awayTeam; + public Goal[] goals; + + /* Practice 8-2. Add playGame() method here */ + public void playGame(){ + int numberOfGoals = (int) (Math.random() * 7); + //System.out.println(numberOfGoals); + Goal[] theGoals = new Goal[numberOfGoals]; + this.goals = theGoals; + GameUtils.addGameGoals(this); + } + + /* Practice 8-2. Add getDescription() method here */ + public String getDescription(){ + StringBuilder returnString = new StringBuilder(); + for(Goal currGoal : this.goals){ + + returnString.append("Goal scored after " + currGoal.theTime + " mins by " + currGoal.thePlayer.playerName + " of " + currGoal.theTeam.teamName + '\n'); + } + return returnString.toString(); + } + +} diff --git a/src/main/java/ralvhe/practices/08-Methods_Practice2/src/soccer/Goal.java b/src/main/java/ralvhe/practices/08-Methods_Practice2/src/soccer/Goal.java new file mode 100644 index 0000000..144a3d0 --- /dev/null +++ b/src/main/java/ralvhe/practices/08-Methods_Practice2/src/soccer/Goal.java @@ -0,0 +1,19 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Goal { + + public Team theTeam; + public Player thePlayer; + public double theTime; + +} diff --git a/src/main/java/ralvhe/practices/08-Methods_Practice2/src/soccer/League.java b/src/main/java/ralvhe/practices/08-Methods_Practice2/src/soccer/League.java new file mode 100644 index 0000000..73a92df --- /dev/null +++ b/src/main/java/ralvhe/practices/08-Methods_Practice2/src/soccer/League.java @@ -0,0 +1,77 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package soccer; + +import utility.GameUtils; + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + Team[] theTeams = createTeams(); + Game[] theGames = createGames(theTeams); + + Game currGame = theGames[0]; + + /* Practice 8-2. Start of code that creates a Goal and assigns to Team and a Game */ + + /* Practice 8-2. End of code that creates a Goal and assigns to a Team and a Game */ + /* Practice 8-2. Add code to initialize the array currGame.goals to a random size */ + currGame.playGame(); + System.out.println(currGame.getDescription()); + // Print out goal details + /*System.out.println("Goal scored after " + + currGame.goals[0].theTime + " mins by " + + currGame.goals[0].thePlayer.playerName + " of " + + currGame.goals[0].theTeam.teamName); + */ + } + + public static Team[] createTeams() { + + Player player1 = new Player(); + player1.playerName = "George Eliot"; + Player player2 = new Player(); + player2.playerName = "Graham Greene"; + Player player3 = new Player(); + player3.playerName = "Geoffrey Chaucer"; + Player[] thePlayers = {player1, player2, player3}; + + Team team1 = new Team(); + team1.teamName = "The Greens"; + team1.playerArray = thePlayers; + + // Create team2 + Team team2 = new Team(); + team2.teamName = "The Reds"; + team2.playerArray = new Player[3]; + team2.playerArray[0] = new Player(); + team2.playerArray[0].playerName = "Robert Service"; + team2.playerArray[1] = new Player(); + team2.playerArray[1].playerName = "Robbie Burns"; + team2.playerArray[2] = new Player(); + team2.playerArray[2].playerName = "Rafael Sabatini"; + + Team[] theTeams = {team1, team2}; + return theTeams; + } + + public static Game[] createGames(Team[] theTeams) { + Game theGame = new Game(); + theGame.homeTeam = theTeams[0]; + theGame.awayTeam = theTeams[1]; + Game[] theGames = {theGame}; + return theGames; + } + +} diff --git a/src/main/java/ralvhe/practices/08-Methods_Practice2/src/soccer/Player.java b/src/main/java/ralvhe/practices/08-Methods_Practice2/src/soccer/Player.java new file mode 100644 index 0000000..07670b0 --- /dev/null +++ b/src/main/java/ralvhe/practices/08-Methods_Practice2/src/soccer/Player.java @@ -0,0 +1,17 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Player { + + public String playerName; + +} diff --git a/src/main/java/ralvhe/practices/08-Methods_Practice2/src/soccer/Team.java b/src/main/java/ralvhe/practices/08-Methods_Practice2/src/soccer/Team.java new file mode 100644 index 0000000..247f229 --- /dev/null +++ b/src/main/java/ralvhe/practices/08-Methods_Practice2/src/soccer/Team.java @@ -0,0 +1,18 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Team { + + public String teamName; + public Player[] playerArray; + +} diff --git a/src/main/java/ralvhe/practices/08-Methods_Practice2/src/utility/GameUtils.java b/src/main/java/ralvhe/practices/08-Methods_Practice2/src/utility/GameUtils.java new file mode 100644 index 0000000..7613a39 --- /dev/null +++ b/src/main/java/ralvhe/practices/08-Methods_Practice2/src/utility/GameUtils.java @@ -0,0 +1,61 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package utility; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Arrays; +import soccer.*; + +/** + * + * @author ksomervi + */ +public class GameUtils { + + public static void addGameGoals(Game currGame) { + + if (currGame.goals == null) { + currGame.goals = new Goal[(int) (Math.random() * 10)]; // If goals not initialized max will be 9 + } + + //System.out.println(currGame.goals.length); + int i = 0; + for (Goal currGoal : currGame.goals) { + currGoal = new Goal(); + currGoal.theTeam = Math.random() > 0.5 ? getHomeTeam(currGame, "home") : getHomeTeam(currGame, "away"); + currGoal.thePlayer = currGoal.theTeam.playerArray[(int) (Math.random() * currGoal.theTeam.playerArray.length)]; + currGoal.theTime = (int) (Math.random() * 90); + currGame.goals[i] = currGoal; + i++; + } + Arrays.sort(currGame.goals, (g1, g2) -> Double.valueOf(g1.theTime).compareTo(Double.valueOf(g2.theTime))); + + } + + // Uses reflection so works with getter method or public field + private static Team getHomeTeam(Game currGame, String homeOrAway) { + Team theTeam = null; + Method m; + Field f; + try { + m = Game.class.getMethod("get" + Character.toUpperCase(homeOrAway.charAt(0)) + homeOrAway.substring(1) + "Team"); + theTeam = (Team)m.invoke(currGame); + } catch (NoSuchMethodException|IllegalAccessException|InvocationTargetException em) { + try { + f = Game.class.getField(homeOrAway + "Team"); + theTeam = (Team)f.get(currGame); + } catch (NoSuchFieldException|IllegalAccessException ef) { + System.out.println("The addGoals() utility requires the Goal class to contain either:\n" + + "public String fields called homeTeam and awayTeam, OR,\n" + + "public accessor methods called getHomeTeam() and getAwayTeam()."); + } + } + return theTeam; + } +} diff --git a/src/main/java/ralvhe/practices/08-Methods_Practice3/src/soccer/Game.java b/src/main/java/ralvhe/practices/08-Methods_Practice3/src/soccer/Game.java new file mode 100644 index 0000000..4a12260 --- /dev/null +++ b/src/main/java/ralvhe/practices/08-Methods_Practice3/src/soccer/Game.java @@ -0,0 +1,46 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +import utility.GameUtils; + +/** + * + * @author Administrator + */ +public class Game { + + public Team homeTeam; + public Team awayTeam; + public Goal[] goals; + + public void playGame(int maxGoals) { + /* Practice 8-2. Add code to initialize the array currGame.goals to a random size */ + int numberOfGoals = (int)(Math.random() * (maxGoals + 1)); + Goal[] theGoals = new Goal[numberOfGoals]; + this.goals = theGoals; + GameUtils.addGameGoals(this); + } + public void playGame(){ + playGame(6); + } + /* Practice 8-3. Add no-parameter playGame() method here */ + + + public String getDescription() { + StringBuilder returnString = new StringBuilder(); + for (Goal currGoal: this.goals) { + returnString.append("Goal scored after " + + currGoal.theTime + " mins by " + + currGoal.thePlayer.playerName + " of " + + currGoal.theTeam.teamName + + "\n"); + } + return returnString.toString(); + } + +} diff --git a/src/main/java/ralvhe/practices/08-Methods_Practice3/src/soccer/Goal.java b/src/main/java/ralvhe/practices/08-Methods_Practice3/src/soccer/Goal.java new file mode 100644 index 0000000..144a3d0 --- /dev/null +++ b/src/main/java/ralvhe/practices/08-Methods_Practice3/src/soccer/Goal.java @@ -0,0 +1,19 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Goal { + + public Team theTeam; + public Player thePlayer; + public double theTime; + +} diff --git a/src/main/java/ralvhe/practices/08-Methods_Practice3/src/soccer/League.java b/src/main/java/ralvhe/practices/08-Methods_Practice3/src/soccer/League.java new file mode 100644 index 0000000..1f32359 --- /dev/null +++ b/src/main/java/ralvhe/practices/08-Methods_Practice3/src/soccer/League.java @@ -0,0 +1,70 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package soccer; + + + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + League theLeague = new League(); + + Team[] theTeams = theLeague.createTeams(); + Game[] theGames = createGames(theTeams); + + Game currGame = theGames[0]; + + currGame.playGame(3); + + System.out.println(currGame.getDescription()); + + } + + public Team[] createTeams() { + + Player player1 = new Player(); + player1.playerName = "George Eliot"; + Player player2 = new Player(); + player2.playerName = "Graham Greene"; + Player player3 = new Player(); + player3.playerName = "Geoffrey Chaucer"; + Player[] thePlayers = {player1, player2, player3}; + + Team team1 = new Team(); + team1.teamName = "The Greens"; + team1.playerArray = thePlayers; + + // Create team2 + Team team2 = new Team(); + team2.teamName = "The Reds"; + team2.playerArray = new Player[3]; + team2.playerArray[0] = new Player(); + team2.playerArray[0].playerName = "Robert Service"; + team2.playerArray[1] = new Player(); + team2.playerArray[1].playerName = "Robbie Burns"; + team2.playerArray[2] = new Player(); + team2.playerArray[2].playerName = "Rafael Sabatini"; + + Team[] theTeams = {team1, team2}; + return theTeams; + } + + public static Game[] createGames(Team[] theTeams) { + Game theGame = new Game(); + theGame.homeTeam = theTeams[0]; + theGame.awayTeam = theTeams[1]; + Game[] theGames = {theGame}; + return theGames; + } + +} diff --git a/src/main/java/ralvhe/practices/08-Methods_Practice3/src/soccer/Player.java b/src/main/java/ralvhe/practices/08-Methods_Practice3/src/soccer/Player.java new file mode 100644 index 0000000..07670b0 --- /dev/null +++ b/src/main/java/ralvhe/practices/08-Methods_Practice3/src/soccer/Player.java @@ -0,0 +1,17 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Player { + + public String playerName; + +} diff --git a/src/main/java/ralvhe/practices/08-Methods_Practice3/src/soccer/Team.java b/src/main/java/ralvhe/practices/08-Methods_Practice3/src/soccer/Team.java new file mode 100644 index 0000000..247f229 --- /dev/null +++ b/src/main/java/ralvhe/practices/08-Methods_Practice3/src/soccer/Team.java @@ -0,0 +1,18 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Team { + + public String teamName; + public Player[] playerArray; + +} diff --git a/src/main/java/ralvhe/practices/08-Methods_Practice3/src/utility/GameUtils.java b/src/main/java/ralvhe/practices/08-Methods_Practice3/src/utility/GameUtils.java new file mode 100644 index 0000000..cbd9d9e --- /dev/null +++ b/src/main/java/ralvhe/practices/08-Methods_Practice3/src/utility/GameUtils.java @@ -0,0 +1,66 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package utility; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Arrays; +import soccer.*; + +/** + * + * @author ksomervi + */ +public class GameUtils { + + public static void addGameGoals(Game currGame) { + + //System.out.println(currGame.awayTeam + " : " + currGame.homeTeam); + + // Or possibly throw an Exception? + if (currGame.goals == null) { + currGame.goals = new Goal[(int) (Math.random() * 10)]; // If goals not initialized max will be 9 + } + + //System.out.println(currGame.goals.length); + int i = 0; + for (Goal currGoal : currGame.goals) { + currGoal = new Goal(); + currGoal.theTeam = Math.random() > 0.5 ? getHomeTeam(currGame, "home") : getHomeTeam(currGame, "away"); + currGoal.thePlayer = currGoal.theTeam.playerArray[(int) (Math.random() * currGoal.theTeam.playerArray.length)]; + currGoal.theTime = (int) (Math.random() * 90); + currGame.goals[i] = currGoal; + i++; + } + Arrays.sort(currGame.goals, (g1, g2) -> Double.valueOf(g1.theTime).compareTo(Double.valueOf(g2.theTime))); + + } + + // Uses reflection so works with getter method or public field + private static Team getHomeTeam(Game currGame, String homeOrAway) { + Team theTeam = null; + Method m; + Field f; + try { + m = Game.class.getMethod("get" + Character.toUpperCase(homeOrAway.charAt(0)) + homeOrAway.substring(1) + "Team"); + theTeam = (Team)m.invoke(currGame); + //System.out.println(theTeam); + } catch (NoSuchMethodException|IllegalAccessException|InvocationTargetException em) { + try { + f = Game.class.getField(homeOrAway + "Team"); + theTeam = (Team)f.get(currGame); + //System.out.println(theTeam); + } catch (NoSuchFieldException|IllegalAccessException ef) { + System.out.println("The addGoals() utility requires the Goal class to contain either:\n" + + "public String fields called homeTeam and awayTeam, OR,\n" + + "public accessor methods called getHomeTeam() and getAwayTeam()."); + } + } + return theTeam; + } +} diff --git a/src/main/java/ralvhe/practices/09-EncapConstructors_Practice1/src/soccer/Game.java b/src/main/java/ralvhe/practices/09-EncapConstructors_Practice1/src/soccer/Game.java new file mode 100644 index 0000000..0bc1008 --- /dev/null +++ b/src/main/java/ralvhe/practices/09-EncapConstructors_Practice1/src/soccer/Game.java @@ -0,0 +1,87 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +import utility.GameUtils; + +/** + * + * @author Administrator + */ +public class Game { + + private Team homeTeam; + private Team awayTeam; + private Goal[] goals; + + public void playGame(int maxGoals) { + /* Practice 8-2. Add code to initialize the array currGame.goals to a random size */ + int numberOfGoals = (int)(Math.random() * maxGoals + 1); + Goal[] theGoals = new Goal[numberOfGoals]; + this.setGoals(theGoals); + GameUtils.addGameGoals(this); + } + + public void playGame() { + playGame(6); + } + + public String getDescription() { + StringBuilder returnString = new StringBuilder(); + for (Goal currGoal: this.getGoals()) { + returnString.append("Goal scored after " + + currGoal.getTheTime() + " mins by " + + currGoal.getThePlayer().getPlayerName() + " of " + + currGoal.getTheTeam().getTeamName() + + "\n"); + } + return returnString.toString(); + } + + /** + * @return the homeTeam + */ + public Team getHomeTeam() { + return homeTeam; + } + + /** + * @param homeTeam the homeTeam to set + */ + public void setHomeTeam(Team homeTeam) { + this.homeTeam = homeTeam; + } + + /** + * @return the awayTeam + */ + public Team getAwayTeam() { + return awayTeam; + } + + /** + * @param awayTeam the awayTeam to set + */ + public void setAwayTeam(Team awayTeam) { + this.awayTeam = awayTeam; + } + + /** + * @return the goals + */ + public Goal[] getGoals() { + return goals; + } + + /** + * @param goals the goals to set + */ + public void setGoals(Goal[] goals) { + this.goals = goals; + } + +} diff --git a/src/main/java/ralvhe/practices/09-EncapConstructors_Practice1/src/soccer/Goal.java b/src/main/java/ralvhe/practices/09-EncapConstructors_Practice1/src/soccer/Goal.java new file mode 100644 index 0000000..f7c01f6 --- /dev/null +++ b/src/main/java/ralvhe/practices/09-EncapConstructors_Practice1/src/soccer/Goal.java @@ -0,0 +1,61 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Goal { + + /** + * @return the theTeam + */ + public Team getTheTeam() { + return theTeam; + } + + /** + * @param theTeam the theTeam to set + */ + public void setTheTeam(Team theTeam) { + this.theTeam = theTeam; + } + + /** + * @return the thePlayer + */ + public Player getThePlayer() { + return thePlayer; + } + + /** + * @param thePlayer the thePlayer to set + */ + public void setThePlayer(Player thePlayer) { + this.thePlayer = thePlayer; + } + + /** + * @return the theTime + */ + public double getTheTime() { + return theTime; + } + + /** + * @param theTime the theTime to set + */ + public void setTheTime(double theTime) { + this.theTime = theTime; + } + + private Team theTeam; + private Player thePlayer; + private double theTime; + +} diff --git a/src/main/java/ralvhe/practices/09-EncapConstructors_Practice1/src/soccer/League.java b/src/main/java/ralvhe/practices/09-EncapConstructors_Practice1/src/soccer/League.java new file mode 100644 index 0000000..e4be636 --- /dev/null +++ b/src/main/java/ralvhe/practices/09-EncapConstructors_Practice1/src/soccer/League.java @@ -0,0 +1,70 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package soccer; + + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + League theLeague = new League(); + + Team[] theTeams = theLeague.createTeams(); + Game[] theGames = theLeague.createGames(theTeams); + + Game currGame = theGames[0]; + + currGame.playGame(); + + System.out.println(currGame.getDescription()); + + } + + public Team[] createTeams() { + + Player player1 = new Player(); + player1.setPlayerName("George Eliot"); + Player player2 = new Player(); + player2.setPlayerName("Graham Greene"); + Player player3 = new Player(); + player3.setPlayerName("Geoffrey Chaucer"); + Player[] thePlayers = {player1, player2, player3}; + + Team team1 = new Team(); + team1.setTeamName("The Greens"); + team1.setPlayerArray(thePlayers); + + // Create team2 + Team team2 = new Team(); + team2.setTeamName("The Reds"); + team2.setPlayerArray(new Player[3]); + team2.getPlayerArray()[0] = new Player(); + team2.getPlayerArray()[0].setPlayerName("Robert Service"); + team2.getPlayerArray()[1] = new Player(); + team2.getPlayerArray()[1].setPlayerName("Robbie Burns"); + team2.getPlayerArray()[2] = new Player(); + team2.getPlayerArray()[2].setPlayerName("Rafael Sabatini"); + + Team[] theTeams = {team1, team2}; + return theTeams; + } + + public Game[] createGames(Team[] theTeams) { + Game theGame = new Game(); + theGame.setHomeTeam(theTeams[0]); + theGame.setAwayTeam(theTeams[1]); + Game[] theGames = {theGame}; + return theGames; + } + +} diff --git a/src/main/java/ralvhe/practices/09-EncapConstructors_Practice1/src/soccer/Player.java b/src/main/java/ralvhe/practices/09-EncapConstructors_Practice1/src/soccer/Player.java new file mode 100644 index 0000000..23d4797 --- /dev/null +++ b/src/main/java/ralvhe/practices/09-EncapConstructors_Practice1/src/soccer/Player.java @@ -0,0 +1,26 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Player { + + private String playerName; + + /** + * @param playerName the playerName to set + */ + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + public String getPlayerName(){ + return this.playerName; + } +} diff --git a/src/main/java/ralvhe/practices/09-EncapConstructors_Practice1/src/soccer/Team.java b/src/main/java/ralvhe/practices/09-EncapConstructors_Practice1/src/soccer/Team.java new file mode 100644 index 0000000..d655fca --- /dev/null +++ b/src/main/java/ralvhe/practices/09-EncapConstructors_Practice1/src/soccer/Team.java @@ -0,0 +1,46 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Team { + + /** + * @return the teamName + */ + public String getTeamName() { + return teamName; + } + + /** + * @param teamName the teamName to set + */ + public void setTeamName(String teamName) { + this.teamName = teamName; + } + + /** + * @return the playerArray + */ + public Player[] getPlayerArray() { + return playerArray; + } + + /** + * @param playerArray the playerArray to set + */ + public void setPlayerArray(Player[] playerArray) { + this.playerArray = playerArray; + } + + private String teamName; + private Player[] playerArray; + +} diff --git a/src/main/java/ralvhe/practices/09-EncapConstructors_Practice1/src/utility/GameUtils.java b/src/main/java/ralvhe/practices/09-EncapConstructors_Practice1/src/utility/GameUtils.java new file mode 100644 index 0000000..4c4aa78 --- /dev/null +++ b/src/main/java/ralvhe/practices/09-EncapConstructors_Practice1/src/utility/GameUtils.java @@ -0,0 +1,41 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package utility; + +import java.util.Arrays; +import soccer.*; + +/** + * + * @author ksomervi + */ +public class GameUtils { + + public static void addGameGoals(Game currGame) { + + //System.out.println(currGame.awayTeam + " : " + currGame.homeTeam); + + // Or possibly throw an Exception? + if (currGame.getGoals() == null) { + currGame.setGoals(new Goal[(int) (Math.random() * 10)]); // If goals not initialized max will be 9 + } + + //System.out.println(currGame.goals.length); + int i = 0; + for (Goal currGoal : currGame.getGoals()) { + currGoal = new Goal(); + currGoal.setTheTeam(Math.random() > 0.5 ? currGame.getHomeTeam() : currGame.getAwayTeam()); + currGoal.setThePlayer(currGoal.getTheTeam().getPlayerArray()[(int) (Math.random() * currGoal.getTheTeam().getPlayerArray().length)]); + currGoal.setTheTime((int) (Math.random() * 90)); + currGame.getGoals()[i] = currGoal; + i++; + } + Arrays.sort(currGame.getGoals(), (g1, g2) -> Double.valueOf(g1.getTheTime()).compareTo(Double.valueOf(g2.getTheTime()))); + + } + + +} diff --git a/src/main/java/ralvhe/practices/09-EncapConstructors_Practice2/src/soccer/Game.java b/src/main/java/ralvhe/practices/09-EncapConstructors_Practice2/src/soccer/Game.java new file mode 100644 index 0000000..e849235 --- /dev/null +++ b/src/main/java/ralvhe/practices/09-EncapConstructors_Practice2/src/soccer/Game.java @@ -0,0 +1,91 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +import utility.GameUtils; + +/** + * + * @author Administrator + */ +public class Game { + + private Team homeTeam; + private Team awayTeam; + private Goal[] goals; + + /* Practice 9-2. Add constructor here */ + public Game(Team homeTeam, Team awayTeam){ + this.homeTeam = homeTeam; + this.awayTeam = awayTeam; + } + public void playGame(int maxGoals) { + int numberOfGoals = (int)(Math.random() * maxGoals + 1); + Goal[] theGoals = new Goal[numberOfGoals]; + this.setGoals(theGoals); + GameUtils.addGameGoals(this); + } + + public void playGame() { + playGame(6); + } + + public String getDescription() { + StringBuilder returnString = new StringBuilder(); + for (Goal currGoal: this.getGoals()) { + returnString.append("Goal scored after " + + currGoal.getTheTime() + " mins by " + + currGoal.getThePlayer().getPlayerName() + " of " + + currGoal.getTheTeam().getTeamName() + + "\n"); + } + return returnString.toString(); + } + + /** + * @return the homeTeam + */ + public Team getHomeTeam() { + return homeTeam; + } + + /** + * @param homeTeam the homeTeam to set + */ + public void setHomeTeam(Team homeTeam) { + this.homeTeam = homeTeam; + } + + /** + * @return the awayTeam + */ + public Team getAwayTeam() { + return awayTeam; + } + + /** + * @param awayTeam the awayTeam to set + */ + public void setAwayTeam(Team awayTeam) { + this.awayTeam = awayTeam; + } + + /** + * @return the goals + */ + public Goal[] getGoals() { + return goals; + } + + /** + * @param goals the goals to set + */ + public void setGoals(Goal[] goals) { + this.goals = goals; + } + +} diff --git a/src/main/java/ralvhe/practices/09-EncapConstructors_Practice2/src/soccer/Goal.java b/src/main/java/ralvhe/practices/09-EncapConstructors_Practice2/src/soccer/Goal.java new file mode 100644 index 0000000..99cce58 --- /dev/null +++ b/src/main/java/ralvhe/practices/09-EncapConstructors_Practice2/src/soccer/Goal.java @@ -0,0 +1,61 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Goal { + + private Team theTeam; + private Player thePlayer; + private double theTime; + + /** + * @return the theTeam + */ + public Team getTheTeam() { + return theTeam; + } + + /** + * @param theTeam the theTeam to set + */ + public void setTheTeam(Team theTeam) { + this.theTeam = theTeam; + } + + /** + * @return the thePlayer + */ + public Player getThePlayer() { + return thePlayer; + } + + /** + * @param thePlayer the thePlayer to set + */ + public void setThePlayer(Player thePlayer) { + this.thePlayer = thePlayer; + } + + /** + * @return the theTime + */ + public double getTheTime() { + return theTime; + } + + /** + * @param theTime the theTime to set + */ + public void setTheTime(double theTime) { + this.theTime = theTime; + } + +} diff --git a/src/main/java/ralvhe/practices/09-EncapConstructors_Practice2/src/soccer/League.java b/src/main/java/ralvhe/practices/09-EncapConstructors_Practice2/src/soccer/League.java new file mode 100644 index 0000000..8fbb81b --- /dev/null +++ b/src/main/java/ralvhe/practices/09-EncapConstructors_Practice2/src/soccer/League.java @@ -0,0 +1,68 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package soccer; + + + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + League theLeague = new League(); + + Team[] theTeams = theLeague.createTeams(); + Game[] theGames = theLeague.createGames(theTeams); + + + + for(Game currGame : theGames){ + currGame.playGame(); + System.out.println(currGame.getDescription()); + } + + + + } + + public Team[] createTeams() { + + Player player1 = new Player("George Eliot"); + Player player2 = new Player("Graham Greene"); + Player player3 = new Player("Geoffrey Chaucer"); + Player[] thePlayers = {player1, player2, player3}; + + Team team1 = new Team("The Greens",thePlayers); + /* Practice 9-2. The following two lines can be removed after the line above has been + modified to pass parameters to the constructor of Team */ + // Create team2 + Team team2 = new Team("The Reds"); + team2.setPlayerArray(new Player[3]); + team2.getPlayerArray()[0] = new Player("Robert Service"); + team2.getPlayerArray()[1] = new Player("Robbie Burns"); + team2.getPlayerArray()[2] = new Player("Rafael Sabatini"); + + Team[] theTeams = {team1, team2}; + return theTeams; + } + + public Game[] createGames(Team[] theTeams) { + Game theGame = new Game(theTeams[0], theTeams[1]); + Game theGame2 = new Game(theTeams[1], theTeams[0]); + Game theGame3 = new Game(theTeams[0], theTeams[1]); + Game theGame4 = new Game(theTeams[1], theTeams[0]); + + Game[] theGames = {theGame, theGame2, theGame3, theGame4}; + return theGames; + } + +} diff --git a/src/main/java/ralvhe/practices/09-EncapConstructors_Practice2/src/soccer/Player.java b/src/main/java/ralvhe/practices/09-EncapConstructors_Practice2/src/soccer/Player.java new file mode 100644 index 0000000..549d5c0 --- /dev/null +++ b/src/main/java/ralvhe/practices/09-EncapConstructors_Practice2/src/soccer/Player.java @@ -0,0 +1,39 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Player { + + private String playerName; + + /* Practice 9-2. Add the constructor here */ + public Player(String playerName){ + this.playerName = playerName; + } + + public Player(){ + + } + /** + * @return the playerName + */ + public String getPlayerName() { + return playerName; + } + + /** + * @param playerName the playerName to set + */ + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + +} diff --git a/src/main/java/ralvhe/practices/09-EncapConstructors_Practice2/src/soccer/Team.java b/src/main/java/ralvhe/practices/09-EncapConstructors_Practice2/src/soccer/Team.java new file mode 100644 index 0000000..a7237c4 --- /dev/null +++ b/src/main/java/ralvhe/practices/09-EncapConstructors_Practice2/src/soccer/Team.java @@ -0,0 +1,57 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Team { + + private String teamName; + private Player[] playerArray; + + /* Practice 9-2. Add the two constructors here */ + public Team (String teamName){ + this.teamName = teamName; + } + public Team (String teamName, Player[] players){ + this(teamName); + this.playerArray = players; + } + public Team (){ + + } + /** + * @return the teamName + */ + public String getTeamName() { + return teamName; + } + + /** + * @param teamName the teamName to set + */ + public void setTeamName(String teamName) { + this.teamName = teamName; + } + + /** + * @return the playerArray + */ + public Player[] getPlayerArray() { + return playerArray; + } + + /** + * @param playerArray the playerArray to set + */ + public void setPlayerArray(Player[] playerArray) { + this.playerArray = playerArray; + } + +} diff --git a/src/main/java/ralvhe/practices/09-EncapConstructors_Practice2/src/utility/GameUtils.java b/src/main/java/ralvhe/practices/09-EncapConstructors_Practice2/src/utility/GameUtils.java new file mode 100644 index 0000000..4c4aa78 --- /dev/null +++ b/src/main/java/ralvhe/practices/09-EncapConstructors_Practice2/src/utility/GameUtils.java @@ -0,0 +1,41 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package utility; + +import java.util.Arrays; +import soccer.*; + +/** + * + * @author ksomervi + */ +public class GameUtils { + + public static void addGameGoals(Game currGame) { + + //System.out.println(currGame.awayTeam + " : " + currGame.homeTeam); + + // Or possibly throw an Exception? + if (currGame.getGoals() == null) { + currGame.setGoals(new Goal[(int) (Math.random() * 10)]); // If goals not initialized max will be 9 + } + + //System.out.println(currGame.goals.length); + int i = 0; + for (Goal currGoal : currGame.getGoals()) { + currGoal = new Goal(); + currGoal.setTheTeam(Math.random() > 0.5 ? currGame.getHomeTeam() : currGame.getAwayTeam()); + currGoal.setThePlayer(currGoal.getTheTeam().getPlayerArray()[(int) (Math.random() * currGoal.getTheTeam().getPlayerArray().length)]); + currGoal.setTheTime((int) (Math.random() * 90)); + currGame.getGoals()[i] = currGoal; + i++; + } + Arrays.sort(currGame.getGoals(), (g1, g2) -> Double.valueOf(g1.getTheTime()).compareTo(Double.valueOf(g2.getTheTime()))); + + } + + +} diff --git a/src/main/java/ralvhe/practices/10-Conditions_Practice1/src/soccer/Game.java b/src/main/java/ralvhe/practices/10-Conditions_Practice1/src/soccer/Game.java new file mode 100644 index 0000000..9df4fd1 --- /dev/null +++ b/src/main/java/ralvhe/practices/10-Conditions_Practice1/src/soccer/Game.java @@ -0,0 +1,124 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +import utility.GameUtils; + +/** + * + * @author Administrator + */ +public class Game { + + private Team homeTeam; + private Team awayTeam; + private Goal[] goals; + + public Game(Team homeTeam, Team awayTeam) { + this.homeTeam = homeTeam; + this.awayTeam = awayTeam; + } + + public void playGame(int maxGoals) { + int numberOfGoals = (int)(Math.random() * maxGoals + 1); + Goal[] theGoals = new Goal[numberOfGoals]; + this.setGoals(theGoals); + GameUtils.addGameGoals(this); + } + + public void playGame() { + playGame(6); + } + + public String getDescription() { + + /* Practice 10-1. Declare two int variables here */ + int homeTeamGoals = 0; + int awayTeamGoals = 0; + StringBuilder returnString = new StringBuilder(); + + /* Practice 10-1. Add code to show teams that are playing */ + returnString.append(homeTeam.getTeamName() + " vs. " + awayTeam.getTeamName() + "\n"); + for (Goal currGoal: this.getGoals()) { + + /* Practice 10-1. Add if block here */ + if(currGoal.getTheTeam() == homeTeam){ + homeTeamGoals++; + } + else{ + awayTeamGoals++; + } + returnString.append("Goal scored after " + + currGoal.getTheTime() + " mins by " + + currGoal.getThePlayer().getPlayerName() + " of " + + currGoal.getTheTeam().getTeamName() + + "\n"); + } + + /* Practice 10-1. Add if block here */ + if(homeTeamGoals == awayTeamGoals){ + returnString.append("It's a draw!"); + homeTeam.incPointsTotal(1); + awayTeam.incPointsTotal(1); + } + else if (homeTeamGoals > awayTeamGoals){ + returnString.append(returnString.append(homeTeam.getTeamName())+ " win"); + homeTeam.incPointsTotal(2); + } + else{ + returnString.append(returnString.append(awayTeam.getTeamName() +" win")); + awayTeam.incPointsTotal(2); + } + returnString.append(" ("+ homeTeamGoals + " - " + awayTeamGoals + ") \n"); + /* Practice 10-1. Add returnString.append() that shows score */ + + return returnString.toString(); + } + + /** + * @return the homeTeam + */ + public Team getHomeTeam() { + return homeTeam; + } + + /** + * @param homeTeam the homeTeam to set + */ + public void setHomeTeam(Team homeTeam) { + this.homeTeam = homeTeam; + } + + /** + * @return the awayTeam + */ + public Team getAwayTeam() { + return awayTeam; + } + + /** + * @param awayTeam the awayTeam to set + */ + public void setAwayTeam(Team awayTeam) { + this.awayTeam = awayTeam; + } + + /** + * @return the goals + */ + public Goal[] getGoals() { + return goals; + } + + /** + * @param goals the goals to set + */ + public void setGoals(Goal[] goals) { + this.goals = goals; + } + +} diff --git a/src/main/java/ralvhe/practices/10-Conditions_Practice1/src/soccer/Goal.java b/src/main/java/ralvhe/practices/10-Conditions_Practice1/src/soccer/Goal.java new file mode 100644 index 0000000..99cce58 --- /dev/null +++ b/src/main/java/ralvhe/practices/10-Conditions_Practice1/src/soccer/Goal.java @@ -0,0 +1,61 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Goal { + + private Team theTeam; + private Player thePlayer; + private double theTime; + + /** + * @return the theTeam + */ + public Team getTheTeam() { + return theTeam; + } + + /** + * @param theTeam the theTeam to set + */ + public void setTheTeam(Team theTeam) { + this.theTeam = theTeam; + } + + /** + * @return the thePlayer + */ + public Player getThePlayer() { + return thePlayer; + } + + /** + * @param thePlayer the thePlayer to set + */ + public void setThePlayer(Player thePlayer) { + this.thePlayer = thePlayer; + } + + /** + * @return the theTime + */ + public double getTheTime() { + return theTime; + } + + /** + * @param theTime the theTime to set + */ + public void setTheTime(double theTime) { + this.theTime = theTime; + } + +} diff --git a/src/main/java/ralvhe/practices/10-Conditions_Practice1/src/soccer/League.java b/src/main/java/ralvhe/practices/10-Conditions_Practice1/src/soccer/League.java new file mode 100644 index 0000000..85a677a --- /dev/null +++ b/src/main/java/ralvhe/practices/10-Conditions_Practice1/src/soccer/League.java @@ -0,0 +1,75 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package soccer; + + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + League theLeague = new League(); + + Team[] theTeams = theLeague.createTeams(); + Game[] theGames = theLeague.createGames(theTeams); + + for (Game currGame: theGames){ + currGame.playGame(); + System.out.println(currGame.getDescription()); + } + + /* Practice 10-1. Add a call to showBestTeam() method here */ + theLeague.showBestTeam(theTeams); + } + + public Team[] createTeams() { + + Player player1 = new Player("George Eliot"); + Player player2 = new Player("Graham Greene"); + Player player3 = new Player("Geoffrey Chaucer"); + Player[] thePlayers = {player1, player2, player3}; + + Team team1 = new Team("The Greens", thePlayers); + + // Create team2 + Team team2 = new Team(); + team2.setTeamName("The Reds"); + team2.setPlayerArray(new Player[3]); + team2.getPlayerArray()[0] = new Player("Robert Service"); + team2.getPlayerArray()[1] = new Player("Robbie Burns"); + team2.getPlayerArray()[2] = new Player("Rafael Sabatini"); + + Team[] theTeams = {team1, team2}; + return theTeams; + } + + public Game[] createGames(Team[] theTeams) { + Game theGame = new Game(theTeams[0], theTeams[1]); + Game theGame2 = new Game(theTeams[1], theTeams[0]); + Game theGame3 = new Game(theTeams[0], theTeams[1]); + Game theGame4 = new Game(theTeams[1], theTeams[0]); + Game[] theGames = {theGame, theGame2, theGame3, theGame4}; + return theGames; + } + + /* Practice 10-1. Add showBestTeam() method here */ + public void showBestTeam(Team[] theTeams){ + Team currBestTeam = theTeams[0]; + System.out.println("\nTeam Points"); + for(Team currTeam: theTeams){ + System.out.println(currTeam.getTeamName() + " : "+ currTeam.getPointsTotal()); + currBestTeam = currTeam.getPointsTotal() > currBestTeam.getPointsTotal()?currTeam:currBestTeam; + } + + System.out.println("Winner of the League is " + currBestTeam.getTeamName()); + } +} diff --git a/src/main/java/ralvhe/practices/10-Conditions_Practice1/src/soccer/Player.java b/src/main/java/ralvhe/practices/10-Conditions_Practice1/src/soccer/Player.java new file mode 100644 index 0000000..6156984 --- /dev/null +++ b/src/main/java/ralvhe/practices/10-Conditions_Practice1/src/soccer/Player.java @@ -0,0 +1,37 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Player { + + private String playerName; + + public Player(String playerName) { + this.playerName = playerName; + } + + public Player() {} + + /** + * @return the playerName + */ + public String getPlayerName() { + return playerName; + } + + /** + * @param playerName the playerName to set + */ + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + +} diff --git a/src/main/java/ralvhe/practices/10-Conditions_Practice1/src/soccer/Team.java b/src/main/java/ralvhe/practices/10-Conditions_Practice1/src/soccer/Team.java new file mode 100644 index 0000000..6887044 --- /dev/null +++ b/src/main/java/ralvhe/practices/10-Conditions_Practice1/src/soccer/Team.java @@ -0,0 +1,73 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Team { + + private String teamName; + private Player[] playerArray; + /* Practice 10-1. Add pointTotal variable here */ + private int pointsTotal; + + /* Practice 10-1. Add incPointsTotal() method here */ + + public void incPointsTotal(int pointsTotal){ + this.pointsTotal += pointsTotal; + } + public int getPointsTotal() { + return pointsTotal; + } + + public void setPointsTotal(int pointsTotal) { + this.pointsTotal = pointsTotal; + } + + + public Team(String teamName) { + this.teamName = teamName; + } + + public Team(String teamName, Player[] players) { + this(teamName); + this.playerArray = players; + } + + public Team() {} + + /** + * @return the teamName + */ + public String getTeamName() { + return teamName; + } + + /** + * @param teamName the teamName to set + */ + public void setTeamName(String teamName) { + this.teamName = teamName; + } + + /** + * @return the playerArray + */ + public Player[] getPlayerArray() { + return playerArray; + } + + /** + * @param playerArray the playerArray to set + */ + public void setPlayerArray(Player[] playerArray) { + this.playerArray = playerArray; + } + +} diff --git a/src/main/java/ralvhe/practices/10-Conditions_Practice1/src/utility/GameUtils.java b/src/main/java/ralvhe/practices/10-Conditions_Practice1/src/utility/GameUtils.java new file mode 100644 index 0000000..4c4aa78 --- /dev/null +++ b/src/main/java/ralvhe/practices/10-Conditions_Practice1/src/utility/GameUtils.java @@ -0,0 +1,41 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package utility; + +import java.util.Arrays; +import soccer.*; + +/** + * + * @author ksomervi + */ +public class GameUtils { + + public static void addGameGoals(Game currGame) { + + //System.out.println(currGame.awayTeam + " : " + currGame.homeTeam); + + // Or possibly throw an Exception? + if (currGame.getGoals() == null) { + currGame.setGoals(new Goal[(int) (Math.random() * 10)]); // If goals not initialized max will be 9 + } + + //System.out.println(currGame.goals.length); + int i = 0; + for (Goal currGoal : currGame.getGoals()) { + currGoal = new Goal(); + currGoal.setTheTeam(Math.random() > 0.5 ? currGame.getHomeTeam() : currGame.getAwayTeam()); + currGoal.setThePlayer(currGoal.getTheTeam().getPlayerArray()[(int) (Math.random() * currGoal.getTheTeam().getPlayerArray().length)]); + currGoal.setTheTime((int) (Math.random() * 90)); + currGame.getGoals()[i] = currGoal; + i++; + } + Arrays.sort(currGame.getGoals(), (g1, g2) -> Double.valueOf(g1.getTheTime()).compareTo(Double.valueOf(g2.getTheTime()))); + + } + + +} diff --git a/src/main/java/ralvhe/practices/10-Conditions_Practice2/src/soccer/Game.java b/src/main/java/ralvhe/practices/10-Conditions_Practice2/src/soccer/Game.java new file mode 100644 index 0000000..b5d7e14 --- /dev/null +++ b/src/main/java/ralvhe/practices/10-Conditions_Practice2/src/soccer/Game.java @@ -0,0 +1,123 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +import utility.GameUtils; + +/** + * + * @author Administrator + */ +public class Game { + + private Team homeTeam; + private Team awayTeam; + private Goal[] goals; + + public Game(Team homeTeam, Team awayTeam) { + this.homeTeam = homeTeam; + this.awayTeam = awayTeam; + } + + public void playGame(int maxGoals) { + int numberOfGoals = (int)(Math.random() * maxGoals + 1); + Goal[] theGoals = new Goal[numberOfGoals]; + this.setGoals(theGoals); + GameUtils.addGameGoals(this); + } + + public void playGame() { + playGame(6); + } + + public String getDescription() { + + int homeTeamGoals = 0; + int awayTeamGoals = 0; + StringBuilder returnString = new StringBuilder(); + + returnString.append(homeTeam.getTeamName() + " vs. " + + awayTeam.getTeamName() + "\n"); + + for (Goal currGoal: this.getGoals()) { + + if (currGoal.getTheTeam()== homeTeam) { + homeTeamGoals++; + /* Practice 10-2. Add code to increment Team.goalsTotal here */ + homeTeam.incGoalsTotal(1); + } else { + awayTeamGoals++; + awayTeam.incGoalsTotal(1); + /* Practice 10-2. Add code to increment Team.goalsTotal here */ + } + + returnString.append("Goal scored after " + + currGoal.getTheTime() + " mins by " + + currGoal.getThePlayer().getPlayerName() + " of " + + currGoal.getTheTeam().getTeamName() + + "\n"); + } + + if (homeTeamGoals == awayTeamGoals) { + returnString.append("It's a draw!"); + homeTeam.incPointsTotal(1); + awayTeam.incPointsTotal(1); + } else if (homeTeamGoals > awayTeamGoals) { + returnString.append(homeTeam.getTeamName() + " win"); + homeTeam.incPointsTotal(2); + } else { + returnString.append(awayTeam.getTeamName() + " win"); + awayTeam.incPointsTotal(2); + } + returnString.append(" (" + homeTeamGoals + " - " + awayTeamGoals + ") \n"); + + return returnString.toString(); + } + + /** + * @return the homeTeam + */ + public Team getHomeTeam() { + return homeTeam; + } + + /** + * @param homeTeam the homeTeam to set + */ + public void setHomeTeam(Team homeTeam) { + this.homeTeam = homeTeam; + } + + /** + * @return the awayTeam + */ + public Team getAwayTeam() { + return awayTeam; + } + + /** + * @param awayTeam the awayTeam to set + */ + public void setAwayTeam(Team awayTeam) { + this.awayTeam = awayTeam; + } + + /** + * @return the goals + */ + public Goal[] getGoals() { + return goals; + } + + /** + * @param goals the goals to set + */ + public void setGoals(Goal[] goals) { + this.goals = goals; + } + +} diff --git a/src/main/java/ralvhe/practices/10-Conditions_Practice2/src/soccer/Goal.java b/src/main/java/ralvhe/practices/10-Conditions_Practice2/src/soccer/Goal.java new file mode 100644 index 0000000..99cce58 --- /dev/null +++ b/src/main/java/ralvhe/practices/10-Conditions_Practice2/src/soccer/Goal.java @@ -0,0 +1,61 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Goal { + + private Team theTeam; + private Player thePlayer; + private double theTime; + + /** + * @return the theTeam + */ + public Team getTheTeam() { + return theTeam; + } + + /** + * @param theTeam the theTeam to set + */ + public void setTheTeam(Team theTeam) { + this.theTeam = theTeam; + } + + /** + * @return the thePlayer + */ + public Player getThePlayer() { + return thePlayer; + } + + /** + * @param thePlayer the thePlayer to set + */ + public void setThePlayer(Player thePlayer) { + this.thePlayer = thePlayer; + } + + /** + * @return the theTime + */ + public double getTheTime() { + return theTime; + } + + /** + * @param theTime the theTime to set + */ + public void setTheTime(double theTime) { + this.theTime = theTime; + } + +} diff --git a/src/main/java/ralvhe/practices/10-Conditions_Practice2/src/soccer/League.java b/src/main/java/ralvhe/practices/10-Conditions_Practice2/src/soccer/League.java new file mode 100644 index 0000000..c89ee43 --- /dev/null +++ b/src/main/java/ralvhe/practices/10-Conditions_Practice2/src/soccer/League.java @@ -0,0 +1,84 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package soccer; + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + League theLeague = new League(); + + Team[] theTeams = theLeague.createTeams(); + Game[] theGames = theLeague.createGames(theTeams); + + for (Game currGame : theGames) { + currGame.playGame(); + System.out.println(currGame.getDescription()); + } + + theLeague.showBestTeam(theTeams); + + } + + public Team[] createTeams() { + + Player player1 = new Player("George Eliot"); + Player player2 = new Player("Graham Greene"); + Player player3 = new Player("Geoffrey Chaucer"); + Player[] thePlayers = {player1, player2, player3}; + + Team team1 = new Team("The Greens", thePlayers); + + // Create team2 + Team team2 = new Team(); + team2.setTeamName("The Reds"); + team2.setPlayerArray(new Player[3]); + team2.getPlayerArray()[0] = new Player("Robert Service"); + team2.getPlayerArray()[1] = new Player("Robbie Burns"); + team2.getPlayerArray()[2] = new Player("Rafael Sabatini"); + + Team[] theTeams = {team1, team2}; + return theTeams; + } + + public Game[] createGames(Team[] theTeams) { + Game theGame = new Game(theTeams[0], theTeams[1]); + Game theGame2 = new Game(theTeams[1], theTeams[0]); + Game theGame3 = new Game(theTeams[0], theTeams[1]); + Game theGame4 = new Game(theTeams[1], theTeams[0]); + Game[] theGames = {theGame, theGame2, theGame3, theGame4}; + return theGames; + } + + public void showBestTeam(Team[] theTeams) { + Team currBestTeam = theTeams[0]; + System.out.println("\nTeam Points"); + + for (Team currTeam : theTeams) { + /* Practice 10-2. Modify the line below to print out the goalsTotal for the current team also */ + System.out.println(currTeam.getTeamName() + " : " + currTeam.getPointsTotal() + " : "+currTeam.getGoalsTotal()); + /* Practice 10-2. Remove ternary statement above then add a replacement if statement here */ + if (currTeam.getPointsTotal() > currBestTeam.getPointsTotal()) { + currBestTeam = currTeam; + } else if (currTeam.getPointsTotal() == currBestTeam.getPointsTotal()) { + if (currTeam.getGoalsTotal() > currBestTeam.getGoalsTotal()) { + currBestTeam = currTeam; + } + } + } + + System.out.println("Winner of the League is " + currBestTeam.getTeamName()); + + } + +} diff --git a/src/main/java/ralvhe/practices/10-Conditions_Practice2/src/soccer/Player.java b/src/main/java/ralvhe/practices/10-Conditions_Practice2/src/soccer/Player.java new file mode 100644 index 0000000..6156984 --- /dev/null +++ b/src/main/java/ralvhe/practices/10-Conditions_Practice2/src/soccer/Player.java @@ -0,0 +1,37 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Player { + + private String playerName; + + public Player(String playerName) { + this.playerName = playerName; + } + + public Player() {} + + /** + * @return the playerName + */ + public String getPlayerName() { + return playerName; + } + + /** + * @param playerName the playerName to set + */ + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + +} diff --git a/src/main/java/ralvhe/practices/10-Conditions_Practice2/src/soccer/Team.java b/src/main/java/ralvhe/practices/10-Conditions_Practice2/src/soccer/Team.java new file mode 100644 index 0000000..9fb7d48 --- /dev/null +++ b/src/main/java/ralvhe/practices/10-Conditions_Practice2/src/soccer/Team.java @@ -0,0 +1,90 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Team { + + private String teamName; + private Player[] playerArray; + private int pointsTotal; + private int goalsTotal; + /* Practice 10-2. Add goalsTotal attribute here */ + + public int getGoalsTotal() { + return goalsTotal; + } + + public void setGoalsTotal(int goalsTotal) { + this.goalsTotal = goalsTotal; + } + + /* Practice 10-2. Add goalsTotalInc() method here */ + public void incGoalsTotal(int goals){ + this.goalsTotal = this.goalsTotal + goals; + } + public void incPointsTotal(int points){ + this.pointsTotal += points; + } + + public Team(String teamName) { + this.teamName = teamName; + } + + public Team(String teamName, Player[] players) { + this(teamName); + this.playerArray = players; + } + + public Team() {} + + /** + * @return the teamName + */ + public String getTeamName() { + return teamName; + } + + /** + * @param teamName the teamName to set + */ + public void setTeamName(String teamName) { + this.teamName = teamName; + } + + /** + * @return the playerArray + */ + public Player[] getPlayerArray() { + return playerArray; + } + + /** + * @param playerArray the playerArray to set + */ + public void setPlayerArray(Player[] playerArray) { + this.playerArray = playerArray; + } + + /** + * @return the pointsTotal + */ + public int getPointsTotal() { + return pointsTotal; + } + + /** + * @param pointsTotal the pointsTotal to set + */ + public void setPointsTotal(int pointsTotal) { + this.pointsTotal = pointsTotal; + } + +} diff --git a/src/main/java/ralvhe/practices/10-Conditions_Practice2/src/utility/GameUtils.java b/src/main/java/ralvhe/practices/10-Conditions_Practice2/src/utility/GameUtils.java new file mode 100644 index 0000000..4c4aa78 --- /dev/null +++ b/src/main/java/ralvhe/practices/10-Conditions_Practice2/src/utility/GameUtils.java @@ -0,0 +1,41 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package utility; + +import java.util.Arrays; +import soccer.*; + +/** + * + * @author ksomervi + */ +public class GameUtils { + + public static void addGameGoals(Game currGame) { + + //System.out.println(currGame.awayTeam + " : " + currGame.homeTeam); + + // Or possibly throw an Exception? + if (currGame.getGoals() == null) { + currGame.setGoals(new Goal[(int) (Math.random() * 10)]); // If goals not initialized max will be 9 + } + + //System.out.println(currGame.goals.length); + int i = 0; + for (Goal currGoal : currGame.getGoals()) { + currGoal = new Goal(); + currGoal.setTheTeam(Math.random() > 0.5 ? currGame.getHomeTeam() : currGame.getAwayTeam()); + currGoal.setThePlayer(currGoal.getTheTeam().getPlayerArray()[(int) (Math.random() * currGoal.getTheTeam().getPlayerArray().length)]); + currGoal.setTheTime((int) (Math.random() * 90)); + currGame.getGoals()[i] = currGoal; + i++; + } + Arrays.sort(currGame.getGoals(), (g1, g2) -> Double.valueOf(g1.getTheTime()).compareTo(Double.valueOf(g2.getTheTime()))); + + } + + +} diff --git a/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice1/src/soccer/Game.java b/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice1/src/soccer/Game.java new file mode 100644 index 0000000..564a707 --- /dev/null +++ b/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice1/src/soccer/Game.java @@ -0,0 +1,121 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +import utility.GameUtils; + +/** + * + * @author Administrator + */ +public class Game { + + private Team homeTeam; + private Team awayTeam; + private Goal[] goals; + + public Game(Team homeTeam, Team awayTeam) { + this.homeTeam = homeTeam; + this.awayTeam = awayTeam; + } + + public void playGame(int maxGoals) { + int numberOfGoals = (int)(Math.random() * maxGoals + 1); + Goal[] theGoals = new Goal[numberOfGoals]; + this.setGoals(theGoals); + GameUtils.addGameGoals(this); + } + + public void playGame() { + playGame(6); + } + + public String getDescription() { + + int homeTeamGoals = 0; + int awayTeamGoals = 0; + StringBuilder returnString = new StringBuilder(); + + returnString.append(this.getHomeTeam().getTeamName() + " vs. " + + this.getAwayTeam().getTeamName() + "\n"); + + for (Goal currGoal: this.getGoals()) { + + if (currGoal.getTheTeam()== homeTeam) { + homeTeamGoals++; + homeTeam.incGoalsTotal(1); + } else { + awayTeamGoals++; + awayTeam.incGoalsTotal(1); + } + + returnString.append("Goal scored after " + + currGoal.getTheTime() + " mins by " + + currGoal.getThePlayer().getPlayerName() + " of " + + currGoal.getTheTeam().getTeamName() + + "\n"); + } + + if (homeTeamGoals == awayTeamGoals) { + returnString.append("It's a draw!"); + this.homeTeam.incPointsTotal(1); + this.awayTeam.incPointsTotal(1); + } else if (homeTeamGoals > awayTeamGoals) { + returnString.append(homeTeam.getTeamName() + " win"); + this.homeTeam.incPointsTotal(1); + } else { + returnString.append(awayTeam.getTeamName() + " win"); + this.awayTeam.incPointsTotal(1); + } + returnString.append(" (" + homeTeamGoals + " - " + awayTeamGoals + ") \n"); + + return returnString.toString(); + } + + /** + * @return the homeTeam + */ + public Team getHomeTeam() { + return homeTeam; + } + + /** + * @param homeTeam the homeTeam to set + */ + public void setHomeTeam(Team homeTeam) { + this.homeTeam = homeTeam; + } + + /** + * @return the awayTeam + */ + public Team getAwayTeam() { + return awayTeam; + } + + /** + * @param awayTeam the awayTeam to set + */ + public void setAwayTeam(Team awayTeam) { + this.awayTeam = awayTeam; + } + + /** + * @return the goals + */ + public Goal[] getGoals() { + return goals; + } + + /** + * @param goals the goals to set + */ + public void setGoals(Goal[] goals) { + this.goals = goals; + } + +} diff --git a/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice1/src/soccer/Goal.java b/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice1/src/soccer/Goal.java new file mode 100644 index 0000000..99cce58 --- /dev/null +++ b/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice1/src/soccer/Goal.java @@ -0,0 +1,61 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Goal { + + private Team theTeam; + private Player thePlayer; + private double theTime; + + /** + * @return the theTeam + */ + public Team getTheTeam() { + return theTeam; + } + + /** + * @param theTeam the theTeam to set + */ + public void setTheTeam(Team theTeam) { + this.theTeam = theTeam; + } + + /** + * @return the thePlayer + */ + public Player getThePlayer() { + return thePlayer; + } + + /** + * @param thePlayer the thePlayer to set + */ + public void setThePlayer(Player thePlayer) { + this.thePlayer = thePlayer; + } + + /** + * @return the theTime + */ + public double getTheTime() { + return theTime; + } + + /** + * @param theTime the theTime to set + */ + public void setTheTime(double theTime) { + this.theTime = theTime; + } + +} diff --git a/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice1/src/soccer/League.java b/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice1/src/soccer/League.java new file mode 100644 index 0000000..444374e --- /dev/null +++ b/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice1/src/soccer/League.java @@ -0,0 +1,81 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package soccer; + +import java.util.ArrayList; +import java.util.StringTokenizer; +import utility.PlayerDatabase; + + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + League theLeague = new League(); + Team[] theTeams = theLeague.createTeams("The Robins,The Crows,The Swallows", 5); + Game[] theGames = theLeague.createGames(theTeams); + + for (Game currGame: theGames){ + currGame.playGame(); + System.out.println(currGame.getDescription()); + } + + theLeague.showBestTeam(theTeams); + + } + + public Team[] createTeams(String teamNames, int teamSize){ + PlayerDatabase playerDB = new PlayerDatabase(); + StringTokenizer teamNameTokens = new StringTokenizer(teamNames, ","); + Team[] theTeams = new Team[teamNameTokens.countTokens()]; + for (int i = 0; i < theTeams.length; i++){ + theTeams[i] = new Team(teamNameTokens.nextToken(),playerDB.getTeam(teamSize)); + } + + return theTeams; + } + + public Game[] createGames(Team[] theTeams) { + ArrayList theGames = new ArrayList(); + for (Team homeTeam: theTeams){ + for(Team awayTeam: theTeams){ + if(homeTeam != awayTeam){ + theGames.add(new Game(homeTeam, awayTeam)); + } + } + } + return ((Game[]) theGames.toArray(new Game[1])); + } + + public void showBestTeam(Team[] theTeams) { + Team currBestTeam = theTeams[0]; + System.out.println("\nTeam Points"); + + for (Team currTeam: theTeams){ + System.out.println(currTeam.getTeamName() + " : " + currTeam.getPointsTotal() + " : " + + currTeam.getGoalsTotal()); + currBestTeam = currTeam.getPointsTotal() > currBestTeam.getPointsTotal()?currTeam:currBestTeam; + if (currTeam.getPointsTotal() > currBestTeam.getPointsTotal()){ + currBestTeam = currTeam; + } else if (currTeam.getPointsTotal() == currBestTeam.getPointsTotal()){ + if (currTeam.getGoalsTotal() > currBestTeam.getGoalsTotal()){ + currBestTeam = currTeam; + } + } + } + + System.out.println("Winner of the League is " + currBestTeam.getTeamName()); + + } + +} diff --git a/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice1/src/soccer/Player.java b/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice1/src/soccer/Player.java new file mode 100644 index 0000000..6156984 --- /dev/null +++ b/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice1/src/soccer/Player.java @@ -0,0 +1,37 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Player { + + private String playerName; + + public Player(String playerName) { + this.playerName = playerName; + } + + public Player() {} + + /** + * @return the playerName + */ + public String getPlayerName() { + return playerName; + } + + /** + * @param playerName the playerName to set + */ + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + +} diff --git a/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice1/src/soccer/Team.java b/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice1/src/soccer/Team.java new file mode 100644 index 0000000..499003e --- /dev/null +++ b/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice1/src/soccer/Team.java @@ -0,0 +1,95 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Team { + + private String teamName; + private Player[] playerArray; + private int pointsTotal; + private int goalsTotal; + + public void incGoalsTotal(int goals){ + this.setGoalsTotal(this.getGoalsTotal() + goals); + } + + public void incPointsTotal(int points){ + this.pointsTotal += points; + } + + public Team(String teamName) { + this.teamName = teamName; + } + + public Team(String teamName, Player[] players) { + this(teamName); + this.playerArray = players; + } + + public Team() {} + + /** + * @return the teamName + */ + public String getTeamName() { + return teamName; + } + + /** + * @param teamName the teamName to set + */ + public void setTeamName(String teamName) { + this.teamName = teamName; + } + + /** + * @return the playerArray + */ + public Player[] getPlayerArray() { + return playerArray; + } + + /** + * @param playerArray the playerArray to set + */ + public void setPlayerArray(Player[] playerArray) { + this.playerArray = playerArray; + } + + /** + * @return the pointsTotal + */ + public int getPointsTotal() { + return pointsTotal; + } + + /** + * @param pointsTotal the pointsTotal to set + */ + public void setPointsTotal(int pointsTotal) { + this.pointsTotal = pointsTotal; + } + + /** + * @return the goalsTotal + */ + public int getGoalsTotal() { + return goalsTotal; + } + + /** + * @param goalsTotal the goalsTotal to set + */ + public void setGoalsTotal(int goalsTotal) { + this.goalsTotal = goalsTotal; + } + +} diff --git a/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice1/src/utility/GameUtils.java b/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice1/src/utility/GameUtils.java new file mode 100644 index 0000000..4c4aa78 --- /dev/null +++ b/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice1/src/utility/GameUtils.java @@ -0,0 +1,41 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package utility; + +import java.util.Arrays; +import soccer.*; + +/** + * + * @author ksomervi + */ +public class GameUtils { + + public static void addGameGoals(Game currGame) { + + //System.out.println(currGame.awayTeam + " : " + currGame.homeTeam); + + // Or possibly throw an Exception? + if (currGame.getGoals() == null) { + currGame.setGoals(new Goal[(int) (Math.random() * 10)]); // If goals not initialized max will be 9 + } + + //System.out.println(currGame.goals.length); + int i = 0; + for (Goal currGoal : currGame.getGoals()) { + currGoal = new Goal(); + currGoal.setTheTeam(Math.random() > 0.5 ? currGame.getHomeTeam() : currGame.getAwayTeam()); + currGoal.setThePlayer(currGoal.getTheTeam().getPlayerArray()[(int) (Math.random() * currGoal.getTheTeam().getPlayerArray().length)]); + currGoal.setTheTime((int) (Math.random() * 90)); + currGame.getGoals()[i] = currGoal; + i++; + } + Arrays.sort(currGame.getGoals(), (g1, g2) -> Double.valueOf(g1.getTheTime()).compareTo(Double.valueOf(g2.getTheTime()))); + + } + + +} diff --git a/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice1/src/utility/PlayerDatabase.java b/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice1/src/utility/PlayerDatabase.java new file mode 100644 index 0000000..65c87af --- /dev/null +++ b/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice1/src/utility/PlayerDatabase.java @@ -0,0 +1,76 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package utility; + +import java.util.*; +import soccer.Player; + + +/** + * + * @author Administrator + */ +public class PlayerDatabase { + + /* Practice 11-2. Declare an ArrayList here */ + private ArrayList players; + /* Practice 11-2. Add Constructor here */ + public PlayerDatabase(){ + StringTokenizer authorTokens = new StringTokenizer(authorList, ","); + players = new ArrayList(); + while(authorTokens.hasMoreTokens()){ + players.add(new Player(authorTokens.nextToken())); + } + } + /* Practice 11-2. Add getTeam() method here */ + public Player[] getTeam(int numberOfPlayers){ + Player[] teamPlayers = new Player[numberOfPlayers]; + for(int i = 0; i < numberOfPlayers; i++){ + int playerIndex = (int) (Math.random() * players.size()); + teamPlayers[i] = players.get(playerIndex); + players.remove(playerIndex); + } + return teamPlayers; + } +String authorList = +"Agatha Christie," + +"Alan Patton," + +"Alexander Solzhenitsyn," + +"Arthur Conan Doyle," + +"Anthony Trollope," + +"Baroness Orczy," + +"Brendan Behan," + +"Brian Moore," + +"Boris Pasternik," + +"Charles Dickens," + +"Charlotte Bronte," + +"Dorothy Parker," + +"Emile Zola," + +"Frank O'Connor," + +"Geoffrey Chaucer," + +"George Eliot," + +"G. K. Chesterton," + +"Graham Green," + +"Henry James," + +"James Joyce," + +"J. M. Synge," + +"J. R. Tolkien," + +"Jane Austin," + +"Leo Tolstoy," + +"Liam O'Flaherty," + +"Marcel Proust," + +"Mark Twain," + +"Oscar Wilde," + +"O. Henry," + +"Samuel Beckett," + +"Sean O'Casey," + +"William Shakespeare," + +"William Makepeace Thackeray," + +"W. B. Yeats," + +"Wilkie Collins"; + +} diff --git a/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice2/src/soccer/Game.java b/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice2/src/soccer/Game.java new file mode 100644 index 0000000..f17b85c --- /dev/null +++ b/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice2/src/soccer/Game.java @@ -0,0 +1,136 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +import java.time.*; +import java.time.format.DateTimeFormatter; +import utility.GameUtils; + +/** + * + * @author Administrator + */ +public class Game { + + private Team homeTeam; + private Team awayTeam; + private Goal[] goals; + + /* Practice 11-2. Add LocalDateTime attribute here */ + private LocalDateTime theDateTime; + + public LocalDateTime getTheDateTime() { + return theDateTime; + } + + public void setTheDateTime(LocalDateTime theDateTime) { + this.theDateTime = theDateTime; + } + /* Practice 11-2. Modify the constructor to include the date and time of the game */ + public Game(Team homeTeam, Team awayTeam,LocalDateTime theDateTime){ + this.homeTeam = homeTeam; + this.awayTeam = awayTeam; + this.theDateTime = theDateTime; + } + + public void playGame(int maxGoals) { + int numberOfGoals = (int)(Math.random() * maxGoals + 1); + Goal[] theGoals = new Goal[numberOfGoals]; + this.setGoals(theGoals); + GameUtils.addGameGoals(this); + } + + public void playGame() { + playGame(6); + } + + public String getDescription() { + + int homeTeamGoals = 0; + int awayTeamGoals = 0; + StringBuilder returnString = new StringBuilder(); + + /* Practice 11-2. Modify the next line to include the date and time of the game */ + returnString.append(this.getHomeTeam().getTeamName() + " vs. " + + this.getAwayTeam().getTeamName() + "\n" + "Date " + this.theDateTime.format(DateTimeFormatter.ISO_DATE) + "\n"); + + for (Goal currGoal: this.getGoals()) { + + if (currGoal.getTheTeam()== homeTeam) { + homeTeamGoals++; + homeTeam.incGoalsTotal(1); + } else { + awayTeamGoals++; + awayTeam.incGoalsTotal(1); + } + + returnString.append("Goal scored after " + + currGoal.getTheTime() + " mins by " + + currGoal.getThePlayer().getPlayerName() + " of " + + currGoal.getTheTeam().getTeamName() + + "\n"); + } + + if (homeTeamGoals == awayTeamGoals) { + returnString.append("It's a draw!"); + this.homeTeam.incPointsTotal(1); + this.awayTeam.incPointsTotal(1); + } else if (homeTeamGoals > awayTeamGoals) { + returnString.append(homeTeam.getTeamName() + " win"); + this.homeTeam.incPointsTotal(1); + } else { + returnString.append(awayTeam.getTeamName() + " win"); + this.awayTeam.incPointsTotal(1); + } + returnString.append(" (" + homeTeamGoals + " - " + awayTeamGoals + ") \n"); + + return returnString.toString(); + } + + /** + * @return the homeTeam + */ + public Team getHomeTeam() { + return homeTeam; + } + + /** + * @param homeTeam the homeTeam to set + */ + public void setHomeTeam(Team homeTeam) { + this.homeTeam = homeTeam; + } + + /** + * @return the awayTeam + */ + public Team getAwayTeam() { + return awayTeam; + } + + /** + * @param awayTeam the awayTeam to set + */ + public void setAwayTeam(Team awayTeam) { + this.awayTeam = awayTeam; + } + + /** + * @return the goals + */ + public Goal[] getGoals() { + return goals; + } + + /** + * @param goals the goals to set + */ + public void setGoals(Goal[] goals) { + this.goals = goals; + } + +} diff --git a/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice2/src/soccer/Goal.java b/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice2/src/soccer/Goal.java new file mode 100644 index 0000000..99cce58 --- /dev/null +++ b/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice2/src/soccer/Goal.java @@ -0,0 +1,61 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Goal { + + private Team theTeam; + private Player thePlayer; + private double theTime; + + /** + * @return the theTeam + */ + public Team getTheTeam() { + return theTeam; + } + + /** + * @param theTeam the theTeam to set + */ + public void setTheTeam(Team theTeam) { + this.theTeam = theTeam; + } + + /** + * @return the thePlayer + */ + public Player getThePlayer() { + return thePlayer; + } + + /** + * @param thePlayer the thePlayer to set + */ + public void setThePlayer(Player thePlayer) { + this.thePlayer = thePlayer; + } + + /** + * @return the theTime + */ + public double getTheTime() { + return theTime; + } + + /** + * @param theTime the theTime to set + */ + public void setTheTime(double theTime) { + this.theTime = theTime; + } + +} diff --git a/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice2/src/soccer/League.java b/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice2/src/soccer/League.java new file mode 100644 index 0000000..8f0df21 --- /dev/null +++ b/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice2/src/soccer/League.java @@ -0,0 +1,103 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package soccer; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.Period; +import java.util.ArrayList; +import java.util.StringTokenizer; +import utility.PlayerDatabase; + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + League theLeague = new League(); + + Team[] theTeams = theLeague.createTeams("The Robins,The Crows,The Swallows", 5); + Game[] theGames = theLeague.createGames(theTeams); + + /* Practice 11-2. Print the League announcement here */ + System.out.println(theLeague.getLeagueAnnouncement(theGames)); + for (Game currGame: theGames){ + currGame.playGame(); + System.out.println(currGame.getDescription()); + } + + theLeague.showBestTeam(theTeams); + + } + + public Team[] createTeams(String teamNames, int teamSize) { + + PlayerDatabase playerDB = new PlayerDatabase(); + + StringTokenizer teamNameTokens = new StringTokenizer(teamNames, ","); + Team[] theTeams = new Team[teamNameTokens.countTokens()]; + for (int i =0; i < theTeams.length; i++){ + theTeams[i] = new Team(teamNameTokens.nextToken(), playerDB.getTeam(teamSize)); + } + + + return theTeams; + } + + public Game[] createGames(Team[] theTeams) { + /* Practice 11-2. Add a line to declare and initialize daysBetweenGames variable */ + ArrayList theGames = new ArrayList(); + int daysBetweenGames = 0; + for (Team homeTeam: theTeams){ + for (Team awayTeam: theTeams){ + if (homeTeam != awayTeam) { + /* Practice 11-2. Increment daysBetweenGames here */ + daysBetweenGames += 21; + /* Practice 11-2. Modify the statement below to add pass LocalDateTime into constructor */ + theGames.add(new Game(homeTeam, awayTeam, LocalDateTime.now().plusDays(daysBetweenGames))); + } + + } + } + + + + return (Game[]) theGames.toArray(new Game[1]); + } + + public void showBestTeam(Team[] theTeams) { + Team currBestTeam = theTeams[0]; + System.out.println("\nTeam Points"); + + for (Team currTeam: theTeams){ + System.out.println(currTeam.getTeamName() + " : " + currTeam.getPointsTotal() + " : " + + currTeam.getGoalsTotal()); + currBestTeam = currTeam.getPointsTotal() > currBestTeam.getPointsTotal()?currTeam:currBestTeam; + if (currTeam.getPointsTotal() > currBestTeam.getPointsTotal()){ + currBestTeam = currTeam; + } else if (currTeam.getPointsTotal() == currBestTeam.getPointsTotal()){ + if (currTeam.getGoalsTotal() > currBestTeam.getGoalsTotal()){ + currBestTeam = currTeam; + } + } + } + + System.out.println("Winner of the League is " + currBestTeam.getTeamName()); + + } + + /* Practice 11-2. Add the getLeagueAnnouncement() method here */ + public String getLeagueAnnouncement(Game[] theGames){ + Period thePeriod = Period.between(theGames[0].getTheDateTime().toLocalDate(),theGames[theGames.length - 1].getTheDateTime().toLocalDate()); + return "The League is scheduled to run for " + thePeriod.getMonths() + " month(s), and " + thePeriod.getDays() + " day(s)\n"; + } +} diff --git a/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice2/src/soccer/Player.java b/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice2/src/soccer/Player.java new file mode 100644 index 0000000..6156984 --- /dev/null +++ b/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice2/src/soccer/Player.java @@ -0,0 +1,37 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Player { + + private String playerName; + + public Player(String playerName) { + this.playerName = playerName; + } + + public Player() {} + + /** + * @return the playerName + */ + public String getPlayerName() { + return playerName; + } + + /** + * @param playerName the playerName to set + */ + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + +} diff --git a/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice2/src/soccer/Team.java b/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice2/src/soccer/Team.java new file mode 100644 index 0000000..499003e --- /dev/null +++ b/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice2/src/soccer/Team.java @@ -0,0 +1,95 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Team { + + private String teamName; + private Player[] playerArray; + private int pointsTotal; + private int goalsTotal; + + public void incGoalsTotal(int goals){ + this.setGoalsTotal(this.getGoalsTotal() + goals); + } + + public void incPointsTotal(int points){ + this.pointsTotal += points; + } + + public Team(String teamName) { + this.teamName = teamName; + } + + public Team(String teamName, Player[] players) { + this(teamName); + this.playerArray = players; + } + + public Team() {} + + /** + * @return the teamName + */ + public String getTeamName() { + return teamName; + } + + /** + * @param teamName the teamName to set + */ + public void setTeamName(String teamName) { + this.teamName = teamName; + } + + /** + * @return the playerArray + */ + public Player[] getPlayerArray() { + return playerArray; + } + + /** + * @param playerArray the playerArray to set + */ + public void setPlayerArray(Player[] playerArray) { + this.playerArray = playerArray; + } + + /** + * @return the pointsTotal + */ + public int getPointsTotal() { + return pointsTotal; + } + + /** + * @param pointsTotal the pointsTotal to set + */ + public void setPointsTotal(int pointsTotal) { + this.pointsTotal = pointsTotal; + } + + /** + * @return the goalsTotal + */ + public int getGoalsTotal() { + return goalsTotal; + } + + /** + * @param goalsTotal the goalsTotal to set + */ + public void setGoalsTotal(int goalsTotal) { + this.goalsTotal = goalsTotal; + } + +} diff --git a/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice2/src/utility/GameUtils.java b/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice2/src/utility/GameUtils.java new file mode 100644 index 0000000..4c4aa78 --- /dev/null +++ b/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice2/src/utility/GameUtils.java @@ -0,0 +1,41 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package utility; + +import java.util.Arrays; +import soccer.*; + +/** + * + * @author ksomervi + */ +public class GameUtils { + + public static void addGameGoals(Game currGame) { + + //System.out.println(currGame.awayTeam + " : " + currGame.homeTeam); + + // Or possibly throw an Exception? + if (currGame.getGoals() == null) { + currGame.setGoals(new Goal[(int) (Math.random() * 10)]); // If goals not initialized max will be 9 + } + + //System.out.println(currGame.goals.length); + int i = 0; + for (Goal currGoal : currGame.getGoals()) { + currGoal = new Goal(); + currGoal.setTheTeam(Math.random() > 0.5 ? currGame.getHomeTeam() : currGame.getAwayTeam()); + currGoal.setThePlayer(currGoal.getTheTeam().getPlayerArray()[(int) (Math.random() * currGoal.getTheTeam().getPlayerArray().length)]); + currGoal.setTheTime((int) (Math.random() * 90)); + currGame.getGoals()[i] = currGoal; + i++; + } + Arrays.sort(currGame.getGoals(), (g1, g2) -> Double.valueOf(g1.getTheTime()).compareTo(Double.valueOf(g2.getTheTime()))); + + } + + +} diff --git a/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice2/src/utility/PlayerDatabase.java b/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice2/src/utility/PlayerDatabase.java new file mode 100644 index 0000000..a75be77 --- /dev/null +++ b/src/main/java/ralvhe/practices/11-ArraysLoopsDates_Practice2/src/utility/PlayerDatabase.java @@ -0,0 +1,80 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package utility; + +import java.util.*; +import soccer.Player; + + + +/** + * + * @author Administrator + */ +public class PlayerDatabase { + + private ArrayList players; + + public PlayerDatabase(){ + StringTokenizer authorTokens = new StringTokenizer(authorList, ","); + players = new ArrayList(); + while (authorTokens.hasMoreTokens()){ + players.add(new Player(authorTokens.nextToken())); + } + } + + public Player[] getTeam(int numberOfPlayers){ + Player[] teamPlayers = new Player[numberOfPlayers]; + for (int i = 0; i < numberOfPlayers; i++){ + int playerIndex = (int) (Math.random() * players.size()); + teamPlayers[i] = players.get(playerIndex); + players.remove(playerIndex); + } + return teamPlayers; + + } + + + +String authorList = +"Agatha Christie," + +"Alan Patton," + +"Alexander Solzhenitsyn," + +"Arthur Conan Doyle," + +"Anthony Trollope," + +"Baroness Orczy," + +"Brendan Behan," + +"Brian Moore," + +"Boris Pasternik," + +"Charles Dickens," + +"Charlotte Bronte," + +"Dorothy Parker," + +"Emile Zola," + +"Frank O'Connor," + +"Geoffrey Chaucer," + +"George Eliot," + +"G. K. Chesterton," + +"Graham Green," + +"Henry James," + +"James Joyce," + +"J. M. Synge," + +"J. R. Tolkien," + +"Jane Austin," + +"Leo Tolstoy," + +"Liam O'Flaherty," + +"Marcel Proust," + +"Mark Twain," + +"Oscar Wilde," + +"O. Henry," + +"Samuel Beckett," + +"Sean O'Casey," + +"William Shakespeare," + +"William Makepeace Thackeray," + +"W. B. Yeats," + +"Wilkie Collins"; + +} diff --git a/src/main/java/ralvhe/practices/12-UsingInheritance_Practice1/src/soccer/Game.java b/src/main/java/ralvhe/practices/12-UsingInheritance_Practice1/src/soccer/Game.java new file mode 100644 index 0000000..68b5110 --- /dev/null +++ b/src/main/java/ralvhe/practices/12-UsingInheritance_Practice1/src/soccer/Game.java @@ -0,0 +1,163 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; + +/** + * + * @author Administrator + */ +public class Game { + + private Team homeTeam; + private Team awayTeam; + private Goal[] goals; + private LocalDateTime theDateTime; + + public Game(Team homeTeam, Team awayTeam, LocalDateTime theDateTime) { + this.homeTeam = homeTeam; + this.awayTeam = awayTeam; + this.theDateTime = theDateTime; + } + + + public void playGame() { + /* Practice 12-1. Remove the following line */ + //playGame(6); + ArrayList eventList = new ArrayList(); + Goal currEvent; + for(int i = 1; i <= 90; i ++){ + if(Math.random() > 0.95){ + //System.out.println(i); + currEvent = new Goal(); + currEvent.setTheTeam(Math.random() > 0.5?homeTeam:awayTeam); + currEvent.setThePlayer(currEvent.getTheTeam().getPlayerArray()[(int)(Math.random()*currEvent.getTheTeam().getPlayerArray().length)]); + currEvent.setTheTime(i); + eventList.add(currEvent); + } + } + this.goals = new Goal[eventList.size()]; + eventList.toArray(goals); + } + + public String getDescription() { + + int homeTeamGoals = 0; + int awayTeamGoals = 0; + StringBuilder returnString = new StringBuilder(); + + returnString.append(this.getHomeTeam().getTeamName() + " vs. " + + this.getAwayTeam().getTeamName() + "\n" + + "Date: " + this.getTheDateTime().format(DateTimeFormatter.ISO_LOCAL_DATE) + "\n"); + + for (Goal currGoal: this.getGoals()) { + + if (currGoal.getTheTeam()== homeTeam) { + homeTeamGoals++; + homeTeam.incGoalsTotal(1); + } else { + awayTeamGoals++; + awayTeam.incGoalsTotal(1); + } + + returnString.append("Goal scored after " + + currGoal.getTheTime() + " mins by " + + currGoal.getThePlayer().getPlayerName() + " of " + + currGoal.getTheTeam().getTeamName() + + "\n"); + } + + if (homeTeamGoals == awayTeamGoals) { + returnString.append("It's a draw!"); + this.homeTeam.incPointsTotal(1); + this.awayTeam.incPointsTotal(1); + } else if (homeTeamGoals > awayTeamGoals) { + returnString.append(homeTeam.getTeamName() + " win"); + this.homeTeam.incPointsTotal(1); + } else { + returnString.append(awayTeam.getTeamName() + " win"); + this.awayTeam.incPointsTotal(1); + } + returnString.append(" (" + homeTeamGoals + " - " + awayTeamGoals + ") \n"); + + return returnString.toString(); + } + + /** + * @return the homeTeam + */ + public Team getHomeTeam() { + return homeTeam; + } + + /** + * @param homeTeam the homeTeam to set + */ + public void setHomeTeam(Team homeTeam) { + this.homeTeam = homeTeam; + } + + /** + * @return the awayTeam + */ + public Team getAwayTeam() { + return awayTeam; + } + + /** + * @param awayTeam the awayTeam to set + */ + public void setAwayTeam(Team awayTeam) { + this.awayTeam = awayTeam; + } + + /** + * @return the goals + */ + public Goal[] getGoals() { + return goals; + } + + /** + * @param goals the goals to set + */ + public void setGoals(Goal[] goals) { + this.goals = goals; + } + + /** + * @return the localDateTime + */ + public LocalDateTime getLocalDateTime() { + return getTheDateTime(); + } + + /** + * @param theDateTime the localDateTime to set + */ + public void setLocalDateTime(LocalDateTime theDateTime) { + this.setTheDateTime(theDateTime); + } + + /** + * @return the theDateTime + */ + public LocalDateTime getTheDateTime() { + return theDateTime; + } + + /** + * @param theDateTime the theDateTime to set + */ + public void setTheDateTime(LocalDateTime theDateTime) { + this.theDateTime = theDateTime; + } + +} diff --git a/src/main/java/ralvhe/practices/12-UsingInheritance_Practice1/src/soccer/Goal.java b/src/main/java/ralvhe/practices/12-UsingInheritance_Practice1/src/soccer/Goal.java new file mode 100644 index 0000000..99cce58 --- /dev/null +++ b/src/main/java/ralvhe/practices/12-UsingInheritance_Practice1/src/soccer/Goal.java @@ -0,0 +1,61 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Goal { + + private Team theTeam; + private Player thePlayer; + private double theTime; + + /** + * @return the theTeam + */ + public Team getTheTeam() { + return theTeam; + } + + /** + * @param theTeam the theTeam to set + */ + public void setTheTeam(Team theTeam) { + this.theTeam = theTeam; + } + + /** + * @return the thePlayer + */ + public Player getThePlayer() { + return thePlayer; + } + + /** + * @param thePlayer the thePlayer to set + */ + public void setThePlayer(Player thePlayer) { + this.thePlayer = thePlayer; + } + + /** + * @return the theTime + */ + public double getTheTime() { + return theTime; + } + + /** + * @param theTime the theTime to set + */ + public void setTheTime(double theTime) { + this.theTime = theTime; + } + +} diff --git a/src/main/java/ralvhe/practices/12-UsingInheritance_Practice1/src/soccer/League.java b/src/main/java/ralvhe/practices/12-UsingInheritance_Practice1/src/soccer/League.java new file mode 100644 index 0000000..ce5bcb2 --- /dev/null +++ b/src/main/java/ralvhe/practices/12-UsingInheritance_Practice1/src/soccer/League.java @@ -0,0 +1,109 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package soccer; + +import java.time.LocalDateTime; +import java.time.Period; +import java.util.ArrayList; +import java.util.StringTokenizer; +import utility.PlayerDatabase; + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + League theLeague = new League(); + + Team[] theTeams = theLeague.createTeams("The Robins,The Crows,The Swallows", 5); + Game[] theGames = theLeague.createGames(theTeams); + + System.out.println(theLeague.getLeagueAnnouncement(theGames)); + for (Game currGame: theGames){ + currGame.playGame(); + + /* Practice 12-1. Add a break command here */ + //break; + /* Practice 12-1. Comment out the line below */ + System.out.println(currGame.getDescription()); + } + + theLeague.showBestTeam(theTeams); + + } + + public Team[] createTeams(String teamNames, int teamSize) { + + PlayerDatabase playerDB = new PlayerDatabase(); + + StringTokenizer teamNameTokens = new StringTokenizer(teamNames, ","); + Team[] theTeams = new Team[teamNameTokens.countTokens()]; + for (int i =0; i < theTeams.length; i++){ + theTeams[i] = new Team(teamNameTokens.nextToken(), playerDB.getTeam(teamSize)); + } + + + return theTeams; + } + + public Game[] createGames(Team[] theTeams) { + int daysBetweenGames = 0; + + ArrayList theGames = new ArrayList(); + + for (Team homeTeam: theTeams){ + for (Team awayTeam: theTeams){ + if (homeTeam != awayTeam) { + daysBetweenGames += 7; + theGames.add(new Game(homeTeam, awayTeam, LocalDateTime.now().plusDays(daysBetweenGames))); + } + + } + } + + + + return (Game[]) theGames.toArray(new Game[1]); + } + + public void showBestTeam(Team[] theTeams) { + Team currBestTeam = theTeams[0]; + System.out.println("\nTeam Points"); + + for (Team currTeam: theTeams){ + System.out.println(currTeam.getTeamName() + " : " + currTeam.getPointsTotal() + " : " + + currTeam.getGoalsTotal()); + currBestTeam = currTeam.getPointsTotal() > currBestTeam.getPointsTotal()?currTeam:currBestTeam; + if (currTeam.getPointsTotal() > currBestTeam.getPointsTotal()){ + currBestTeam = currTeam; + } else if (currTeam.getPointsTotal() == currBestTeam.getPointsTotal()){ + if (currTeam.getGoalsTotal() > currBestTeam.getGoalsTotal()){ + currBestTeam = currTeam; + } + } + } + + System.out.println("Winner of the League is " + currBestTeam.getTeamName()); + + } + + public String getLeagueAnnouncement(Game[] theGames){ + + Period thePeriod = Period.between(theGames[0].getTheDateTime().toLocalDate(), + theGames[theGames.length - 1].getTheDateTime().toLocalDate()); + + return "The league is scheduled to run for " + + thePeriod.getMonths() + " month(s), and " + + thePeriod.getDays() + " day(s)\n"; + } + +} diff --git a/src/main/java/ralvhe/practices/12-UsingInheritance_Practice1/src/soccer/Player.java b/src/main/java/ralvhe/practices/12-UsingInheritance_Practice1/src/soccer/Player.java new file mode 100644 index 0000000..6156984 --- /dev/null +++ b/src/main/java/ralvhe/practices/12-UsingInheritance_Practice1/src/soccer/Player.java @@ -0,0 +1,37 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Player { + + private String playerName; + + public Player(String playerName) { + this.playerName = playerName; + } + + public Player() {} + + /** + * @return the playerName + */ + public String getPlayerName() { + return playerName; + } + + /** + * @param playerName the playerName to set + */ + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + +} diff --git a/src/main/java/ralvhe/practices/12-UsingInheritance_Practice1/src/soccer/Team.java b/src/main/java/ralvhe/practices/12-UsingInheritance_Practice1/src/soccer/Team.java new file mode 100644 index 0000000..499003e --- /dev/null +++ b/src/main/java/ralvhe/practices/12-UsingInheritance_Practice1/src/soccer/Team.java @@ -0,0 +1,95 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Team { + + private String teamName; + private Player[] playerArray; + private int pointsTotal; + private int goalsTotal; + + public void incGoalsTotal(int goals){ + this.setGoalsTotal(this.getGoalsTotal() + goals); + } + + public void incPointsTotal(int points){ + this.pointsTotal += points; + } + + public Team(String teamName) { + this.teamName = teamName; + } + + public Team(String teamName, Player[] players) { + this(teamName); + this.playerArray = players; + } + + public Team() {} + + /** + * @return the teamName + */ + public String getTeamName() { + return teamName; + } + + /** + * @param teamName the teamName to set + */ + public void setTeamName(String teamName) { + this.teamName = teamName; + } + + /** + * @return the playerArray + */ + public Player[] getPlayerArray() { + return playerArray; + } + + /** + * @param playerArray the playerArray to set + */ + public void setPlayerArray(Player[] playerArray) { + this.playerArray = playerArray; + } + + /** + * @return the pointsTotal + */ + public int getPointsTotal() { + return pointsTotal; + } + + /** + * @param pointsTotal the pointsTotal to set + */ + public void setPointsTotal(int pointsTotal) { + this.pointsTotal = pointsTotal; + } + + /** + * @return the goalsTotal + */ + public int getGoalsTotal() { + return goalsTotal; + } + + /** + * @param goalsTotal the goalsTotal to set + */ + public void setGoalsTotal(int goalsTotal) { + this.goalsTotal = goalsTotal; + } + +} diff --git a/src/main/java/ralvhe/practices/12-UsingInheritance_Practice1/src/utility/PlayerDatabase.java b/src/main/java/ralvhe/practices/12-UsingInheritance_Practice1/src/utility/PlayerDatabase.java new file mode 100644 index 0000000..a75be77 --- /dev/null +++ b/src/main/java/ralvhe/practices/12-UsingInheritance_Practice1/src/utility/PlayerDatabase.java @@ -0,0 +1,80 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package utility; + +import java.util.*; +import soccer.Player; + + + +/** + * + * @author Administrator + */ +public class PlayerDatabase { + + private ArrayList players; + + public PlayerDatabase(){ + StringTokenizer authorTokens = new StringTokenizer(authorList, ","); + players = new ArrayList(); + while (authorTokens.hasMoreTokens()){ + players.add(new Player(authorTokens.nextToken())); + } + } + + public Player[] getTeam(int numberOfPlayers){ + Player[] teamPlayers = new Player[numberOfPlayers]; + for (int i = 0; i < numberOfPlayers; i++){ + int playerIndex = (int) (Math.random() * players.size()); + teamPlayers[i] = players.get(playerIndex); + players.remove(playerIndex); + } + return teamPlayers; + + } + + + +String authorList = +"Agatha Christie," + +"Alan Patton," + +"Alexander Solzhenitsyn," + +"Arthur Conan Doyle," + +"Anthony Trollope," + +"Baroness Orczy," + +"Brendan Behan," + +"Brian Moore," + +"Boris Pasternik," + +"Charles Dickens," + +"Charlotte Bronte," + +"Dorothy Parker," + +"Emile Zola," + +"Frank O'Connor," + +"Geoffrey Chaucer," + +"George Eliot," + +"G. K. Chesterton," + +"Graham Green," + +"Henry James," + +"James Joyce," + +"J. M. Synge," + +"J. R. Tolkien," + +"Jane Austin," + +"Leo Tolstoy," + +"Liam O'Flaherty," + +"Marcel Proust," + +"Mark Twain," + +"Oscar Wilde," + +"O. Henry," + +"Samuel Beckett," + +"Sean O'Casey," + +"William Shakespeare," + +"William Makepeace Thackeray," + +"W. B. Yeats," + +"Wilkie Collins"; + +} diff --git a/src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/src/soccer/Game.java b/src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/src/soccer/Game.java new file mode 100644 index 0000000..63cd973 --- /dev/null +++ b/src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/src/soccer/Game.java @@ -0,0 +1,165 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; + +/** + * + * @author Administrator + */ + +public class Game { + + private Team homeTeam; + private Team awayTeam; + private GameEvent[] gameEvents; + private LocalDateTime theDateTime; + + public Game(Team homeTeam, Team awayTeam, LocalDateTime theDateTime) { + this.homeTeam = homeTeam; + this.awayTeam = awayTeam; + this.theDateTime = theDateTime; + } + + public void playGame() { + ArrayList eventList = new ArrayList(); + GameEvent currEvent; + for (int i = 1; i <=90; i++){ + + if (Math.random() > 0.95){ + currEvent = Math.random() > 0.6?new Goal():new Possession(); + currEvent.setTheTeam(Math.random() > 0.5?homeTeam: awayTeam); + currEvent.setThePlayer(currEvent.getTheTeam(). + getPlayerArray()[(int)(Math.random() * currEvent.getTheTeam().getPlayerArray().length)]); + currEvent.setTheTime(i); + eventList.add(currEvent); + //System.out.println(i); + } + this.gameEvents = new GameEvent[eventList.size()]; + eventList.toArray(gameEvents); + + } + } + + public String getDescription() { + + int homeTeamGoals = 0; + int awayTeamGoals = 0; + StringBuilder returnString = new StringBuilder(); + + returnString.append(this.getHomeTeam().getTeamName() + " vs. " + + this.getAwayTeam().getTeamName() + "\n" + + "Date: " + this.getTheDateTime().format(DateTimeFormatter.ISO_LOCAL_DATE) + "\n"); + + for (GameEvent currEvent: this.getEvents()) { + + if (currEvent.getTheTeam()== homeTeam) { + homeTeamGoals++; + homeTeam.incGoalsTotal(1); + } else { + awayTeamGoals++; + awayTeam.incGoalsTotal(1); + } + + /* Practice 12-2. Modify the text printed */ + returnString.append("Goal scored after " + + currEvent.getTheTime() + " mins by " + + currEvent.getThePlayer().getPlayerName() + " of " + + currEvent.getTheTeam().getTeamName() + + "\n"); + } + + if (homeTeamGoals == awayTeamGoals) { + returnString.append("It's a draw!"); + this.homeTeam.incPointsTotal(1); + this.awayTeam.incPointsTotal(1); + } else if (homeTeamGoals > awayTeamGoals) { + returnString.append(homeTeam.getTeamName() + " win"); + this.homeTeam.incPointsTotal(1); + } else { + returnString.append(awayTeam.getTeamName() + " win"); + this.awayTeam.incPointsTotal(1); + } + returnString.append(" (" + homeTeamGoals + " - " + awayTeamGoals + ") \n"); + + return returnString.toString(); + } + + /** + * @return the homeTeam + */ + public Team getHomeTeam() { + return homeTeam; + } + + /** + * @param homeTeam the homeTeam to set + */ + public void setHomeTeam(Team homeTeam) { + this.homeTeam = homeTeam; + } + + /** + * @return the awayTeam + */ + public Team getAwayTeam() { + return awayTeam; + } + + /** + * @param awayTeam the awayTeam to set + */ + public void setAwayTeam(Team awayTeam) { + this.awayTeam = awayTeam; + } + + /** + * @return the gameEvents + */ + public GameEvent[] getEvents() { + return gameEvents; + } + + /** + * @param gameEvents the gameEvents to set + */ + public void setEvents(GameEvent[] gameEvents) { + this.gameEvents = gameEvents; + } + + /** + * @return the localDateTime + */ + public LocalDateTime getLocalDateTime() { + return getTheDateTime(); + } + + /** + * @param theDateTime the localDateTime to set + */ + public void setLocalDateTime(LocalDateTime theDateTime) { + this.setTheDateTime(theDateTime); + } + + /** + * @return the theDateTime + */ + public LocalDateTime getTheDateTime() { + return theDateTime; + } + + /** + * @param theDateTime the theDateTime to set + */ + public void setTheDateTime(LocalDateTime theDateTime) { + this.theDateTime = theDateTime; + } + +} diff --git a/src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/src/soccer/GameEvent.java b/src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/src/soccer/GameEvent.java new file mode 100644 index 0000000..5781656 --- /dev/null +++ b/src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/src/soccer/GameEvent.java @@ -0,0 +1,59 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package soccer; + +/** + * + * @author ralhe + */ +public abstract class GameEvent { + private Team theTeam; + private Player thePlayer; + private double theTime; + + /** + * @return the theTeam + */ + public Team getTheTeam() { + return theTeam; + } + + /** + * @param theTeam the theTeam to set + */ + public void setTheTeam(Team theTeam) { + this.theTeam = theTeam; + } + + /** + * @return the thePlayer + */ + public Player getThePlayer() { + return thePlayer; + } + + /** + * @param thePlayer the thePlayer to set + */ + public void setThePlayer(Player thePlayer) { + this.thePlayer = thePlayer; + } + + /** + * @return the theTime + */ + public double getTheTime() { + return theTime; + } + + /** + * @param theTime the theTime to set + */ + public void setTheTime(double theTime) { + this.theTime = theTime; + } + +} diff --git a/src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/src/soccer/Goal.java b/src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/src/soccer/Goal.java new file mode 100644 index 0000000..1a77635 --- /dev/null +++ b/src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/src/soccer/Goal.java @@ -0,0 +1,21 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ + +/* Practice 12-2. Make this class extend GameEvent */ +public class Goal extends GameEvent{ + + /* Practice 12-2. Start selection here for code in Goal to cut and paste to GameEvent */ + + /* Practice 12-2. End selection here for code in Goal to cut and paste to GameEvent */ + +} diff --git a/src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/src/soccer/League.java b/src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/src/soccer/League.java new file mode 100644 index 0000000..5fbf1e2 --- /dev/null +++ b/src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/src/soccer/League.java @@ -0,0 +1,106 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package soccer; + +import java.time.LocalDateTime; +import java.time.Period; +import java.util.ArrayList; +import java.util.StringTokenizer; +import utility.PlayerDatabase; + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + League theLeague = new League(); + + Team[] theTeams = theLeague.createTeams("The Robins,The Crows,The Swallows", 5); + Game[] theGames = theLeague.createGames(theTeams); + + System.out.println(theLeague.getLeagueAnnouncement(theGames)); + for (Game currGame: theGames){ + currGame.playGame(); + //break; + System.out.println(currGame.getDescription()); + } + + theLeague.showBestTeam(theTeams); + + } + + public Team[] createTeams(String teamNames, int teamSize) { + + PlayerDatabase playerDB = new PlayerDatabase(); + + StringTokenizer teamNameTokens = new StringTokenizer(teamNames, ","); + Team[] theTeams = new Team[teamNameTokens.countTokens()]; + for (int i =0; i < theTeams.length; i++){ + theTeams[i] = new Team(teamNameTokens.nextToken(), playerDB.getTeam(teamSize)); + } + + + return theTeams; + } + + public Game[] createGames(Team[] theTeams) { + int daysBetweenGames = 0; + + ArrayList theGames = new ArrayList(); + + for (Team homeTeam: theTeams){ + for (Team awayTeam: theTeams){ + if (homeTeam != awayTeam) { + daysBetweenGames += 7; + theGames.add(new Game(homeTeam, awayTeam, LocalDateTime.now().plusDays(daysBetweenGames))); + } + + } + } + + + + return (Game[]) theGames.toArray(new Game[1]); + } + + public void showBestTeam(Team[] theTeams) { + Team currBestTeam = theTeams[0]; + System.out.println("\nTeam Points"); + + for (Team currTeam: theTeams){ + System.out.println(currTeam.getTeamName() + " : " + currTeam.getPointsTotal() + " : " + + currTeam.getGoalsTotal()); + currBestTeam = currTeam.getPointsTotal() > currBestTeam.getPointsTotal()?currTeam:currBestTeam; + if (currTeam.getPointsTotal() > currBestTeam.getPointsTotal()){ + currBestTeam = currTeam; + } else if (currTeam.getPointsTotal() == currBestTeam.getPointsTotal()){ + if (currTeam.getGoalsTotal() > currBestTeam.getGoalsTotal()){ + currBestTeam = currTeam; + } + } + } + + System.out.println("Winner of the League is " + currBestTeam.getTeamName()); + + } + + public String getLeagueAnnouncement(Game[] theGames){ + + Period thePeriod = Period.between(theGames[0].getTheDateTime().toLocalDate(), + theGames[theGames.length - 1].getTheDateTime().toLocalDate()); + + return "The league is scheduled to run for " + + thePeriod.getMonths() + " month(s), and " + + thePeriod.getDays() + " day(s)\n"; + } + +} diff --git a/src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/src/soccer/Player.java b/src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/src/soccer/Player.java new file mode 100644 index 0000000..6156984 --- /dev/null +++ b/src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/src/soccer/Player.java @@ -0,0 +1,37 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Player { + + private String playerName; + + public Player(String playerName) { + this.playerName = playerName; + } + + public Player() {} + + /** + * @return the playerName + */ + public String getPlayerName() { + return playerName; + } + + /** + * @param playerName the playerName to set + */ + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + +} diff --git a/src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/src/soccer/Possession.java b/src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/src/soccer/Possession.java new file mode 100644 index 0000000..6c341a7 --- /dev/null +++ b/src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/src/soccer/Possession.java @@ -0,0 +1,14 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package soccer; + +/** + * + * @author ralhe + */ +public class Possession extends GameEvent{ + +} diff --git a/src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/src/soccer/Team.java b/src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/src/soccer/Team.java new file mode 100644 index 0000000..499003e --- /dev/null +++ b/src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/src/soccer/Team.java @@ -0,0 +1,95 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Team { + + private String teamName; + private Player[] playerArray; + private int pointsTotal; + private int goalsTotal; + + public void incGoalsTotal(int goals){ + this.setGoalsTotal(this.getGoalsTotal() + goals); + } + + public void incPointsTotal(int points){ + this.pointsTotal += points; + } + + public Team(String teamName) { + this.teamName = teamName; + } + + public Team(String teamName, Player[] players) { + this(teamName); + this.playerArray = players; + } + + public Team() {} + + /** + * @return the teamName + */ + public String getTeamName() { + return teamName; + } + + /** + * @param teamName the teamName to set + */ + public void setTeamName(String teamName) { + this.teamName = teamName; + } + + /** + * @return the playerArray + */ + public Player[] getPlayerArray() { + return playerArray; + } + + /** + * @param playerArray the playerArray to set + */ + public void setPlayerArray(Player[] playerArray) { + this.playerArray = playerArray; + } + + /** + * @return the pointsTotal + */ + public int getPointsTotal() { + return pointsTotal; + } + + /** + * @param pointsTotal the pointsTotal to set + */ + public void setPointsTotal(int pointsTotal) { + this.pointsTotal = pointsTotal; + } + + /** + * @return the goalsTotal + */ + public int getGoalsTotal() { + return goalsTotal; + } + + /** + * @param goalsTotal the goalsTotal to set + */ + public void setGoalsTotal(int goalsTotal) { + this.goalsTotal = goalsTotal; + } + +} diff --git a/src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/src/utility/PlayerDatabase.java b/src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/src/utility/PlayerDatabase.java new file mode 100644 index 0000000..a75be77 --- /dev/null +++ b/src/main/java/ralvhe/practices/12-UsingInheritance_Practice2/src/utility/PlayerDatabase.java @@ -0,0 +1,80 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package utility; + +import java.util.*; +import soccer.Player; + + + +/** + * + * @author Administrator + */ +public class PlayerDatabase { + + private ArrayList players; + + public PlayerDatabase(){ + StringTokenizer authorTokens = new StringTokenizer(authorList, ","); + players = new ArrayList(); + while (authorTokens.hasMoreTokens()){ + players.add(new Player(authorTokens.nextToken())); + } + } + + public Player[] getTeam(int numberOfPlayers){ + Player[] teamPlayers = new Player[numberOfPlayers]; + for (int i = 0; i < numberOfPlayers; i++){ + int playerIndex = (int) (Math.random() * players.size()); + teamPlayers[i] = players.get(playerIndex); + players.remove(playerIndex); + } + return teamPlayers; + + } + + + +String authorList = +"Agatha Christie," + +"Alan Patton," + +"Alexander Solzhenitsyn," + +"Arthur Conan Doyle," + +"Anthony Trollope," + +"Baroness Orczy," + +"Brendan Behan," + +"Brian Moore," + +"Boris Pasternik," + +"Charles Dickens," + +"Charlotte Bronte," + +"Dorothy Parker," + +"Emile Zola," + +"Frank O'Connor," + +"Geoffrey Chaucer," + +"George Eliot," + +"G. K. Chesterton," + +"Graham Green," + +"Henry James," + +"James Joyce," + +"J. M. Synge," + +"J. R. Tolkien," + +"Jane Austin," + +"Leo Tolstoy," + +"Liam O'Flaherty," + +"Marcel Proust," + +"Mark Twain," + +"Oscar Wilde," + +"O. Henry," + +"Samuel Beckett," + +"Sean O'Casey," + +"William Shakespeare," + +"William Makepeace Thackeray," + +"W. B. Yeats," + +"Wilkie Collins"; + +} diff --git a/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/src/soccer/Game.java b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/src/soccer/Game.java new file mode 100644 index 0000000..14de25b --- /dev/null +++ b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/src/soccer/Game.java @@ -0,0 +1,167 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package soccer; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; + +/** + * + * @author Administrator + */ +public class Game { + + private Team homeTeam; + private Team awayTeam; + private GameEvent[] goals; + private LocalDateTime theDateTime; + + public Game(Team homeTeam, Team awayTeam, LocalDateTime theDateTime) { + this.homeTeam = homeTeam; + this.awayTeam = awayTeam; + this.theDateTime = theDateTime; + } + + public void playGame() { + ArrayList eventList = new ArrayList(); + GameEvent currEvent; + for (int i = 1; i <= 90; i++) { + /* Practice 13-1. Change expression to Math.random > 0.8 */ + if (Math.random() > 0.95) { + currEvent = Math.random() > 0.6 ? new Goal() : new Possession(); + /* Practice 13-1. Change first part of ternary operator toMath.random > 0.8 */ + currEvent.setTheTeam(Math.random() > 0.5 ? homeTeam : awayTeam); + currEvent.setThePlayer(currEvent.getTheTeam(). + getPlayerArray()[(int) (Math.random() * currEvent.getTheTeam().getPlayerArray().length)]); + currEvent.setTheTime(i); + eventList.add(currEvent); + //System.out.println(i); + } + this.goals = new GameEvent[eventList.size()]; + eventList.toArray(goals); + + } + } + + public String getDescription() { + + int homeTeamGoals = 0; + int awayTeamGoals = 0; + StringBuilder returnString = new StringBuilder(); + + returnString.append(this.getHomeTeam().getTeamName() + " vs. " + + this.getAwayTeam().getTeamName() + "\n" + + "Date: " + this.getTheDateTime().format(DateTimeFormatter.ISO_LOCAL_DATE) + "\n"); + + for (GameEvent currEvent : this.getEvents()) { + + /* Practice 13-1. Start if block here to determine if currEvent is a Goal */ + if (currEvent instanceof Goal) { + if (currEvent.getTheTeam() == homeTeam) { + homeTeamGoals++; + homeTeam.incGoalsTotal(1); + } else { + awayTeamGoals++; + awayTeam.incGoalsTotal(1); + } + } + /* Practice 13-1. End if block here to determine if currEvent is a Goal */ + + returnString.append(currEvent + " after " + + currEvent.getTheTime() + " mins by " + + currEvent.getThePlayer().getPlayerName() + " of " + + currEvent.getTheTeam().getTeamName() + + "\n"); + } + + if (homeTeamGoals == awayTeamGoals) { + returnString.append("It's a draw!"); + this.homeTeam.incPointsTotal(1); + this.awayTeam.incPointsTotal(1); + } else if (homeTeamGoals > awayTeamGoals) { + returnString.append(homeTeam.getTeamName() + " win"); + this.homeTeam.incPointsTotal(1); + } else { + returnString.append(awayTeam.getTeamName() + " win"); + this.awayTeam.incPointsTotal(1); + } + returnString.append(" (" + homeTeamGoals + " - " + awayTeamGoals + ") \n"); + + return returnString.toString(); + } + + /** + * @return the homeTeam + */ + public Team getHomeTeam() { + return homeTeam; + } + + /** + * @param homeTeam the homeTeam to set + */ + public void setHomeTeam(Team homeTeam) { + this.homeTeam = homeTeam; + } + + /** + * @return the awayTeam + */ + public Team getAwayTeam() { + return awayTeam; + } + + /** + * @param awayTeam the awayTeam to set + */ + public void setAwayTeam(Team awayTeam) { + this.awayTeam = awayTeam; + } + + /** + * @return the goals + */ + public GameEvent[] getEvents() { + return goals; + } + + /** + * @param goals the goals to set + */ + public void setEvents(GameEvent[] goals) { + this.goals = goals; + } + + /** + * @return the localDateTime + */ + public LocalDateTime getLocalDateTime() { + return getTheDateTime(); + } + + /** + * @param theDateTime the localDateTime to set + */ + public void setLocalDateTime(LocalDateTime theDateTime) { + this.setTheDateTime(theDateTime); + } + + /** + * @return the theDateTime + */ + public LocalDateTime getTheDateTime() { + return theDateTime; + } + + /** + * @param theDateTime the theDateTime to set + */ + public void setTheDateTime(LocalDateTime theDateTime) { + this.theDateTime = theDateTime; + } + +} diff --git a/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/src/soccer/GameEvent.java b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/src/soccer/GameEvent.java new file mode 100644 index 0000000..44885d9 --- /dev/null +++ b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/src/soccer/GameEvent.java @@ -0,0 +1,62 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author ksomervi + */ +public abstract class GameEvent { + + private Team theTeam; + private Player thePlayer; + private double theTime; + + /** + * @return the theTeam + */ + public Team getTheTeam() { + return theTeam; + } + + /** + * @param theTeam the theTeam to set + */ + public void setTheTeam(Team theTeam) { + this.theTeam = theTeam; + } + + /** + * @return the thePlayer + */ + public Player getThePlayer() { + return thePlayer; + } + + /** + * @param thePlayer the thePlayer to set + */ + public void setThePlayer(Player thePlayer) { + this.thePlayer = thePlayer; + } + + /** + * @return the theTime + */ + public double getTheTime() { + return theTime; + } + + /** + * @param theTime the theTime to set + */ + public void setTheTime(double theTime) { + this.theTime = theTime; + } + + +} diff --git a/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/src/soccer/Goal.java b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/src/soccer/Goal.java new file mode 100644 index 0000000..2d24167 --- /dev/null +++ b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/src/soccer/Goal.java @@ -0,0 +1,19 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Goal extends GameEvent { + + /* Practice 13-1. Add a toString() method */ + public String toString(){ + return "Goal scored"; + } +} diff --git a/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/src/soccer/League.java b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/src/soccer/League.java new file mode 100644 index 0000000..5fbf1e2 --- /dev/null +++ b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/src/soccer/League.java @@ -0,0 +1,106 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package soccer; + +import java.time.LocalDateTime; +import java.time.Period; +import java.util.ArrayList; +import java.util.StringTokenizer; +import utility.PlayerDatabase; + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + League theLeague = new League(); + + Team[] theTeams = theLeague.createTeams("The Robins,The Crows,The Swallows", 5); + Game[] theGames = theLeague.createGames(theTeams); + + System.out.println(theLeague.getLeagueAnnouncement(theGames)); + for (Game currGame: theGames){ + currGame.playGame(); + //break; + System.out.println(currGame.getDescription()); + } + + theLeague.showBestTeam(theTeams); + + } + + public Team[] createTeams(String teamNames, int teamSize) { + + PlayerDatabase playerDB = new PlayerDatabase(); + + StringTokenizer teamNameTokens = new StringTokenizer(teamNames, ","); + Team[] theTeams = new Team[teamNameTokens.countTokens()]; + for (int i =0; i < theTeams.length; i++){ + theTeams[i] = new Team(teamNameTokens.nextToken(), playerDB.getTeam(teamSize)); + } + + + return theTeams; + } + + public Game[] createGames(Team[] theTeams) { + int daysBetweenGames = 0; + + ArrayList theGames = new ArrayList(); + + for (Team homeTeam: theTeams){ + for (Team awayTeam: theTeams){ + if (homeTeam != awayTeam) { + daysBetweenGames += 7; + theGames.add(new Game(homeTeam, awayTeam, LocalDateTime.now().plusDays(daysBetweenGames))); + } + + } + } + + + + return (Game[]) theGames.toArray(new Game[1]); + } + + public void showBestTeam(Team[] theTeams) { + Team currBestTeam = theTeams[0]; + System.out.println("\nTeam Points"); + + for (Team currTeam: theTeams){ + System.out.println(currTeam.getTeamName() + " : " + currTeam.getPointsTotal() + " : " + + currTeam.getGoalsTotal()); + currBestTeam = currTeam.getPointsTotal() > currBestTeam.getPointsTotal()?currTeam:currBestTeam; + if (currTeam.getPointsTotal() > currBestTeam.getPointsTotal()){ + currBestTeam = currTeam; + } else if (currTeam.getPointsTotal() == currBestTeam.getPointsTotal()){ + if (currTeam.getGoalsTotal() > currBestTeam.getGoalsTotal()){ + currBestTeam = currTeam; + } + } + } + + System.out.println("Winner of the League is " + currBestTeam.getTeamName()); + + } + + public String getLeagueAnnouncement(Game[] theGames){ + + Period thePeriod = Period.between(theGames[0].getTheDateTime().toLocalDate(), + theGames[theGames.length - 1].getTheDateTime().toLocalDate()); + + return "The league is scheduled to run for " + + thePeriod.getMonths() + " month(s), and " + + thePeriod.getDays() + " day(s)\n"; + } + +} diff --git a/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/src/soccer/Player.java b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/src/soccer/Player.java new file mode 100644 index 0000000..6156984 --- /dev/null +++ b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/src/soccer/Player.java @@ -0,0 +1,37 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Player { + + private String playerName; + + public Player(String playerName) { + this.playerName = playerName; + } + + public Player() {} + + /** + * @return the playerName + */ + public String getPlayerName() { + return playerName; + } + + /** + * @param playerName the playerName to set + */ + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + +} diff --git a/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/src/soccer/Possession.java b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/src/soccer/Possession.java new file mode 100644 index 0000000..b01f6c1 --- /dev/null +++ b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/src/soccer/Possession.java @@ -0,0 +1,19 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author ksomervi + */ +public class Possession extends GameEvent { + + /* Practice 13-1. Add a toString() method */ + public String toString(){ + return "Possession"; + } +} diff --git a/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/src/soccer/Team.java b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/src/soccer/Team.java new file mode 100644 index 0000000..499003e --- /dev/null +++ b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/src/soccer/Team.java @@ -0,0 +1,95 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Team { + + private String teamName; + private Player[] playerArray; + private int pointsTotal; + private int goalsTotal; + + public void incGoalsTotal(int goals){ + this.setGoalsTotal(this.getGoalsTotal() + goals); + } + + public void incPointsTotal(int points){ + this.pointsTotal += points; + } + + public Team(String teamName) { + this.teamName = teamName; + } + + public Team(String teamName, Player[] players) { + this(teamName); + this.playerArray = players; + } + + public Team() {} + + /** + * @return the teamName + */ + public String getTeamName() { + return teamName; + } + + /** + * @param teamName the teamName to set + */ + public void setTeamName(String teamName) { + this.teamName = teamName; + } + + /** + * @return the playerArray + */ + public Player[] getPlayerArray() { + return playerArray; + } + + /** + * @param playerArray the playerArray to set + */ + public void setPlayerArray(Player[] playerArray) { + this.playerArray = playerArray; + } + + /** + * @return the pointsTotal + */ + public int getPointsTotal() { + return pointsTotal; + } + + /** + * @param pointsTotal the pointsTotal to set + */ + public void setPointsTotal(int pointsTotal) { + this.pointsTotal = pointsTotal; + } + + /** + * @return the goalsTotal + */ + public int getGoalsTotal() { + return goalsTotal; + } + + /** + * @param goalsTotal the goalsTotal to set + */ + public void setGoalsTotal(int goalsTotal) { + this.goalsTotal = goalsTotal; + } + +} diff --git a/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/src/utility/PlayerDatabase.java b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/src/utility/PlayerDatabase.java new file mode 100644 index 0000000..a75be77 --- /dev/null +++ b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice1/src/utility/PlayerDatabase.java @@ -0,0 +1,80 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package utility; + +import java.util.*; +import soccer.Player; + + + +/** + * + * @author Administrator + */ +public class PlayerDatabase { + + private ArrayList players; + + public PlayerDatabase(){ + StringTokenizer authorTokens = new StringTokenizer(authorList, ","); + players = new ArrayList(); + while (authorTokens.hasMoreTokens()){ + players.add(new Player(authorTokens.nextToken())); + } + } + + public Player[] getTeam(int numberOfPlayers){ + Player[] teamPlayers = new Player[numberOfPlayers]; + for (int i = 0; i < numberOfPlayers; i++){ + int playerIndex = (int) (Math.random() * players.size()); + teamPlayers[i] = players.get(playerIndex); + players.remove(playerIndex); + } + return teamPlayers; + + } + + + +String authorList = +"Agatha Christie," + +"Alan Patton," + +"Alexander Solzhenitsyn," + +"Arthur Conan Doyle," + +"Anthony Trollope," + +"Baroness Orczy," + +"Brendan Behan," + +"Brian Moore," + +"Boris Pasternik," + +"Charles Dickens," + +"Charlotte Bronte," + +"Dorothy Parker," + +"Emile Zola," + +"Frank O'Connor," + +"Geoffrey Chaucer," + +"George Eliot," + +"G. K. Chesterton," + +"Graham Green," + +"Henry James," + +"James Joyce," + +"J. M. Synge," + +"J. R. Tolkien," + +"Jane Austin," + +"Leo Tolstoy," + +"Liam O'Flaherty," + +"Marcel Proust," + +"Mark Twain," + +"Oscar Wilde," + +"O. Henry," + +"Samuel Beckett," + +"Sean O'Casey," + +"William Shakespeare," + +"William Makepeace Thackeray," + +"W. B. Yeats," + +"Wilkie Collins"; + +} diff --git a/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/src/soccer/Game.java b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/src/soccer/Game.java new file mode 100644 index 0000000..dc2b710 --- /dev/null +++ b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/src/soccer/Game.java @@ -0,0 +1,167 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; + + +/** + * + * @author Administrator + */ +public class Game { + + private Team homeTeam; + private Team awayTeam; + private GameEvent[] goals; + private LocalDateTime theDateTime; + + public Game(Team homeTeam, Team awayTeam, LocalDateTime theDateTime) { + this.homeTeam = homeTeam; + this.awayTeam = awayTeam; + this.theDateTime = theDateTime; + } + + public void playGame() { + ArrayList eventList = new ArrayList(); + GameEvent currEvent; + for (int i = 1; i <=90; i++){ + + if (Math.random() > 0.8){ + + currEvent = Math.random() > 0.8?new Goal():new Possession(); + currEvent.setTheTeam(Math.random() > 0.5?homeTeam: awayTeam); + currEvent.setThePlayer(currEvent.getTheTeam(). + getPlayerArray()[(int)(Math.random() * currEvent.getTheTeam().getPlayerArray().length)]); + currEvent.setTheTime(i); + eventList.add(currEvent); + //System.out.println(i); + } + this.goals = new GameEvent[eventList.size()]; + eventList.toArray(goals); + + } + } + + public String getDescription() { + + int homeTeamGoals = 0; + int awayTeamGoals = 0; + StringBuilder returnString = new StringBuilder(); + + returnString.append(this.getHomeTeam().getTeamName() + " vs. " + + this.getAwayTeam().getTeamName() + "\n" + + "Date: " + this.getTheDateTime().format(DateTimeFormatter.ISO_LOCAL_DATE) + "\n"); + + for (GameEvent currEvent: this.getEvents()) { + + if (currEvent instanceof Goal) { + if (currEvent.getTheTeam()== homeTeam) { + homeTeamGoals++; + homeTeam.incGoalsTotal(1); + } else { + awayTeamGoals++; + awayTeam.incGoalsTotal(1); + } + } + + returnString.append(currEvent +" after " + + currEvent.getTheTime() + " mins by " + + currEvent.getThePlayer().getPlayerName() + " of " + + currEvent.getTheTeam().getTeamName() + + "\n"); + } + + if (homeTeamGoals == awayTeamGoals) { + returnString.append("It's a draw!"); + this.homeTeam.incPointsTotal(1); + this.awayTeam.incPointsTotal(1); + } else if (homeTeamGoals > awayTeamGoals) { + returnString.append(homeTeam.getTeamName() + " win"); + this.homeTeam.incPointsTotal(1); + } else { + returnString.append(awayTeam.getTeamName() + " win"); + this.awayTeam.incPointsTotal(1); + } + returnString.append(" (" + homeTeamGoals + " - " + awayTeamGoals + ") \n"); + + return returnString.toString(); + } + + /** + * @return the homeTeam + */ + public Team getHomeTeam() { + return homeTeam; + } + + /** + * @param homeTeam the homeTeam to set + */ + public void setHomeTeam(Team homeTeam) { + this.homeTeam = homeTeam; + } + + /** + * @return the awayTeam + */ + public Team getAwayTeam() { + return awayTeam; + } + + /** + * @param awayTeam the awayTeam to set + */ + public void setAwayTeam(Team awayTeam) { + this.awayTeam = awayTeam; + } + + /** + * @return the goals + */ + public GameEvent[] getEvents() { + return goals; + } + + /** + * @param goals the goals to set + */ + public void setEvents(GameEvent[] goals) { + this.goals = goals; + } + + /** + * @return the localDateTime + */ + public LocalDateTime getLocalDateTime() { + return getTheDateTime(); + } + + /** + * @param theDateTime the localDateTime to set + */ + public void setLocalDateTime(LocalDateTime theDateTime) { + this.setTheDateTime(theDateTime); + } + + /** + * @return the theDateTime + */ + public LocalDateTime getTheDateTime() { + return theDateTime; + } + + /** + * @param theDateTime the theDateTime to set + */ + public void setTheDateTime(LocalDateTime theDateTime) { + this.theDateTime = theDateTime; + } + +} diff --git a/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/src/soccer/GameEvent.java b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/src/soccer/GameEvent.java new file mode 100644 index 0000000..44885d9 --- /dev/null +++ b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/src/soccer/GameEvent.java @@ -0,0 +1,62 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author ksomervi + */ +public abstract class GameEvent { + + private Team theTeam; + private Player thePlayer; + private double theTime; + + /** + * @return the theTeam + */ + public Team getTheTeam() { + return theTeam; + } + + /** + * @param theTeam the theTeam to set + */ + public void setTheTeam(Team theTeam) { + this.theTeam = theTeam; + } + + /** + * @return the thePlayer + */ + public Player getThePlayer() { + return thePlayer; + } + + /** + * @param thePlayer the thePlayer to set + */ + public void setThePlayer(Player thePlayer) { + this.thePlayer = thePlayer; + } + + /** + * @return the theTime + */ + public double getTheTime() { + return theTime; + } + + /** + * @param theTime the theTime to set + */ + public void setTheTime(double theTime) { + this.theTime = theTime; + } + + +} diff --git a/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/src/soccer/Goal.java b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/src/soccer/Goal.java new file mode 100644 index 0000000..ee69fa8 --- /dev/null +++ b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/src/soccer/Goal.java @@ -0,0 +1,18 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Goal extends GameEvent { + + public String toString(){ + return "Goal scored"; + } +} diff --git a/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/src/soccer/League.java b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/src/soccer/League.java new file mode 100644 index 0000000..1f1b4cd --- /dev/null +++ b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/src/soccer/League.java @@ -0,0 +1,106 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package soccer; + +import java.time.LocalDateTime; +import java.time.Period; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.StringTokenizer; +import utility.PlayerDatabase; + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + League theLeague = new League(); + + Team[] theTeams = theLeague.createTeams("The Robins,The Crows,The Swallows", 5); + Game[] theGames = theLeague.createGames(theTeams); + + System.out.println(theLeague.getLeagueAnnouncement(theGames)); + for (Game currGame: theGames){ + currGame.playGame(); + //break; + System.out.println(currGame.getDescription()); + } + + theLeague.showBestTeam(theTeams); + + } + + public Team[] createTeams(String teamNames, int teamSize) { + + PlayerDatabase playerDB = new PlayerDatabase(); + + StringTokenizer teamNameTokens = new StringTokenizer(teamNames, ","); + Team[] theTeams = new Team[teamNameTokens.countTokens()]; + for (int i =0; i < theTeams.length; i++){ + theTeams[i] = new Team(teamNameTokens.nextToken(), playerDB.getTeam(teamSize)); + } + + + return theTeams; + } + + public Game[] createGames(Team[] theTeams) { + int daysBetweenGames = 0; + + ArrayList theGames = new ArrayList(); + + for (Team homeTeam: theTeams){ + for (Team awayTeam: theTeams){ + if (homeTeam != awayTeam) { + daysBetweenGames += 7; + theGames.add(new Game(homeTeam, awayTeam, LocalDateTime.now().plusDays(daysBetweenGames))); + } + + } + } + + + + return (Game[]) theGames.toArray(new Game[1]); + } + + public void showBestTeam(Team[] theTeams) { + + /* Practice 13-2. Add statement to sort theTeams here */ + Arrays.sort(theTeams); + Team currBestTeam = theTeams[0]; + System.out.println("\nTeam Points"); + + for (Team currTeam: theTeams){ + System.out.println(currTeam.getTeamName() + " : " + currTeam.getPointsTotal() + " : " + + currTeam.getGoalsTotal()); + + /* Practice 13-2. Remove all conditional code starting here */ + + /* Practice 13-2. Remove all conditional code between this point and above comment */ + } + + System.out.println("Winner of the League is " + currBestTeam.getTeamName()); + + } + + public String getLeagueAnnouncement(Game[] theGames){ + + Period thePeriod = Period.between(theGames[0].getTheDateTime().toLocalDate(), + theGames[theGames.length - 1].getTheDateTime().toLocalDate()); + + return "The league is scheduled to run for " + + thePeriod.getMonths() + " month(s), and " + + thePeriod.getDays() + " day(s)\n"; + } + +} diff --git a/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/src/soccer/Player.java b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/src/soccer/Player.java new file mode 100644 index 0000000..6156984 --- /dev/null +++ b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/src/soccer/Player.java @@ -0,0 +1,37 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Player { + + private String playerName; + + public Player(String playerName) { + this.playerName = playerName; + } + + public Player() {} + + /** + * @return the playerName + */ + public String getPlayerName() { + return playerName; + } + + /** + * @param playerName the playerName to set + */ + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + +} diff --git a/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/src/soccer/Possession.java b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/src/soccer/Possession.java new file mode 100644 index 0000000..fbb14f5 --- /dev/null +++ b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/src/soccer/Possession.java @@ -0,0 +1,19 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author ksomervi + */ +public class Possession extends GameEvent { + + public String toString(){ + return "Possession"; + } + +} diff --git a/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/src/soccer/Team.java b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/src/soccer/Team.java new file mode 100644 index 0000000..31f3753 --- /dev/null +++ b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/src/soccer/Team.java @@ -0,0 +1,110 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ + +/* Practice 13-2. Change class declaration so that it implements Comparable */ +public class Team implements Comparable{ + + private String teamName; + private Player[] playerArray; + private int pointsTotal; + private int goalsTotal; + + /* Practice 13-2. Add the compareTo() method here */ + + + public void incGoalsTotal(int goals){ + this.setGoalsTotal(this.getGoalsTotal() + goals); + } + + public void incPointsTotal(int points){ + this.pointsTotal += points; + } + + public Team(String teamName) { + this.teamName = teamName; + } + + public Team(String teamName, Player[] players) { + this(teamName); + this.playerArray = players; + } + + public Team() {} + + /** + * @return the teamName + */ + public String getTeamName() { + return teamName; + } + + /** + * @param teamName the teamName to set + */ + public void setTeamName(String teamName) { + this.teamName = teamName; + } + + /** + * @return the playerArray + */ + public Player[] getPlayerArray() { + return playerArray; + } + + /** + * @param playerArray the playerArray to set + */ + public void setPlayerArray(Player[] playerArray) { + this.playerArray = playerArray; + } + + /** + * @return the pointsTotal + */ + public int getPointsTotal() { + return pointsTotal; + } + + /** + * @param pointsTotal the pointsTotal to set + */ + public void setPointsTotal(int pointsTotal) { + this.pointsTotal = pointsTotal; + } + + /** + * @return the goalsTotal + */ + public int getGoalsTotal() { + return goalsTotal; + } + + /** + * @param goalsTotal the goalsTotal to set + */ + public void setGoalsTotal(int goalsTotal) { + this.goalsTotal = goalsTotal; + } + public int compareTo(Object theTeam) { + int returnValue = -1; + if(this.getPointsTotal() < ((Team)theTeam).getPointsTotal()){ + returnValue = 1; + }else if (this.getPointsTotal() == ((Team)theTeam).getPointsTotal()){ + if(this.getGoalsTotal() < ((Team)theTeam).getGoalsTotal()){ + returnValue = 1; + } + } + return returnValue; + } +} diff --git a/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/src/utility/PlayerDatabase.java b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/src/utility/PlayerDatabase.java new file mode 100644 index 0000000..a75be77 --- /dev/null +++ b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice2/src/utility/PlayerDatabase.java @@ -0,0 +1,80 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package utility; + +import java.util.*; +import soccer.Player; + + + +/** + * + * @author Administrator + */ +public class PlayerDatabase { + + private ArrayList players; + + public PlayerDatabase(){ + StringTokenizer authorTokens = new StringTokenizer(authorList, ","); + players = new ArrayList(); + while (authorTokens.hasMoreTokens()){ + players.add(new Player(authorTokens.nextToken())); + } + } + + public Player[] getTeam(int numberOfPlayers){ + Player[] teamPlayers = new Player[numberOfPlayers]; + for (int i = 0; i < numberOfPlayers; i++){ + int playerIndex = (int) (Math.random() * players.size()); + teamPlayers[i] = players.get(playerIndex); + players.remove(playerIndex); + } + return teamPlayers; + + } + + + +String authorList = +"Agatha Christie," + +"Alan Patton," + +"Alexander Solzhenitsyn," + +"Arthur Conan Doyle," + +"Anthony Trollope," + +"Baroness Orczy," + +"Brendan Behan," + +"Brian Moore," + +"Boris Pasternik," + +"Charles Dickens," + +"Charlotte Bronte," + +"Dorothy Parker," + +"Emile Zola," + +"Frank O'Connor," + +"Geoffrey Chaucer," + +"George Eliot," + +"G. K. Chesterton," + +"Graham Green," + +"Henry James," + +"James Joyce," + +"J. M. Synge," + +"J. R. Tolkien," + +"Jane Austin," + +"Leo Tolstoy," + +"Liam O'Flaherty," + +"Marcel Proust," + +"Mark Twain," + +"Oscar Wilde," + +"O. Henry," + +"Samuel Beckett," + +"Sean O'Casey," + +"William Shakespeare," + +"William Makepeace Thackeray," + +"W. B. Yeats," + +"Wilkie Collins"; + +} diff --git a/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/src/soccer/Game.java b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/src/soccer/Game.java new file mode 100644 index 0000000..42636e2 --- /dev/null +++ b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/src/soccer/Game.java @@ -0,0 +1,170 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; + + +/** + * + * @author Administrator + */ +public class Game { + + private Team homeTeam; + private Team awayTeam; + private GameEvent[] goals; + private LocalDateTime theDateTime; + + public Game(Team homeTeam, Team awayTeam, LocalDateTime theDateTime) { + this.homeTeam = homeTeam; + this.awayTeam = awayTeam; + this.theDateTime = theDateTime; + } + + public void playGame() { + ArrayList eventList = new ArrayList(); + GameEvent currEvent; + for (int i = 1; i <=90; i++){ + + if (Math.random() > 0.8){ + + currEvent = Math.random() > 0.8?new Goal():new Possession(); + currEvent.setTheTeam(Math.random() > 0.5?homeTeam: awayTeam); + currEvent.setThePlayer(currEvent.getTheTeam(). + getPlayerArray()[(int)(Math.random() * currEvent.getTheTeam().getPlayerArray().length)]); + currEvent.setTheTime(i); + eventList.add(currEvent); + //System.out.println(i); + } + this.goals = new GameEvent[eventList.size()]; + eventList.toArray(goals); + + } + } + + public String getDescription() { + + int homeTeamGoals = 0; + int awayTeamGoals = 0; + StringBuilder returnString = new StringBuilder(); + + returnString.append(this.getHomeTeam().getTeamName() + " vs. " + + this.getAwayTeam().getTeamName() + "\n" + + "Date: " + this.getTheDateTime().format(DateTimeFormatter.ISO_LOCAL_DATE) + "\n"); + + for (GameEvent currEvent: this.getEvents()) { + + if (currEvent instanceof Goal) { + if (currEvent.getTheTeam()== homeTeam) { + homeTeamGoals++; + homeTeam.incGoalsTotal(1); + } else { + awayTeamGoals++; + awayTeam.incGoalsTotal(1); + } + + /* Practice 13-3. Add the line to increment goals scored for a player */ + currEvent.getThePlayer().incGoalsScored(); + } + + returnString.append(currEvent +" after " + + currEvent.getTheTime() + " mins by " + + currEvent.getThePlayer().getPlayerName() + " of " + + currEvent.getTheTeam().getTeamName() + + "\n"); + } + + if (homeTeamGoals == awayTeamGoals) { + returnString.append("It's a draw!"); + this.homeTeam.incPointsTotal(1); + this.awayTeam.incPointsTotal(1); + } else if (homeTeamGoals > awayTeamGoals) { + returnString.append(homeTeam.getTeamName() + " win"); + this.homeTeam.incPointsTotal(1); + } else { + returnString.append(awayTeam.getTeamName() + " win"); + this.awayTeam.incPointsTotal(1); + } + returnString.append(" (" + homeTeamGoals + " - " + awayTeamGoals + ") \n"); + + return returnString.toString(); + } + + /** + * @return the homeTeam + */ + public Team getHomeTeam() { + return homeTeam; + } + + /** + * @param homeTeam the homeTeam to set + */ + public void setHomeTeam(Team homeTeam) { + this.homeTeam = homeTeam; + } + + /** + * @return the awayTeam + */ + public Team getAwayTeam() { + return awayTeam; + } + + /** + * @param awayTeam the awayTeam to set + */ + public void setAwayTeam(Team awayTeam) { + this.awayTeam = awayTeam; + } + + /** + * @return the goals + */ + public GameEvent[] getEvents() { + return goals; + } + + /** + * @param goals the goals to set + */ + public void setEvents(GameEvent[] goals) { + this.goals = goals; + } + + /** + * @return the localDateTime + */ + public LocalDateTime getLocalDateTime() { + return getTheDateTime(); + } + + /** + * @param theDateTime the localDateTime to set + */ + public void setLocalDateTime(LocalDateTime theDateTime) { + this.setTheDateTime(theDateTime); + } + + /** + * @return the theDateTime + */ + public LocalDateTime getTheDateTime() { + return theDateTime; + } + + /** + * @param theDateTime the theDateTime to set + */ + public void setTheDateTime(LocalDateTime theDateTime) { + this.theDateTime = theDateTime; + } + +} diff --git a/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/src/soccer/GameEvent.java b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/src/soccer/GameEvent.java new file mode 100644 index 0000000..44885d9 --- /dev/null +++ b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/src/soccer/GameEvent.java @@ -0,0 +1,62 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author ksomervi + */ +public abstract class GameEvent { + + private Team theTeam; + private Player thePlayer; + private double theTime; + + /** + * @return the theTeam + */ + public Team getTheTeam() { + return theTeam; + } + + /** + * @param theTeam the theTeam to set + */ + public void setTheTeam(Team theTeam) { + this.theTeam = theTeam; + } + + /** + * @return the thePlayer + */ + public Player getThePlayer() { + return thePlayer; + } + + /** + * @param thePlayer the thePlayer to set + */ + public void setThePlayer(Player thePlayer) { + this.thePlayer = thePlayer; + } + + /** + * @return the theTime + */ + public double getTheTime() { + return theTime; + } + + /** + * @param theTime the theTime to set + */ + public void setTheTime(double theTime) { + this.theTime = theTime; + } + + +} diff --git a/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/src/soccer/Goal.java b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/src/soccer/Goal.java new file mode 100644 index 0000000..ee69fa8 --- /dev/null +++ b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/src/soccer/Goal.java @@ -0,0 +1,18 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Goal extends GameEvent { + + public String toString(){ + return "Goal scored"; + } +} diff --git a/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/src/soccer/League.java b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/src/soccer/League.java new file mode 100644 index 0000000..ff2d8da --- /dev/null +++ b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/src/soccer/League.java @@ -0,0 +1,113 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package soccer; + +import java.time.LocalDateTime; +import java.time.Period; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.StringTokenizer; +import utility.PlayerDatabase; + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + League theLeague = new League(); + + Team[] theTeams = theLeague.createTeams("The Robins,The Crows,The Swallows", 5); + Game[] theGames = theLeague.createGames(theTeams); + + System.out.println(theLeague.getLeagueAnnouncement(theGames)); + for (Game currGame: theGames){ + currGame.playGame(); + //break; + System.out.println(currGame.getDescription()); + } + + theLeague.showBestTeam(theTeams); + theLeague.showBestPlayers(theTeams); + } + + public Team[] createTeams(String teamNames, int teamSize) { + + PlayerDatabase playerDB = new PlayerDatabase(); + + StringTokenizer teamNameTokens = new StringTokenizer(teamNames, ","); + Team[] theTeams = new Team[teamNameTokens.countTokens()]; + for (int i =0; i < theTeams.length; i++){ + theTeams[i] = new Team(teamNameTokens.nextToken(), playerDB.getTeam(teamSize)); + } + + + return theTeams; + } + + public Game[] createGames(Team[] theTeams) { + int daysBetweenGames = 0; + + ArrayList theGames = new ArrayList(); + + for (Team homeTeam: theTeams){ + for (Team awayTeam: theTeams){ + if (homeTeam != awayTeam) { + daysBetweenGames += 7; + theGames.add(new Game(homeTeam, awayTeam, LocalDateTime.now().plusDays(daysBetweenGames))); + } + + } + } + + + + return (Game[]) theGames.toArray(new Game[1]); + } + + public void showBestTeam(Team[] theTeams) { + + Arrays.sort(theTeams); + Team currBestTeam = theTeams[0]; + System.out.println("\nTeam Points"); + + for (Team currTeam: theTeams){ + System.out.println(currTeam.getTeamName() + " : " + currTeam.getPointsTotal() + " : " + + currTeam.getGoalsTotal()); + + } + + System.out.println("Winner of the League is " + currBestTeam.getTeamName()); + + } + + public String getLeagueAnnouncement(Game[] theGames){ + + Period thePeriod = Period.between(theGames[0].getTheDateTime().toLocalDate(), + theGames[theGames.length - 1].getTheDateTime().toLocalDate()); + + return "The league is scheduled to run for " + + thePeriod.getMonths() + " month(s), and " + + thePeriod.getDays() + " day(s)\n"; + } + public void showBestPlayers(Team[] theTeams){ + ArrayList thePlayers = new ArrayList(); + for(Team currTeam: theTeams){ + thePlayers.addAll(Arrays.asList(currTeam.getPlayerArray())); + System.out.println("\n\nBest Players"); + } + Collections.sort(thePlayers,(p1,p2) -> Double.valueOf(p2.getGoalsScored()).compareTo(Double.valueOf(p1.getGoalsScored()))); + for(Player currPlayer: thePlayers){ + System.out.println(currPlayer.getPlayerName() + " : "+ currPlayer.getGoalsScored()); + } + } +} diff --git a/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/src/soccer/Player.java b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/src/soccer/Player.java new file mode 100644 index 0000000..338e149 --- /dev/null +++ b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/src/soccer/Player.java @@ -0,0 +1,50 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Player { + + private String playerName; + private int goalsScored; + + public void incGoalsScored(){ + this.goalsScored++; + } + + public int getGoalsScored() { + return goalsScored; + } + + public void setGoalsScored(int goalsScored) { + this.goalsScored = goalsScored; + } + + public Player(String playerName) { + this.playerName = playerName; + } + + public Player() {} + + /** + * @return the playerName + */ + public String getPlayerName() { + return playerName; + } + + /** + * @param playerName the playerName to set + */ + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + +} diff --git a/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/src/soccer/Possession.java b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/src/soccer/Possession.java new file mode 100644 index 0000000..fbb14f5 --- /dev/null +++ b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/src/soccer/Possession.java @@ -0,0 +1,19 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author ksomervi + */ +public class Possession extends GameEvent { + + public String toString(){ + return "Possession"; + } + +} diff --git a/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/src/soccer/Team.java b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/src/soccer/Team.java new file mode 100644 index 0000000..4cd9d5a --- /dev/null +++ b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/src/soccer/Team.java @@ -0,0 +1,107 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Team implements Comparable { + + private String teamName; + private Player[] playerArray; + private int pointsTotal; + private int goalsTotal; + + public int compareTo(Object theTeam){ + int returnValue = -1; + if (this.getPointsTotal()< ((Team)theTeam).getPointsTotal()) { + returnValue = 1; + } else if (this.getPointsTotal() == ((Team)theTeam).getPointsTotal()){ + if (this.getGoalsTotal()< ((Team)theTeam).getGoalsTotal()) { + returnValue = 1; + } + } + return returnValue; + } + + public void incGoalsTotal(int goals){ + this.setGoalsTotal(this.getGoalsTotal() + goals); + } + + public void incPointsTotal(int points){ + this.pointsTotal += points; + } + + public Team(String teamName) { + this.teamName = teamName; + } + + public Team(String teamName, Player[] players) { + this(teamName); + this.playerArray = players; + } + + public Team() {} + + /** + * @return the teamName + */ + public String getTeamName() { + return teamName; + } + + /** + * @param teamName the teamName to set + */ + public void setTeamName(String teamName) { + this.teamName = teamName; + } + + /** + * @return the playerArray + */ + public Player[] getPlayerArray() { + return playerArray; + } + + /** + * @param playerArray the playerArray to set + */ + public void setPlayerArray(Player[] playerArray) { + this.playerArray = playerArray; + } + + /** + * @return the pointsTotal + */ + public int getPointsTotal() { + return pointsTotal; + } + + /** + * @param pointsTotal the pointsTotal to set + */ + public void setPointsTotal(int pointsTotal) { + this.pointsTotal = pointsTotal; + } + + /** + * @return the goalsTotal + */ + public int getGoalsTotal() { + return goalsTotal; + } + + /** + * @param goalsTotal the goalsTotal to set + */ + public void setGoalsTotal(int goalsTotal) { + this.goalsTotal = goalsTotal; + } + +} diff --git a/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/src/utility/PlayerDatabase.java b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/src/utility/PlayerDatabase.java new file mode 100644 index 0000000..a75be77 --- /dev/null +++ b/src/main/java/ralvhe/practices/13-UsingInterfaces_Practice3/src/utility/PlayerDatabase.java @@ -0,0 +1,80 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package utility; + +import java.util.*; +import soccer.Player; + + + +/** + * + * @author Administrator + */ +public class PlayerDatabase { + + private ArrayList players; + + public PlayerDatabase(){ + StringTokenizer authorTokens = new StringTokenizer(authorList, ","); + players = new ArrayList(); + while (authorTokens.hasMoreTokens()){ + players.add(new Player(authorTokens.nextToken())); + } + } + + public Player[] getTeam(int numberOfPlayers){ + Player[] teamPlayers = new Player[numberOfPlayers]; + for (int i = 0; i < numberOfPlayers; i++){ + int playerIndex = (int) (Math.random() * players.size()); + teamPlayers[i] = players.get(playerIndex); + players.remove(playerIndex); + } + return teamPlayers; + + } + + + +String authorList = +"Agatha Christie," + +"Alan Patton," + +"Alexander Solzhenitsyn," + +"Arthur Conan Doyle," + +"Anthony Trollope," + +"Baroness Orczy," + +"Brendan Behan," + +"Brian Moore," + +"Boris Pasternik," + +"Charles Dickens," + +"Charlotte Bronte," + +"Dorothy Parker," + +"Emile Zola," + +"Frank O'Connor," + +"Geoffrey Chaucer," + +"George Eliot," + +"G. K. Chesterton," + +"Graham Green," + +"Henry James," + +"James Joyce," + +"J. M. Synge," + +"J. R. Tolkien," + +"Jane Austin," + +"Leo Tolstoy," + +"Liam O'Flaherty," + +"Marcel Proust," + +"Mark Twain," + +"Oscar Wilde," + +"O. Henry," + +"Samuel Beckett," + +"Sean O'Casey," + +"William Shakespeare," + +"William Makepeace Thackeray," + +"W. B. Yeats," + +"Wilkie Collins"; + +} diff --git a/src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/soccer/Game.java b/src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/soccer/Game.java new file mode 100644 index 0000000..a416ca8 --- /dev/null +++ b/src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/soccer/Game.java @@ -0,0 +1,168 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; + + +/** + * + * @author Administrator + */ +public class Game { + + private Team homeTeam; + private Team awayTeam; + private GameEvent[] goals; + private LocalDateTime theDateTime; + + public Game(Team homeTeam, Team awayTeam, LocalDateTime theDateTime) { + this.homeTeam = homeTeam; + this.awayTeam = awayTeam; + this.theDateTime = theDateTime; + } + + public void playGame() { + ArrayList eventList = new ArrayList(); + GameEvent currEvent; + for (int i = 1; i <=90; i++){ + + if (Math.random() > 0.8){ + + currEvent = Math.random() > 0.8?new Goal():new Possession(); + currEvent.setTheTeam(Math.random() > 0.5?homeTeam: awayTeam); + currEvent.setThePlayer(currEvent.getTheTeam(). + getPlayerArray()[(int)(Math.random() * currEvent.getTheTeam().getPlayerArray().length)]); + currEvent.setTheTime(i); + eventList.add(currEvent); + //System.out.println(i); + } + this.goals = new GameEvent[eventList.size()]; + eventList.toArray(goals); + + } + } + + public String getDescription() { + + int homeTeamGoals = 0; + int awayTeamGoals = 0; + StringBuilder returnString = new StringBuilder(); + + returnString.append(this.getHomeTeam().getTeamName() + " vs. " + + this.getAwayTeam().getTeamName() + "\n" + + "Date: " + this.getTheDateTime().format(DateTimeFormatter.ISO_LOCAL_DATE) + "\n"); + + for (GameEvent currEvent: this.getEvents()) { + + if (currEvent instanceof Goal) { + if (currEvent.getTheTeam()== homeTeam) { + homeTeamGoals++; + homeTeam.incGoalsTotal(1); + } else { + awayTeamGoals++; + awayTeam.incGoalsTotal(1); + } + currEvent.getThePlayer().incGoalsScored(); + } + + returnString.append(currEvent +" after " + + currEvent.getTheTime() + " mins by " + + currEvent.getThePlayer().getPlayerName() + " of " + + currEvent.getTheTeam().getTeamName() + + "\n"); + } + + if (homeTeamGoals == awayTeamGoals) { + returnString.append("It's a draw!"); + this.homeTeam.incPointsTotal(1); + this.awayTeam.incPointsTotal(1); + } else if (homeTeamGoals > awayTeamGoals) { + returnString.append(homeTeam.getTeamName() + " win"); + this.homeTeam.incPointsTotal(1); + } else { + returnString.append(awayTeam.getTeamName() + " win"); + this.awayTeam.incPointsTotal(1); + } + returnString.append(" (" + homeTeamGoals + " - " + awayTeamGoals + ") \n"); + + return returnString.toString(); + } + + /** + * @return the homeTeam + */ + public Team getHomeTeam() { + return homeTeam; + } + + /** + * @param homeTeam the homeTeam to set + */ + public void setHomeTeam(Team homeTeam) { + this.homeTeam = homeTeam; + } + + /** + * @return the awayTeam + */ + public Team getAwayTeam() { + return awayTeam; + } + + /** + * @param awayTeam the awayTeam to set + */ + public void setAwayTeam(Team awayTeam) { + this.awayTeam = awayTeam; + } + + /** + * @return the goals + */ + public GameEvent[] getEvents() { + return goals; + } + + /** + * @param goals the goals to set + */ + public void setEvents(GameEvent[] goals) { + this.goals = goals; + } + + /** + * @return the localDateTime + */ + public LocalDateTime getLocalDateTime() { + return getTheDateTime(); + } + + /** + * @param theDateTime the localDateTime to set + */ + public void setLocalDateTime(LocalDateTime theDateTime) { + this.setTheDateTime(theDateTime); + } + + /** + * @return the theDateTime + */ + public LocalDateTime getTheDateTime() { + return theDateTime; + } + + /** + * @param theDateTime the theDateTime to set + */ + public void setTheDateTime(LocalDateTime theDateTime) { + this.theDateTime = theDateTime; + } + +} diff --git a/src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/soccer/GameEvent.java b/src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/soccer/GameEvent.java new file mode 100644 index 0000000..44885d9 --- /dev/null +++ b/src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/soccer/GameEvent.java @@ -0,0 +1,62 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author ksomervi + */ +public abstract class GameEvent { + + private Team theTeam; + private Player thePlayer; + private double theTime; + + /** + * @return the theTeam + */ + public Team getTheTeam() { + return theTeam; + } + + /** + * @param theTeam the theTeam to set + */ + public void setTheTeam(Team theTeam) { + this.theTeam = theTeam; + } + + /** + * @return the thePlayer + */ + public Player getThePlayer() { + return thePlayer; + } + + /** + * @param thePlayer the thePlayer to set + */ + public void setThePlayer(Player thePlayer) { + this.thePlayer = thePlayer; + } + + /** + * @return the theTime + */ + public double getTheTime() { + return theTime; + } + + /** + * @param theTime the theTime to set + */ + public void setTheTime(double theTime) { + this.theTime = theTime; + } + + +} diff --git a/src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/soccer/Goal.java b/src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/soccer/Goal.java new file mode 100644 index 0000000..ee69fa8 --- /dev/null +++ b/src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/soccer/Goal.java @@ -0,0 +1,18 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Goal extends GameEvent { + + public String toString(){ + return "Goal scored"; + } +} diff --git a/src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/soccer/League.java b/src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/soccer/League.java new file mode 100644 index 0000000..99437d2 --- /dev/null +++ b/src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/soccer/League.java @@ -0,0 +1,124 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package soccer; + +import java.time.LocalDateTime; +import java.time.Period; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.StringTokenizer; +import utility.PlayerDatabase; +import utility.PlayerDatabaseException; + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + League theLeague = new League(); + + /* Practice 14-1, Step 3a. Start a try block here */ + try { + /* Practice 14-1, Step 2a. Modify the line below to add more teams and players */ + Team[] theTeams = theLeague.createTeams("The Robins,The Crows,The Swallows,The Owls", 11); + Game[] theGames = theLeague.createGames(theTeams); + + System.out.println(theLeague.getLeagueAnnouncement(theGames)); + for (Game currGame : theGames) { + currGame.playGame(); + //break; + System.out.println(currGame.getDescription()); + } + + theLeague.showBestTeam(theTeams); + theLeague.showBestPlayers(theTeams); + + /* Practice 14-1, Step 3b. Close the try block here */ + } /* Practice 14-1, Step 3d. Add a catch block here */ catch (Exception e) { + e.printStackTrace(System.err); + } + } + + /* Practice 14-1, Step 6e. Modify the signature to throw PlayerDatabaseException */ + public Team[] createTeams(String teamNames, int teamSize) throws PlayerDatabaseException{ + + PlayerDatabase playerDB = new PlayerDatabase(); + + StringTokenizer teamNameTokens = new StringTokenizer(teamNames, ","); + Team[] theTeams = new Team[teamNameTokens.countTokens()]; + for (int i = 0; i < theTeams.length; i++) { + theTeams[i] = new Team(teamNameTokens.nextToken(), playerDB.getTeam(teamSize)); + } + + return theTeams; + } + + public Game[] createGames(Team[] theTeams) { + int daysBetweenGames = 0; + + ArrayList theGames = new ArrayList(); + + for (Team homeTeam : theTeams) { + for (Team awayTeam : theTeams) { + if (homeTeam != awayTeam) { + daysBetweenGames += 7; + theGames.add(new Game(homeTeam, awayTeam, LocalDateTime.now().plusDays(daysBetweenGames))); + } + + } + } + + return (Game[]) theGames.toArray(new Game[1]); + } + + public void showBestTeam(Team[] theTeams) { + + Arrays.sort(theTeams); + Team currBestTeam = theTeams[0]; + System.out.println("\nTeam Points"); + + for (Team currTeam : theTeams) { + System.out.println(currTeam.getTeamName() + " : " + currTeam.getPointsTotal() + " : " + + currTeam.getGoalsTotal()); + + } + + System.out.println("Winner of the League is " + currBestTeam.getTeamName()); + + } + + public String getLeagueAnnouncement(Game[] theGames) { + + Period thePeriod = Period.between(theGames[0].getTheDateTime().toLocalDate(), + theGames[theGames.length - 1].getTheDateTime().toLocalDate()); + + return "The league is scheduled to run for " + + thePeriod.getMonths() + " month(s), and " + + thePeriod.getDays() + " day(s)\n"; + } + + public void showBestPlayers(Team[] theTeams) { + ArrayList thePlayers = new ArrayList(); + for (Team currTeam : theTeams) { + thePlayers.addAll(Arrays.asList(currTeam.getPlayerArray())); + } + + Collections.sort(thePlayers, (p1, p2) -> Double.valueOf(p2.getGoalsScored()).compareTo(Double.valueOf(p1.getGoalsScored()))); + + System.out.println("\n\nBest Players"); + for (Player currPlayer : thePlayers) { + System.out.println(currPlayer.getPlayerName() + " : " + currPlayer.getGoalsScored()); + } + } + +} diff --git a/src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/soccer/Player.java b/src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/soccer/Player.java new file mode 100644 index 0000000..729ea8f --- /dev/null +++ b/src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/soccer/Player.java @@ -0,0 +1,49 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Player { + + private String playerName; + private int goalsScored; + + public void incGoalsScored() { + this.goalsScored++; + } + + public Player(String playerName) { + this.playerName = playerName; + } + + public Player() {} + + /** + * @return the playerName + */ + public String getPlayerName() { + return playerName; + } + + /** + * @param playerName the playerName to set + */ + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + + /** + * @return the goalsScored + */ + public int getGoalsScored() { + return goalsScored; + } + +} diff --git a/src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/soccer/Possession.java b/src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/soccer/Possession.java new file mode 100644 index 0000000..fbb14f5 --- /dev/null +++ b/src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/soccer/Possession.java @@ -0,0 +1,19 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author ksomervi + */ +public class Possession extends GameEvent { + + public String toString(){ + return "Possession"; + } + +} diff --git a/src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/soccer/Team.java b/src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/soccer/Team.java new file mode 100644 index 0000000..4cd9d5a --- /dev/null +++ b/src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/soccer/Team.java @@ -0,0 +1,107 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package soccer; + +/** + * + * @author Administrator + */ +public class Team implements Comparable { + + private String teamName; + private Player[] playerArray; + private int pointsTotal; + private int goalsTotal; + + public int compareTo(Object theTeam){ + int returnValue = -1; + if (this.getPointsTotal()< ((Team)theTeam).getPointsTotal()) { + returnValue = 1; + } else if (this.getPointsTotal() == ((Team)theTeam).getPointsTotal()){ + if (this.getGoalsTotal()< ((Team)theTeam).getGoalsTotal()) { + returnValue = 1; + } + } + return returnValue; + } + + public void incGoalsTotal(int goals){ + this.setGoalsTotal(this.getGoalsTotal() + goals); + } + + public void incPointsTotal(int points){ + this.pointsTotal += points; + } + + public Team(String teamName) { + this.teamName = teamName; + } + + public Team(String teamName, Player[] players) { + this(teamName); + this.playerArray = players; + } + + public Team() {} + + /** + * @return the teamName + */ + public String getTeamName() { + return teamName; + } + + /** + * @param teamName the teamName to set + */ + public void setTeamName(String teamName) { + this.teamName = teamName; + } + + /** + * @return the playerArray + */ + public Player[] getPlayerArray() { + return playerArray; + } + + /** + * @param playerArray the playerArray to set + */ + public void setPlayerArray(Player[] playerArray) { + this.playerArray = playerArray; + } + + /** + * @return the pointsTotal + */ + public int getPointsTotal() { + return pointsTotal; + } + + /** + * @param pointsTotal the pointsTotal to set + */ + public void setPointsTotal(int pointsTotal) { + this.pointsTotal = pointsTotal; + } + + /** + * @return the goalsTotal + */ + public int getGoalsTotal() { + return goalsTotal; + } + + /** + * @param goalsTotal the goalsTotal to set + */ + public void setGoalsTotal(int goalsTotal) { + this.goalsTotal = goalsTotal; + } + +} diff --git a/src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/utility/PlayerDatabase.java b/src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/utility/PlayerDatabase.java new file mode 100644 index 0000000..eb42ce5 --- /dev/null +++ b/src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/utility/PlayerDatabase.java @@ -0,0 +1,84 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package utility; + +import java.util.*; +import soccer.Player; + +/** + * + * @author Administrator + */ +public class PlayerDatabase { + + private ArrayList players; + + public PlayerDatabase() { + StringTokenizer authorTokens = new StringTokenizer(authorList, ","); + players = new ArrayList(); + while (authorTokens.hasMoreTokens()) { + players.add(new Player(authorTokens.nextToken())); + } + } + + /* Practice 14-1, Step 6d. Modify the signature to throw PlayerDatabaseException */ + public Player[] getTeam(int numberOfPlayers) throws PlayerDatabaseException{ + Player[] teamPlayers = new Player[numberOfPlayers]; + for (int i = 0; i < numberOfPlayers; i++) { + int playerIndex = (int) (Math.random() * players.size()); + + /* Practice 14-1, Step 6b. Start a try block here */ + try { + teamPlayers[i] = players.get(playerIndex); + players.remove(playerIndex); + /* Practice 14-1, Step 6b. End the try block here */ + } catch (IndexOutOfBoundsException ie) { + /* Practice 14-1, Step 6c. Add catch block here */ + throw new PlayerDatabaseException("Not enough players in the database for the teams requested."); + } + } + return teamPlayers; + + } + + String authorList + = "Agatha Christie," + + "Alan Patton," + + "Alexander Solzhenitsyn," + + "Arthur Conan Doyle," + + "Anthony Trollope," + + "Baroness Orczy," + + "Brendan Behan," + + "Brian Moore," + + "Boris Pasternik," + + "Charles Dickens," + + "Charlotte Bronte," + + "Dorothy Parker," + + "Emile Zola," + + "Frank O'Connor," + + "Geoffrey Chaucer," + + "George Eliot," + + "G. K. Chesterton," + + "Graham Green," + + "Henry James," + + "James Joyce," + + "J. M. Synge," + + "J. R. Tolkien," + + "Jane Austin," + + "Leo Tolstoy," + + "Liam O'Flaherty," + + "Marcel Proust," + + "Mark Twain," + + "Oscar Wilde," + + "O. Henry," + + "Samuel Beckett," + + "Sean O'Casey," + + "William Shakespeare," + + "William Makepeace Thackeray," + + "W. B. Yeats," + + "Wilkie Collins"; + +} diff --git a/src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/utility/PlayerDatabaseException.java b/src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/utility/PlayerDatabaseException.java new file mode 100644 index 0000000..5e1e8e0 --- /dev/null +++ b/src/main/java/ralvhe/practices/14-Exceptions_Practice1/src/utility/PlayerDatabaseException.java @@ -0,0 +1,20 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package utility; + +/** + * + * @author ralhe + */ +public class PlayerDatabaseException extends Exception{ + public PlayerDatabaseException(){ + + } + public PlayerDatabaseException(String message){ + super(message); + } + +}