diff --git a/pom.xml b/pom.xml index f970892..134de9d 100644 --- a/pom.xml +++ b/pom.xml @@ -32,5 +32,7 @@ 1.8 1.8 + 1.8 + 1.8 \ No newline at end of file diff --git a/src/main/java/andresnc/practices/practice_06_objectsclasses_1/soccer/Game.java b/src/main/java/andresnc/practices/practice_06_objectsclasses_1/soccer/Game.java new file mode 100644 index 0000000..6f8ce95 --- /dev/null +++ b/src/main/java/andresnc/practices/practice_06_objectsclasses_1/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 andresnc.practices.practice_06_objectsclasses_1.soccer; + +/** + * + * @author andres + */ +public class Game { + public Team homeTeam; + public Team awayTeam; + public Goal[] goals; +} diff --git a/src/main/java/andresnc/practices/practice_06_objectsclasses_1/soccer/Goal.java b/src/main/java/andresnc/practices/practice_06_objectsclasses_1/soccer/Goal.java new file mode 100644 index 0000000..4d8fcc9 --- /dev/null +++ b/src/main/java/andresnc/practices/practice_06_objectsclasses_1/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 andresnc.practices.practice_06_objectsclasses_1.soccer; + +/** + * + * @author andres + */ +public class Goal { + public Team theTeam; + public Player thePLayer; + public double theTime; +} diff --git a/src/main/java/andresnc/practices/practice_06_objectsclasses_1/soccer/League.java b/src/main/java/andresnc/practices/practice_06_objectsclasses_1/soccer/League.java new file mode 100644 index 0000000..640e93d --- /dev/null +++ b/src/main/java/andresnc/practices/practice_06_objectsclasses_1/soccer/League.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 andresnc.practices.practice_06_objectsclasses_1.soccer; + +/** + * + * @author andres + */ +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; + + for (Player thePlayer: team1.playerArray) { + System.out.println(thePlayer.playerName); + } + + 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: team2.playerArray) { + System.out.println(thePlayer.playerName); + } + + } + +} diff --git a/src/main/java/andresnc/practices/practice_06_objectsclasses_1/soccer/Player.java b/src/main/java/andresnc/practices/practice_06_objectsclasses_1/soccer/Player.java new file mode 100644 index 0000000..aff64ba --- /dev/null +++ b/src/main/java/andresnc/practices/practice_06_objectsclasses_1/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 andresnc.practices.practice_06_objectsclasses_1.soccer; + +/** + * + * @author andres + */ +public class Player { + public String playerName; +} diff --git a/src/main/java/andresnc/practices/practice_06_objectsclasses_1/soccer/Team.java b/src/main/java/andresnc/practices/practice_06_objectsclasses_1/soccer/Team.java new file mode 100644 index 0000000..e7400d2 --- /dev/null +++ b/src/main/java/andresnc/practices/practice_06_objectsclasses_1/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 andresnc.practices.practice_06_objectsclasses_1.soccer; + +/** + * + * @author andres + */ +public class Team { + public String teamName; + public Player[] playerArray; +} diff --git a/src/main/java/jasgz/Main.java b/src/main/java/jasgz/Main.java deleted file mode 100644 index 55aa562..0000000 --- a/src/main/java/jasgz/Main.java +++ /dev/null @@ -1,10 +0,0 @@ -package jasgz; - -public class Main { - - public static void main(String[] args) { - // TODO Auto-generated method stub - - } - -} diff --git a/src/main/java/jasgz/practices/practice_06_objectsclasses_1/soccer/Game.java b/src/main/java/jasgz/practices/practice_06_objectsclasses_1/soccer/Game.java new file mode 100644 index 0000000..835432b --- /dev/null +++ b/src/main/java/jasgz/practices/practice_06_objectsclasses_1/soccer/Game.java @@ -0,0 +1,9 @@ + +package jasgz.practices.practice_06_objectsclasses_1.soccer; + +public class Game { + public Team homeTeam; + public Team awayTeam; + public Goal[] goals; + +} diff --git a/src/main/java/jasgz/practices/practice_06_objectsclasses_1/soccer/Goal.java b/src/main/java/jasgz/practices/practice_06_objectsclasses_1/soccer/Goal.java new file mode 100644 index 0000000..1e75d17 --- /dev/null +++ b/src/main/java/jasgz/practices/practice_06_objectsclasses_1/soccer/Goal.java @@ -0,0 +1,7 @@ +package jasgz.practices.practice_06_objectsclasses_1.soccer; + +public class Goal { + public Team theTeam; + public Player thePlayer; + public double theTime; +} diff --git a/src/main/java/jasgz/practices/practice_06_objectsclasses_1/soccer/League.java b/src/main/java/jasgz/practices/practice_06_objectsclasses_1/soccer/League.java new file mode 100644 index 0000000..28ccd48 --- /dev/null +++ b/src/main/java/jasgz/practices/practice_06_objectsclasses_1/soccer/League.java @@ -0,0 +1,40 @@ +package jasgz.practices.practice_06_objectsclasses_1.soccer; + +public class League { + + public static void main(String []args){ + Player player1= new Player(); + player1.playerName="George Eliot";//PLAYER OF THE TEAM ONE "THE GREENS" + + Player player2= new Player(); + player2.playerName="Graham Greene";//PLAYER OF THE TEAM ONE "THE GREENS" + + Player player3= new Player(); + player3.playerName="Geoffrey Chaucer";//PLAYER OF THE TEAM ONE "THE GREENS" + + Player[]thePlayers={player1, player2, player3}; + + Team team1= new Team();//TEAM ONE + team1.teamName="The Greens"; + team1.playerArray= thePlayers; + + Team team2=new Team();//TEAM TWO + team2.teamName="The Reds"; + team2.playerArray= new Player[3]; + team2.playerArray[0]=new Player(); + team2.playerArray[0].playerName="Robert Service";//PLAYER OF THE TEAM "THE REDS" + team2.playerArray[1]= new Player(); + team2.playerArray[1].playerName="Robbie Burns";//PLAYER OF THE TEAM "THE REDS" + team2.playerArray[2]= new Player(); + team2.playerArray[2].playerName="Rafael Sabatini";//PLAYER OF THE TEAM "THE REDS" + + player1.playerName="Robert Service";//REMOVING PLAYER WITH THE NAME "GEORGE ELIOT" + 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/jasgz/practices/practice_06_objectsclasses_1/soccer/Player.java b/src/main/java/jasgz/practices/practice_06_objectsclasses_1/soccer/Player.java new file mode 100644 index 0000000..b843cd2 --- /dev/null +++ b/src/main/java/jasgz/practices/practice_06_objectsclasses_1/soccer/Player.java @@ -0,0 +1,5 @@ +package jasgz.practices.practice_06_objectsclasses_1.soccer; + +public class Player { +public String playerName; +} diff --git a/src/main/java/jasgz/practices/practice_06_objectsclasses_1/soccer/Team.java b/src/main/java/jasgz/practices/practice_06_objectsclasses_1/soccer/Team.java new file mode 100644 index 0000000..55663ca --- /dev/null +++ b/src/main/java/jasgz/practices/practice_06_objectsclasses_1/soccer/Team.java @@ -0,0 +1,10 @@ +package jasgz.practices.practice_06_objectsclasses_1.soccer; + +public class Team { + public String teamName; + public Player[] playerArray; + + + + +} diff --git a/src/main/java/jasgz/practices/practice_06_objectsclasses_2/soccer/Game.java b/src/main/java/jasgz/practices/practice_06_objectsclasses_2/soccer/Game.java new file mode 100644 index 0000000..638b0ce --- /dev/null +++ b/src/main/java/jasgz/practices/practice_06_objectsclasses_2/soccer/Game.java @@ -0,0 +1,7 @@ +package jasgz.practices.practice_06_objectsclasses_2.soccer; + +public class Game { + public Team homeTeam; + public Team awayTeam; + public Goal[] goals; +} diff --git a/src/main/java/jasgz/practices/practice_06_objectsclasses_2/soccer/Goal.java b/src/main/java/jasgz/practices/practice_06_objectsclasses_2/soccer/Goal.java new file mode 100644 index 0000000..b9fd7dd --- /dev/null +++ b/src/main/java/jasgz/practices/practice_06_objectsclasses_2/soccer/Goal.java @@ -0,0 +1,8 @@ +package jasgz.practices.practice_06_objectsclasses_2.soccer; + +public class Goal { + public Team theTeam; + public Player thePlayer; + public double theTime; + +} diff --git a/src/main/java/jasgz/practices/practice_06_objectsclasses_2/soccer/League.java b/src/main/java/jasgz/practices/practice_06_objectsclasses_2/soccer/League.java new file mode 100644 index 0000000..e848e2d --- /dev/null +++ b/src/main/java/jasgz/practices/practice_06_objectsclasses_2/soccer/League.java @@ -0,0 +1,54 @@ +package jasgz.practices.practice_06_objectsclasses_2.soccer; + +public class League { + + public static void main(String[] args) { + Game currGame = new Game(); + + Player player1 = new Player(); + player1.playerName = "George Eliot";//PLAYER OF THE TEAM ONE "THE GREENS" + + Player player2 = new Player(); + player2.playerName = "Graham Greene";//PLAYER OF THE TEAM ONE "THE GREENS" + + Player player3 = new Player(); + player3.playerName = "Geoffrey Chaucer";//PLAYER OF THE TEAM ONE "THE GREENS" + + Player[] thePlayers = {player1, player2, player3}; + + Team team1 = new Team();//TEAM ONE + team1.teamName = "The Greens"; + team1.playerArray = thePlayers; + + Team team2 = new Team();//TEAM TWO + team2.teamName = "The Reds"; + team2.playerArray = new Player[3]; + team2.playerArray[0] = new Player(); + team2.playerArray[0].playerName = "Robert Service";//PLAYER OF THE TEAM "THE REDS" + team2.playerArray[1] = new Player(); + team2.playerArray[1].playerName = "Robbie Burns";//PLAYER OF THE TEAM "THE REDS" + team2.playerArray[2] = new Player(); + team2.playerArray[2].playerName = "Rafael Sabatini";//PLAYER OF THE TEAM "THE REDS" + + + player1.playerName = "Robert Service";//REMOVING PLAYER WITH THE NAME "GEORGE ELIOT" + + 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); + + + + } +} diff --git a/src/main/java/jasgz/practices/practice_06_objectsclasses_2/soccer/Player.java b/src/main/java/jasgz/practices/practice_06_objectsclasses_2/soccer/Player.java new file mode 100644 index 0000000..233bd8f --- /dev/null +++ b/src/main/java/jasgz/practices/practice_06_objectsclasses_2/soccer/Player.java @@ -0,0 +1,5 @@ +package jasgz.practices.practice_06_objectsclasses_2.soccer; + +public class Player { + public String playerName; +} diff --git a/src/main/java/jasgz/practices/practice_06_objectsclasses_2/soccer/Team.java b/src/main/java/jasgz/practices/practice_06_objectsclasses_2/soccer/Team.java new file mode 100644 index 0000000..44966dc --- /dev/null +++ b/src/main/java/jasgz/practices/practice_06_objectsclasses_2/soccer/Team.java @@ -0,0 +1,6 @@ +package jasgz.practices.practice_06_objectsclasses_2.soccer; + +public class Team { + public String teamName; + public Player[] playerArray; +} diff --git a/src/main/java/jorgeantonio/exercises/ex03_1_exercise/ShoppingCart.java b/src/main/java/jorgeantonio/exercises/ex03_1_exercise/ShoppingCart.java new file mode 100644 index 0000000..56e45b7 --- /dev/null +++ b/src/main/java/jorgeantonio/exercises/ex03_1_exercise/ShoppingCart.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 jorgeantonio.exercises.ex03_1_exercise; + +/** + * + * @author Hp + */ +public class ShoppingCart { + public static void main (String[] args){ + System.out.println("Este es mi mensaje"); + } +} diff --git a/src/main/java/jorgeantonio/exercises/ex04_1_exercise/ShoppingCart.java b/src/main/java/jorgeantonio/exercises/ex04_1_exercise/ShoppingCart.java new file mode 100644 index 0000000..91b6c8d --- /dev/null +++ b/src/main/java/jorgeantonio/exercises/ex04_1_exercise/ShoppingCart.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 jorgeantonio.exercises.ex04_1_exercise; + +/** + * + * @author Hp + */ +public class ShoppingCart { + public static void main(String[] args) { + // Declare and initialize String variables. Do not initialize message yet. + String custName = "Jorge ", itemDesc = "loves the music"; + String message; + + + // Assign the message variable + message = custName + itemDesc; + + // Print and run the code + + System.out.println(message); + } +} diff --git a/src/main/java/jorgeantonio/exercises/ex04_2_exercise/ShoppingCart.java b/src/main/java/jorgeantonio/exercises/ex04_2_exercise/ShoppingCart.java new file mode 100644 index 0000000..211ef4c --- /dev/null +++ b/src/main/java/jorgeantonio/exercises/ex04_2_exercise/ShoppingCart.java @@ -0,0 +1,36 @@ +/* + * 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 jorgeantonio.exercises.ex04_2_exercise; + +/** + * + * @author Hp + */ +public class ShoppingCart { + public static void main(String[] args) { + String custName = "Jorge"; + String itemDesc = "Discs"; + String message = custName+" bought "+itemDesc; + double price = 52.0 , tax = 5.2; + int quantity = 2; + double total; + // Declare and initialize numeric fields: price, tax, quantity, and total. + + + // Modify message to include quantity + message = custName + " bought " + quantity + " " + itemDesc; + + System.out.println(message); + + total = price * quantity * tax; + + + // Calculate total and then print the total cost + + System.out.println("and they cost " + total); + + } +} diff --git a/src/main/java/jorgeantonio/exercises/ex05_1_exercise/ShoppingCart.java b/src/main/java/jorgeantonio/exercises/ex05_1_exercise/ShoppingCart.java new file mode 100644 index 0000000..51b618e --- /dev/null +++ b/src/main/java/jorgeantonio/exercises/ex05_1_exercise/ShoppingCart.java @@ -0,0 +1,43 @@ +/* + * 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 jorgeantonio.exercises.ex05_1_exercise; +public class ShoppingCart { + + public static void main(String[] args) { + String custName = "Mary Smith"; + String itemDesc = "Shirt"; + + // numeric fields + double price = 21.99; + int quantity = 2; + double tax = 1.04; + double total; + String message = custName+" wants to purchase "+quantity+" "+itemDesc; + + // Calculating total cost + total = (price*quantity)*tax; + + + // Declare outOfStock variable and initialize it + boolean outOfStock = true; + + // Test quantity and modify message if quantity > 1. + if(quantity > 1){ + message = message +"s"; + } + + // Test outOfStock and notify user in either case. + + if(outOfStock == true){ + System.out.println("The item unavailable"); + }else{ + System.out.println(message); + System.out.println("And the total is " + total); + } + + } + +} diff --git a/src/main/java/jorgeantonio/exercises/ex05_2_exercise/ShoppingCart.java b/src/main/java/jorgeantonio/exercises/ex05_2_exercise/ShoppingCart.java new file mode 100644 index 0000000..35230fb --- /dev/null +++ b/src/main/java/jorgeantonio/exercises/ex05_2_exercise/ShoppingCart.java @@ -0,0 +1,35 @@ +/* + * 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 jorgeantonio.exercises.ex05_2_exercise; + +/** + * + * @author Hp + */ + +public class ShoppingCart { + + public static void main(String[] args) { + // local variables + String custName = "Mary Smith"; + String message = custName + " wants to purchase a several items."; + + //Declare and initialize the items String array + String items [] = new String[]{"Shirt","Jeans","Shoes",""}; + + + + // Change message to show the number of items purchased. + + message = "The items in total are " + items.length; + + System.out.println(message); + // Print an element from the items array. + System.out.print(items[0]+"\n"); + + } + +} \ No newline at end of file diff --git a/src/main/java/jorgeantonio/exercises/ex05_3_exercise/ShoppingCart.java b/src/main/java/jorgeantonio/exercises/ex05_3_exercise/ShoppingCart.java new file mode 100644 index 0000000..d71be3a --- /dev/null +++ b/src/main/java/jorgeantonio/exercises/ex05_3_exercise/ShoppingCart.java @@ -0,0 +1,35 @@ +/* + * 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 jorgeantonio.exercises.ex05_3_exercise; + +public class ShoppingCart { + + public static void main(String[] args) { + // local variables + String custName = "Mary Smith"; + String message; + double price = 21.99; + int quantity = 2; + double tax = 1.04; + + String items[]; + items = new String[4]; + items[0] = "Shirt"; + items[1] = "Belt"; + items[2] = "Scarf"; + items[3] = "Skirt"; + + message = custName + " wants to purchase "+items.length+" items."; + System.out.println(message); + + for (int i = 0; i < items.length; i++) { + System.out.println(items[i]); + } + + + } +} + diff --git a/src/main/java/jorgeantonio/exercises/ex06_1_exercise/Item.java b/src/main/java/jorgeantonio/exercises/ex06_1_exercise/Item.java new file mode 100644 index 0000000..fc125b3 --- /dev/null +++ b/src/main/java/jorgeantonio/exercises/ex06_1_exercise/Item.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 jorgeantonio.exercises.ex06_1_exercise; + +/** + * + * @author Hp + */ +public class Item { + public int id; + public String descr; + public int quantity; + public double price; +} diff --git a/src/main/java/jorgeantonio/exercises/ex06_2_exercise/Item.java b/src/main/java/jorgeantonio/exercises/ex06_2_exercise/Item.java new file mode 100644 index 0000000..6fa03fa --- /dev/null +++ b/src/main/java/jorgeantonio/exercises/ex06_2_exercise/Item.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 jorgeantonio.exercises.ex06_2_exercise; + + + +/** + * + * @author Hp + */ +public class Item { + public int itemId; + public String descr; + public int quantity; + public double price; +} diff --git a/src/main/java/jorgeantonio/exercises/ex06_2_exercise/ShoppingCart.java b/src/main/java/jorgeantonio/exercises/ex06_2_exercise/ShoppingCart.java new file mode 100644 index 0000000..15980d7 --- /dev/null +++ b/src/main/java/jorgeantonio/exercises/ex06_2_exercise/ShoppingCart.java @@ -0,0 +1,35 @@ +/* + * 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 jorgeantonio.exercises.ex06_2_exercise; + +public class ShoppingCart { + + public static void main(String args[]) { + // Declare and initialize 2 Item objects + Item item1 = new Item(); + Item item2 = new Item(); + + item1.itemId = 1; + item1.descr = "Playera de Brasil"; + item1.price = 500.00; + item1.quantity = 5; + + item2.itemId = 1; + item2.descr = "Playera de Mexico"; + item2.price = 1150.00; + item2.quantity = 5; + + // Print both item descriptions and run code. + System.out.println("The first description is " + item1.descr + "\n" + + "The second description is " + item2.descr); + + item1 = item2; + + System.out.println("The new description is " + item1.descr); + + } + +} \ No newline at end of file diff --git a/src/main/java/jorgeantonio/exercises/ex07_1_exercise/ShoppingCart.java b/src/main/java/jorgeantonio/exercises/ex07_1_exercise/ShoppingCart.java new file mode 100644 index 0000000..04291e0 --- /dev/null +++ b/src/main/java/jorgeantonio/exercises/ex07_1_exercise/ShoppingCart.java @@ -0,0 +1,24 @@ +/* + * 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 jorgeantonio.exercises.ex07_1_exercise; + +public class ShoppingCart { + public static void main (String[] args){ + String custName = "Steve Smith"; + String firstName; + int spaceIdx; + + // Get the index of the space character (" ") in custName. + spaceIdx = custName.indexOf(" "); + + + // Use the substring method parse out the first name and print it. + firstName = custName.substring(0,spaceIdx); + + System.out.println(firstName); + + } +} \ No newline at end of file diff --git a/src/main/java/jorgeantonio/exercises/ex08_1_exercise/Item.java b/src/main/java/jorgeantonio/exercises/ex08_1_exercise/Item.java new file mode 100644 index 0000000..ad3e7b6 --- /dev/null +++ b/src/main/java/jorgeantonio/exercises/ex08_1_exercise/Item.java @@ -0,0 +1,18 @@ + +package jorgeantonio.exercises.ex08_1_exercise; + +public class Item { + char color; + + public boolean SetColor(char colorCode){ + if(colorCode == ' '){ + return false; + }else{ + this.color = colorCode; + return true; + } + + } + + +} diff --git a/src/main/java/jorgeantonio/exercises/ex08_1_exercise/ShoppingCart.java b/src/main/java/jorgeantonio/exercises/ex08_1_exercise/ShoppingCart.java new file mode 100644 index 0000000..ae69778 --- /dev/null +++ b/src/main/java/jorgeantonio/exercises/ex08_1_exercise/ShoppingCart.java @@ -0,0 +1,22 @@ + +package jorgeantonio.exercises.ex08_1_exercise; + +public class ShoppingCart { + public static void main(String[] args){ + Item item1 = new Item(); + if(true == item1.SetColor('e')){ + System.out.println("THE NEW COLOR IS " + item1.color); + }else{ + System.out.println("INVALID COLOR"); + } + + // Call the setColor method on item1. Print the new color value from + // item1 if the method returns true. Otherwise print an "invalid color" + // message. + + + // Test the class, using both valid and invalid colors. + + + } +} diff --git a/src/main/java/jorgeantonio/exercises/ex08_2_exercise/Item.java b/src/main/java/jorgeantonio/exercises/ex08_2_exercise/Item.java new file mode 100644 index 0000000..8286019 --- /dev/null +++ b/src/main/java/jorgeantonio/exercises/ex08_2_exercise/Item.java @@ -0,0 +1,36 @@ + +package jorgeantonio.exercises.ex08_2_exercise; + + +public class Item { + String desc; + int quantity; + double price; + char colorCode = 'U'; //'U' = Undetermined + + public void displayItem() { + System.out.println("Item: " + desc + ", " + quantity + ", " + + price + ", "+colorCode); + } + + // Write a public 3-arg setItemDisplay method that returns void. + public void setItemFields(String desc, int quantity, double price){ + this.desc = desc; + this.quantity = quantity; + this.price = price; + } + public int setItemFields(String desc, int quantity, double price,char colorCode){ + if(colorCode == ' '){ + return-1; + }else{ + this.colorCode = colorCode; + setItemFields(desc, quantity, price); + return 1; + } + } + + // Write a public 4-arg setItemDisplay method that returns an int. + + + +} diff --git a/src/main/java/jorgeantonio/exercises/ex08_2_exercise/ShoppingCart.java b/src/main/java/jorgeantonio/exercises/ex08_2_exercise/ShoppingCart.java new file mode 100644 index 0000000..bc2ee18 --- /dev/null +++ b/src/main/java/jorgeantonio/exercises/ex08_2_exercise/ShoppingCart.java @@ -0,0 +1,25 @@ + +package jorgeantonio.exercises.ex08_2_exercise; + + +public class ShoppingCart { + public static void main(String[] args) { + Item item1 = new Item(); + + // Call the 3-arg setItemFields method and then call displayItem. + item1.setItemFields("Saco Negro", 5, 250.00); + item1.displayItem(); + + // Call the 4-arg setItemFields method, checking the return value. + + if(item1.setItemFields("Playera Gris", 50, 100.00, 'B') > 0){ + item1.displayItem(); + }else{ + System.out.println("Invalid Color"); + } + // Test your code for both valid and invalid values + + + + } +} diff --git a/src/main/java/jorgeantonio/exercises/ex09_1_exercise/Customer.java b/src/main/java/jorgeantonio/exercises/ex09_1_exercise/Customer.java new file mode 100644 index 0000000..f0aea30 --- /dev/null +++ b/src/main/java/jorgeantonio/exercises/ex09_1_exercise/Customer.java @@ -0,0 +1,24 @@ +package jorgeantonio.exercises.ex09_1_exercise; + + +public class Customer { + private String name; + private String ssn; + + + // Encapsulate this class. Make ssn read only + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSsn() { + return ssn; + } + + + +} diff --git a/src/main/java/jorgeantonio/exercises/ex09_2_exercise/Customer.java b/src/main/java/jorgeantonio/exercises/ex09_2_exercise/Customer.java new file mode 100644 index 0000000..9c0369a --- /dev/null +++ b/src/main/java/jorgeantonio/exercises/ex09_2_exercise/Customer.java @@ -0,0 +1,25 @@ +package jorgeantonio.exercises.ex09_2_exercise; + + +public class Customer { + private String name; + private String ssn; + + //Add a custom constructor + public Customer(String name, String ssn){ + this.name = name; + this.ssn = ssn; + } + + + public String getName(){ + return name; + } + public void setName(String n){ + name = n; + } + + public String getSSN(){ + return ssn; + } +} diff --git a/src/main/java/jorgeantonio/exercises/ex09_2_exercise/ShoppingCart.java b/src/main/java/jorgeantonio/exercises/ex09_2_exercise/ShoppingCart.java new file mode 100644 index 0000000..6cf7af4 --- /dev/null +++ b/src/main/java/jorgeantonio/exercises/ex09_2_exercise/ShoppingCart.java @@ -0,0 +1,15 @@ +package jorgeantonio.exercises.ex09_2_exercise; + + +public class ShoppingCart { + + public static void main(String args[]) { + // Declare, instantiate, and initialize a Customer object + Customer customer = new Customer("Jorge Gaona Puch","JGP1234"); + + System.out.println(customer.getName()); + // Print the customer object name + + + } +} diff --git a/src/main/java/jorgeantonio/exercises/ex10_1_exercise/TestClass.java b/src/main/java/jorgeantonio/exercises/ex10_1_exercise/TestClass.java new file mode 100644 index 0000000..d68bdc8 --- /dev/null +++ b/src/main/java/jorgeantonio/exercises/ex10_1_exercise/TestClass.java @@ -0,0 +1,21 @@ + +package jorgeantonio.exercises.ex10_1_exercise; + + +public class TestClass { + + public static void main(String args[]) { + int x = 4, y = 9; + if (y / x < 3) { + x += y; + } else { + x *= y; + } + System.out.println("After if stmt, x = " + x); + + int ternario = (y / x < 3) ? (x += y) : (x *= y); + + System.out.println("After ternary stmt, x = " + x); + // Use a ternary operator to perform the same logic as above. + } +} diff --git a/src/main/java/jorgeantonio/exercises/ex10_2_exercise/Order.java b/src/main/java/jorgeantonio/exercises/ex10_2_exercise/Order.java new file mode 100644 index 0000000..4330110 --- /dev/null +++ b/src/main/java/jorgeantonio/exercises/ex10_2_exercise/Order.java @@ -0,0 +1,49 @@ + +package jorgeantonio.exercises.ex10_2_exercise; + +public class Order { + + static final char CORP = 'C'; + static final char PRIVATE = 'P'; + static final char NONPROFIT = 'N'; + String name; + double total; + String stateCode; + double discount; + char custType; + + public Order(String name, double total, String state, char custType) { + this.name = name; + this.total = total; + this.stateCode = state; + this.custType = custType; + calcDiscount(); + } + + public String getDiscount(){ + return Double.toString(discount) + "%"; + } + + // Code the calcDiscount method. + public void calcDiscount() { + if(custType == 'N'){ + if (total > 900) { + discount = 10.00; + } else { + discount = 5.00; + } + }else if(custType == 'P'){ + if (total > 900) { + discount = 7.00; + } else { + discount = 0.00; + } + }else if(custType == 'C'){ + if (total < 500) { + discount = 8.00; + } else { + discount = 5.00; + } + } + } +} diff --git a/src/main/java/jorgeantonio/exercises/ex10_2_exercise/ShoppingCart.java b/src/main/java/jorgeantonio/exercises/ex10_2_exercise/ShoppingCart.java new file mode 100644 index 0000000..05fa197 --- /dev/null +++ b/src/main/java/jorgeantonio/exercises/ex10_2_exercise/ShoppingCart.java @@ -0,0 +1,9 @@ + +package jorgeantonio.exercises.ex10_2_exercise; + +public class ShoppingCart { + public static void main(String args[]){ + Order order = new Order("Rick Wilson", 910.00, "VA", Order.NONPROFIT); + System.out.println("Discount is: "+ order.getDiscount()); + } +} diff --git a/src/main/java/jorgeantonio/exercises/ex10_3_exercise/Order.java b/src/main/java/jorgeantonio/exercises/ex10_3_exercise/Order.java new file mode 100644 index 0000000..eb0b5bb --- /dev/null +++ b/src/main/java/jorgeantonio/exercises/ex10_3_exercise/Order.java @@ -0,0 +1,45 @@ + +package jorgeantonio.exercises.ex10_3_exercise; + +public class Order { + static final char CORP = 'C'; + static final char PRIVATE = 'P'; + static final char NONPROFIT = 'N'; + String name; + double total; + String stateCode; + double discount; + char custType; + + public Order(String name, double total, String state, char custType) { + this.name = name; + this.total = total; + this.stateCode = state; + this.custType = custType; + calcDiscount(); + } + + public String getDiscount(){ + return Double.toString(discount) + "%"; + } + + public void calcDiscount() { + // Replace the if logic with a switch statement. + + switch(custType){ + case CORP: + discount = (total > 500) ? 8.00 : 5.00; + break; + case PRIVATE: + discount = (total > 900) ? 7.00 : 0.00; + break; + case NONPROFIT: + discount = (total > 900) ? 10.00 : 5.00; + break; + default: + discount = 0.0; + break; + + } + } +} diff --git a/src/main/java/jorgeantonio/exercises/ex10_3_exercise/ShoppingCart.java b/src/main/java/jorgeantonio/exercises/ex10_3_exercise/ShoppingCart.java new file mode 100644 index 0000000..3d6eee3 --- /dev/null +++ b/src/main/java/jorgeantonio/exercises/ex10_3_exercise/ShoppingCart.java @@ -0,0 +1,9 @@ + +package jorgeantonio.exercises.ex10_3_exercise; + +public class ShoppingCart { + public static void main(String args[]){ + Order order = new Order("Rick Wilson", 910.00, "VA", Order.NONPROFIT); + System.out.println("Discount is: "+ order.getDiscount()); + } +} diff --git a/src/main/java/rosario/exercises/exercise_03_shoppingcart/ex03_1_exercise/ShoppingCart.java b/src/main/java/rosario/exercises/exercise_03_shoppingcart/ex03_1_exercise/ShoppingCart.java new file mode 100644 index 0000000..678c039 --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_03_shoppingcart/ex03_1_exercise/ShoppingCart.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 rosario.exercises.exercise_03_shoppingcart.ex03_1_exercise; + +/** + * + * @author rosario + */ +public class ShoppingCart { + + public static void main (String[] args){ + System.out.println("Hola mi nombre es Rosario :)"); + } +} diff --git a/src/main/java/rosario/exercises/exercise_04_variables/ex04_1_exercise/ShoppingCart.java b/src/main/java/rosario/exercises/exercise_04_variables/ex04_1_exercise/ShoppingCart.java new file mode 100644 index 0000000..0d12438 --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_04_variables/ex04_1_exercise/ShoppingCart.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 rosario.exercises.exercise_04_variables.ex04_1_exercise; + +/** + * + * @author rosario + */ +public class ShoppingCart { + public static void main(String[] args){ + String custName = "Mary Smith wants to"; + String itemDesc = " purchase a Shirt"; + String message = custName + itemDesc; + + System.out.println(message); + } +} diff --git a/src/main/java/rosario/exercises/exercise_04_variables/ex04_2_exercise/ShoppingCart.java b/src/main/java/rosario/exercises/exercise_04_variables/ex04_2_exercise/ShoppingCart.java new file mode 100644 index 0000000..1838b99 --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_04_variables/ex04_2_exercise/ShoppingCart.java @@ -0,0 +1,34 @@ +/* + * 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 rosario.exercises.exercise_04_variables.ex04_2_exercise; + +/** + * + * @author rosario + */ +public class ShoppingCart { + public static void main(String[] args) { + String custName = "Mary Smith"; + String itemDesc = "Shirt"; + String message = custName+" wants to purchase a "+itemDesc; + + // Declare and initialize numeric fields: price, tax, quantity, and total. + double price = 3; + double tax = 2; + int quantity = 1; + double total; + + // Modify message to include quantity + message = custName+" wants to purchase "+quantity+" "+itemDesc; + + System.out.println(message); + + // Calculate total and then print the total cost + total = price * quantity * tax; + System.out.println("Total cost with tax is: "+total); + + } +} diff --git a/src/main/java/rosario/exercises/exercise_05_conditionsarraysloops/ex05_1_exercise/ShoppingCart.java b/src/main/java/rosario/exercises/exercise_05_conditionsarraysloops/ex05_1_exercise/ShoppingCart.java new file mode 100644 index 0000000..0eaf74d --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_05_conditionsarraysloops/ex05_1_exercise/ShoppingCart.java @@ -0,0 +1,45 @@ +/* + * 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 rosario.exercises.exercise_05_conditionsarraysloops.ex05_1_exercise; + +/** + * + * @author rosario + */ +public class ShoppingCart { + public static void main(String[] args) { + String custName = "Mary Smith"; + String itemDesc = "Shirt"; + + // numeric fields + double price = 21.99; + int quantity = 2; + double tax = 1.04; + double total; + String message = custName+" wants to purchase "+quantity+" "+itemDesc; + System.out.println(message); + + // Calculating total cost + total = (price*quantity)*tax; + + // Declare outOfStock variable and initialize it + boolean outOfStock= false; + + // Test quantity and modify message if quantity > 1. + if(quantity>1){ + message = custName+" wants to purchase "+quantity+" "+itemDesc+"s"; + System.out.println(message); + } + + // Test outOfStock and notify user in either case. + if(outOfStock){ + System.out.println("Item is unavailable."); + }else{ + System.out.println(message); + System.out.println("Total: "+total); + } + } +} diff --git a/src/main/java/rosario/exercises/exercise_05_conditionsarraysloops/ex05_2_exercise/ShoppingCart.java b/src/main/java/rosario/exercises/exercise_05_conditionsarraysloops/ex05_2_exercise/ShoppingCart.java new file mode 100644 index 0000000..3eb6245 --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_05_conditionsarraysloops/ex05_2_exercise/ShoppingCart.java @@ -0,0 +1,29 @@ +/* + * 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 rosario.exercises.exercise_05_conditionsarraysloops.ex05_2_exercise; + +/** + * + * @author rosario + */ +public class ShoppingCart { + public static void main(String[] args) { + // local variables + String custName = "Mary Smith"; + String message = custName + " wants to purchase a several items."; + + //Declare and initialize the items String array + String []fruits = {"Mango","Sandia","Uva","Fresa"}; + + // Change message to show the number of items purchased. + message = custName + " wants to purchase "+fruits.length+" items."; + System.out.println(message); + + // Print an element from the items array. + System.out.println(fruits[3]); + + } +} diff --git a/src/main/java/rosario/exercises/exercise_05_conditionsarraysloops/ex05_3_exercise/ShoppingCart.java b/src/main/java/rosario/exercises/exercise_05_conditionsarraysloops/ex05_3_exercise/ShoppingCart.java new file mode 100644 index 0000000..de60948 --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_05_conditionsarraysloops/ex05_3_exercise/ShoppingCart.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 rosario.exercises.exercise_05_conditionsarraysloops.ex05_3_exercise; + +/** + * + * @author rosario + */ +public class ShoppingCart { + public static void main(String[] args) { + // local variables + String custName = "Mary Smith"; + String message; + double price = 21.99; + int quantity = 2; + double tax = 1.04; + + String items[]; + items = new String[4]; + items[0] = "Shirt"; + items[1] = "Belt"; + items[2] = "Scarf"; + items[3] = "Skirt"; + + message = custName + " wants to purchase "+items.length+" items."; + System.out.println(message); + + // Iterate through and print out the items from the items array + for (String item : items) { + System.err.println("Items purchased: " + item); + } + + for(int i=0;i 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/rosario/practices/practice_08_methods_3/soccer/Game.java b/src/main/java/rosario/practices/practice_08_methods_3/soccer/Game.java new file mode 100644 index 0000000..516f689 --- /dev/null +++ b/src/main/java/rosario/practices/practice_08_methods_3/soccer/Game.java @@ -0,0 +1,43 @@ +/* + * 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 rosario.practices.practice_08_methods_3.soccer; + +import rosario.practices.practice_08_methods_3.utility.GameUtils; + +/** + * + * @author rosario + */ +public class Game { + + public Team homeTeam; + public Team awayTeam; + public Goal[] goals; + + public void playGame(int maxGoals) { + int numberOfGoals = (int) (Math.random() * (maxGoals + 1)); + Goal[] theGoals = new Goal[numberOfGoals]; + this.goals = theGoals; + GameUtils.addGameGoals(this); + } + + public void playGame() { + playGame(6); + } + + 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/rosario/practices/practice_08_methods_3/soccer/Goal.java b/src/main/java/rosario/practices/practice_08_methods_3/soccer/Goal.java new file mode 100644 index 0000000..a28c8c3 --- /dev/null +++ b/src/main/java/rosario/practices/practice_08_methods_3/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 rosario.practices.practice_08_methods_3.soccer; + +/** + * + * @author rosario + */ +public class Goal { + + public Team theTeam; + public Player thePlayer; + public double theTime; + +} diff --git a/src/main/java/rosario/practices/practice_08_methods_3/soccer/League.java b/src/main/java/rosario/practices/practice_08_methods_3/soccer/League.java new file mode 100644 index 0000000..9225d9e --- /dev/null +++ b/src/main/java/rosario/practices/practice_08_methods_3/soccer/League.java @@ -0,0 +1,65 @@ +/* + * 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 rosario.practices.practice_08_methods_3.soccer; + + + +/** + * + * @author rosario + */ +public class League { + + 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.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/rosario/practices/practice_08_methods_3/soccer/Player.java b/src/main/java/rosario/practices/practice_08_methods_3/soccer/Player.java new file mode 100644 index 0000000..0dd7529 --- /dev/null +++ b/src/main/java/rosario/practices/practice_08_methods_3/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 rosario.practices.practice_08_methods_3.soccer; + +/** + * + * @author rosario + */ +public class Player { + + public String playerName; + +} diff --git a/src/main/java/rosario/practices/practice_08_methods_3/soccer/Team.java b/src/main/java/rosario/practices/practice_08_methods_3/soccer/Team.java new file mode 100644 index 0000000..149ef35 --- /dev/null +++ b/src/main/java/rosario/practices/practice_08_methods_3/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 rosario.practices.practice_08_methods_3.soccer; + +/** + * + * @author rosario + */ +public class Team { + + public String teamName; + public Player[] playerArray; + +} diff --git a/src/main/java/rosario/practices/practice_08_methods_3/utility/GameUtils.java b/src/main/java/rosario/practices/practice_08_methods_3/utility/GameUtils.java new file mode 100644 index 0000000..63f1053 --- /dev/null +++ b/src/main/java/rosario/practices/practice_08_methods_3/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 rosario.practices.practice_08_methods_3.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 rosario.practices.practice_08_methods_3.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/rosario/practices/practice_09_EncapConstructors_1/soccer/Game.java b/src/main/java/rosario/practices/practice_09_EncapConstructors_1/soccer/Game.java new file mode 100644 index 0000000..1b99254 --- /dev/null +++ b/src/main/java/rosario/practices/practice_09_EncapConstructors_1/soccer/Game.java @@ -0,0 +1,69 @@ +/* + * 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 rosario.practices.practice_09_EncapConstructors_1.soccer; + +import rosario.practices.practice_09_EncapConstructors_1.utility.GameUtils; + +/** + * + * @author rosario + */ +public class Game { + + public Team homeTeam; + public Team awayTeam; + public Goal[] goals; + + public Team getHomeTeam() { + return homeTeam; + } + + public void setHomeTeam(Team homeTeam) { + this.homeTeam = homeTeam; + } + + public Team getAwayTeam() { + return awayTeam; + } + + public void setAwayTeam(Team awayTeam) { + this.awayTeam = awayTeam; + } + + public Goal[] getGoals() { + return goals; + } + + public void setGoals(Goal[] goals) { + this.goals = 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); + } + + 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/rosario/practices/practice_09_EncapConstructors_1/soccer/Goal.java b/src/main/java/rosario/practices/practice_09_EncapConstructors_1/soccer/Goal.java new file mode 100644 index 0000000..04ddf97 --- /dev/null +++ b/src/main/java/rosario/practices/practice_09_EncapConstructors_1/soccer/Goal.java @@ -0,0 +1,44 @@ +/* + * 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 rosario.practices.practice_09_EncapConstructors_1.soccer; + +/** + * + * @author rosario + */ +public class Goal { + + public Team theTeam; + public Player thePlayer; + public double theTime; + + public Team getTheTeam() { + return theTeam; + } + + public void setTheTeam(Team theTeam) { + this.theTeam = theTeam; + } + + public Player getThePlayer() { + return thePlayer; + } + + public void setThePlayer(Player thePlayer) { + this.thePlayer = thePlayer; + } + + public double getTheTime() { + return theTime; + } + + public void setTheTime(double theTime) { + this.theTime = theTime; + } + + +} diff --git a/src/main/java/rosario/practices/practice_09_EncapConstructors_1/soccer/League.java b/src/main/java/rosario/practices/practice_09_EncapConstructors_1/soccer/League.java new file mode 100644 index 0000000..7158b80 --- /dev/null +++ b/src/main/java/rosario/practices/practice_09_EncapConstructors_1/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 rosario.practices.practice_09_EncapConstructors_1.soccer; + + +/** + * + * @author rosario + */ +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.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 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/rosario/practices/practice_09_EncapConstructors_1/soccer/Player.java b/src/main/java/rosario/practices/practice_09_EncapConstructors_1/soccer/Player.java new file mode 100644 index 0000000..e415975 --- /dev/null +++ b/src/main/java/rosario/practices/practice_09_EncapConstructors_1/soccer/Player.java @@ -0,0 +1,27 @@ +/* + * 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 rosario.practices.practice_09_EncapConstructors_1.soccer; + +/** + * + * @author rosario + */ +public class Player { + + public String playerName; + + public String getPlayerName() { + return playerName; + } + + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + + + +} diff --git a/src/main/java/rosario/practices/practice_09_EncapConstructors_1/soccer/Team.java b/src/main/java/rosario/practices/practice_09_EncapConstructors_1/soccer/Team.java new file mode 100644 index 0000000..4d1aed2 --- /dev/null +++ b/src/main/java/rosario/practices/practice_09_EncapConstructors_1/soccer/Team.java @@ -0,0 +1,35 @@ +/* + * 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 rosario.practices.practice_09_EncapConstructors_1.soccer; + +/** + * + * @author rosario + */ +public class Team { + + public String teamName; + public Player[] playerArray; + + public String getTeamName() { + return teamName; + } + + public void setTeamName(String teamName) { + this.teamName = teamName; + } + + public Player[] getPlayerArray() { + return playerArray; + } + + public void setPlayerArray(Player[] playerArray) { + this.playerArray = playerArray; + } + + +} diff --git a/src/main/java/rosario/practices/practice_09_EncapConstructors_1/utility/GameUtils.java b/src/main/java/rosario/practices/practice_09_EncapConstructors_1/utility/GameUtils.java new file mode 100644 index 0000000..eebcb25 --- /dev/null +++ b/src/main/java/rosario/practices/practice_09_EncapConstructors_1/utility/GameUtils.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 rosario.practices.practice_09_EncapConstructors_1.utility; + +import java.util.Arrays; +import rosario.practices.practice_09_EncapConstructors_1.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 ? currGame.homeTeam : currGame.awayTeam; + 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))); + + } + +} diff --git a/src/main/java/rosario/practices/practice_09_EncapConstructors_2/soccer/Game.java b/src/main/java/rosario/practices/practice_09_EncapConstructors_2/soccer/Game.java new file mode 100644 index 0000000..f644748 --- /dev/null +++ b/src/main/java/rosario/practices/practice_09_EncapConstructors_2/soccer/Game.java @@ -0,0 +1,92 @@ +/* + * 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 rosario.practices.practice_09_EncapConstructors_2.soccer; + +import rosario.practices.practice_09_EncapConstructors_2.utility.GameUtils; + +/** + * + * @author rosario + */ +public class Game { + + private Team homeTeam; + private Team awayTeam; + private Goal[] goals; + + /*Add constructor*/ + 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/rosario/practices/practice_09_EncapConstructors_2/soccer/Goal.java b/src/main/java/rosario/practices/practice_09_EncapConstructors_2/soccer/Goal.java new file mode 100644 index 0000000..f5d7788 --- /dev/null +++ b/src/main/java/rosario/practices/practice_09_EncapConstructors_2/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 rosario.practices.practice_09_EncapConstructors_2.soccer; + +/** + * + * @author rosario + */ +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/rosario/practices/practice_09_EncapConstructors_2/soccer/League.java b/src/main/java/rosario/practices/practice_09_EncapConstructors_2/soccer/League.java new file mode 100644 index 0000000..5f9f093 --- /dev/null +++ b/src/main/java/rosario/practices/practice_09_EncapConstructors_2/soccer/League.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 rosario.practices.practice_09_EncapConstructors_2.soccer; + + + +/** + * + * @author rosario + */ +public class League { + 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); + + // 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; + } + +} diff --git a/src/main/java/rosario/practices/practice_09_EncapConstructors_2/soccer/Player.java b/src/main/java/rosario/practices/practice_09_EncapConstructors_2/soccer/Player.java new file mode 100644 index 0000000..28c7a90 --- /dev/null +++ b/src/main/java/rosario/practices/practice_09_EncapConstructors_2/soccer/Player.java @@ -0,0 +1,36 @@ +/* + * 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 rosario.practices.practice_09_EncapConstructors_2.soccer; + +/** + * + * @author rosario + */ +public class Player { + + private String playerName; + + /*Add the constructor*/ + public Player(String playerName){ + this.playerName = playerName; + } + + /** + * @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/rosario/practices/practice_09_EncapConstructors_2/soccer/Team.java b/src/main/java/rosario/practices/practice_09_EncapConstructors_2/soccer/Team.java new file mode 100644 index 0000000..1db4262 --- /dev/null +++ b/src/main/java/rosario/practices/practice_09_EncapConstructors_2/soccer/Team.java @@ -0,0 +1,54 @@ +/* + * 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 rosario.practices.practice_09_EncapConstructors_2.soccer; + +/** + * + * @author rosario + */ +public class Team { + + private String teamName; + private Player[] playerArray; + + /*Add the two constructors*/ + public Team(String teamName, Player[] players){ + this.teamName = 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/rosario/practices/practice_09_EncapConstructors_2/utility/GameUtils.java b/src/main/java/rosario/practices/practice_09_EncapConstructors_2/utility/GameUtils.java new file mode 100644 index 0000000..183eaec --- /dev/null +++ b/src/main/java/rosario/practices/practice_09_EncapConstructors_2/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 rosario.practices.practice_09_EncapConstructors_2.utility; + +import java.util.Arrays; +import rosario.practices.practice_09_EncapConstructors_2.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/rosario/practices/practice_10_conditions_1/soccer/Game.java b/src/main/java/rosario/practices/practice_10_conditions_1/soccer/Game.java new file mode 100644 index 0000000..b651fd2 --- /dev/null +++ b/src/main/java/rosario/practices/practice_10_conditions_1/soccer/Game.java @@ -0,0 +1,117 @@ +/* + * 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 rosario.practices.practice_10_conditions_1.soccer; + +import rosario.practices.practice_10_conditions_1.utility.GameUtils; + +/** + * + * @author rosario + */ +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 ++; + }else{ + awayTeamGoals ++; + } + + 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/rosario/practices/practice_10_conditions_1/soccer/Goal.java b/src/main/java/rosario/practices/practice_10_conditions_1/soccer/Goal.java new file mode 100644 index 0000000..7718dcc --- /dev/null +++ b/src/main/java/rosario/practices/practice_10_conditions_1/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 rosario.practices.practice_10_conditions_1.soccer; + +/** + * + * @author rosario + */ +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/rosario/practices/practice_10_conditions_1/soccer/League.java b/src/main/java/rosario/practices/practice_10_conditions_1/soccer/League.java new file mode 100644 index 0000000..1a1d7d7 --- /dev/null +++ b/src/main/java/rosario/practices/practice_10_conditions_1/soccer/League.java @@ -0,0 +1,69 @@ +/* + * 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 rosario.practices.practice_10_conditions_1.soccer; + + +/** + * + * @author rosario + */ +public class League { + 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) { + 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/rosario/practices/practice_10_conditions_1/soccer/Player.java b/src/main/java/rosario/practices/practice_10_conditions_1/soccer/Player.java new file mode 100644 index 0000000..2ace2d5 --- /dev/null +++ b/src/main/java/rosario/practices/practice_10_conditions_1/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 rosario.practices.practice_10_conditions_1.soccer; + +/** + * + * @author rosario + */ +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/rosario/practices/practice_10_conditions_1/soccer/Team.java b/src/main/java/rosario/practices/practice_10_conditions_1/soccer/Team.java new file mode 100644 index 0000000..3af1c8b --- /dev/null +++ b/src/main/java/rosario/practices/practice_10_conditions_1/soccer/Team.java @@ -0,0 +1,72 @@ +/* + * 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 rosario.practices.practice_10_conditions_1.soccer; + +/** + * + * @author rosario + */ +public class Team { + + private String teamName; + private Player[] playerArray; + private int pointsTotal; + + public void incPointsTotal(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; + } + + public int getPointsTotal() { + return pointsTotal; + } + + public void setPointsTotal(int pointsTotal) { + this.pointsTotal = pointsTotal; + } + + + +} diff --git a/src/main/java/rosario/practices/practice_10_conditions_1/utility/GameUtils.java b/src/main/java/rosario/practices/practice_10_conditions_1/utility/GameUtils.java new file mode 100644 index 0000000..4f4bd36 --- /dev/null +++ b/src/main/java/rosario/practices/practice_10_conditions_1/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 rosario.practices.practice_10_conditions_1.utility; + +import java.util.Arrays; +import rosario.practices.practice_10_conditions_1.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/rosario/practices/practice_10_conditions_2/soccer/Game.java b/src/main/java/rosario/practices/practice_10_conditions_2/soccer/Game.java new file mode 100644 index 0000000..7373beb --- /dev/null +++ b/src/main/java/rosario/practices/practice_10_conditions_2/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 rosario.practices.practice_10_conditions_2.soccer; + +import rosario.practices.practice_10_conditions_2.utility.GameUtils; + +/** + * + * @author rosario + */ +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++; + homeTeam.goalsTotalInc(1); + } else { + awayTeamGoals++; + awayTeam.goalsTotalInc(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!"); + 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/rosario/practices/practice_10_conditions_2/soccer/Goal.java b/src/main/java/rosario/practices/practice_10_conditions_2/soccer/Goal.java new file mode 100644 index 0000000..2d5a8f5 --- /dev/null +++ b/src/main/java/rosario/practices/practice_10_conditions_2/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 rosario.practices.practice_10_conditions_2.soccer; + +/** + * + * @author rosario + */ +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/rosario/practices/practice_10_conditions_2/soccer/League.java b/src/main/java/rosario/practices/practice_10_conditions_2/soccer/League.java new file mode 100644 index 0000000..ad13d27 --- /dev/null +++ b/src/main/java/rosario/practices/practice_10_conditions_2/soccer/League.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 rosario.practices.practice_10_conditions_2.soccer; + + + +/** + * + * @author rosario + */ +public class League { + 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){ + System.out.println(currTeam.getTeamName() + " : " + currTeam.getPointsTotal() + " : " + currTeam.getGoalsTotal()); + 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/rosario/practices/practice_10_conditions_2/soccer/Player.java b/src/main/java/rosario/practices/practice_10_conditions_2/soccer/Player.java new file mode 100644 index 0000000..70fc20d --- /dev/null +++ b/src/main/java/rosario/practices/practice_10_conditions_2/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 rosario.practices.practice_10_conditions_2.soccer; + +/** + * + * @author rosario + */ +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/rosario/practices/practice_10_conditions_2/soccer/Team.java b/src/main/java/rosario/practices/practice_10_conditions_2/soccer/Team.java new file mode 100644 index 0000000..13e18ef --- /dev/null +++ b/src/main/java/rosario/practices/practice_10_conditions_2/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 rosario.practices.practice_10_conditions_2.soccer; + +/** + * + * @author rosario + */ +public class Team { + + private String teamName; + private Player[] playerArray; + private int pointsTotal; + private int goalsTotal; + + public void goalsTotalInc(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; + } + + public int getGoalsTotal() { + return goalsTotal; + } + + public void setGoalsTotal(int goalsTotal) { + this.goalsTotal = goalsTotal; + } + + +} diff --git a/src/main/java/rosario/practices/practice_10_conditions_2/utility/GameUtils.java b/src/main/java/rosario/practices/practice_10_conditions_2/utility/GameUtils.java new file mode 100644 index 0000000..e766fa9 --- /dev/null +++ b/src/main/java/rosario/practices/practice_10_conditions_2/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 rosario.practices.practice_10_conditions_2.utility; + +import java.util.Arrays; +import rosario.practices.practice_10_conditions_2.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/rosario/practices/practice_11_arraysloopsdates_1/soccer/Game.java b/src/main/java/rosario/practices/practice_11_arraysloopsdates_1/soccer/Game.java new file mode 100644 index 0000000..25e4e3e --- /dev/null +++ b/src/main/java/rosario/practices/practice_11_arraysloopsdates_1/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 rosario.practices.practice_11_arraysloopsdates_1.soccer; + +import rosario.practices.practice_11_arraysloopsdates_1.utility.GameUtils; + +/** + * + * @author rosario + */ +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/rosario/practices/practice_11_arraysloopsdates_1/soccer/Goal.java b/src/main/java/rosario/practices/practice_11_arraysloopsdates_1/soccer/Goal.java new file mode 100644 index 0000000..19519c0 --- /dev/null +++ b/src/main/java/rosario/practices/practice_11_arraysloopsdates_1/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 rosario.practices.practice_11_arraysloopsdates_1.soccer; + +/** + * + * @author rosario + */ +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/rosario/practices/practice_11_arraysloopsdates_1/soccer/League.java b/src/main/java/rosario/practices/practice_11_arraysloopsdates_1/soccer/League.java new file mode 100644 index 0000000..3cc42fe --- /dev/null +++ b/src/main/java/rosario/practices/practice_11_arraysloopsdates_1/soccer/League.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 rosario.practices.practice_11_arraysloopsdates_1.soccer; + +import java.util.*; +import rosario.practices.practice_11_arraysloopsdates_1.utility.PlayerDatabase; + + +/** + * + * @author rosario + */ +public class League { + public static void main(String[] args) { + + League theLeague = new League(); + + Team[] theTeams = theLeague.createTeams("The Robins, The Crows, the Swallows", 3); + 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/rosario/practices/practice_11_arraysloopsdates_1/soccer/Player.java b/src/main/java/rosario/practices/practice_11_arraysloopsdates_1/soccer/Player.java new file mode 100644 index 0000000..c549665 --- /dev/null +++ b/src/main/java/rosario/practices/practice_11_arraysloopsdates_1/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 rosario.practices.practice_11_arraysloopsdates_1.soccer; + +/** + * + * @author rosario + */ +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/rosario/practices/practice_11_arraysloopsdates_1/soccer/Team.java b/src/main/java/rosario/practices/practice_11_arraysloopsdates_1/soccer/Team.java new file mode 100644 index 0000000..44bb88c --- /dev/null +++ b/src/main/java/rosario/practices/practice_11_arraysloopsdates_1/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 rosario.practices.practice_11_arraysloopsdates_1.soccer; + +/** + * + * @author rosario + */ +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/rosario/practices/practice_11_arraysloopsdates_1/utility/GameUtils.java b/src/main/java/rosario/practices/practice_11_arraysloopsdates_1/utility/GameUtils.java new file mode 100644 index 0000000..3d4b0de --- /dev/null +++ b/src/main/java/rosario/practices/practice_11_arraysloopsdates_1/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 rosario.practices.practice_11_arraysloopsdates_1.utility; + +import java.util.Arrays; +import rosario.practices.practice_11_arraysloopsdates_1.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/rosario/practices/practice_11_arraysloopsdates_1/utility/PlayerDatabase.java b/src/main/java/rosario/practices/practice_11_arraysloopsdates_1/utility/PlayerDatabase.java new file mode 100644 index 0000000..4ce8ec2 --- /dev/null +++ b/src/main/java/rosario/practices/practice_11_arraysloopsdates_1/utility/PlayerDatabase.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 rosario.practices.practice_11_arraysloopsdates_1.utility; + +import java.util.*; +import rosario.practices.practice_11_arraysloopsdates_1.soccer.Player; + + +/** + * + * @author rosario + */ +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/rosario/practices/practice_11_arraysloopsdates_2/soccer/Game.java b/src/main/java/rosario/practices/practice_11_arraysloopsdates_2/soccer/Game.java new file mode 100644 index 0000000..bcafaa3 --- /dev/null +++ b/src/main/java/rosario/practices/practice_11_arraysloopsdates_2/soccer/Game.java @@ -0,0 +1,137 @@ +/* + * 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 rosario.practices.practice_11_arraysloopsdates_2.soccer; + +import java.time.*; +import java.time.format.DateTimeFormatter; +import rosario.practices.practice_11_arraysloopsdates_2.utility.GameUtils; + +/** + * + * @author rosario + */ +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(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" + + "Date " + + this.theDateTime.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; + } + + public LocalDateTime getTheDateTime() { + return theDateTime; + } + + public void setTheDateTime(LocalDateTime theDateTime) { + this.theDateTime = theDateTime; + } + + +} diff --git a/src/main/java/rosario/practices/practice_11_arraysloopsdates_2/soccer/Goal.java b/src/main/java/rosario/practices/practice_11_arraysloopsdates_2/soccer/Goal.java new file mode 100644 index 0000000..c391baa --- /dev/null +++ b/src/main/java/rosario/practices/practice_11_arraysloopsdates_2/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 rosario.practices.practice_11_arraysloopsdates_2.soccer; + +/** + * + * @author rosario + */ +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/rosario/practices/practice_11_arraysloopsdates_2/soccer/League.java b/src/main/java/rosario/practices/practice_11_arraysloopsdates_2/soccer/League.java new file mode 100644 index 0000000..f4b20b2 --- /dev/null +++ b/src/main/java/rosario/practices/practice_11_arraysloopsdates_2/soccer/League.java @@ -0,0 +1,97 @@ +/* + * 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 rosario.practices.practice_11_arraysloopsdates_2.soccer; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.Period; +import java.util.ArrayList; +import java.util.StringTokenizer; +import rosario.practices.practice_11_arraysloopsdates_2.utility.PlayerDatabase; + +/** + * + * @author rosario + */ +public class League { + 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) { + 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()); + + } + + /* 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/rosario/practices/practice_11_arraysloopsdates_2/soccer/Player.java b/src/main/java/rosario/practices/practice_11_arraysloopsdates_2/soccer/Player.java new file mode 100644 index 0000000..e21532f --- /dev/null +++ b/src/main/java/rosario/practices/practice_11_arraysloopsdates_2/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 rosario.practices.practice_11_arraysloopsdates_2.soccer; + +/** + * + * @author rosario + */ +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/rosario/practices/practice_11_arraysloopsdates_2/soccer/Team.java b/src/main/java/rosario/practices/practice_11_arraysloopsdates_2/soccer/Team.java new file mode 100644 index 0000000..9e0fe02 --- /dev/null +++ b/src/main/java/rosario/practices/practice_11_arraysloopsdates_2/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 rosario.practices.practice_11_arraysloopsdates_2.soccer; + +/** + * + * @author rosario + */ +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/rosario/practices/practice_11_arraysloopsdates_2/utility/GameUtils.java b/src/main/java/rosario/practices/practice_11_arraysloopsdates_2/utility/GameUtils.java new file mode 100644 index 0000000..39798dc --- /dev/null +++ b/src/main/java/rosario/practices/practice_11_arraysloopsdates_2/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 rosario.practices.practice_11_arraysloopsdates_2.utility; + +import java.util.Arrays; +import rosario.practices.practice_11_arraysloopsdates_2.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/rosario/practices/practice_11_arraysloopsdates_2/utility/PlayerDatabase.java b/src/main/java/rosario/practices/practice_11_arraysloopsdates_2/utility/PlayerDatabase.java new file mode 100644 index 0000000..1b8c4b1 --- /dev/null +++ b/src/main/java/rosario/practices/practice_11_arraysloopsdates_2/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 rosario.practices.practice_11_arraysloopsdates_2.utility; + +import java.util.*; +import rosario.practices.practice_11_arraysloopsdates_2.soccer.Player; + + + +/** + * + * @author rosario + */ +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/rosario/practices/practice_12_usinginheritance_1/soccer/Game.java b/src/main/java/rosario/practices/practice_12_usinginheritance_1/soccer/Game.java new file mode 100644 index 0000000..c078c74 --- /dev/null +++ b/src/main/java/rosario/practices/practice_12_usinginheritance_1/soccer/Game.java @@ -0,0 +1,162 @@ +/* + * 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 rosario.practices.practice_12_usinginheritance_1.soccer; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; + +/** + * + * @author rosario + */ +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() { + ArrayList eventList = new ArrayList(); + Goal currEvent; + for (int i = 0; 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/rosario/practices/practice_12_usinginheritance_1/soccer/Goal.java b/src/main/java/rosario/practices/practice_12_usinginheritance_1/soccer/Goal.java new file mode 100644 index 0000000..fdb6e53 --- /dev/null +++ b/src/main/java/rosario/practices/practice_12_usinginheritance_1/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 rosario.practices.practice_12_usinginheritance_1.soccer; + +/** + * + * @author rosario + */ +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/rosario/practices/practice_12_usinginheritance_1/soccer/League.java b/src/main/java/rosario/practices/practice_12_usinginheritance_1/soccer/League.java new file mode 100644 index 0000000..d4d801c --- /dev/null +++ b/src/main/java/rosario/practices/practice_12_usinginheritance_1/soccer/League.java @@ -0,0 +1,101 @@ +/* + * 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 rosario.practices.practice_12_usinginheritance_1.soccer; + +import java.time.LocalDateTime; +import java.time.Period; +import java.util.ArrayList; +import java.util.StringTokenizer; +import rosario.practices.practice_12_usinginheritance_1.utility.PlayerDatabase; + +/** + * + * @author rosario + */ +public class League { + 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/rosario/practices/practice_12_usinginheritance_1/soccer/Player.java b/src/main/java/rosario/practices/practice_12_usinginheritance_1/soccer/Player.java new file mode 100644 index 0000000..dac85f7 --- /dev/null +++ b/src/main/java/rosario/practices/practice_12_usinginheritance_1/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 rosario.practices.practice_12_usinginheritance_1.soccer; + +/** + * + * @author rosario + */ +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/rosario/practices/practice_12_usinginheritance_1/soccer/Team.java b/src/main/java/rosario/practices/practice_12_usinginheritance_1/soccer/Team.java new file mode 100644 index 0000000..f710c31 --- /dev/null +++ b/src/main/java/rosario/practices/practice_12_usinginheritance_1/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 rosario.practices.practice_12_usinginheritance_1.soccer; + +/** + * + * @author rosario + */ +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/rosario/practices/practice_12_usinginheritance_1/utility/PlayerDatabase.java b/src/main/java/rosario/practices/practice_12_usinginheritance_1/utility/PlayerDatabase.java new file mode 100644 index 0000000..bf5f5d7 --- /dev/null +++ b/src/main/java/rosario/practices/practice_12_usinginheritance_1/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 rosario.practices.practice_12_usinginheritance_1.utility; + +import java.util.*; +import rosario.practices.practice_12_usinginheritance_1.soccer.Player; + + + +/** + * + * @author rosario + */ +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/rosario/practices/practice_12_usinginheritance_2/soccer/Game.java b/src/main/java/rosario/practices/practice_12_usinginheritance_2/soccer/Game.java new file mode 100644 index 0000000..8b54556 --- /dev/null +++ b/src/main/java/rosario/practices/practice_12_usinginheritance_2/soccer/Game.java @@ -0,0 +1,164 @@ +/* + * 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 rosario.practices.practice_12_usinginheritance_2.soccer; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; + +/** + * + * @author rosario + */ + +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.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.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.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/rosario/practices/practice_12_usinginheritance_2/soccer/GameEvent.java b/src/main/java/rosario/practices/practice_12_usinginheritance_2/soccer/GameEvent.java new file mode 100644 index 0000000..6cff295 --- /dev/null +++ b/src/main/java/rosario/practices/practice_12_usinginheritance_2/soccer/GameEvent.java @@ -0,0 +1,58 @@ +/* + * 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 rosario.practices.practice_12_usinginheritance_2.soccer; + +/** + * + * @author rosario + */ +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/rosario/practices/practice_12_usinginheritance_2/soccer/Goal.java b/src/main/java/rosario/practices/practice_12_usinginheritance_2/soccer/Goal.java new file mode 100644 index 0000000..1d71122 --- /dev/null +++ b/src/main/java/rosario/practices/practice_12_usinginheritance_2/soccer/Goal.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 rosario.practices.practice_12_usinginheritance_2.soccer; + +/** + * + * @author rosario + */ + +public class Goal extends GameEvent{ +} diff --git a/src/main/java/rosario/practices/practice_12_usinginheritance_2/soccer/League.java b/src/main/java/rosario/practices/practice_12_usinginheritance_2/soccer/League.java new file mode 100644 index 0000000..4aa3838 --- /dev/null +++ b/src/main/java/rosario/practices/practice_12_usinginheritance_2/soccer/League.java @@ -0,0 +1,96 @@ +/* + * 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 rosario.practices.practice_12_usinginheritance_2.soccer; + +import java.time.*; +import java.util.ArrayList; +import java.util.StringTokenizer; +import rosario.practices.practice_12_usinginheritance_2.utility.PlayerDatabase; + +/** + * + * @author rosario + */ +public class League { + 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/rosario/practices/practice_12_usinginheritance_2/soccer/Player.java b/src/main/java/rosario/practices/practice_12_usinginheritance_2/soccer/Player.java new file mode 100644 index 0000000..35a1092 --- /dev/null +++ b/src/main/java/rosario/practices/practice_12_usinginheritance_2/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 rosario.practices.practice_12_usinginheritance_2.soccer; + +/** + * + * @author rosario + */ +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/rosario/practices/practice_12_usinginheritance_2/soccer/Possession.java b/src/main/java/rosario/practices/practice_12_usinginheritance_2/soccer/Possession.java new file mode 100644 index 0000000..e67e122 --- /dev/null +++ b/src/main/java/rosario/practices/practice_12_usinginheritance_2/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 rosario.practices.practice_12_usinginheritance_2.soccer; + +/** + * + * @author rosario + */ +public class Possession extends GameEvent{ + +} diff --git a/src/main/java/rosario/practices/practice_12_usinginheritance_2/soccer/Team.java b/src/main/java/rosario/practices/practice_12_usinginheritance_2/soccer/Team.java new file mode 100644 index 0000000..bb31536 --- /dev/null +++ b/src/main/java/rosario/practices/practice_12_usinginheritance_2/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 rosario.practices.practice_12_usinginheritance_2.soccer; + +/** + * + * @author rosario + */ +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/rosario/practices/practice_12_usinginheritance_2/utility/PlayerDatabase.java b/src/main/java/rosario/practices/practice_12_usinginheritance_2/utility/PlayerDatabase.java new file mode 100644 index 0000000..4d49325 --- /dev/null +++ b/src/main/java/rosario/practices/practice_12_usinginheritance_2/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 rosario.practices.practice_12_usinginheritance_2.utility; + +import java.util.*; +import rosario.practices.practice_12_usinginheritance_2.soccer.Player; + + + +/** + * + * @author rosario + */ +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/rosario/practices/practice_13_usinginterfaces_1/soccer/Game.java b/src/main/java/rosario/practices/practice_13_usinginterfaces_1/soccer/Game.java new file mode 100644 index 0000000..795b199 --- /dev/null +++ b/src/main/java/rosario/practices/practice_13_usinginterfaces_1/soccer/Game.java @@ -0,0 +1,162 @@ +/* + * 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 rosario.practices.practice_13_usinginterfaces_1.soccer; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; + +/** + * + * @author rosario + */ +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.6 ? new Goal() : new Possession(); + currEvent.setTheTeam(Math.random() > 0.8 ? 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/rosario/practices/practice_13_usinginterfaces_1/soccer/GameEvent.java b/src/main/java/rosario/practices/practice_13_usinginterfaces_1/soccer/GameEvent.java new file mode 100644 index 0000000..3715615 --- /dev/null +++ b/src/main/java/rosario/practices/practice_13_usinginterfaces_1/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 rosario.practices.practice_13_usinginterfaces_1.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/rosario/practices/practice_13_usinginterfaces_1/soccer/Goal.java b/src/main/java/rosario/practices/practice_13_usinginterfaces_1/soccer/Goal.java new file mode 100644 index 0000000..1d70103 --- /dev/null +++ b/src/main/java/rosario/practices/practice_13_usinginterfaces_1/soccer/Goal.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 rosario.practices.practice_13_usinginterfaces_1.soccer; + +/** + * + * @author rosario + */ +public class Goal extends GameEvent { + public String toString(){ + return "Goal scored"; + } +} diff --git a/src/main/java/rosario/practices/practice_13_usinginterfaces_1/soccer/League.java b/src/main/java/rosario/practices/practice_13_usinginterfaces_1/soccer/League.java new file mode 100644 index 0000000..c24fbd2 --- /dev/null +++ b/src/main/java/rosario/practices/practice_13_usinginterfaces_1/soccer/League.java @@ -0,0 +1,102 @@ +/* + * 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 rosario.practices.practice_13_usinginterfaces_1.soccer; + +import java.time.LocalDateTime; +import java.time.Period; +import java.util.ArrayList; +import java.util.StringTokenizer; +import rosario.practices.practice_13_usinginterfaces_1.utility.PlayerDatabase; + +/** + * + * @author rosario + */ +public class League { + 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/rosario/practices/practice_13_usinginterfaces_1/soccer/Player.java b/src/main/java/rosario/practices/practice_13_usinginterfaces_1/soccer/Player.java new file mode 100644 index 0000000..b1994ff --- /dev/null +++ b/src/main/java/rosario/practices/practice_13_usinginterfaces_1/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 rosario.practices.practice_13_usinginterfaces_1.soccer; + +/** + * + * @author rosario + */ +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/rosario/practices/practice_13_usinginterfaces_1/soccer/Possession.java b/src/main/java/rosario/practices/practice_13_usinginterfaces_1/soccer/Possession.java new file mode 100644 index 0000000..eb36d1d --- /dev/null +++ b/src/main/java/rosario/practices/practice_13_usinginterfaces_1/soccer/Possession.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 rosario.practices.practice_13_usinginterfaces_1.soccer; + +/** + * + * @author rosario + */ +public class Possession extends GameEvent { + public String toString(){ + return "Possession"; + } +} diff --git a/src/main/java/rosario/practices/practice_13_usinginterfaces_1/soccer/Team.java b/src/main/java/rosario/practices/practice_13_usinginterfaces_1/soccer/Team.java new file mode 100644 index 0000000..5e94424 --- /dev/null +++ b/src/main/java/rosario/practices/practice_13_usinginterfaces_1/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 rosario.practices.practice_13_usinginterfaces_1.soccer; + +/** + * + * @author rosario + */ +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/rosario/practices/practice_13_usinginterfaces_1/utility/PlayerDatabase.java b/src/main/java/rosario/practices/practice_13_usinginterfaces_1/utility/PlayerDatabase.java new file mode 100644 index 0000000..7d564c4 --- /dev/null +++ b/src/main/java/rosario/practices/practice_13_usinginterfaces_1/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 rosario.practices.practice_13_usinginterfaces_1.utility; + +import java.util.*; +import rosario.practices.practice_13_usinginterfaces_1.soccer.Player; + + + +/** + * + * @author rosario + */ +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/rosario/practices/practice_13_usinginterfaces_2/soccer/Game.java b/src/main/java/rosario/practices/practice_13_usinginterfaces_2/soccer/Game.java new file mode 100644 index 0000000..291e4d4 --- /dev/null +++ b/src/main/java/rosario/practices/practice_13_usinginterfaces_2/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 rosario.practices.practice_13_usinginterfaces_2.soccer; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; + + +/** + * + * @author rosario + */ +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/rosario/practices/practice_13_usinginterfaces_2/soccer/GameEvent.java b/src/main/java/rosario/practices/practice_13_usinginterfaces_2/soccer/GameEvent.java new file mode 100644 index 0000000..e20f31e --- /dev/null +++ b/src/main/java/rosario/practices/practice_13_usinginterfaces_2/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 rosario.practices.practice_13_usinginterfaces_2.soccer; + +/** + * + * @author rosario + */ +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/rosario/practices/practice_13_usinginterfaces_2/soccer/Goal.java b/src/main/java/rosario/practices/practice_13_usinginterfaces_2/soccer/Goal.java new file mode 100644 index 0000000..55a9e8d --- /dev/null +++ b/src/main/java/rosario/practices/practice_13_usinginterfaces_2/soccer/Goal.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 rosario.practices.practice_13_usinginterfaces_2.soccer; + +/** + * + * @author rosario + */ +public class Goal extends GameEvent { + public String toString(){ + return "Goal scored"; + } +} diff --git a/src/main/java/rosario/practices/practice_13_usinginterfaces_2/soccer/League.java b/src/main/java/rosario/practices/practice_13_usinginterfaces_2/soccer/League.java new file mode 100644 index 0000000..d7c8623 --- /dev/null +++ b/src/main/java/rosario/practices/practice_13_usinginterfaces_2/soccer/League.java @@ -0,0 +1,93 @@ +/* + * 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 rosario.practices.practice_13_usinginterfaces_2.soccer; + +import java.time.LocalDateTime; +import java.time.Period; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.StringTokenizer; +import rosario.practices.practice_13_usinginterfaces_2.utility.*; + +/** + * + * @author rosario + */ +public class League { + 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) { + 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"; + } + +} diff --git a/src/main/java/rosario/practices/practice_13_usinginterfaces_2/soccer/Player.java b/src/main/java/rosario/practices/practice_13_usinginterfaces_2/soccer/Player.java new file mode 100644 index 0000000..bebefe1 --- /dev/null +++ b/src/main/java/rosario/practices/practice_13_usinginterfaces_2/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 rosario.practices.practice_13_usinginterfaces_2.soccer; + +/** + * + * @author rosario + */ +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/rosario/practices/practice_13_usinginterfaces_2/soccer/Possession.java b/src/main/java/rosario/practices/practice_13_usinginterfaces_2/soccer/Possession.java new file mode 100644 index 0000000..07f7af0 --- /dev/null +++ b/src/main/java/rosario/practices/practice_13_usinginterfaces_2/soccer/Possession.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 rosario.practices.practice_13_usinginterfaces_2.soccer; + +/** + * + * @author rosario + */ +public class Possession extends GameEvent { + public String toString(){ + return "Possession"; + } +} diff --git a/src/main/java/rosario/practices/practice_13_usinginterfaces_2/soccer/Team.java b/src/main/java/rosario/practices/practice_13_usinginterfaces_2/soccer/Team.java new file mode 100644 index 0000000..5bdcee1 --- /dev/null +++ b/src/main/java/rosario/practices/practice_13_usinginterfaces_2/soccer/Team.java @@ -0,0 +1,108 @@ +/* + * 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 rosario.practices.practice_13_usinginterfaces_2.soccer; + +/** + * + * @author rosario + */ + +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/rosario/practices/practice_13_usinginterfaces_2/utility/PlayerDatabase.java b/src/main/java/rosario/practices/practice_13_usinginterfaces_2/utility/PlayerDatabase.java new file mode 100644 index 0000000..7d1148a --- /dev/null +++ b/src/main/java/rosario/practices/practice_13_usinginterfaces_2/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 rosario.practices.practice_13_usinginterfaces_2.utility; + +import java.util.*; +import rosario.practices.practice_13_usinginterfaces_2.soccer.Player; + + + +/** + * + * @author rosario + */ +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/rosario/practices/practice_13_usinginterfaces_3/soccer/Game.java b/src/main/java/rosario/practices/practice_13_usinginterfaces_3/soccer/Game.java new file mode 100644 index 0000000..108c390 --- /dev/null +++ b/src/main/java/rosario/practices/practice_13_usinginterfaces_3/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 rosario.practices.practice_13_usinginterfaces_3.soccer; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; + + +/** + * + * @author rosario + */ +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/rosario/practices/practice_13_usinginterfaces_3/soccer/GameEvent.java b/src/main/java/rosario/practices/practice_13_usinginterfaces_3/soccer/GameEvent.java new file mode 100644 index 0000000..5ca9462 --- /dev/null +++ b/src/main/java/rosario/practices/practice_13_usinginterfaces_3/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 rosario.practices.practice_13_usinginterfaces_3.soccer; + +/** + * + * @author rosario + */ +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/rosario/practices/practice_13_usinginterfaces_3/soccer/Goal.java b/src/main/java/rosario/practices/practice_13_usinginterfaces_3/soccer/Goal.java new file mode 100644 index 0000000..3390a7e --- /dev/null +++ b/src/main/java/rosario/practices/practice_13_usinginterfaces_3/soccer/Goal.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 rosario.practices.practice_13_usinginterfaces_3.soccer; + +/** + * + * @author rosario + */ +public class Goal extends GameEvent { + public String toString(){ + return "Goal scored"; + } +} diff --git a/src/main/java/rosario/practices/practice_13_usinginterfaces_3/soccer/League.java b/src/main/java/rosario/practices/practice_13_usinginterfaces_3/soccer/League.java new file mode 100644 index 0000000..b01db4c --- /dev/null +++ b/src/main/java/rosario/practices/practice_13_usinginterfaces_3/soccer/League.java @@ -0,0 +1,114 @@ +/* + * 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 rosario.practices.practice_13_usinginterfaces_3.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 rosario.practices.practice_13_usinginterfaces_3.utility.PlayerDatabase; + +/** + * + * @author rosario + */ +public class League { + 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())); + } + 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/rosario/practices/practice_13_usinginterfaces_3/soccer/Player.java b/src/main/java/rosario/practices/practice_13_usinginterfaces_3/soccer/Player.java new file mode 100644 index 0000000..dd94c8f --- /dev/null +++ b/src/main/java/rosario/practices/practice_13_usinginterfaces_3/soccer/Player.java @@ -0,0 +1,47 @@ +/* + * 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 rosario.practices.practice_13_usinginterfaces_3.soccer; + +/** + * + * @author rosario + */ +public class Player { + + private String playerName; + private int goalsScored; + + public Player(String playerName) { + this.playerName = playerName; + } + public Player() {} + public void incGoalsScored(){ + this.goalsScored ++; + } + + /** + * @return the playerName + */ + public String getPlayerName() { + return playerName; + } + + /** + * @param playerName the playerName to set + */ + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + + public int getGoalsScored() { + return goalsScored; + } + + public void setGoalsScored(int goalsScored) { + this.goalsScored = goalsScored; + } +} diff --git a/src/main/java/rosario/practices/practice_13_usinginterfaces_3/soccer/Possession.java b/src/main/java/rosario/practices/practice_13_usinginterfaces_3/soccer/Possession.java new file mode 100644 index 0000000..33081ea --- /dev/null +++ b/src/main/java/rosario/practices/practice_13_usinginterfaces_3/soccer/Possession.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 rosario.practices.practice_13_usinginterfaces_3.soccer; + +/** + * + * @author rosario + */ +public class Possession extends GameEvent { + public String toString(){ + return "Possession"; + } + +} diff --git a/src/main/java/rosario/practices/practice_13_usinginterfaces_3/soccer/Team.java b/src/main/java/rosario/practices/practice_13_usinginterfaces_3/soccer/Team.java new file mode 100644 index 0000000..df4c21f --- /dev/null +++ b/src/main/java/rosario/practices/practice_13_usinginterfaces_3/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 rosario.practices.practice_13_usinginterfaces_3.soccer; + +/** + * + * @author rosario + */ +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/rosario/practices/practice_13_usinginterfaces_3/utility/PlayerDatabase.java b/src/main/java/rosario/practices/practice_13_usinginterfaces_3/utility/PlayerDatabase.java new file mode 100644 index 0000000..a350b51 --- /dev/null +++ b/src/main/java/rosario/practices/practice_13_usinginterfaces_3/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 rosario.practices.practice_13_usinginterfaces_3.utility; + +import java.util.*; +import rosario.practices.practice_13_usinginterfaces_3.soccer.Player; + + + +/** + * + * @author rosario + */ +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/rosario/practices/practice_14_exceptions_1/soccer/Game.java b/src/main/java/rosario/practices/practice_14_exceptions_1/soccer/Game.java new file mode 100644 index 0000000..9288425 --- /dev/null +++ b/src/main/java/rosario/practices/practice_14_exceptions_1/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 rosario.practices.practice_14_exceptions_1.soccer; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; + + +/** + * + * @author rosario + */ +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/rosario/practices/practice_14_exceptions_1/soccer/GameEvent.java b/src/main/java/rosario/practices/practice_14_exceptions_1/soccer/GameEvent.java new file mode 100644 index 0000000..f485613 --- /dev/null +++ b/src/main/java/rosario/practices/practice_14_exceptions_1/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 rosario.practices.practice_14_exceptions_1.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/rosario/practices/practice_14_exceptions_1/soccer/Goal.java b/src/main/java/rosario/practices/practice_14_exceptions_1/soccer/Goal.java new file mode 100644 index 0000000..0a5c127 --- /dev/null +++ b/src/main/java/rosario/practices/practice_14_exceptions_1/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 rosario.practices.practice_14_exceptions_1.soccer; + +/** + * + * @author rosario + */ +public class Goal extends GameEvent { + + public String toString(){ + return "Goal scored"; + } +} diff --git a/src/main/java/rosario/practices/practice_14_exceptions_1/soccer/League.java b/src/main/java/rosario/practices/practice_14_exceptions_1/soccer/League.java new file mode 100644 index 0000000..5c38595 --- /dev/null +++ b/src/main/java/rosario/practices/practice_14_exceptions_1/soccer/League.java @@ -0,0 +1,125 @@ +/* + * 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 rosario.practices.practice_14_exceptions_1.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 rosario.practices.practice_14_exceptions_1.utility.PlayerDatabase; +import rosario.practices.practice_14_exceptions_1.utility.PlayerDatabaseException; + +/** + * + * @author rosario + */ +public class League { + 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 */ + }catch (Exception e){ + e.printStackTrace(System.err); + } + /* Practice 14-1, Step 3d. Add a catch block here */ + + } + + /* 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/rosario/practices/practice_14_exceptions_1/soccer/Player.java b/src/main/java/rosario/practices/practice_14_exceptions_1/soccer/Player.java new file mode 100644 index 0000000..e818885 --- /dev/null +++ b/src/main/java/rosario/practices/practice_14_exceptions_1/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 rosario.practices.practice_14_exceptions_1.soccer; + +/** + * + * @author rosario + */ +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/rosario/practices/practice_14_exceptions_1/soccer/Possession.java b/src/main/java/rosario/practices/practice_14_exceptions_1/soccer/Possession.java new file mode 100644 index 0000000..ead6bd2 --- /dev/null +++ b/src/main/java/rosario/practices/practice_14_exceptions_1/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 rosario.practices.practice_14_exceptions_1.soccer; + +/** + * + * @author ksomervi + */ +public class Possession extends GameEvent { + + public String toString(){ + return "Possession"; + } + +} diff --git a/src/main/java/rosario/practices/practice_14_exceptions_1/soccer/Team.java b/src/main/java/rosario/practices/practice_14_exceptions_1/soccer/Team.java new file mode 100644 index 0000000..a4564b1 --- /dev/null +++ b/src/main/java/rosario/practices/practice_14_exceptions_1/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 rosario.practices.practice_14_exceptions_1.soccer; + +/** + * + * @author rosario + */ +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/rosario/practices/practice_14_exceptions_1/utility/PlayerDatabase.java b/src/main/java/rosario/practices/practice_14_exceptions_1/utility/PlayerDatabase.java new file mode 100644 index 0000000..5406113 --- /dev/null +++ b/src/main/java/rosario/practices/practice_14_exceptions_1/utility/PlayerDatabase.java @@ -0,0 +1,89 @@ +/* + * 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 rosario.practices.practice_14_exceptions_1.utility; + +import java.util.*; +import rosario.practices.practice_14_exceptions_1.soccer.Player; + + + +/** + * + * @author rosario + */ +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); + }catch (IndexOutOfBoundsException ie){ + throw new PlayerDatabaseException("Not enough player in the " + + " database for the teams request."); + } + /* Practice 14-1, Step 6b. End the try block here */ + /* Practice 14-1, Step 6c. Add catch block here */ + } + 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/rosario/practices/practice_14_exceptions_1/utility/PlayerDatabaseException.java b/src/main/java/rosario/practices/practice_14_exceptions_1/utility/PlayerDatabaseException.java new file mode 100644 index 0000000..c596f94 --- /dev/null +++ b/src/main/java/rosario/practices/practice_14_exceptions_1/utility/PlayerDatabaseException.java @@ -0,0 +1,11 @@ +package rosario.practices.practice_14_exceptions_1.utility; + +/** + * + * @author rosario + */ +public class PlayerDatabaseException extends Exception{ + public PlayerDatabaseException(String message){ + super(message); + } +} diff --git a/src/main/java/sofflix/exercises/ex03_1_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex03_1_exercise/ShoppingCart.java new file mode 100644 index 0000000..4c9a2aa --- /dev/null +++ b/src/main/java/sofflix/exercises/ex03_1_exercise/ShoppingCart.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 ex03_1_exercise; + +/** + * + * @author Hp + */ +public class ShoppingCart { + public static void main (String[] args){ + System.out.println("Mi nombre es Felix..."); + } +} diff --git a/src/main/java/sofflix/exercises/ex04_1_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex04_1_exercise/ShoppingCart.java new file mode 100644 index 0000000..1f23b42 --- /dev/null +++ b/src/main/java/sofflix/exercises/ex04_1_exercise/ShoppingCart.java @@ -0,0 +1,18 @@ + +package ex04_1_exercise; + +public class ShoppingCart { + + public static void main(String[] args) { + // Declare and initialize String variables. Do not initialize message yet. + String custName = "Omar Sotelo"; + String itemDesc = "camisa negra"; + String message; + + // Assign the message variable + message = custName + " quiere comprar una "+ itemDesc; + + // Print and run the code + System.out.println(message); + } +} \ No newline at end of file diff --git a/src/main/java/sofflix/exercises/ex04_2_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex04_2_exercise/ShoppingCart.java new file mode 100644 index 0000000..a5073aa --- /dev/null +++ b/src/main/java/sofflix/exercises/ex04_2_exercise/ShoppingCart.java @@ -0,0 +1,28 @@ + +package ex04_2_exercise; + +public class ShoppingCart { + + public static void main(String[] args) { + String custName = "Mary Smith"; + String itemDesc = "Shirt"; + String message = custName+" wants to purchase a "+itemDesc; + + // Declare and initialize numeric fields: price, tax, quantity, and total. + double price = 5.0; + double tax = 1.16; + int quantity = 1; + double total; + + // Modify message to include quantity + message = custName+" wants to purchase a "+ quantity +" "+ itemDesc; + + System.out.println(message); + + // Calculate total and then print the total cost + total = price * quantity * tax; + + System.out.println("Total cost with tax is:"+total); + + } +} diff --git a/src/main/java/sofflix/exercises/ex05_1_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex05_1_exercise/ShoppingCart.java new file mode 100644 index 0000000..fa6c1fe --- /dev/null +++ b/src/main/java/sofflix/exercises/ex05_1_exercise/ShoppingCart.java @@ -0,0 +1,44 @@ +/* + * 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 ex05_1_exercise; + +/** + * + * @author Hp + */ +public class ShoppingCart { + public static void main(String[] args) { + String custName = "Mary Smith"; + String itemDesc = "Shirt"; + + // numeric fields + double price = 21.99; + int quantity = 2; + double tax = 1.04; + double total; + String message = custName+" wants to purchase "+quantity+" "+itemDesc; + + // Calculating total cost + total = (price*quantity)*tax; + + + // Declare outOfStock variable and initialize it + boolean outOfStock = true; + + // Test quantity and modify message if quantity > 1. + if(quantity > 1){ + message = message + "s"; + } + + // Test outOfStock and notify user in either case. + if(outOfStock){ + System.out.println("The article is no longer available"); + }else{ + System.out.println(message); + System.out.println("The total cost with taxes is: "+total); + } + } +} diff --git a/src/main/java/sofflix/exercises/ex05_2_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex05_2_exercise/ShoppingCart.java new file mode 100644 index 0000000..23af317 --- /dev/null +++ b/src/main/java/sofflix/exercises/ex05_2_exercise/ShoppingCart.java @@ -0,0 +1,33 @@ +/* + * 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 ex05_2_exercise; + +/** + * + * @author Hp + */ +public class ShoppingCart { + public static void main(String[] args) { + // local variables + String custName = "Mary Smith"; + String message = custName + " wants to purchase a several items."; + + //Declare and initialize the items String array + String[] clothes = {"Shirt","Pants","T-Shirt","Socks"}; + + + + // Change message to show the number of items purchased. + message = custName + " wants to purchase "+ clothes.length +" items"; + + System.out.println(message); + // Print an element from the items array. + System.out.println(custName +" wants to purchase a "+clothes[0]); + + //Si se ejecuta con las posicion 4 se genera una excepcion java.lang.ArrayIndexOutOfBoundsException: 4 + } +} + diff --git a/src/main/java/sofflix/exercises/ex05_3_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex05_3_exercise/ShoppingCart.java new file mode 100644 index 0000000..04e9a97 --- /dev/null +++ b/src/main/java/sofflix/exercises/ex05_3_exercise/ShoppingCart.java @@ -0,0 +1,38 @@ +/* + * 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 ex05_3_exercise; + +/** + * + * @author Hp + */ +public class ShoppingCart { + public static void main(String[] args) { + // local variables + String custName = "Mary Smith"; + String message; + double price = 21.99; + int quantity = 2; + double tax = 1.04; + + String items[]; + items = new String[4]; + items[0] = "Shirt"; + items[1] = "Belt"; + items[2] = "Scarf"; + items[3] = "Skirt"; + + message = custName + " wants to purchase "+items.length+" items."; + System.out.println(message); + + // Iterate through and print out the items from the items array + for (int i = 0; i < items.length; i++) { + String item = items[i]; + System.out.println("Items purchased: "+ item); + } + + } +} diff --git a/src/main/java/sofflix/exercises/ex06_1_exercise/Item.java b/src/main/java/sofflix/exercises/ex06_1_exercise/Item.java new file mode 100644 index 0000000..80e58e5 --- /dev/null +++ b/src/main/java/sofflix/exercises/ex06_1_exercise/Item.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 ex06_1_exercise; + +/** + * + * @author Hp + */ +public class Item { + public int ID; + public String descr; + public int quantity; + public double price; + + public Item(String descr){ + this.descr = descr; + } +} diff --git a/src/main/java/sofflix/exercises/ex06_2_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex06_2_exercise/ShoppingCart.java new file mode 100644 index 0000000..dbaf94e --- /dev/null +++ b/src/main/java/sofflix/exercises/ex06_2_exercise/ShoppingCart.java @@ -0,0 +1,29 @@ +/* + * 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 ex06_2_exercise; + +import ex06_1_exercise.Item; + +/** + * + * @author Hp + */ +public class ShoppingCart { + public static void main(String args[]) { + // Declare and initialize 2 Item objects + Item item1 = new Item("Shirt"); + Item item2 = new Item("Pants"); + + // Print both item descriptions and run code. + System.out.println("item1: "+ item1.descr); + System.out.println("item2: "+ item2.descr); + + // Assign one item to another and run it again. + item1 = item2; + System.out.println("item1: "+ item1.descr); + System.out.println("item2: "+ item2.descr); + } +} diff --git a/src/main/java/sofflix/exercises/ex07_1_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex07_1_exercise/ShoppingCart.java new file mode 100644 index 0000000..15a4c62 --- /dev/null +++ b/src/main/java/sofflix/exercises/ex07_1_exercise/ShoppingCart.java @@ -0,0 +1,25 @@ +/* + * 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 ex07_1_exercise; + +/** + * + * @author Hp + */ +public class ShoppingCart { + public static void main (String[] args){ + String custName = "Steve Smith"; + String firstName; + int spaceIdx; + + // Get the index of the space character (" ") in custName. + spaceIdx = custName.indexOf(" "); + + // Use the substring method parse out the first name and print it. + firstName = custName.substring(0,spaceIdx); + System.out.println("First Name: "+ firstName); + } +} diff --git a/src/main/java/sofflix/exercises/ex07_2_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex07_2_exercise/ShoppingCart.java new file mode 100644 index 0000000..6ac077c --- /dev/null +++ b/src/main/java/sofflix/exercises/ex07_2_exercise/ShoppingCart.java @@ -0,0 +1,35 @@ +/* + * 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 ex07_2_exercise; + +/** + * + * @author Hp + */ +public class ShoppingCart { + public static void main (String[] args){ + String custName = "Steve Smith"; + String firstName; + int spaceIdx; + StringBuilder sb; + + spaceIdx = custName.indexOf(" "); + firstName = custName.substring(0, spaceIdx); + System.out.println(firstName); + + //Instantiate and initialize sb to firstName. + sb = new StringBuilder(firstName); + // Put the full name back together, using StringBuilder append method. + String lastName = " Smith"; + sb.append(lastName); + System.out.println("Complete Name: "+ sb); + // You can just enter the String literal for the last name. + sb = new StringBuilder(firstName); + sb.append(custName.substring(custName.indexOf(" "))); + // Print the full name. + System.out.println("Complete Name: "+ sb); + } +} diff --git a/src/main/java/sofflix/exercises/ex07_3_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex07_3_exercise/ShoppingCart.java new file mode 100644 index 0000000..8aba6b2 --- /dev/null +++ b/src/main/java/sofflix/exercises/ex07_3_exercise/ShoppingCart.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 ex07_3_exercise; + +/** + * + * @author Hp + */ +public class ShoppingCart { + public static void main(String[] args){ + int int1; + + // Declare and initialize variables of type long, float, and char. + long lLong = 10_000_000_000_000L; + float fFloat = 5.9F; + char cChar = 'A'; + // Print the long variable. + System.out.println("The value Long is: "+ lLong); + // Assign the long to the int and print the int variable. + int1 = (int) lLong; + System.out.println("The Value int: "+ int1); + } +} diff --git a/src/main/java/sofflix/exercises/ex08_1_exercise/Item.java b/src/main/java/sofflix/exercises/ex08_1_exercise/Item.java new file mode 100644 index 0000000..e393b43 --- /dev/null +++ b/src/main/java/sofflix/exercises/ex08_1_exercise/Item.java @@ -0,0 +1,24 @@ +/* + * 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 ex08_1_exercise; + +/** + * + * @author Hp + */ +public class Item { + char color; + + // declare and code the setColor method + public boolean setColor(char colorCode){ + if(colorCode == ' '){ + return false; + }else{ + color = colorCode; + return true; + } + } +} diff --git a/src/main/java/sofflix/exercises/ex08_1_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex08_1_exercise/ShoppingCart.java new file mode 100644 index 0000000..eee67be --- /dev/null +++ b/src/main/java/sofflix/exercises/ex08_1_exercise/ShoppingCart.java @@ -0,0 +1,34 @@ +/* + * 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 ex08_1_exercise; + +/** + * + * @author Hp + */ +public class ShoppingCart { + + public static void main(String[] args) { + Item item1 = new Item(); + + // Call the setColor method on item1. Print the new color value from + // item1 if the method returns true. Otherwise print an "invalid color" + // message. + if (item1.setColor(' ')) { + System.out.println(item1.color); + } else { + System.out.println("Invalid color"); + } + + // Test the class, using both valid and invalid colors. + if (item1.setColor('R')) { + System.out.println(item1.color); + } else { + System.out.println("Invalid color"); + } + + } +} diff --git a/src/main/java/sofflix/exercises/ex08_2_exercise/Item.java b/src/main/java/sofflix/exercises/ex08_2_exercise/Item.java new file mode 100644 index 0000000..d792ea9 --- /dev/null +++ b/src/main/java/sofflix/exercises/ex08_2_exercise/Item.java @@ -0,0 +1,42 @@ +/* + * 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 ex08_2_exercise; + +/** + * + * @author Hp + */ +public class Item { + String desc; + int quantity; + double price; + char colorCode = 'U'; //'U' = Undetermined + + public void displayItem() { + System.out.println("Item: " + desc + ", " + quantity + ", " + + price + ", "+colorCode); + } + + // Write a public 3-arg setItemDisplay method that returns void. + public void setItemFields(String desc,int quantity,double price){ + this.desc = desc; + this.quantity = quantity; + this.price = price; + + } + + + // Write a public 4-arg setItemDisplay method that returns an int. + public int setItemFields(String desc,int quantity,double price, char colorCode){ + if(colorCode == ' '){ + return -1; + }else{ + this.colorCode = colorCode; + setItemFields(desc, quantity, price); + return this.colorCode; + } + } +} \ No newline at end of file diff --git a/src/main/java/sofflix/exercises/ex08_2_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex08_2_exercise/ShoppingCart.java new file mode 100644 index 0000000..af5ece8 --- /dev/null +++ b/src/main/java/sofflix/exercises/ex08_2_exercise/ShoppingCart.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 ex08_2_exercise; + +/** + * + * @author Hp + */ +public class ShoppingCart { + public static void main(String[] args) { + Item item1 = new Item(); + + // Call the 3-arg setItemFields method and then call displayItem. + item1.setItemFields("Shirt", 2, 450.00); + item1.displayItem(); + + // Call the 4-arg setItemFields method, checking the return value. + int value = item1.setItemFields("Shirt", 2, 450, 'R'); + System.out.println("Value: "+ value); + + // Test your code for both valid and invalid values + if(value<0){ + System.out.println("invalid color code message"); + }else{ + item1.displayItem(); + } + + value = item1.setItemFields("Shirt", 2, 450, ' '); + + if(value<0){ + System.out.println("invalid color code message"); + }else{ + item1.displayItem(); + } + + } +} \ No newline at end of file diff --git a/src/main/java/sofflix/exercises/ex09_1_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex09_1_exercise/ShoppingCart.java new file mode 100644 index 0000000..f399621 --- /dev/null +++ b/src/main/java/sofflix/exercises/ex09_1_exercise/ShoppingCart.java @@ -0,0 +1,35 @@ +/* + * 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 ex09_1_exercise; + +/** + * + * @author Hp + */ +public class ShoppingCart { + private String name; + private String ssn; + + // Encapsulate this class. Make ssn read only. + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSsn() { + return ssn; + } + + private void setSsn(String ssn) { + this.ssn = ssn; + } + + +} diff --git a/src/main/java/sofflix/exercises/ex09_2_exercise/Customer.java b/src/main/java/sofflix/exercises/ex09_2_exercise/Customer.java new file mode 100644 index 0000000..9b173e6 --- /dev/null +++ b/src/main/java/sofflix/exercises/ex09_2_exercise/Customer.java @@ -0,0 +1,32 @@ +/* + * 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 ex09_2_exercise; + +/** + * + * @author Hp + */ +public class Customer { + private String name; + private String ssn; + + //Add a custom constructor + public Customer(String name, String ssn){ + this.name = name; + this.ssn = ssn; + } + + public String getName(){ + return name; + } + public void setName(String n){ + name = n; + } + + public String getSSN(){ + return ssn; + } +} diff --git a/src/main/java/sofflix/exercises/ex09_2_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex09_2_exercise/ShoppingCart.java new file mode 100644 index 0000000..47389a5 --- /dev/null +++ b/src/main/java/sofflix/exercises/ex09_2_exercise/ShoppingCart.java @@ -0,0 +1,23 @@ +/* + * 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 ex09_2_exercise; + +/** + * + * @author Hp + */ +public class ShoppingCart { + + public static void main(String args[]) { + // Declare, instantiate, and initialize a Customer object + Customer shop = new Customer("Felix", "1234423423"); + + + // Print the customer object name + System.out.println("name: "+ shop.getName()); + + } +} diff --git a/src/main/java/sofflix/exercises/ex10_1_exercise/TestClass.java b/src/main/java/sofflix/exercises/ex10_1_exercise/TestClass.java new file mode 100644 index 0000000..be05b29 --- /dev/null +++ b/src/main/java/sofflix/exercises/ex10_1_exercise/TestClass.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 ex10_1_exercise; + +/** + * + * @author Hp + */ +public class TestClass { + public static void main(String args[]) { + int x = 4, y = 9; + if (y / x < 3) { + x += y; + } else { + x *= y; + } + System.out.println("After if stmt, x = " + x); + + // Use a ternary operator to perform the same logic as above. + x = (y/x<3)?(x+=y):(x*=y); + System.out.println("After if stmt, x = " + x); + } +} diff --git a/src/main/java/sofflix/exercises/ex10_2_exercise/Order.java b/src/main/java/sofflix/exercises/ex10_2_exercise/Order.java new file mode 100644 index 0000000..527c00b --- /dev/null +++ b/src/main/java/sofflix/exercises/ex10_2_exercise/Order.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 ex10_2_exercise; + +/** + * + * @author Hp + */ +public class Order { + + static final char CORP = 'C'; + static final char PRIVATE = 'P'; + static final char NONPROFIT = 'N'; + String name; + double total; + String stateCode; + double discount; + char custType; + + public Order(String name, double total, String state, char custType) { + this.name = name; + this.total = total; + this.stateCode = state; + this.custType = custType; + calcDiscount(); + } + + public String getDiscount() { + return Double.toString(discount) + "%"; + } + + // Code the calcDiscount method. + public void calcDiscount() { + if (custType == NONPROFIT) { + if (total > 900) { + discount = 10.0; + } else { + discount = 5.0; + } + } else if (custType == PRIVATE) { + if (total > 900) { + discount = 7.0; + } else { + discount = 0; + } + } else if (custType == CORP) { + if(total<500){ + discount = 8.0; + }else{ + discount = 5.0; + } + } + } +} diff --git a/src/main/java/sofflix/exercises/ex10_2_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex10_2_exercise/ShoppingCart.java new file mode 100644 index 0000000..51f68f2 --- /dev/null +++ b/src/main/java/sofflix/exercises/ex10_2_exercise/ShoppingCart.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 ex10_2_exercise; + +/** + * + * @author Hp + */ +public class ShoppingCart { + public static void main(String args[]){ + Order order = new Order("Rick Wilson", 910.00, "VA", Order.NONPROFIT); + System.out.println("Discount is: "+ order.getDiscount()); + } +} diff --git a/src/main/java/sofflix/exercises/ex10_3_exercise/Order.java b/src/main/java/sofflix/exercises/ex10_3_exercise/Order.java new file mode 100644 index 0000000..c2675db --- /dev/null +++ b/src/main/java/sofflix/exercises/ex10_3_exercise/Order.java @@ -0,0 +1,47 @@ +/* + * 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 ex10_3_exercise; + +/** + * + * @author Hp + */ +public class Order { + static final char CORP = 'C'; + static final char PRIVATE = 'P'; + static final char NONPROFIT = 'N'; + String name; + double total; + String stateCode; + double discount; + char custType; + + public Order(String name, double total, String state, char custType) { + this.name = name; + this.total = total; + this.stateCode = state; + this.custType = custType; + calcDiscount(); + } + + public String getDiscount(){ + return Double.toString(discount) + "%"; + } + + public void calcDiscount() { + // Replace the if logic with a switch statement. + switch (custType) { + case NONPROFIT: discount = total>900? 10.00 : 5.00; + break; + case PRIVATE: discount = total >900? 7.00 : 0.00; + break; + case CORP: discount = total <500? 8.00 : 5.00; + break; + default: System.out.println("custype not allowed"); + break; + } + } +} diff --git a/src/main/java/sofflix/exercises/ex10_3_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex10_3_exercise/ShoppingCart.java new file mode 100644 index 0000000..63e25c8 --- /dev/null +++ b/src/main/java/sofflix/exercises/ex10_3_exercise/ShoppingCart.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 ex10_3_exercise; + +/** + * + * @author Hp + */ +public class ShoppingCart { + public static void main(String args[]){ + Order order = new Order("Rick Wilson", 910.00, "VA", Order.NONPROFIT); + System.out.println("Discount is: "+ order.getDiscount()); + } +} diff --git a/src/main/java/sofflix/practices/practice_06_objectsclasses_1/soccer/Game.java b/src/main/java/sofflix/practices/practice_06_objectsclasses_1/soccer/Game.java new file mode 100644 index 0000000..3084f28 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_06_objectsclasses_1/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 Hp + */ +public class Game { + public Team homeTeam; + public Team awayTeam; + public Goal[] goals; +} diff --git a/src/main/java/sofflix/practices/practice_06_objectsclasses_1/soccer/Goal.java b/src/main/java/sofflix/practices/practice_06_objectsclasses_1/soccer/Goal.java new file mode 100644 index 0000000..cfc2f19 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_06_objectsclasses_1/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 Hp + */ +public class Goal { + public Team theTeam; + public Player thePlayer; + public double theTime; +} diff --git a/src/main/java/sofflix/practices/practice_06_objectsclasses_1/soccer/League.java b/src/main/java/sofflix/practices/practice_06_objectsclasses_1/soccer/League.java new file mode 100644 index 0000000..561d9aa --- /dev/null +++ b/src/main/java/sofflix/practices/practice_06_objectsclasses_1/soccer/League.java @@ -0,0 +1,55 @@ +/* + * 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 Hp + */ +public class League { + public static void main(String[] args) { + 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"; + for (Player thePlayer:team1.playerArray) { + System.out.println(thePlayer.playerName); + } + + 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 = "Robie Burns"; + + team2.playerArray[2] = new Player(); + team2.playerArray[2].playerName = "Rafael Sabatini"; + + for (Player thePlayer:team2.playerArray) { + System.out.println(thePlayer.playerName); + } + + + + + } +} diff --git a/src/main/java/sofflix/practices/practice_06_objectsclasses_1/soccer/Player.java b/src/main/java/sofflix/practices/practice_06_objectsclasses_1/soccer/Player.java new file mode 100644 index 0000000..c03fc87 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_06_objectsclasses_1/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 Hp + */ +public class Player { + public String playerName; +} diff --git a/src/main/java/sofflix/practices/practice_06_objectsclasses_1/soccer/Team.java b/src/main/java/sofflix/practices/practice_06_objectsclasses_1/soccer/Team.java new file mode 100644 index 0000000..bf26686 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_06_objectsclasses_1/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 Hp + */ +public class Team { + public String teamName; + public Player[] playerArray; +} diff --git a/src/main/java/sofflix/practices/practice_06_objectsclasses_2/soccer/Game.java b/src/main/java/sofflix/practices/practice_06_objectsclasses_2/soccer/Game.java new file mode 100644 index 0000000..f9732f7 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_06_objectsclasses_2/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/sofflix/practices/practice_06_objectsclasses_2/soccer/Goal.java b/src/main/java/sofflix/practices/practice_06_objectsclasses_2/soccer/Goal.java new file mode 100644 index 0000000..144a3d0 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_06_objectsclasses_2/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/sofflix/practices/practice_06_objectsclasses_2/soccer/League.java b/src/main/java/sofflix/practices/practice_06_objectsclasses_2/soccer/League.java new file mode 100644 index 0000000..19c907d --- /dev/null +++ b/src/main/java/sofflix/practices/practice_06_objectsclasses_2/soccer/League.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 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"; + + /* Practice 6-2. Remove the two for loops below */ + + + /* Practice 6-2. Create a Game here */ + Game currGame = new Game(); + currGame.homeTeam = team1; + currGame.awayTeam = team2; + + + /* Practice 6-2. Create a Goal object here */ + Goal goal1 = new Goal(); + goal1.thePlayer = currGame.homeTeam.playerArray[2]; + goal1.theTeam = currGame.homeTeam; + goal1.theTime = 55; + /* Practice 6-2. Put Goal object in a Goal array and assign Goal array to goals attribute of Game object */ + Goal[] theGoals = {goal1}; + currGame.goals = theGoals; + /* Practice 6-2. Print out the score of the Game */ + System.out.println("Goal scored after "+ + currGame.goals[0].theTime+ " mins by "+ + currGame.goals[0].thePlayer.playerName+" of "+ + currGame.goals[0].theTeam.teamName); + + String var =" Algo con espacios plus "; + System.out.println(var.trim()); + + + } +} diff --git a/src/main/java/sofflix/practices/practice_06_objectsclasses_2/soccer/Player.java b/src/main/java/sofflix/practices/practice_06_objectsclasses_2/soccer/Player.java new file mode 100644 index 0000000..07670b0 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_06_objectsclasses_2/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/sofflix/practices/practice_06_objectsclasses_2/soccer/Team.java b/src/main/java/sofflix/practices/practice_06_objectsclasses_2/soccer/Team.java new file mode 100644 index 0000000..247f229 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_06_objectsclasses_2/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/sofflix/practices/practice_07_manipulateformat_1/soccer/Game.java b/src/main/java/sofflix/practices/practice_07_manipulateformat_1/soccer/Game.java new file mode 100644 index 0000000..f9732f7 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_07_manipulateformat_1/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/sofflix/practices/practice_07_manipulateformat_1/soccer/Goal.java b/src/main/java/sofflix/practices/practice_07_manipulateformat_1/soccer/Goal.java new file mode 100644 index 0000000..144a3d0 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_07_manipulateformat_1/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/sofflix/practices/practice_07_manipulateformat_1/soccer/League.java b/src/main/java/sofflix/practices/practice_07_manipulateformat_1/soccer/League.java new file mode 100644 index 0000000..7799f30 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_07_manipulateformat_1/soccer/League.java @@ -0,0 +1,83 @@ +/* + * 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]); + + + + } + } + + for (Player thePlayer : team1.playerArray) { + String name[] = thePlayer.playerName.split(" "); + StringBuilder familyNameFirst = new StringBuilder(); + 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/sofflix/practices/practice_07_manipulateformat_1/soccer/Player.java b/src/main/java/sofflix/practices/practice_07_manipulateformat_1/soccer/Player.java new file mode 100644 index 0000000..07670b0 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_07_manipulateformat_1/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/sofflix/practices/practice_07_manipulateformat_1/soccer/Team.java b/src/main/java/sofflix/practices/practice_07_manipulateformat_1/soccer/Team.java new file mode 100644 index 0000000..247f229 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_07_manipulateformat_1/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; + +}