diff --git a/src/main/java/sofflix/exercises/ex03_1_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex03_1_exercise/ShoppingCart.java index 4c9a2aa..131b0c8 100644 --- a/src/main/java/sofflix/exercises/ex03_1_exercise/ShoppingCart.java +++ b/src/main/java/sofflix/exercises/ex03_1_exercise/ShoppingCart.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package ex03_1_exercise; +package sofflix.exercises.ex03_1_exercise; /** * diff --git a/src/main/java/sofflix/exercises/ex04_1_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex04_1_exercise/ShoppingCart.java index 1f23b42..5ac0fee 100644 --- a/src/main/java/sofflix/exercises/ex04_1_exercise/ShoppingCart.java +++ b/src/main/java/sofflix/exercises/ex04_1_exercise/ShoppingCart.java @@ -1,5 +1,5 @@ -package ex04_1_exercise; +package sofflix.exercises.ex04_1_exercise; public class ShoppingCart { diff --git a/src/main/java/sofflix/exercises/ex04_2_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex04_2_exercise/ShoppingCart.java index a5073aa..085a932 100644 --- a/src/main/java/sofflix/exercises/ex04_2_exercise/ShoppingCart.java +++ b/src/main/java/sofflix/exercises/ex04_2_exercise/ShoppingCart.java @@ -1,5 +1,5 @@ -package ex04_2_exercise; +package sofflix.exercises.ex04_2_exercise; public class ShoppingCart { diff --git a/src/main/java/sofflix/exercises/ex05_1_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex05_1_exercise/ShoppingCart.java index fa6c1fe..f357bda 100644 --- a/src/main/java/sofflix/exercises/ex05_1_exercise/ShoppingCart.java +++ b/src/main/java/sofflix/exercises/ex05_1_exercise/ShoppingCart.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package ex05_1_exercise; +package sofflix.exercises.ex05_1_exercise; /** * diff --git a/src/main/java/sofflix/exercises/ex05_2_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex05_2_exercise/ShoppingCart.java index 23af317..67adeab 100644 --- a/src/main/java/sofflix/exercises/ex05_2_exercise/ShoppingCart.java +++ b/src/main/java/sofflix/exercises/ex05_2_exercise/ShoppingCart.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package ex05_2_exercise; +package sofflix.exercises.ex05_2_exercise; /** * diff --git a/src/main/java/sofflix/exercises/ex05_3_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex05_3_exercise/ShoppingCart.java index 04e9a97..a213b77 100644 --- a/src/main/java/sofflix/exercises/ex05_3_exercise/ShoppingCart.java +++ b/src/main/java/sofflix/exercises/ex05_3_exercise/ShoppingCart.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package ex05_3_exercise; +package sofflix.exercises.ex05_3_exercise; /** * diff --git a/src/main/java/sofflix/exercises/ex06_1_exercise/Item.java b/src/main/java/sofflix/exercises/ex06_1_exercise/Item.java index 80e58e5..2ee9388 100644 --- a/src/main/java/sofflix/exercises/ex06_1_exercise/Item.java +++ b/src/main/java/sofflix/exercises/ex06_1_exercise/Item.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package ex06_1_exercise; +package sofflix.exercises.ex06_1_exercise; /** * diff --git a/src/main/java/sofflix/exercises/ex06_2_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex06_2_exercise/ShoppingCart.java index dbaf94e..6c2f03f 100644 --- a/src/main/java/sofflix/exercises/ex06_2_exercise/ShoppingCart.java +++ b/src/main/java/sofflix/exercises/ex06_2_exercise/ShoppingCart.java @@ -3,9 +3,9 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package ex06_2_exercise; +package sofflix.exercises.ex06_2_exercise; -import ex06_1_exercise.Item; +import sofflix.exercises.ex06_1_exercise.Item; /** * diff --git a/src/main/java/sofflix/exercises/ex07_1_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex07_1_exercise/ShoppingCart.java index 15a4c62..239d4c8 100644 --- a/src/main/java/sofflix/exercises/ex07_1_exercise/ShoppingCart.java +++ b/src/main/java/sofflix/exercises/ex07_1_exercise/ShoppingCart.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package ex07_1_exercise; +package sofflix.exercises.ex07_1_exercise; /** * diff --git a/src/main/java/sofflix/exercises/ex07_2_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex07_2_exercise/ShoppingCart.java index 6ac077c..ec5347a 100644 --- a/src/main/java/sofflix/exercises/ex07_2_exercise/ShoppingCart.java +++ b/src/main/java/sofflix/exercises/ex07_2_exercise/ShoppingCart.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package ex07_2_exercise; +package sofflix.exercises.ex07_2_exercise; /** * diff --git a/src/main/java/sofflix/exercises/ex07_3_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex07_3_exercise/ShoppingCart.java index 8aba6b2..63bce06 100644 --- a/src/main/java/sofflix/exercises/ex07_3_exercise/ShoppingCart.java +++ b/src/main/java/sofflix/exercises/ex07_3_exercise/ShoppingCart.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package ex07_3_exercise; +package sofflix.exercises.ex07_3_exercise; /** * diff --git a/src/main/java/sofflix/exercises/ex08_1_exercise/Item.java b/src/main/java/sofflix/exercises/ex08_1_exercise/Item.java index e393b43..e34c90b 100644 --- a/src/main/java/sofflix/exercises/ex08_1_exercise/Item.java +++ b/src/main/java/sofflix/exercises/ex08_1_exercise/Item.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package ex08_1_exercise; +package sofflix.exercises.ex08_1_exercise; /** * diff --git a/src/main/java/sofflix/exercises/ex08_1_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex08_1_exercise/ShoppingCart.java index eee67be..61776e3 100644 --- a/src/main/java/sofflix/exercises/ex08_1_exercise/ShoppingCart.java +++ b/src/main/java/sofflix/exercises/ex08_1_exercise/ShoppingCart.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package ex08_1_exercise; +package sofflix.exercises.ex08_1_exercise; /** * diff --git a/src/main/java/sofflix/exercises/ex08_2_exercise/Item.java b/src/main/java/sofflix/exercises/ex08_2_exercise/Item.java index d792ea9..f901acd 100644 --- a/src/main/java/sofflix/exercises/ex08_2_exercise/Item.java +++ b/src/main/java/sofflix/exercises/ex08_2_exercise/Item.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package ex08_2_exercise; +package sofflix.exercises.ex08_2_exercise; /** * diff --git a/src/main/java/sofflix/exercises/ex08_2_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex08_2_exercise/ShoppingCart.java index af5ece8..7c7efc5 100644 --- a/src/main/java/sofflix/exercises/ex08_2_exercise/ShoppingCart.java +++ b/src/main/java/sofflix/exercises/ex08_2_exercise/ShoppingCart.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package ex08_2_exercise; +package sofflix.exercises.ex08_2_exercise; /** * diff --git a/src/main/java/sofflix/exercises/ex09_1_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex09_1_exercise/ShoppingCart.java index f399621..5958282 100644 --- a/src/main/java/sofflix/exercises/ex09_1_exercise/ShoppingCart.java +++ b/src/main/java/sofflix/exercises/ex09_1_exercise/ShoppingCart.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package ex09_1_exercise; +package sofflix.exercises.ex09_1_exercise; /** * diff --git a/src/main/java/sofflix/exercises/ex09_2_exercise/Customer.java b/src/main/java/sofflix/exercises/ex09_2_exercise/Customer.java index 9b173e6..a0e8a45 100644 --- a/src/main/java/sofflix/exercises/ex09_2_exercise/Customer.java +++ b/src/main/java/sofflix/exercises/ex09_2_exercise/Customer.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package ex09_2_exercise; +package sofflix.exercises.ex09_2_exercise; /** * diff --git a/src/main/java/sofflix/exercises/ex09_2_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex09_2_exercise/ShoppingCart.java index 47389a5..322f1bf 100644 --- a/src/main/java/sofflix/exercises/ex09_2_exercise/ShoppingCart.java +++ b/src/main/java/sofflix/exercises/ex09_2_exercise/ShoppingCart.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package ex09_2_exercise; +package sofflix.exercises.ex09_2_exercise; /** * diff --git a/src/main/java/sofflix/exercises/ex10_1_exercise/TestClass.java b/src/main/java/sofflix/exercises/ex10_1_exercise/TestClass.java index be05b29..39b5dee 100644 --- a/src/main/java/sofflix/exercises/ex10_1_exercise/TestClass.java +++ b/src/main/java/sofflix/exercises/ex10_1_exercise/TestClass.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package ex10_1_exercise; +package sofflix.exercises.ex10_1_exercise; /** * diff --git a/src/main/java/sofflix/exercises/ex10_2_exercise/Order.java b/src/main/java/sofflix/exercises/ex10_2_exercise/Order.java index 527c00b..529ce94 100644 --- a/src/main/java/sofflix/exercises/ex10_2_exercise/Order.java +++ b/src/main/java/sofflix/exercises/ex10_2_exercise/Order.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package ex10_2_exercise; +package sofflix.exercises.ex10_2_exercise; /** * diff --git a/src/main/java/sofflix/exercises/ex10_2_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex10_2_exercise/ShoppingCart.java index 51f68f2..86b196c 100644 --- a/src/main/java/sofflix/exercises/ex10_2_exercise/ShoppingCart.java +++ b/src/main/java/sofflix/exercises/ex10_2_exercise/ShoppingCart.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package ex10_2_exercise; +package sofflix.exercises.ex10_2_exercise; /** * diff --git a/src/main/java/sofflix/exercises/ex10_3_exercise/Order.java b/src/main/java/sofflix/exercises/ex10_3_exercise/Order.java index c2675db..b2538e1 100644 --- a/src/main/java/sofflix/exercises/ex10_3_exercise/Order.java +++ b/src/main/java/sofflix/exercises/ex10_3_exercise/Order.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package ex10_3_exercise; +package sofflix.exercises.ex10_3_exercise; /** * diff --git a/src/main/java/sofflix/exercises/ex10_3_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex10_3_exercise/ShoppingCart.java index 63e25c8..fd5a65e 100644 --- a/src/main/java/sofflix/exercises/ex10_3_exercise/ShoppingCart.java +++ b/src/main/java/sofflix/exercises/ex10_3_exercise/ShoppingCart.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package ex10_3_exercise; +package sofflix.exercises.ex10_3_exercise; /** * diff --git a/src/main/java/sofflix/exercises/ex11_1_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex11_1_exercise/ShoppingCart.java new file mode 100644 index 0000000..cfdf0e5 --- /dev/null +++ b/src/main/java/sofflix/exercises/ex11_1_exercise/ShoppingCart.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 sofflix.exercises.ex11_1_exercise; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +/** + * + * @author Omar + */ +public class ShoppingCart { + public static void main(String[] args){ + // Declare a LocalDateTime object, orderDate + LocalDateTime orderDate; + + // Initialize the orderDate to the current date and time. Print it. + orderDate = LocalDateTime.now(); + System.out.println(orderDate); + // Format orderDate using ISO_LOCAL_DATE; Print it. + String orderDateFormat = orderDate.format(DateTimeFormatter.ISO_LOCAL_DATE); + System.out.println(orderDateFormat); + } +} diff --git a/src/main/java/sofflix/exercises/ex11_2_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex11_2_exercise/ShoppingCart.java new file mode 100644 index 0000000..05fd3ae --- /dev/null +++ b/src/main/java/sofflix/exercises/ex11_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 sofflix.exercises.ex11_2_exercise; + +/** + * + * @author Omar + */ +public class ShoppingCart { + public static void main(String[] args) { + String name; + int age; + + + if(args.length <2){ + // Print an error message if fewer than 2 args are passed in. + System.out.println("Se requieren dos argumentos de entrada..."); + }else{ + // Parse the args array to populate name and age. + name = args[0]; + age = Integer.parseInt(args[1]); + System.out.println("name: "+name); + System.out.println("age: "+age); + } + + + + + } +} diff --git a/src/main/java/sofflix/exercises/ex11_3_exercise/Item.java b/src/main/java/sofflix/exercises/ex11_3_exercise/Item.java new file mode 100644 index 0000000..b41e65b --- /dev/null +++ b/src/main/java/sofflix/exercises/ex11_3_exercise/Item.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 sofflix.exercises.ex11_3_exercise; + +/** + * + * @author Omar + */ +public class Item { + private int id; + private String desc; + private double price; + private boolean isBackOrdered; + private static int nextId = 1; + + public Item(String desc, double price) { + setId(); + this.desc = desc; + this.price = price; + checkStock(); + } + + private void checkStock(){ // typically would query a database here + + // Negative random numbers will result in isBackOrdered = true + double num = Math.random() * 1000; + + if ((int)num % 2 == 1 ){ + this.isBackOrdered = true; + System.out.println(getDesc() +" is back ordered."); + } + else this.isBackOrdered(); + } + + private void setId() { + id = Item.nextId++; + } + + public int getId() { + return id; + } + + public String getDesc() { + return desc; + } + + public void setDesc(String desc) { + this.desc = desc; + } + + public double getPrice() { + return price; + } + + public void setPrice(double price) { + this.price = price; + } + + public boolean isBackOrdered() { + return isBackOrdered; + } +} diff --git a/src/main/java/sofflix/exercises/ex11_3_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex11_3_exercise/ShoppingCart.java new file mode 100644 index 0000000..90f66b6 --- /dev/null +++ b/src/main/java/sofflix/exercises/ex11_3_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 sofflix.exercises.ex11_3_exercise; + +/** + * + * @author Omar + */ +public class ShoppingCart { + Item[] items = {new Item("Shirt",25.60), + new Item("WristBand",0), + new Item("Pants",35.99)}; + + public static void main(String[] args){ + ShoppingCart cart = new ShoppingCart(); + cart.displayTotal(); + } + + // Use a standard for loop to iterate through the items array, adding up the total price + // Skip any items that are back ordered. Display the Shopping Cart total. + public void displayTotal(){ + double total = 0.0; + for (int i = 0; i < items.length; i++) { + Item item = items[i]; + if(!item.isBackOrdered()){ + continue; + }else{ + total += item.getPrice(); + } + } + System.out.println("La suma total de los articulos es : "+ total); + } +} diff --git a/src/main/java/sofflix/exercises/ex11_4_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex11_4_exercise/ShoppingCart.java new file mode 100644 index 0000000..2deff1f --- /dev/null +++ b/src/main/java/sofflix/exercises/ex11_4_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 sofflix.exercises.ex11_4_exercise; + +import java.util.ArrayList; + +/** + * + * @author Omar + */ +public class ShoppingCart { + public static void main(String[] args){ + // Declare, instantiate, and initialize an ArrayList of Strings. Print and test your code. + ArrayList items = new ArrayList(); + items.add("Shirt"); + items.add("Jeans"); + items.add("Socks"); + for(String item: items){ + System.out.println("item: "+ item); + } + // add (insert) another element at a specific index + System.out.println("================================="); + items.add(2, "Shorts"); + for(String item: items){ + System.out.println("item: "+ item); + } + // Check for the existence of a specific String element. + // If it exists, remove it. + if(items.contains("Jeans")){ + items.remove("Jeans"); + } + System.out.println("================================="); + for(String item: items){ + System.out.println("item: "+ item); + } + } +} diff --git a/src/main/java/sofflix/exercises/ex12_1_exercise/Item.java b/src/main/java/sofflix/exercises/ex12_1_exercise/Item.java new file mode 100644 index 0000000..6a7e717 --- /dev/null +++ b/src/main/java/sofflix/exercises/ex12_1_exercise/Item.java @@ -0,0 +1,60 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package sofflix.exercises.ex12_1_exercise; + +/** + * + * @author Omar + */ +public class Item { + private int id; + private String desc; + private double price; + static int nextId = 1; + + public Item(){ + // set default values + setId(); + setDesc("No description assigned."); + setPrice(0.00); + } + + public Item(String desc, double price) { + setId(); + setDesc(desc); + setPrice(price); + } + + public void display(){ + System.out.println("Item description: "+getDesc()); + System.out.println("\tID: "+getId()); + System.out.println("\tPrice: "+getPrice()); + } + + private void setId() { + id = Item.nextId++; + } + + public int getId() { + return id; + } + + public String getDesc() { + return desc; + } + + private void setDesc(String desc) { + this.desc = desc; + } + + public double getPrice() { + return price; + } + + private void setPrice(double price) { + this.price = price; + } +} diff --git a/src/main/java/sofflix/exercises/ex12_1_exercise/Shirt.java b/src/main/java/sofflix/exercises/ex12_1_exercise/Shirt.java new file mode 100644 index 0000000..534daa5 --- /dev/null +++ b/src/main/java/sofflix/exercises/ex12_1_exercise/Shirt.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 sofflix.exercises.ex12_1_exercise; + +/** + * + * @author Omar + */ +public class Shirt extends Item{ + private char size; + private char colorCode; + + public Shirt(double price, char size, char colorCode ) { + super("Shirt", price); + this.size = size; + this.colorCode = colorCode; + } + + +} diff --git a/src/main/java/sofflix/exercises/ex12_1_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex12_1_exercise/ShoppingCart.java new file mode 100644 index 0000000..5738db3 --- /dev/null +++ b/src/main/java/sofflix/exercises/ex12_1_exercise/ShoppingCart.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 sofflix.exercises.ex12_1_exercise; + +/** + * + * @author Omar + */ +public class ShoppingCart { + public static void main(String[] args){ + + // instantiate a Shirt object and call display() on the object reference + Shirt shirt = new Shirt(500,'L','B'); + shirt.display(); + } +} diff --git a/src/main/java/sofflix/exercises/ex12_2_exercise/Item.java b/src/main/java/sofflix/exercises/ex12_2_exercise/Item.java new file mode 100644 index 0000000..fe34f2d --- /dev/null +++ b/src/main/java/sofflix/exercises/ex12_2_exercise/Item.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 sofflix.exercises.ex12_2_exercise; + +/** + * + * @author Omar + */ +public class Item { + private int id; + private String desc; + private double price; + static int nextId = 1; + + // Default constructor sets default values + public Item(){ + setId(); + setDesc("No description assigned."); + setPrice(0.00); + } + + // Overloaded constructor takes description and price + public Item(String desc, double price) { + setId(); + setDesc(desc); + setPrice(price); + } + + public void display(){ + System.out.println("Item description: "+getDesc()); + System.out.println("\tID: "+getId()); + System.out.println("\tPrice: "+getPrice()); + } + + // Getter and Setter methods + private void setId() { + id = Item.nextId++; + } + + public int getId() { + return id; + } + + public String getDesc() { + return desc; + } + + private void setDesc(String desc) { + this.desc = desc; + } + + public double getPrice() { + return price; + } + + private void setPrice(double price) { + this.price = price; + } +} diff --git a/src/main/java/sofflix/exercises/ex12_2_exercise/Shirt.java b/src/main/java/sofflix/exercises/ex12_2_exercise/Shirt.java new file mode 100644 index 0000000..a59cdcb --- /dev/null +++ b/src/main/java/sofflix/exercises/ex12_2_exercise/Shirt.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 sofflix.exercises.ex12_2_exercise; + +/** + * + * @author Omar + */ +public class Shirt extends Item{ + private char size; + private char colorCode; + + public Shirt(double price, char size, char colorCode){ + super ("Shirt", price); + this.size = size; + this.colorCode = colorCode; + } + + // Override display() in the superclass to also show size and colorCode. + // Avoid duplicating code. + public void display(){ + super.display(); + System.out.println("size: "+this.size); + System.out.println("colorCode: "+this.colorCode); + } +} diff --git a/src/main/java/sofflix/exercises/ex12_2_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex12_2_exercise/ShoppingCart.java new file mode 100644 index 0000000..08dd745 --- /dev/null +++ b/src/main/java/sofflix/exercises/ex12_2_exercise/ShoppingCart.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 sofflix.exercises.ex12_2_exercise; + +/** + * + * @author Omar + */ +public class ShoppingCart { + public static void main(String[] args){ + // instantiate a Shirt object and call the display() method + Shirt shirt = new Shirt(25.99, 'M', 'P'); + shirt.display(); + + } +} diff --git a/src/main/java/sofflix/exercises/ex12_3_exercise/Item.java b/src/main/java/sofflix/exercises/ex12_3_exercise/Item.java new file mode 100644 index 0000000..3082620 --- /dev/null +++ b/src/main/java/sofflix/exercises/ex12_3_exercise/Item.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 sofflix.exercises.ex12_3_exercise; + +/** + * + * @author Omar + */ +public class Item { + private int id; + private String desc; + private double price; + static int nextId = 1; + + // Default constructor sets default values + public Item(){ + setId(); + setDesc("No description assigned."); + setPrice(0.00); + } + + // Overloaded constructor takes description and price + public Item(String desc, double price) { + setId(); + setDesc(desc); + setPrice(price); + } + + public void display(){ + System.out.println("Item description: "+getDesc()); + System.out.println("\tID: "+getId()); + System.out.println("\tPrice: "+getPrice()); + } + + // Getter and Setter methods + private void setId() { + id = Item.nextId++; + } + + public int getId() { + return id; + } + + public String getDesc() { + return desc; + } + + private void setDesc(String desc) { + this.desc = desc; + } + + public double getPrice() { + return price; + } + + private void setPrice(double price) { + this.price = price; + } +} diff --git a/src/main/java/sofflix/exercises/ex12_3_exercise/Shirt.java b/src/main/java/sofflix/exercises/ex12_3_exercise/Shirt.java new file mode 100644 index 0000000..f7685ce --- /dev/null +++ b/src/main/java/sofflix/exercises/ex12_3_exercise/Shirt.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 sofflix.exercises.ex12_3_exercise; + +/** + * + * @author Omar + */ +public class Shirt extends Item{ + private char size; + private char colorCode; + + public Shirt(double price, char size, char colorCode){ + super ("Shirt", price); + this.size = size; + this.colorCode = colorCode; + } + + public void display(){ + super.display(); + System.out.println("\tSize: "+size); + System.out.println("\tColor Code: "+ colorCode); + } + + // Code a public getColor method that converts the colorCode to a the color name + // Use a switch statement. Return the color name. + + public String getColor(){ + String color; + switch(this.colorCode){ + case 'R': color ="Red"; + break; + case 'B': color="Blue"; + break; + case 'Y': color="Yellow"; + break; + default: color="Error colorCode"; + } + return color; + } +} diff --git a/src/main/java/sofflix/exercises/ex12_3_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex12_3_exercise/ShoppingCart.java new file mode 100644 index 0000000..2615a76 --- /dev/null +++ b/src/main/java/sofflix/exercises/ex12_3_exercise/ShoppingCart.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 sofflix.exercises.ex12_3_exercise; + +/** + * + * @author Omar + */ +public class ShoppingCart { + public static void main(String[] args) { + // declare and instantiate a Shirt object using an Item reference type + Shirt shirtReference = new Shirt(200, 'S', 'B'); + + // call the display method on the object, then the getColor method + shirtReference.display(); + if(shirtReference instanceof Shirt){ + Shirt shirt = (Shirt) shirtReference; + String color = shirt.getColor(); + System.out.println("The color shirt is: "+ color); + }else{ + System.out.println("The object is not a Shirt"); + } + } +} diff --git a/src/main/java/sofflix/exercises/ex13_1_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex13_1_exercise/ShoppingCart.java new file mode 100644 index 0000000..de28a27 --- /dev/null +++ b/src/main/java/sofflix/exercises/ex13_1_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 te +mplate in the editor. + */ +package sofflix.exercises.ex13_1_exercise; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * + * @author Hp + */ +public class ShoppingCart { + public static void main(String[] args){ + String[] days = {"monday","saturday","tuesday","sunday","friday"}; + + // Convert the days array into an ArrayList + List listDays = new ArrayList(Arrays.asList(days)); + // Loop through the ArrayList, printing out "sunday" elements in + for (String day : listDays) { + // upper case (use toUpperCase() method of String class) + if(day.equals("sunday")){ + System.out.println(day.toUpperCase()); + }else{ + // Print all other days in lower case + System.out.println(day); + } + } + // Print out the ArrayList + System.out.println(listDays); + } +} diff --git a/src/main/java/sofflix/exercises/ex13_2_exercise/Item.java b/src/main/java/sofflix/exercises/ex13_2_exercise/Item.java new file mode 100644 index 0000000..3213d04 --- /dev/null +++ b/src/main/java/sofflix/exercises/ex13_2_exercise/Item.java @@ -0,0 +1,78 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package sofflix.exercises.ex13_2_exercise; + +/** + * + * @author Hp + */ +public class Item { + private int id; + private String desc; + private double price; + private char colorCode; + static int nextId = 1; + + // Default constructor sets default values + public Item(){ + setId(); + setDesc("No description assigned."); + setPrice(0.00); + setColorCode('U'); + } + + // Overloaded constructor takes description and price + public Item(String desc, double price, char cCode) { + setId(); + setDesc(desc); + setPrice(price); + setColorCode(cCode); + } + + public void display(){ + System.out.println("Item description: "+getDesc()); + System.out.println("\tID: "+getId()); + System.out.println("\tPrice: "+getPrice()); + System.out.println("\tColor: "+getColorCode()); + } + + // Getter and Setter methods + private void setId() { + id = Item.nextId++; + } + + public int getId() { + return id; + } + + public String getDesc() { + return desc; + } + + private void setDesc(String desc) { + this.desc = desc; + } + + public double getPrice() { + return price; + } + + private void setPrice(double price) { + this.price = price; + } + + public char getColorCode() { + return colorCode; + } + + public void setColorCode(char colorCode) { + this.colorCode = colorCode; + } + + public String toString(){ + return this.getDesc(); + } +} diff --git a/src/main/java/sofflix/exercises/ex13_2_exercise/Shirt.java b/src/main/java/sofflix/exercises/ex13_2_exercise/Shirt.java new file mode 100644 index 0000000..9fbc586 --- /dev/null +++ b/src/main/java/sofflix/exercises/ex13_2_exercise/Shirt.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 sofflix.exercises.ex13_2_exercise; + +/** + * + * @author Hp + */ +public class Shirt extends Item{ + private char size; + + public Shirt(double price, char size, char colorCode){ + super ("Shirt", price, colorCode); + this.setSize(size); + } + + public void display(){ + super.display(); + System.out.println("\tSize: "+getSize()); + } + + public char getSize() { + return size; + } + + public void setSize(char size) { + this.size = size; + } +} diff --git a/src/main/java/sofflix/exercises/ex13_2_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex13_2_exercise/ShoppingCart.java new file mode 100644 index 0000000..eacffb6 --- /dev/null +++ b/src/main/java/sofflix/exercises/ex13_2_exercise/ShoppingCart.java @@ -0,0 +1,46 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package sofflix.exercises.ex13_2_exercise; + +import java.util.ArrayList; +import java.util.function.Predicate; + +/** + * + * @author Hp + */ +public class ShoppingCart { + ArrayList items = new ArrayList(); + + public static void main(String[] args){ + ShoppingCart cart = new ShoppingCart(); + cart.fillCart(); + cart.removeItemFromCart("Trousers"); + + } + + public void fillCart(){ + items.add(new Shirt(40.95,'M','R')); + items.add(new Shirt(32.99,'M','Y')); + items.add(new Trousers(59.99,34,'B',"Relaxed",'M')); + items.add(new Trousers(75.50,8,'G',"Skinny",'F')); + } + + public void removeItemFromCart(String desc){ + // remove all Trousers from the items list, then print out the list + items.removeIf(new Predicate(){ + public boolean test(Item item){ + if(item.getDesc().equals("Trousers")){ + return true; + }else{ + return false; + } + } + }); + + System.out.println(items); + } +} diff --git a/src/main/java/sofflix/exercises/ex13_2_exercise/Trousers.java b/src/main/java/sofflix/exercises/ex13_2_exercise/Trousers.java new file mode 100644 index 0000000..f234d99 --- /dev/null +++ b/src/main/java/sofflix/exercises/ex13_2_exercise/Trousers.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 sofflix.exercises.ex13_2_exercise; + +/** + * + * @author Hp + */ +public class Trousers extends Item { + private int size; + private char gender; + private String fit; + + public Trousers(double price, int size, char colorCode, String fit,char gender){ + super ("Trousers", price, colorCode); + this.setSize(size); + this.setGender(gender); + this.setFit(fit); + } + + public void display(){ + super.display(); + System.out.println("\tSize: "+getSize()); + System.out.println("\tGender: "+ getGender()); + System.out.println("\tFit: "+ getFit()); + } + + public int getSize() { + return size; + } + + public void setSize(int size) { + this.size = size; + } + + public char getGender() { + return gender; + } + + public void setGender(char gender) { + this.gender = gender; + } + + public String getFit() { + return fit; + } + + public void setFit(String fit) { + this.fit = fit; + } +} diff --git a/src/main/java/sofflix/exercises/ex14_1_exercise/Calculator.java b/src/main/java/sofflix/exercises/ex14_1_exercise/Calculator.java new file mode 100644 index 0000000..9e7417e --- /dev/null +++ b/src/main/java/sofflix/exercises/ex14_1_exercise/Calculator.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 sofflix.exercises.ex14_1_exercise; + +/** + * + * @author Hp + */ +public class Calculator { + public int add(int x, int y){ + return x + y; + } + // This method could throw an ArithmeticException + public double divide(int x, int y)throws ArithmeticException{ + return x / y; + } +} diff --git a/src/main/java/sofflix/exercises/ex14_1_exercise/ShoppingCart.java b/src/main/java/sofflix/exercises/ex14_1_exercise/ShoppingCart.java new file mode 100644 index 0000000..f29ec13 --- /dev/null +++ b/src/main/java/sofflix/exercises/ex14_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 sofflix.exercises.ex14_1_exercise; + +/** + * + * @author Hp + */ +public class ShoppingCart { + public static void main(String[] args) { + Calculator calc = new Calculator(); + int addResult = calc.add(43, 79); + System.out.println("Add Result: " + addResult); + + // Add exception handling to catch ArithmeticException + try{ + double divResult = calc.divide(15, 0); + System.out.println("Division Result: " + divResult); + }catch(ArithmeticException e){ + e.printStackTrace(System.err); + } + } +} 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 index 3084f28..ab5ee9f 100644 --- 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 @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package soccer; +package sofflix.practices.practice_06_objectsclasses_1.soccer; /** * 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 index cfc2f19..1e06d0f 100644 --- 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 @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package soccer; +package sofflix.practices.practice_06_objectsclasses_1.soccer; /** * 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 index 561d9aa..762e68d 100644 --- 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 @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package soccer; +package sofflix.practices.practice_06_objectsclasses_1.soccer; /** * 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 index c03fc87..54a3523 100644 --- 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 @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package soccer; +package sofflix.practices.practice_06_objectsclasses_1.soccer; /** * 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 index bf26686..579f893 100644 --- 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 @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package soccer; +package sofflix.practices.practice_06_objectsclasses_1.soccer; /** * 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 index f9732f7..546592d 100644 --- 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 @@ -4,7 +4,7 @@ * and open the template in the editor. */ -package soccer; +package sofflix.practices.practice_06_objectsclasses_2.soccer; /** * 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 index 144a3d0..6237d05 100644 --- 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 @@ -4,7 +4,7 @@ * and open the template in the editor. */ -package soccer; +package sofflix.practices.practice_06_objectsclasses_2.soccer; /** * 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 index 19c907d..f6920ca 100644 --- 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 @@ -4,7 +4,7 @@ * and open the template in the editor. */ -package soccer; +package sofflix.practices.practice_06_objectsclasses_2.soccer; /** * 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 index 07670b0..3b3bbde 100644 --- 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 @@ -4,7 +4,7 @@ * and open the template in the editor. */ -package soccer; +package sofflix.practices.practice_06_objectsclasses_2.soccer; /** * 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 index 247f229..5488e6c 100644 --- 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 @@ -4,7 +4,7 @@ * and open the template in the editor. */ -package soccer; +package sofflix.practices.practice_06_objectsclasses_2.soccer; /** * 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 index f9732f7..6d4eb2f 100644 --- 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 @@ -4,7 +4,7 @@ * and open the template in the editor. */ -package soccer; +package sofflix.practices.practice_07_manipulateformat_1.soccer; /** * 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 index 144a3d0..fdfe50d 100644 --- 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 @@ -4,7 +4,7 @@ * and open the template in the editor. */ -package soccer; +package sofflix.practices.practice_07_manipulateformat_1.soccer; /** * 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 index 7799f30..a93fb79 100644 --- 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 @@ -4,7 +4,7 @@ * and open the template in the editor. */ -package soccer; +package sofflix.practices.practice_07_manipulateformat_1.soccer; /** * 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 index 07670b0..8927a21 100644 --- 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 @@ -4,7 +4,7 @@ * and open the template in the editor. */ -package soccer; +package sofflix.practices.practice_07_manipulateformat_1.soccer; /** * 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 index 247f229..0fa536c 100644 --- 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 @@ -4,7 +4,7 @@ * and open the template in the editor. */ -package soccer; +package sofflix.practices.practice_07_manipulateformat_1.soccer; /** * diff --git a/src/main/java/sofflix/practices/practice_08_methods_1/soccer/Game.java b/src/main/java/sofflix/practices/practice_08_methods_1/soccer/Game.java new file mode 100644 index 0000000..b1ae4d2 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_08_methods_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 sofflix.practices.practice_08_methods_1.soccer; + +/** + * + * @author Administrator + */ +public class Game { + + public Team homeTeam; + public Team awayTeam; + public Goal[] goals; + +} diff --git a/src/main/java/sofflix/practices/practice_08_methods_1/soccer/Goal.java b/src/main/java/sofflix/practices/practice_08_methods_1/soccer/Goal.java new file mode 100644 index 0000000..491915a --- /dev/null +++ b/src/main/java/sofflix/practices/practice_08_methods_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 sofflix.practices.practice_08_methods_1.soccer; + +/** + * + * @author Administrator + */ +public class Goal { + + public Team theTeam; + public Player thePlayer; + public double theTime; + +} diff --git a/src/main/java/sofflix/practices/practice_08_methods_1/soccer/League.java b/src/main/java/sofflix/practices/practice_08_methods_1/soccer/League.java new file mode 100644 index 0000000..7ca3642 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_08_methods_1/soccer/League.java @@ -0,0 +1,77 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package sofflix.practices.practice_08_methods_1.soccer; + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + Team[] theTeams = createTeams(); + Game[] theGames = createGames(theTeams); + Game currGame = theGames[0]; + /* Practice 8-1. Delete next three lines */ + Goal goal1 = new Goal(); + goal1.thePlayer = currGame.homeTeam.playerArray[2]; + goal1.theTeam = currGame.homeTeam; + goal1.theTime = 55; + Goal[] theGoals = {goal1}; + currGame.goals = theGoals; + + System.out.println("Goal scored after " + + currGame.goals[0].theTime + " mins by " + + currGame.goals[0].thePlayer.playerName + " of " + + currGame.goals[0].theTeam.teamName); + + } + + /* Practice 8-1. Add new static method createTeams() here */ + public static Team[] createTeams() { + /* Practice 8-1. Start of code that builds teams */ + // 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 8-1. End of code that builds teams */ + Team[] theTeams = {team1, team2}; + return theTeams; + } + + /* Practice 8-1. Add new static method createGames() here */ + public static Game[] createGames(Team[] theTeams) { + Game theGame = new Game(); + theGame.homeTeam = theTeams[0]; + theGame.awayTeam = theTeams[1]; + Game[] theGames = {theGame}; + return theGames; + } +} diff --git a/src/main/java/sofflix/practices/practice_08_methods_1/soccer/Player.java b/src/main/java/sofflix/practices/practice_08_methods_1/soccer/Player.java new file mode 100644 index 0000000..4403132 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_08_methods_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 sofflix.practices.practice_08_methods_1.soccer; + +/** + * + * @author Administrator + */ +public class Player { + + public String playerName; + +} diff --git a/src/main/java/sofflix/practices/practice_08_methods_1/soccer/Team.java b/src/main/java/sofflix/practices/practice_08_methods_1/soccer/Team.java new file mode 100644 index 0000000..e977c72 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_08_methods_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 sofflix.practices.practice_08_methods_1.soccer; + +/** + * + * @author Administrator + */ +public class Team { + + public String teamName; + public Player[] playerArray; + +} diff --git a/src/main/java/sofflix/practices/practice_08_methods_2/soccer/Game.java b/src/main/java/sofflix/practices/practice_08_methods_2/soccer/Game.java new file mode 100644 index 0000000..950c8bb --- /dev/null +++ b/src/main/java/sofflix/practices/practice_08_methods_2/soccer/Game.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 sofflix.practices.practice_08_methods_2.soccer; + +import utility.GameUtils; + +/** + * + * @author Administrator + */ +public class Game { + + public Team homeTeam; + public Team awayTeam; + public Goal[] goals; + + /* Practice 8-2. Add playGame() method here */ + public void playGame() { + int numberOfGoals = (int) (Math.random() * 7); + Goal[] theGoals = new Goal[numberOfGoals]; + this.goals = theGoals; + GameUtils.addGameGoals(this); + } + + /* Practice 8-2. Add getDescription() method here */ + public String getDescription() { + StringBuilder returnString = new StringBuilder(); + for (Goal currGoal : this.goals) { + returnString.append("Goal scored after " + + currGoal.theTime + " mins by " + + currGoal.thePlayer.playerName + + " of " + currGoal.theTeam.teamName+ + "\n"); + } + return returnString.toString(); + } + +} diff --git a/src/main/java/sofflix/practices/practice_08_methods_2/soccer/Goal.java b/src/main/java/sofflix/practices/practice_08_methods_2/soccer/Goal.java new file mode 100644 index 0000000..e00cfa8 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_08_methods_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 sofflix.practices.practice_08_methods_2.soccer; + +/** + * + * @author Administrator + */ +public class Goal { + + public Team theTeam; + public Player thePlayer; + public double theTime; + +} diff --git a/src/main/java/sofflix/practices/practice_08_methods_2/soccer/League.java b/src/main/java/sofflix/practices/practice_08_methods_2/soccer/League.java new file mode 100644 index 0000000..7c3a2d3 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_08_methods_2/soccer/League.java @@ -0,0 +1,75 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package sofflix.practices.practice_08_methods_2.soccer; + +import utility.GameUtils; + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + Team[] theTeams = createTeams(); + Game[] theGames = createGames(theTeams); + + /* Practice 8-2. Start of code that creates a Goal and assigns to Team and a Game */ + /* Practice 8-2. End of code that creates a Goal and assigns to a Team and a Game */ + /* Practice 8-2. Add code to initialize the array currGame.goals to a random size */ + Game currGame = theGames[0]; + GameUtils.addGameGoals(currGame); + currGame.playGame(); + System.out.println(currGame.getDescription()); + // Print out goal details +// System.out.println("Goal scored after " +// + currGame.goals[0].theTime + " mins by " +// + currGame.goals[0].thePlayer.playerName + " of " +// + currGame.goals[0].theTeam.teamName); + } + + public static Team[] createTeams() { + + Player player1 = new Player(); + player1.playerName = "George Eliot"; + Player player2 = new Player(); + player2.playerName = "Graham Greene"; + Player player3 = new Player(); + player3.playerName = "Geoffrey Chaucer"; + Player[] thePlayers = {player1, player2, player3}; + + Team team1 = new Team(); + team1.teamName = "The Greens"; + team1.playerArray = thePlayers; + + // Create team2 + Team team2 = new Team(); + team2.teamName = "The Reds"; + team2.playerArray = new Player[3]; + team2.playerArray[0] = new Player(); + team2.playerArray[0].playerName = "Robert Service"; + team2.playerArray[1] = new Player(); + team2.playerArray[1].playerName = "Robbie Burns"; + team2.playerArray[2] = new Player(); + team2.playerArray[2].playerName = "Rafael Sabatini"; + + Team[] theTeams = {team1, team2}; + return theTeams; + } + + public static Game[] createGames(Team[] theTeams) { + Game theGame = new Game(); + theGame.homeTeam = theTeams[0]; + theGame.awayTeam = theTeams[1]; + Game[] theGames = {theGame}; + return theGames; + } + +} diff --git a/src/main/java/sofflix/practices/practice_08_methods_2/soccer/Player.java b/src/main/java/sofflix/practices/practice_08_methods_2/soccer/Player.java new file mode 100644 index 0000000..3319cba --- /dev/null +++ b/src/main/java/sofflix/practices/practice_08_methods_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 sofflix.practices.practice_08_methods_2.soccer; + +/** + * + * @author Administrator + */ +public class Player { + + public String playerName; + +} diff --git a/src/main/java/sofflix/practices/practice_08_methods_2/soccer/Team.java b/src/main/java/sofflix/practices/practice_08_methods_2/soccer/Team.java new file mode 100644 index 0000000..b42a8d6 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_08_methods_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 sofflix.practices.practice_08_methods_2.soccer; + +/** + * + * @author Administrator + */ +public class Team { + + public String teamName; + public Player[] playerArray; + +} diff --git a/src/main/java/sofflix/practices/practice_08_methods_2/utility/GameUtils.java b/src/main/java/sofflix/practices/practice_08_methods_2/utility/GameUtils.java new file mode 100644 index 0000000..45c45b3 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_08_methods_2/utility/GameUtils.java @@ -0,0 +1,61 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package sofflix.practices.practice_08_methods_2.utility; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Arrays; +import soccer.*; + +/** + * + * @author ksomervi + */ +public class GameUtils { + + public static void addGameGoals(Game currGame) { + + if (currGame.goals == null) { + currGame.goals = new Goal[(int) (Math.random() * 10)]; // If goals not initialized max will be 9 + } + + //System.out.println(currGame.goals.length); + int i = 0; + for (Goal currGoal : currGame.goals) { + currGoal = new Goal(); + currGoal.theTeam = Math.random() > 0.5 ? getHomeTeam(currGame, "home") : getHomeTeam(currGame, "away"); + currGoal.thePlayer = currGoal.theTeam.playerArray[(int) (Math.random() * currGoal.theTeam.playerArray.length)]; + currGoal.theTime = (int) (Math.random() * 90); + currGame.goals[i] = currGoal; + i++; + } + Arrays.sort(currGame.goals, (g1, g2) -> Double.valueOf(g1.theTime).compareTo(Double.valueOf(g2.theTime))); + + } + + // Uses reflection so works with getter method or public field + private static Team getHomeTeam(Game currGame, String homeOrAway) { + Team theTeam = null; + Method m; + Field f; + try { + m = Game.class.getMethod("get" + Character.toUpperCase(homeOrAway.charAt(0)) + homeOrAway.substring(1) + "Team"); + theTeam = (Team)m.invoke(currGame); + } catch (NoSuchMethodException|IllegalAccessException|InvocationTargetException em) { + try { + f = Game.class.getField(homeOrAway + "Team"); + theTeam = (Team)f.get(currGame); + } catch (NoSuchFieldException|IllegalAccessException ef) { + System.out.println("The addGoals() utility requires the Goal class to contain either:\n" + + "public String fields called homeTeam and awayTeam, OR,\n" + + "public accessor methods called getHomeTeam() and getAwayTeam()."); + } + } + return theTeam; + } +} diff --git a/src/main/java/sofflix/practices/practice_08_methods_3/soccer/Game.java b/src/main/java/sofflix/practices/practice_08_methods_3/soccer/Game.java new file mode 100644 index 0000000..59d181b --- /dev/null +++ b/src/main/java/sofflix/practices/practice_08_methods_3/soccer/Game.java @@ -0,0 +1,46 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package sofflix.practices.practice_08_methods_3.soccer; + +import utility.GameUtils; + +/** + * + * @author Administrator + */ +public class Game { + + public Team homeTeam; + public Team awayTeam; + public Goal[] goals; + + public void playGame(int maxGoals) { + /* Practice 8-2. Add code to initialize the array currGame.goals to a random size */ + int numberOfGoals = (int)(Math.random() * (maxGoals + 1)); + Goal[] theGoals = new Goal[numberOfGoals]; + this.goals = theGoals; + GameUtils.addGameGoals(this); + } + + /* Practice 8-3. Add no-parameter playGame() method here */ + 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/sofflix/practices/practice_08_methods_3/soccer/Goal.java b/src/main/java/sofflix/practices/practice_08_methods_3/soccer/Goal.java new file mode 100644 index 0000000..895d8d4 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_08_methods_3.soccer; + +/** + * + * @author Administrator + */ +public class Goal { + + public Team theTeam; + public Player thePlayer; + public double theTime; + +} diff --git a/src/main/java/sofflix/practices/practice_08_methods_3/soccer/League.java b/src/main/java/sofflix/practices/practice_08_methods_3/soccer/League.java new file mode 100644 index 0000000..8aa605f --- /dev/null +++ b/src/main/java/sofflix/practices/practice_08_methods_3/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 sofflix.practices.practice_08_methods_3.soccer; + + + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + League theLeague = new League(); + Team[] theTeams = theLeague.createTeams(); + Game[] theGames = theLeague.createGames(theTeams); + + Game currGame = theGames[0]; + + currGame.playGame(); + + System.out.println(currGame.getDescription()); + + } + + public static Team[] createTeams() { + + Player player1 = new Player(); + player1.playerName = "George Eliot"; + Player player2 = new Player(); + player2.playerName = "Graham Greene"; + Player player3 = new Player(); + player3.playerName = "Geoffrey Chaucer"; + Player[] thePlayers = {player1, player2, player3}; + + Team team1 = new Team(); + team1.teamName = "The Greens"; + team1.playerArray = thePlayers; + + // Create team2 + Team team2 = new Team(); + team2.teamName = "The Reds"; + team2.playerArray = new Player[3]; + team2.playerArray[0] = new Player(); + team2.playerArray[0].playerName = "Robert Service"; + team2.playerArray[1] = new Player(); + team2.playerArray[1].playerName = "Robbie Burns"; + team2.playerArray[2] = new Player(); + team2.playerArray[2].playerName = "Rafael Sabatini"; + + Team[] theTeams = {team1, team2}; + return theTeams; + } + + public 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/sofflix/practices/practice_08_methods_3/soccer/Player.java b/src/main/java/sofflix/practices/practice_08_methods_3/soccer/Player.java new file mode 100644 index 0000000..05debc7 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_08_methods_3.soccer; + +/** + * + * @author Administrator + */ +public class Player { + + public String playerName; + +} diff --git a/src/main/java/sofflix/practices/practice_08_methods_3/soccer/Team.java b/src/main/java/sofflix/practices/practice_08_methods_3/soccer/Team.java new file mode 100644 index 0000000..ea2b3ab --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_08_methods_3.soccer; + +/** + * + * @author Administrator + */ +public class Team { + + public String teamName; + public Player[] playerArray; + +} diff --git a/src/main/java/sofflix/practices/practice_08_methods_3/utility/GameUtils.java b/src/main/java/sofflix/practices/practice_08_methods_3/utility/GameUtils.java new file mode 100644 index 0000000..b39ee44 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.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 sofflix.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/sofflix/practices/practice_09_encapconstructors_1/soccer/Game.java b/src/main/java/sofflix/practices/practice_09_encapconstructors_1/soccer/Game.java new file mode 100644 index 0000000..cef2690 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_09_encapconstructors_1/soccer/Game.java @@ -0,0 +1,87 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package sofflix.practices.practice_09_encapconstructors_1.soccer; + +import sofflix.practices.practice_09_encapconstructors_1.utility.GameUtils; + +/** + * + * @author Administrator + */ +public class Game { + + private Team homeTeam; + private Team awayTeam; + private Goal[] goals; + + public void playGame(int maxGoals) { + /* Practice 8-2. Add code to initialize the array currGame.goals to a random size */ + int numberOfGoals = (int)(Math.random() * maxGoals + 1); + Goal[] theGoals = new Goal[numberOfGoals]; + this.setGoals(theGoals); + GameUtils.addGameGoals(this); + } + + public void playGame() { + playGame(6); + } + + public String getDescription() { + StringBuilder returnString = new StringBuilder(); + for (Goal currGoal: this.getGoals()) { + returnString.append("Goal scored after " + + currGoal.getTheTime() + " mins by " + + currGoal.getThePlayer().getPlayerName() + " of " + + currGoal.getTheTeam().getTeamName() + + "\n"); + } + return returnString.toString(); + } + + /** + * @return the homeTeam + */ + public Team getHomeTeam() { + return homeTeam; + } + + /** + * @param homeTeam the homeTeam to set + */ + public void setHomeTeam(Team homeTeam) { + this.homeTeam = homeTeam; + } + + /** + * @return the awayTeam + */ + public Team getAwayTeam() { + return awayTeam; + } + + /** + * @param awayTeam the awayTeam to set + */ + public void setAwayTeam(Team awayTeam) { + this.awayTeam = awayTeam; + } + + /** + * @return the goals + */ + public Goal[] getGoals() { + return goals; + } + + /** + * @param goals the goals to set + */ + public void setGoals(Goal[] goals) { + this.goals = goals; + } + +} diff --git a/src/main/java/sofflix/practices/practice_09_encapconstructors_1/soccer/Goal.java b/src/main/java/sofflix/practices/practice_09_encapconstructors_1/soccer/Goal.java new file mode 100644 index 0000000..af2e6a6 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_09_encapconstructors_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 sofflix.practices.practice_09_encapconstructors_1.soccer; + +/** + * + * @author Administrator + */ +public class Goal { + + private Team theTeam; + private Player thePlayer; + private double theTime; + + /** + * @return the theTeam + */ + public Team getTheTeam() { + return theTeam; + } + + /** + * @param theTeam the theTeam to set + */ + public void setTheTeam(Team theTeam) { + this.theTeam = theTeam; + } + + /** + * @return the thePlayer + */ + public Player getThePlayer() { + return thePlayer; + } + + /** + * @param thePlayer the thePlayer to set + */ + public void setThePlayer(Player thePlayer) { + this.thePlayer = thePlayer; + } + + /** + * @return the theTime + */ + public double getTheTime() { + return theTime; + } + + /** + * @param theTime the theTime to set + */ + public void setTheTime(double theTime) { + this.theTime = theTime; + } + +} diff --git a/src/main/java/sofflix/practices/practice_09_encapconstructors_1/soccer/League.java b/src/main/java/sofflix/practices/practice_09_encapconstructors_1/soccer/League.java new file mode 100644 index 0000000..c83dec8 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_09_encapconstructors_1.soccer; + + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + League theLeague = new League(); + + Team[] theTeams = theLeague.createTeams(); + Game[] theGames = theLeague.createGames(theTeams); + + Game currGame = theGames[0]; + + currGame.playGame(); + + System.out.println(currGame.getDescription()); + + } + + public Team[] createTeams() { + + Player player1 = new Player(); + player1.setPlayerName("George Eliot"); + Player player2 = new Player(); + player2.setPlayerName("Graham Greene"); + Player player3 = new Player(); + player3.setPlayerName("Geoffrey Chaucer"); + Player[] thePlayers = {player1, player2, player3}; + + Team team1 = new Team(); + team1.setTeamName("The Greens"); + team1.setPlayerArray(thePlayers); + + // Create team2 + Team team2 = new Team(); + team2.setTeamName("The Reds"); + team2.setPlayerArray(new Player[3]); + team2.getPlayerArray()[0] = new Player(); + team2.getPlayerArray()[0].setPlayerName("Robert Service"); + team2.getPlayerArray()[1] = new Player(); + team2.getPlayerArray()[1].setPlayerName("Robbie Burns"); + team2.getPlayerArray()[2] = new Player(); + team2.getPlayerArray()[2].setPlayerName("Rafael Sabatini"); + + Team[] theTeams = {team1, team2}; + return theTeams; + } + + public Game[] createGames(Team[] theTeams) { + Game theGame = new Game(); + theGame.setHomeTeam(theTeams[0]); + theGame.setAwayTeam(theTeams[1]); + Game[] theGames = {theGame}; + return theGames; + } + +} diff --git a/src/main/java/sofflix/practices/practice_09_encapconstructors_1/soccer/Player.java b/src/main/java/sofflix/practices/practice_09_encapconstructors_1/soccer/Player.java new file mode 100644 index 0000000..9813e7e --- /dev/null +++ b/src/main/java/sofflix/practices/practice_09_encapconstructors_1/soccer/Player.java @@ -0,0 +1,31 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package sofflix.practices.practice_09_encapconstructors_1.soccer; + +/** + * + * @author Administrator + */ +public class Player { + + private String 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/sofflix/practices/practice_09_encapconstructors_1/soccer/Team.java b/src/main/java/sofflix/practices/practice_09_encapconstructors_1/soccer/Team.java new file mode 100644 index 0000000..e3de839 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_09_encapconstructors_1/soccer/Team.java @@ -0,0 +1,46 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package sofflix.practices.practice_09_encapconstructors_1.soccer; + +/** + * + * @author Administrator + */ +public class Team { + + private String teamName; + private Player[] playerArray; + + /** + * @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/sofflix/practices/practice_09_encapconstructors_1/utility/GameUtils.java b/src/main/java/sofflix/practices/practice_09_encapconstructors_1/utility/GameUtils.java new file mode 100644 index 0000000..928c3bd --- /dev/null +++ b/src/main/java/sofflix/practices/practice_09_encapconstructors_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 sofflix.practices.practice_09_encapconstructors_1.utility; + +import java.util.Arrays; +import sofflix.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.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/sofflix/practices/practice_09_encapconstructors_2/soccer/Game.java b/src/main/java/sofflix/practices/practice_09_encapconstructors_2/soccer/Game.java new file mode 100644 index 0000000..f568451 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_09_encapconstructors_2.soccer; + +import sofflix.practices.practice_09_encapconstructors_2.utility.GameUtils; + +/** + * + * @author Administrator + */ +public class Game { + + private Team homeTeam; + private Team awayTeam; + private Goal[] goals; + + /* Practice 9-2. Add constructor here */ + public Game(Team homeTeam, Team awayTeam){ + this.homeTeam = homeTeam; + this.awayTeam = awayTeam; + } + + public void playGame(int maxGoals) { + int numberOfGoals = (int)(Math.random() * maxGoals + 1); + Goal[] theGoals = new Goal[numberOfGoals]; + this.setGoals(theGoals); + GameUtils.addGameGoals(this); + } + + public void playGame() { + playGame(6); + } + + public String getDescription() { + StringBuilder returnString = new StringBuilder(); + for (Goal currGoal: this.getGoals()) { + returnString.append("Goal scored after " + + currGoal.getTheTime() + " mins by " + + currGoal.getThePlayer().getPlayerName() + " of " + + currGoal.getTheTeam().getTeamName() + + "\n"); + } + return returnString.toString(); + } + + /** + * @return the homeTeam + */ + public Team getHomeTeam() { + return homeTeam; + } + + /** + * @param homeTeam the homeTeam to set + */ + public void setHomeTeam(Team homeTeam) { + this.homeTeam = homeTeam; + } + + /** + * @return the awayTeam + */ + public Team getAwayTeam() { + return awayTeam; + } + + /** + * @param awayTeam the awayTeam to set + */ + public void setAwayTeam(Team awayTeam) { + this.awayTeam = awayTeam; + } + + /** + * @return the goals + */ + public Goal[] getGoals() { + return goals; + } + + /** + * @param goals the goals to set + */ + public void setGoals(Goal[] goals) { + this.goals = goals; + } + +} diff --git a/src/main/java/sofflix/practices/practice_09_encapconstructors_2/soccer/Goal.java b/src/main/java/sofflix/practices/practice_09_encapconstructors_2/soccer/Goal.java new file mode 100644 index 0000000..0670845 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_09_encapconstructors_2.soccer; + +/** + * + * @author Administrator + */ +public class Goal { + + private Team theTeam; + private Player thePlayer; + private double theTime; + + /** + * @return the theTeam + */ + public Team getTheTeam() { + return theTeam; + } + + /** + * @param theTeam the theTeam to set + */ + public void setTheTeam(Team theTeam) { + this.theTeam = theTeam; + } + + /** + * @return the thePlayer + */ + public Player getThePlayer() { + return thePlayer; + } + + /** + * @param thePlayer the thePlayer to set + */ + public void setThePlayer(Player thePlayer) { + this.thePlayer = thePlayer; + } + + /** + * @return the theTime + */ + public double getTheTime() { + return theTime; + } + + /** + * @param theTime the theTime to set + */ + public void setTheTime(double theTime) { + this.theTime = theTime; + } + +} diff --git a/src/main/java/sofflix/practices/practice_09_encapconstructors_2/soccer/League.java b/src/main/java/sofflix/practices/practice_09_encapconstructors_2/soccer/League.java new file mode 100644 index 0000000..468a3a4 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_09_encapconstructors_2/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 sofflix.practices.practice_09_encapconstructors_2.soccer; + + + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + League theLeague = new League(); + + Team[] theTeams = theLeague.createTeams(); + Game[] theGames = theLeague.createGames(theTeams); + + for (Game currGame : theGames) { + currGame.playGame(); + System.out.println(currGame.getDescription()); + } + } + + public Team[] createTeams() { + + Player player1 = new Player("George Eliot"); + Player player2 = new Player("Graham Greene"); + Player player3 = new Player("Geoffrey Chaucer"); + Player[] thePlayers = {player1, player2, player3}; + + Team team1 = new Team("The Greens", thePlayers); + /* Practice 9-2. The following two lines can be removed after the line above has been + modified to pass parameters to the constructor of Team */ + + + // Create team2 + Team team2 = new Team(); + 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/sofflix/practices/practice_09_encapconstructors_2/soccer/Player.java b/src/main/java/sofflix/practices/practice_09_encapconstructors_2/soccer/Player.java new file mode 100644 index 0000000..4a0f906 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_09_encapconstructors_2.soccer; + +/** + * + * @author Administrator + */ +public class Player { + + private String playerName; + + /* Practice 9-2. Add the constructor here */ + public Player(String playerName){ + this.playerName = playerName; + } + public Player(){} + /** + * @return the playerName + */ + public String getPlayerName() { + return playerName; + } + + /** + * @param playerName the playerName to set + */ + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + +} diff --git a/src/main/java/sofflix/practices/practice_09_encapconstructors_2/soccer/Team.java b/src/main/java/sofflix/practices/practice_09_encapconstructors_2/soccer/Team.java new file mode 100644 index 0000000..5a0be93 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_09_encapconstructors_2/soccer/Team.java @@ -0,0 +1,57 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package sofflix.practices.practice_09_encapconstructors_2.soccer; + +/** + * + * @author Administrator + */ +public class Team { + + private String teamName; + private Player[] playerArray; + + /* Practice 9-2. Add the two constructors here */ + public Team(String teamName){ + this.teamName=teamName; + } + public Team(String teamName, Player[] players ){ + this(teamName); + this.playerArray = players; + } + + public Team(){} + + /** + * @return the teamName + */ + public String getTeamName() { + return teamName; + } + + /** + * @param teamName the teamName to set + */ + public void setTeamName(String teamName) { + this.teamName = teamName; + } + + /** + * @return the playerArray + */ + public Player[] getPlayerArray() { + return playerArray; + } + + /** + * @param playerArray the playerArray to set + */ + public void setPlayerArray(Player[] playerArray) { + this.playerArray = playerArray; + } + +} diff --git a/src/main/java/sofflix/practices/practice_09_encapconstructors_2/utility/GameUtils.java b/src/main/java/sofflix/practices/practice_09_encapconstructors_2/utility/GameUtils.java new file mode 100644 index 0000000..15bcd08 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_09_encapconstructors_2.utility; + +import java.util.Arrays; +import sofflix.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/sofflix/practices/practice_10_conditions_1/soccer/Game.java b/src/main/java/sofflix/practices/practice_10_conditions_1/soccer/Game.java new file mode 100644 index 0000000..6cf3025 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_10_conditions_1/soccer/Game.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 sofflix.practices.practice_10_conditions_1.soccer; + +import sofflix.practices.practice_10_conditions_1.utility.GameUtils; + +/** + * + * @author Administrator + */ +public class Game { + + private Team homeTeam; + private Team awayTeam; + private Goal[] goals; + + public Game(Team homeTeam, Team awayTeam) { + this.homeTeam = homeTeam; + this.awayTeam = awayTeam; + } + + public void playGame(int maxGoals) { + int numberOfGoals = (int)(Math.random() * maxGoals + 1); + Goal[] theGoals = new Goal[numberOfGoals]; + this.setGoals(theGoals); + GameUtils.addGameGoals(this); + } + + public void playGame() { + playGame(6); + } + + public String getDescription() { + + /* Practice 10-1. Declare two int variables here */ + int homeTeamGoals=0; + int awayTeamGoals=0; + + StringBuilder returnString = new StringBuilder(); + + /* Practice 10-1. Add code to show teams that are playing */ + + for (Goal currGoal: this.getGoals()) { + + /* Practice 10-1. Add if block here */ + if(currGoal.getTheTeam() == homeTeam){ + homeTeamGoals++; + }else{ + awayTeamGoals++; + } + + returnString.append("Goal scored after " + + currGoal.getTheTime() + " mins by " + + currGoal.getThePlayer().getPlayerName() + " of " + + currGoal.getTheTeam().getTeamName() + + "\n"); + } + + /* Practice 10-1. Add if block here */ + if(homeTeamGoals == awayTeamGoals){ + returnString.append("It's a draw!"); + homeTeam.incPOintsTotal(1); + awayTeam.incPOintsTotal(1); + } + else if(homeTeamGoals > awayTeamGoals){ + returnString.append(returnString.append(homeTeam.getTeamName()+ "win")); + homeTeam.incPOintsTotal(2); + } + else{ + returnString.append(returnString.append(awayTeam.getTeamName() + " win")); + awayTeam.incPOintsTotal(2); + } + /* Practice 10-1. Add returnString.append() that shows score */ + returnString.append(" ("+homeTeamGoals + " - " +awayTeamGoals + ") \n"); + returnString.append(homeTeam.getTeamName() + " vs. " + awayTeam.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/sofflix/practices/practice_10_conditions_1/soccer/Goal.java b/src/main/java/sofflix/practices/practice_10_conditions_1/soccer/Goal.java new file mode 100644 index 0000000..2dcd7b5 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_10_conditions_1.soccer; + +/** + * + * @author Administrator + */ +public class Goal { + + private Team theTeam; + private Player thePlayer; + private double theTime; + + /** + * @return the theTeam + */ + public Team getTheTeam() { + return theTeam; + } + + /** + * @param theTeam the theTeam to set + */ + public void setTheTeam(Team theTeam) { + this.theTeam = theTeam; + } + + /** + * @return the thePlayer + */ + public Player getThePlayer() { + return thePlayer; + } + + /** + * @param thePlayer the thePlayer to set + */ + public void setThePlayer(Player thePlayer) { + this.thePlayer = thePlayer; + } + + /** + * @return the theTime + */ + public double getTheTime() { + return theTime; + } + + /** + * @param theTime the theTime to set + */ + public void setTheTime(double theTime) { + this.theTime = theTime; + } + +} diff --git a/src/main/java/sofflix/practices/practice_10_conditions_1/soccer/League.java b/src/main/java/sofflix/practices/practice_10_conditions_1/soccer/League.java new file mode 100644 index 0000000..a1169c1 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_10_conditions_1/soccer/League.java @@ -0,0 +1,75 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package sofflix.practices.practice_10_conditions_1.soccer; + + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + League theLeague = new League(); + + Team[] theTeams = theLeague.createTeams(); + Game[] theGames = theLeague.createGames(theTeams); + + for (Game currGame: theGames){ + currGame.playGame(); + System.out.println(currGame.getDescription()); + } + + /* Practice 10-1. Add a call to showBestTeam() method here */ + theLeague.showBestTeam(theTeams); + } + + public Team[] createTeams() { + + Player player1 = new Player("George Eliot"); + Player player2 = new Player("Graham Greene"); + Player player3 = new Player("Geoffrey Chaucer"); + Player[] thePlayers = {player1, player2, player3}; + + Team team1 = new Team("The Greens", thePlayers); + + // Create team2 + Team team2 = new Team(); + team2.setTeamName("The Reds"); + team2.setPlayerArray(new Player[3]); + team2.getPlayerArray()[0] = new Player("Robert Service"); + team2.getPlayerArray()[1] = new Player("Robbie Burns"); + team2.getPlayerArray()[2] = new Player("Rafael Sabatini"); + + Team[] theTeams = {team1, team2}; + return theTeams; + } + + public Game[] createGames(Team[] theTeams) { + Game theGame = new Game(theTeams[0], theTeams[1]); + Game theGame2 = new Game(theTeams[1], theTeams[0]); + Game theGame3 = new Game(theTeams[0], theTeams[1]); + Game theGame4 = new Game(theTeams[1], theTeams[0]); + Game[] theGames = {theGame, theGame2, theGame3, theGame4}; + return theGames; + } + + /* Practice 10-1. Add showBestTeam() method here */ + public void showBestTeam(Team[] theTeams){ + Team currBesTeam = theTeams[0]; + + System.out.println("\nTeam Points"); + for (Team currTeam : theTeams) { + System.out.println(currTeam.getTeamName() + ":" + currTeam.getPointsTotal()); + currBesTeam = currTeam.getPointsTotal() > currBesTeam.getPointsTotal()?currTeam:currBesTeam; + } + System.out.println("Winner of the league is " + currBesTeam.getTeamName()); + } +} diff --git a/src/main/java/sofflix/practices/practice_10_conditions_1/soccer/Player.java b/src/main/java/sofflix/practices/practice_10_conditions_1/soccer/Player.java new file mode 100644 index 0000000..6ddc47a --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_10_conditions_1.soccer; + +/** + * + * @author Administrator + */ +public class Player { + + private String playerName; + + public Player(String playerName) { + this.playerName = playerName; + } + + public Player() {} + + /** + * @return the playerName + */ + public String getPlayerName() { + return playerName; + } + + /** + * @param playerName the playerName to set + */ + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + +} diff --git a/src/main/java/sofflix/practices/practice_10_conditions_1/soccer/Team.java b/src/main/java/sofflix/practices/practice_10_conditions_1/soccer/Team.java new file mode 100644 index 0000000..bfa1abf --- /dev/null +++ b/src/main/java/sofflix/practices/practice_10_conditions_1/soccer/Team.java @@ -0,0 +1,79 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package sofflix.practices.practice_10_conditions_1.soccer; + +/** + * + * @author Administrator + */ +public class Team { + + private String teamName; + private Player[] playerArray; + + /* Practice 10-1. Add pointTotal variable here */ + private int pointsTotal; + + /* Practice 10-1. Add incPointsTotal() method here */ + public void incPOintsTotal(int pointsTotal){ + this.pointsTotal += pointsTotal; + } + + public Team(String teamName) { + this.teamName = teamName; + } + + public Team(String teamName, Player[] players) { + this(teamName); + this.playerArray = players; + } + + public Team() {} + + /** + * @return the teamName + */ + public String getTeamName() { + return teamName; + } + + /** + * @param teamName the teamName to set + */ + public void setTeamName(String teamName) { + this.teamName = teamName; + } + + /** + * @return the playerArray + */ + public Player[] getPlayerArray() { + return playerArray; + } + + /** + * @param playerArray the playerArray to set + */ + public void setPlayerArray(Player[] playerArray) { + this.playerArray = playerArray; + } + + /** + * @return the pointsTotal + */ + public int getPointsTotal() { + return pointsTotal; + } + + /** + * @param pointsTotal the pointsTotal to set + */ + public void setPointsTotal(int pointsTotal) { + this.pointsTotal = pointsTotal; + } + +} diff --git a/src/main/java/sofflix/practices/practice_10_conditions_1/utility/GameUtils.java b/src/main/java/sofflix/practices/practice_10_conditions_1/utility/GameUtils.java new file mode 100644 index 0000000..e6a7756 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_10_conditions_1.utility; + +import java.util.Arrays; +import sofflix.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/sofflix/practices/practice_10_conditions_2/soccer/Game.java b/src/main/java/sofflix/practices/practice_10_conditions_2/soccer/Game.java new file mode 100644 index 0000000..774d68f --- /dev/null +++ b/src/main/java/sofflix/practices/practice_10_conditions_2/soccer/Game.java @@ -0,0 +1,123 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package sofflix.practices.practice_10_conditions_2.soccer; + +import sofflix.practices.practice_10_conditions_2.utility.GameUtils; + +/** + * + * @author Administrator + */ +public class Game { + + private Team homeTeam; + private Team awayTeam; + private Goal[] goals; + + public Game(Team homeTeam, Team awayTeam) { + this.homeTeam = homeTeam; + this.awayTeam = awayTeam; + } + + public void playGame(int maxGoals) { + int numberOfGoals = (int)(Math.random() * maxGoals + 1); + Goal[] theGoals = new Goal[numberOfGoals]; + this.setGoals(theGoals); + GameUtils.addGameGoals(this); + } + + public void playGame() { + playGame(6); + } + + public String getDescription() { + + int homeTeamGoals = 0; + int awayTeamGoals = 0; + StringBuilder returnString = new StringBuilder(); + + returnString.append(homeTeam.getTeamName() + " vs. " + + awayTeam.getTeamName() + "\n"); + + for (Goal currGoal: this.getGoals()) { + + if (currGoal.getTheTeam()== homeTeam) { + homeTeamGoals++; + /* Practice 10-2. Add code to increment Team.goalsTotal here */ + //theTeams[0].incGoalsToTal(1); + } else { + awayTeamGoals++; + /* Practice 10-2. Add code to increment Team.goalsTotal here */ + //theTeams[1].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!"); + 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/sofflix/practices/practice_10_conditions_2/soccer/Goal.java b/src/main/java/sofflix/practices/practice_10_conditions_2/soccer/Goal.java new file mode 100644 index 0000000..2cc33f4 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_10_conditions_2.soccer; + +/** + * + * @author Administrator + */ +public class Goal { + + private Team theTeam; + private Player thePlayer; + private double theTime; + + /** + * @return the theTeam + */ + public Team getTheTeam() { + return theTeam; + } + + /** + * @param theTeam the theTeam to set + */ + public void setTheTeam(Team theTeam) { + this.theTeam = theTeam; + } + + /** + * @return the thePlayer + */ + public Player getThePlayer() { + return thePlayer; + } + + /** + * @param thePlayer the thePlayer to set + */ + public void setThePlayer(Player thePlayer) { + this.thePlayer = thePlayer; + } + + /** + * @return the theTime + */ + public double getTheTime() { + return theTime; + } + + /** + * @param theTime the theTime to set + */ + public void setTheTime(double theTime) { + this.theTime = theTime; + } + +} diff --git a/src/main/java/sofflix/practices/practice_10_conditions_2/soccer/League.java b/src/main/java/sofflix/practices/practice_10_conditions_2/soccer/League.java new file mode 100644 index 0000000..6e96c73 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_10_conditions_2/soccer/League.java @@ -0,0 +1,87 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package sofflix.practices.practice_10_conditions_2.soccer; + + + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + League theLeague = new League(); + + Team[] theTeams = theLeague.createTeams(); + Game[] theGames = theLeague.createGames(theTeams); + + for (Game currGame: theGames){ + currGame.playGame(); + System.out.println(currGame.getDescription()); + } + + theLeague.showBestTeam(theTeams); + + } + + public Team[] createTeams() { + + Player player1 = new Player("George Eliot"); + Player player2 = new Player("Graham Greene"); + Player player3 = new Player("Geoffrey Chaucer"); + Player[] thePlayers = {player1, player2, player3}; + + Team team1 = new Team("The Greens", thePlayers); + + // Create team2 + Team team2 = new Team(); + team2.setTeamName("The Reds"); + team2.setPlayerArray(new Player[3]); + team2.getPlayerArray()[0] = new Player("Robert Service"); + team2.getPlayerArray()[1] = new Player("Robbie Burns"); + team2.getPlayerArray()[2] = new Player("Rafael Sabatini"); + + Team[] theTeams = {team1, team2}; + return theTeams; + } + + public Game[] createGames(Team[] theTeams) { + Game theGame = new Game(theTeams[0], theTeams[1]); + Game theGame2 = new Game(theTeams[1], theTeams[0]); + Game theGame3 = new Game(theTeams[0], theTeams[1]); + Game theGame4 = new Game(theTeams[1], theTeams[0]); + Game[] theGames = {theGame, theGame2, theGame3, theGame4}; + return theGames; + } + + public void showBestTeam(Team[] theTeams) { + Team currBestTeam = theTeams[0]; + System.out.println("\nTeam Points"); + + for (Team currTeam: theTeams){ + /* Practice 10-2. Modify the line below to print out the goalsTotal for the current team also */ + System.out.println(currTeam.getTeamName() + " : " + currTeam.getPointsTotal() + ":" +currTeam.getGoalsTotal()); + currBestTeam = currTeam.getPointsTotal() > currBestTeam.getPointsTotal()?currTeam:currBestTeam; + /* Practice 10-2. Remove ternary statement above then add a replacement if statement here */ + if(currTeam.getPointsTotal() > currBestTeam.getPointsTotal()){ + currBestTeam = currTeam; + }else if(currTeam.getPointsTotal() == currBestTeam.getPointsTotal()){ + if(currTeam.getGoalsTotal() > currBestTeam.getGoalsTotal()){ + currBestTeam = currTeam; + } + } + } + + System.out.println("Winner of the League is " + currBestTeam.getTeamName()); + + } + +} diff --git a/src/main/java/sofflix/practices/practice_10_conditions_2/soccer/Player.java b/src/main/java/sofflix/practices/practice_10_conditions_2/soccer/Player.java new file mode 100644 index 0000000..13101bf --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_10_conditions_2.soccer; + +/** + * + * @author Administrator + */ +public class Player { + + private String playerName; + + public Player(String playerName) { + this.playerName = playerName; + } + + public Player() {} + + /** + * @return the playerName + */ + public String getPlayerName() { + return playerName; + } + + /** + * @param playerName the playerName to set + */ + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + +} diff --git a/src/main/java/sofflix/practices/practice_10_conditions_2/soccer/Team.java b/src/main/java/sofflix/practices/practice_10_conditions_2/soccer/Team.java new file mode 100644 index 0000000..c7017a3 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_10_conditions_2/soccer/Team.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 sofflix.practices.practice_10_conditions_2.soccer; + +/** + * + * @author Administrator + */ +public class Team { + + private String teamName; + private Player[] playerArray; + private int pointsTotal; + + /* Practice 10-2. Add goalsTotal attribute here */ + private int goalsTotal; + /* Practice 10-2. Add goalsTotalInc() method here */ + public void incGoalsTotal(int goals){ + this.setGoalsTotal(this.getGoalsTotal() + goals); + } + + public void incPointsTotal(int points){ + this.pointsTotal += points; + } + + public Team(String teamName) { + this.teamName = teamName; + } + + public Team(String teamName, Player[] players) { + this(teamName); + this.playerArray = players; + } + + public Team() {} + + /** + * @return the teamName + */ + public String getTeamName() { + return teamName; + } + + /** + * @param teamName the teamName to set + */ + public void setTeamName(String teamName) { + this.teamName = teamName; + } + + /** + * @return the playerArray + */ + public Player[] getPlayerArray() { + return playerArray; + } + + /** + * @param playerArray the playerArray to set + */ + public void setPlayerArray(Player[] playerArray) { + this.playerArray = playerArray; + } + + /** + * @return the pointsTotal + */ + public int getPointsTotal() { + return pointsTotal; + } + + /** + * @param pointsTotal the pointsTotal to set + */ + public void setPointsTotal(int pointsTotal) { + this.pointsTotal = pointsTotal; + } + + /** + * @return the goalsTotal + */ + public int getGoalsTotal() { + return goalsTotal; + } + + /** + * @param goalsTotal the goalsTotal to set + */ + public void setGoalsTotal(int goalsTotal) { + this.goalsTotal = goalsTotal; + } + +} diff --git a/src/main/java/sofflix/practices/practice_10_conditions_2/utility/GameUtils.java b/src/main/java/sofflix/practices/practice_10_conditions_2/utility/GameUtils.java new file mode 100644 index 0000000..5666a8d --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_10_conditions_2.utility; + +import java.util.Arrays; +import sofflix.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/sofflix/practices/practice_11_arraysloopsdates_1/soccer/Game.java b/src/main/java/sofflix/practices/practice_11_arraysloopsdates_1/soccer/Game.java new file mode 100644 index 0000000..173eb08 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_11_arraysloopsdates_1.soccer; + +import sofflix.practices.practice_11_arraysloopsdates_1.utility.GameUtils; + +/** + * + * @author Administrator + */ +public class Game { + + private Team homeTeam; + private Team awayTeam; + private Goal[] goals; + + public Game(Team homeTeam, Team awayTeam) { + this.homeTeam = homeTeam; + this.awayTeam = awayTeam; + } + + public void playGame(int maxGoals) { + int numberOfGoals = (int)(Math.random() * maxGoals + 1); + Goal[] theGoals = new Goal[numberOfGoals]; + this.setGoals(theGoals); + GameUtils.addGameGoals(this); + } + + public void playGame() { + playGame(6); + } + + public String getDescription() { + + int homeTeamGoals = 0; + int awayTeamGoals = 0; + StringBuilder returnString = new StringBuilder(); + + returnString.append(this.getHomeTeam().getTeamName() + " vs. " + + this.getAwayTeam().getTeamName() + "\n"); + + for (Goal currGoal: this.getGoals()) { + + if (currGoal.getTheTeam()== homeTeam) { + homeTeamGoals++; + homeTeam.incGoalsTotal(1); + } else { + awayTeamGoals++; + awayTeam.incGoalsTotal(1); + } + + returnString.append("Goal scored after " + + currGoal.getTheTime() + " mins by " + + currGoal.getThePlayer().getPlayerName() + " of " + + currGoal.getTheTeam().getTeamName() + + "\n"); + } + + if (homeTeamGoals == awayTeamGoals) { + returnString.append("It's a draw!"); + this.homeTeam.incPointsTotal(1); + this.awayTeam.incPointsTotal(1); + } else if (homeTeamGoals > awayTeamGoals) { + returnString.append(homeTeam.getTeamName() + " win"); + this.homeTeam.incPointsTotal(1); + } else { + returnString.append(awayTeam.getTeamName() + " win"); + this.awayTeam.incPointsTotal(1); + } + returnString.append(" (" + homeTeamGoals + " - " + awayTeamGoals + ") \n"); + + return returnString.toString(); + } + + /** + * @return the homeTeam + */ + public Team getHomeTeam() { + return homeTeam; + } + + /** + * @param homeTeam the homeTeam to set + */ + public void setHomeTeam(Team homeTeam) { + this.homeTeam = homeTeam; + } + + /** + * @return the awayTeam + */ + public Team getAwayTeam() { + return awayTeam; + } + + /** + * @param awayTeam the awayTeam to set + */ + public void setAwayTeam(Team awayTeam) { + this.awayTeam = awayTeam; + } + + /** + * @return the goals + */ + public Goal[] getGoals() { + return goals; + } + + /** + * @param goals the goals to set + */ + public void setGoals(Goal[] goals) { + this.goals = goals; + } + +} diff --git a/src/main/java/sofflix/practices/practice_11_arraysloopsdates_1/soccer/Goal.java b/src/main/java/sofflix/practices/practice_11_arraysloopsdates_1/soccer/Goal.java new file mode 100644 index 0000000..d95c587 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_11_arraysloopsdates_1.soccer; + +/** + * + * @author Administrator + */ +public class Goal { + + private Team theTeam; + private Player thePlayer; + private double theTime; + + /** + * @return the theTeam + */ + public Team getTheTeam() { + return theTeam; + } + + /** + * @param theTeam the theTeam to set + */ + public void setTheTeam(Team theTeam) { + this.theTeam = theTeam; + } + + /** + * @return the thePlayer + */ + public Player getThePlayer() { + return thePlayer; + } + + /** + * @param thePlayer the thePlayer to set + */ + public void setThePlayer(Player thePlayer) { + this.thePlayer = thePlayer; + } + + /** + * @return the theTime + */ + public double getTheTime() { + return theTime; + } + + /** + * @param theTime the theTime to set + */ + public void setTheTime(double theTime) { + this.theTime = theTime; + } + +} diff --git a/src/main/java/sofflix/practices/practice_11_arraysloopsdates_1/soccer/League.java b/src/main/java/sofflix/practices/practice_11_arraysloopsdates_1/soccer/League.java new file mode 100644 index 0000000..a59da09 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_11_arraysloopsdates_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 sofflix.practices.practice_11_arraysloopsdates_1.soccer; + +import java.util.ArrayList; +import java.util.StringTokenizer; +import sofflix.practices.practice_11_arraysloopsdates_1.utility.PlayerDatabase; + + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + League theLeague = new League(); + + Team[] theTeams = theLeague.createTeams("The Robins,The Crows,The Swallows",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/sofflix/practices/practice_11_arraysloopsdates_1/soccer/Player.java b/src/main/java/sofflix/practices/practice_11_arraysloopsdates_1/soccer/Player.java new file mode 100644 index 0000000..206f7b1 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_11_arraysloopsdates_1.soccer; + +/** + * + * @author Administrator + */ +public class Player { + + private String playerName; + + public Player(String playerName) { + this.playerName = playerName; + } + + public Player() {} + + /** + * @return the playerName + */ + public String getPlayerName() { + return playerName; + } + + /** + * @param playerName the playerName to set + */ + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + +} diff --git a/src/main/java/sofflix/practices/practice_11_arraysloopsdates_1/soccer/Team.java b/src/main/java/sofflix/practices/practice_11_arraysloopsdates_1/soccer/Team.java new file mode 100644 index 0000000..f217a50 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_11_arraysloopsdates_1.soccer; + +/** + * + * @author Administrator + */ +public class Team { + + private String teamName; + private Player[] playerArray; + private int pointsTotal; + private int goalsTotal; + + public void incGoalsTotal(int goals){ + this.setGoalsTotal(this.getGoalsTotal() + goals); + } + + public void incPointsTotal(int points){ + this.pointsTotal += points; + } + + public Team(String teamName) { + this.teamName = teamName; + } + + public Team(String teamName, Player[] players) { + this(teamName); + this.playerArray = players; + } + + public Team() {} + + /** + * @return the teamName + */ + public String getTeamName() { + return teamName; + } + + /** + * @param teamName the teamName to set + */ + public void setTeamName(String teamName) { + this.teamName = teamName; + } + + /** + * @return the playerArray + */ + public Player[] getPlayerArray() { + return playerArray; + } + + /** + * @param playerArray the playerArray to set + */ + public void setPlayerArray(Player[] playerArray) { + this.playerArray = playerArray; + } + + /** + * @return the pointsTotal + */ + public int getPointsTotal() { + return pointsTotal; + } + + /** + * @param pointsTotal the pointsTotal to set + */ + public void setPointsTotal(int pointsTotal) { + this.pointsTotal = pointsTotal; + } + + /** + * @return the goalsTotal + */ + public int getGoalsTotal() { + return goalsTotal; + } + + /** + * @param goalsTotal the goalsTotal to set + */ + public void setGoalsTotal(int goalsTotal) { + this.goalsTotal = goalsTotal; + } + +} diff --git a/src/main/java/sofflix/practices/practice_11_arraysloopsdates_1/utility/GameUtils.java b/src/main/java/sofflix/practices/practice_11_arraysloopsdates_1/utility/GameUtils.java new file mode 100644 index 0000000..9feff56 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_11_arraysloopsdates_1.utility; + +import java.util.Arrays; +import sofflix.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/sofflix/practices/practice_11_arraysloopsdates_1/utility/PlayerDatabase.java b/src/main/java/sofflix/practices/practice_11_arraysloopsdates_1/utility/PlayerDatabase.java new file mode 100644 index 0000000..c5fccdf --- /dev/null +++ b/src/main/java/sofflix/practices/practice_11_arraysloopsdates_1/utility/PlayerDatabase.java @@ -0,0 +1,78 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package sofflix.practices.practice_11_arraysloopsdates_1.utility; + +import java.util.ArrayList; +import java.util.StringTokenizer; +import sofflix.practices.practice_11_arraysloopsdates_1.soccer.Player; + + +/** + * + * @author Administrator + */ +public class PlayerDatabase { + + /* Practice 11-2. Declare an ArrayList here */ + private ArrayList players; + /* Practice 11-2. Add Constructor here */ + public PlayerDatabase(){ + StringTokenizer authorTokens = new StringTokenizer(authorList,","); + players = new ArrayList(); + while(authorTokens.hasMoreTokens()){ + players.add(new Player(authorTokens.nextToken())); + } + } + /* Practice 11-2. Add getTeam() method here */ + public Player[] getTeam(int numberOfPlayers){ + Player[] teamPlayers = new Player[numberOfPlayers]; + for (int i = 0; i < numberOfPlayers; i++) { + int playerIndex = (int) (Math.random()*players.size()); + teamPlayers[i] = players.get(playerIndex); + players.remove(playerIndex); + } + return teamPlayers; + } + +String authorList = +"Agatha Christie," + +"Alan Patton," + +"Alexander Solzhenitsyn," + +"Arthur Conan Doyle," + +"Anthony Trollope," + +"Baroness Orczy," + +"Brendan Behan," + +"Brian Moore," + +"Boris Pasternik," + +"Charles Dickens," + +"Charlotte Bronte," + +"Dorothy Parker," + +"Emile Zola," + +"Frank O'Connor," + +"Geoffrey Chaucer," + +"George Eliot," + +"G. K. Chesterton," + +"Graham Green," + +"Henry James," + +"James Joyce," + +"J. M. Synge," + +"J. R. Tolkien," + +"Jane Austin," + +"Leo Tolstoy," + +"Liam O'Flaherty," + +"Marcel Proust," + +"Mark Twain," + +"Oscar Wilde," + +"O. Henry," + +"Samuel Beckett," + +"Sean O'Casey," + +"William Shakespeare," + +"William Makepeace Thackeray," + +"W. B. Yeats," + +"Wilkie Collins"; + +} diff --git a/src/main/java/sofflix/practices/practice_11_arraysloopsdates_2/soccer/Game.java b/src/main/java/sofflix/practices/practice_11_arraysloopsdates_2/soccer/Game.java new file mode 100644 index 0000000..76cc207 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_11_arraysloopsdates_2/soccer/Game.java @@ -0,0 +1,144 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package sofflix.practices.practice_11_arraysloopsdates_2.soccer; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import sofflix.practices.practice_11_arraysloopsdates_2.utility.GameUtils; + +/** + * + * @author Administrator + */ +public class Game { + + private Team homeTeam; + private Team awayTeam; + private Goal[] goals; + + /* Practice 11-2. Add LocalDateTime attribute here */ + private LocalDateTime theDateTime; + + /* Practice 11-2. Modify the constructor to include the date and time of the game */ + public Game(Team homeTeam, Team awayTeam, LocalDateTime theDateTime) { + this.homeTeam = homeTeam; + this.awayTeam = awayTeam; + this.theDateTime = theDateTime; + } + + public void playGame(int maxGoals) { + int numberOfGoals = (int)(Math.random() * maxGoals + 1); + Goal[] theGoals = new Goal[numberOfGoals]; + this.setGoals(theGoals); + GameUtils.addGameGoals(this); + } + + public void playGame() { + playGame(6); + } + + public String getDescription() { + + int homeTeamGoals = 0; + int awayTeamGoals = 0; + StringBuilder returnString = new StringBuilder(); + + /* Practice 11-2. Modify the next line to include the date and time of the game */ + returnString.append(this.getHomeTeam().getTeamName() + " vs. " + + this.getAwayTeam().getTeamName() + "\n" + + "Date " + this.theDateTime.format(DateTimeFormatter.ISO_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 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/sofflix/practices/practice_11_arraysloopsdates_2/soccer/Goal.java b/src/main/java/sofflix/practices/practice_11_arraysloopsdates_2/soccer/Goal.java new file mode 100644 index 0000000..b419b79 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_11_arraysloopsdates_2.soccer; + +/** + * + * @author Administrator + */ +public class Goal { + + private Team theTeam; + private Player thePlayer; + private double theTime; + + /** + * @return the theTeam + */ + public Team getTheTeam() { + return theTeam; + } + + /** + * @param theTeam the theTeam to set + */ + public void setTheTeam(Team theTeam) { + this.theTeam = theTeam; + } + + /** + * @return the thePlayer + */ + public Player getThePlayer() { + return thePlayer; + } + + /** + * @param thePlayer the thePlayer to set + */ + public void setThePlayer(Player thePlayer) { + this.thePlayer = thePlayer; + } + + /** + * @return the theTime + */ + public double getTheTime() { + return theTime; + } + + /** + * @param theTime the theTime to set + */ + public void setTheTime(double theTime) { + this.theTime = theTime; + } + +} diff --git a/src/main/java/sofflix/practices/practice_11_arraysloopsdates_2/soccer/League.java b/src/main/java/sofflix/practices/practice_11_arraysloopsdates_2/soccer/League.java new file mode 100644 index 0000000..ba18072 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_11_arraysloopsdates_2/soccer/League.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 sofflix.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 sofflix.practices.practice_11_arraysloopsdates_2.utility.PlayerDatabase; + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + League theLeague = new League(); + + Team[] theTeams = theLeague.createTeams("The Robins,The Crows,The Swallows", 5); + Game[] theGames = theLeague.createGames(theTeams); + + /* Practice 11-2. Print the League announcement here */ + System.out.println(theLeague.getLeagueAnnouncement(theGames)); + for (Game currGame: theGames){ + currGame.playGame(); + System.out.println(currGame.getDescription()); + } + + theLeague.showBestTeam(theTeams); + + } + + public Team[] createTeams(String teamNames, int teamSize) { + + PlayerDatabase playerDB = new PlayerDatabase(); + + StringTokenizer teamNameTokens = new StringTokenizer(teamNames, ","); + Team[] theTeams = new Team[teamNameTokens.countTokens()]; + for (int i =0; i < theTeams.length; i++){ + theTeams[i] = new Team(teamNameTokens.nextToken(), playerDB.getTeam(teamSize)); + } + + + return theTeams; + } + + public Game[] createGames(Team[] theTeams) { + /* Practice 11-2. Add a line to declare and initialize daysBetweenGames variable */ + int daysBetweenGames =0; + ArrayList theGames = new ArrayList(); + + for (Team homeTeam: theTeams){ + for (Team awayTeam: theTeams){ + if (homeTeam != awayTeam) { + /* Practice 11-2. Increment daysBetweenGames here */ + daysBetweenGames += 7; + /* Practice 11-2. Modify the statement below to add pass LocalDateTime into constructor */ + theGames.add(new Game(homeTeam, awayTeam, LocalDateTime.now().plusDays(daysBetweenGames))); + } + + } + } + + + + return (Game[]) theGames.toArray(new Game[1]); + } + + public void showBestTeam(Team[] theTeams) { + Team currBestTeam = theTeams[0]; + System.out.println("\nTeam Points"); + + for (Team currTeam: theTeams){ + System.out.println(currTeam.getTeamName() + " : " + currTeam.getPointsTotal() + " : " + + currTeam.getGoalsTotal()); + currBestTeam = currTeam.getPointsTotal() > currBestTeam.getPointsTotal()?currTeam:currBestTeam; + if (currTeam.getPointsTotal() > currBestTeam.getPointsTotal()){ + currBestTeam = currTeam; + } else if (currTeam.getPointsTotal() == currBestTeam.getPointsTotal()){ + if (currTeam.getGoalsTotal() > currBestTeam.getGoalsTotal()){ + currBestTeam = currTeam; + } + } + } + + System.out.println("Winner of the League is " + currBestTeam.getTeamName()); + + } + + /* Practice 11-2. Add the getLeagueAnnouncement() method here */ + public String getLeagueAnnouncement (Game[] theGames){ + Period thePeriod = Period.between(theGames[0].getTheDateTime().toLocalDate(), theGames[theGames.length - 1 ].getTheDateTime().toLocalDate()); + StringBuilder returnString = new StringBuilder(); + returnString.append("The League is scheduled to run for "); + returnString.append(thePeriod.getMonths() + " month(s), and "); + returnString.append(thePeriod.getDays() + " day(s)\n"); + return returnString.toString(); + } +} diff --git a/src/main/java/sofflix/practices/practice_11_arraysloopsdates_2/soccer/Player.java b/src/main/java/sofflix/practices/practice_11_arraysloopsdates_2/soccer/Player.java new file mode 100644 index 0000000..dc66e8e --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_11_arraysloopsdates_2.soccer; + +/** + * + * @author Administrator + */ +public class Player { + + private String playerName; + + public Player(String playerName) { + this.playerName = playerName; + } + + public Player() {} + + /** + * @return the playerName + */ + public String getPlayerName() { + return playerName; + } + + /** + * @param playerName the playerName to set + */ + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + +} diff --git a/src/main/java/sofflix/practices/practice_11_arraysloopsdates_2/soccer/Team.java b/src/main/java/sofflix/practices/practice_11_arraysloopsdates_2/soccer/Team.java new file mode 100644 index 0000000..749d532 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_11_arraysloopsdates_2.soccer; + +/** + * + * @author Administrator + */ +public class Team { + + private String teamName; + private Player[] playerArray; + private int pointsTotal; + private int goalsTotal; + + public void incGoalsTotal(int goals){ + this.setGoalsTotal(this.getGoalsTotal() + goals); + } + + public void incPointsTotal(int points){ + this.pointsTotal += points; + } + + public Team(String teamName) { + this.teamName = teamName; + } + + public Team(String teamName, Player[] players) { + this(teamName); + this.playerArray = players; + } + + public Team() {} + + /** + * @return the teamName + */ + public String getTeamName() { + return teamName; + } + + /** + * @param teamName the teamName to set + */ + public void setTeamName(String teamName) { + this.teamName = teamName; + } + + /** + * @return the playerArray + */ + public Player[] getPlayerArray() { + return playerArray; + } + + /** + * @param playerArray the playerArray to set + */ + public void setPlayerArray(Player[] playerArray) { + this.playerArray = playerArray; + } + + /** + * @return the pointsTotal + */ + public int getPointsTotal() { + return pointsTotal; + } + + /** + * @param pointsTotal the pointsTotal to set + */ + public void setPointsTotal(int pointsTotal) { + this.pointsTotal = pointsTotal; + } + + /** + * @return the goalsTotal + */ + public int getGoalsTotal() { + return goalsTotal; + } + + /** + * @param goalsTotal the goalsTotal to set + */ + public void setGoalsTotal(int goalsTotal) { + this.goalsTotal = goalsTotal; + } + +} diff --git a/src/main/java/sofflix/practices/practice_11_arraysloopsdates_2/utility/GameUtils.java b/src/main/java/sofflix/practices/practice_11_arraysloopsdates_2/utility/GameUtils.java new file mode 100644 index 0000000..748ed65 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_11_arraysloopsdates_2.utility; + +import java.util.Arrays; +import sofflix.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/sofflix/practices/practice_11_arraysloopsdates_2/utility/PlayerDatabase.java b/src/main/java/sofflix/practices/practice_11_arraysloopsdates_2/utility/PlayerDatabase.java new file mode 100644 index 0000000..a65a5b3 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_11_arraysloopsdates_2.utility; + +import java.util.*; +import sofflix.practices.practice_11_arraysloopsdates_2.soccer.Player; + + + +/** + * + * @author Administrator + */ +public class PlayerDatabase { + + private ArrayList players; + + public PlayerDatabase(){ + StringTokenizer authorTokens = new StringTokenizer(authorList, ","); + players = new ArrayList(); + while (authorTokens.hasMoreTokens()){ + players.add(new Player(authorTokens.nextToken())); + } + } + + public Player[] getTeam(int numberOfPlayers){ + Player[] teamPlayers = new Player[numberOfPlayers]; + for (int i = 0; i < numberOfPlayers; i++){ + int playerIndex = (int) (Math.random() * players.size()); + teamPlayers[i] = players.get(playerIndex); + players.remove(playerIndex); + } + return teamPlayers; + + } + + + +String authorList = +"Agatha Christie," + +"Alan Patton," + +"Alexander Solzhenitsyn," + +"Arthur Conan Doyle," + +"Anthony Trollope," + +"Baroness Orczy," + +"Brendan Behan," + +"Brian Moore," + +"Boris Pasternik," + +"Charles Dickens," + +"Charlotte Bronte," + +"Dorothy Parker," + +"Emile Zola," + +"Frank O'Connor," + +"Geoffrey Chaucer," + +"George Eliot," + +"G. K. Chesterton," + +"Graham Green," + +"Henry James," + +"James Joyce," + +"J. M. Synge," + +"J. R. Tolkien," + +"Jane Austin," + +"Leo Tolstoy," + +"Liam O'Flaherty," + +"Marcel Proust," + +"Mark Twain," + +"Oscar Wilde," + +"O. Henry," + +"Samuel Beckett," + +"Sean O'Casey," + +"William Shakespeare," + +"William Makepeace Thackeray," + +"W. B. Yeats," + +"Wilkie Collins"; + +} diff --git a/src/main/java/sofflix/practices/practice_12_usinginheritance_1/soccer/Game.java b/src/main/java/sofflix/practices/practice_12_usinginheritance_1/soccer/Game.java new file mode 100644 index 0000000..7737f96 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_12_usinginheritance_1/soccer/Game.java @@ -0,0 +1,160 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package sofflix.practices.practice_12_usinginheritance_1.soccer; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; + +/** + * + * @author Administrator + */ +public class Game { + + private Team homeTeam; + private Team awayTeam; + private Goal[] goals; + private LocalDateTime theDateTime; + + public Game(Team homeTeam, Team awayTeam, LocalDateTime theDateTime) { + this.homeTeam = homeTeam; + this.awayTeam = awayTeam; + this.theDateTime = theDateTime; + } + + public void playGame() { + ArrayList eventList = new ArrayList(); + Goal currEvent; + for (int i = 1; i <= 90; i++) { + if(Math.random() > 0.95){ + //System.out.println(i); + currEvent = new Goal(); + currEvent.setTheTeam(Math.random() > 0.5?homeTeam:awayTeam); + currEvent.setThePlayer(currEvent.getTheTeam().getPlayerArray()[(int) (Math.random() * currEvent.getTheTeam().getPlayerArray().length)]); + currEvent.setTheTime(i); + eventList.add(currEvent); + } + } + this.goals = new Goal[eventList.size()]; + eventList.toArray(goals); + } + + public String getDescription() { + + int homeTeamGoals = 0; + int awayTeamGoals = 0; + StringBuilder returnString = new StringBuilder(); + + returnString.append(this.getHomeTeam().getTeamName() + " vs. " + + this.getAwayTeam().getTeamName() + "\n" + + "Date: " + this.getTheDateTime().format(DateTimeFormatter.ISO_LOCAL_DATE) + "\n"); + + for (Goal currGoal: this.getGoals()) { + + if (currGoal.getTheTeam()== homeTeam) { + homeTeamGoals++; + homeTeam.incGoalsTotal(1); + } else { + awayTeamGoals++; + awayTeam.incGoalsTotal(1); + } + + returnString.append("Goal scored after " + + currGoal.getTheTime() + " mins by " + + currGoal.getThePlayer().getPlayerName() + " of " + + currGoal.getTheTeam().getTeamName() + + "\n"); + } + + if (homeTeamGoals == awayTeamGoals) { + returnString.append("It's a draw!"); + this.homeTeam.incPointsTotal(1); + this.awayTeam.incPointsTotal(1); + } else if (homeTeamGoals > awayTeamGoals) { + returnString.append(homeTeam.getTeamName() + " win"); + this.homeTeam.incPointsTotal(1); + } else { + returnString.append(awayTeam.getTeamName() + " win"); + this.awayTeam.incPointsTotal(1); + } + returnString.append(" (" + homeTeamGoals + " - " + awayTeamGoals + ") \n"); + + return returnString.toString(); + } + + /** + * @return the homeTeam + */ + public Team getHomeTeam() { + return homeTeam; + } + + /** + * @param homeTeam the homeTeam to set + */ + public void setHomeTeam(Team homeTeam) { + this.homeTeam = homeTeam; + } + + /** + * @return the awayTeam + */ + public Team getAwayTeam() { + return awayTeam; + } + + /** + * @param awayTeam the awayTeam to set + */ + public void setAwayTeam(Team awayTeam) { + this.awayTeam = awayTeam; + } + + /** + * @return the goals + */ + public Goal[] getGoals() { + return goals; + } + + /** + * @param goals the goals to set + */ + public void setGoals(Goal[] goals) { + this.goals = goals; + } + + /** + * @return the localDateTime + */ + public LocalDateTime getLocalDateTime() { + return getTheDateTime(); + } + + /** + * @param theDateTime the localDateTime to set + */ + public void setLocalDateTime(LocalDateTime theDateTime) { + this.setTheDateTime(theDateTime); + } + + /** + * @return the theDateTime + */ + public LocalDateTime getTheDateTime() { + return theDateTime; + } + + /** + * @param theDateTime the theDateTime to set + */ + public void setTheDateTime(LocalDateTime theDateTime) { + this.theDateTime = theDateTime; + } + +} diff --git a/src/main/java/sofflix/practices/practice_12_usinginheritance_1/soccer/Goal.java b/src/main/java/sofflix/practices/practice_12_usinginheritance_1/soccer/Goal.java new file mode 100644 index 0000000..070f0ba --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_12_usinginheritance_1.soccer; + +/** + * + * @author Administrator + */ +public class Goal { + + private Team theTeam; + private Player thePlayer; + private double theTime; + + /** + * @return the theTeam + */ + public Team getTheTeam() { + return theTeam; + } + + /** + * @param theTeam the theTeam to set + */ + public void setTheTeam(Team theTeam) { + this.theTeam = theTeam; + } + + /** + * @return the thePlayer + */ + public Player getThePlayer() { + return thePlayer; + } + + /** + * @param thePlayer the thePlayer to set + */ + public void setThePlayer(Player thePlayer) { + this.thePlayer = thePlayer; + } + + /** + * @return the theTime + */ + public double getTheTime() { + return theTime; + } + + /** + * @param theTime the theTime to set + */ + public void setTheTime(double theTime) { + this.theTime = theTime; + } + +} diff --git a/src/main/java/sofflix/practices/practice_12_usinginheritance_1/soccer/League.java b/src/main/java/sofflix/practices/practice_12_usinginheritance_1/soccer/League.java new file mode 100644 index 0000000..c99b7a2 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_12_usinginheritance_1/soccer/League.java @@ -0,0 +1,109 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package sofflix.practices.practice_12_usinginheritance_1.soccer; + +import java.time.LocalDateTime; +import java.time.Period; +import java.util.ArrayList; +import java.util.StringTokenizer; +import sofflix.practices.practice_12_usinginheritance_1.utility.PlayerDatabase; + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + League theLeague = new League(); + + Team[] theTeams = theLeague.createTeams("The Robins,The Crows,The Swallows", 5); + Game[] theGames = theLeague.createGames(theTeams); + + System.out.println(theLeague.getLeagueAnnouncement(theGames)); + for (Game currGame: theGames){ + currGame.playGame(); + + /* Practice 12-1. Add a break command here */ + + /* Practice 12-1. Comment out the line below */ + System.out.println(currGame.getDescription()); + } + + //theLeague.showBestTeam(theTeams); + + } + + public Team[] createTeams(String teamNames, int teamSize) { + + PlayerDatabase playerDB = new PlayerDatabase(); + + StringTokenizer teamNameTokens = new StringTokenizer(teamNames, ","); + Team[] theTeams = new Team[teamNameTokens.countTokens()]; + for (int i =0; i < theTeams.length; i++){ + theTeams[i] = new Team(teamNameTokens.nextToken(), playerDB.getTeam(teamSize)); + } + + + return theTeams; + } + + public Game[] createGames(Team[] theTeams) { + int daysBetweenGames = 0; + + ArrayList theGames = new ArrayList(); + + for (Team homeTeam: theTeams){ + for (Team awayTeam: theTeams){ + if (homeTeam != awayTeam) { + daysBetweenGames += 7; + theGames.add(new Game(homeTeam, awayTeam, LocalDateTime.now().plusDays(daysBetweenGames))); + } + + } + } + + + + return (Game[]) theGames.toArray(new Game[1]); + } + + public void showBestTeam(Team[] theTeams) { + Team currBestTeam = theTeams[0]; + System.out.println("\nTeam Points"); + + for (Team currTeam: theTeams){ + System.out.println(currTeam.getTeamName() + " : " + currTeam.getPointsTotal() + " : " + + currTeam.getGoalsTotal()); + currBestTeam = currTeam.getPointsTotal() > currBestTeam.getPointsTotal()?currTeam:currBestTeam; + if (currTeam.getPointsTotal() > currBestTeam.getPointsTotal()){ + currBestTeam = currTeam; + } else if (currTeam.getPointsTotal() == currBestTeam.getPointsTotal()){ + if (currTeam.getGoalsTotal() > currBestTeam.getGoalsTotal()){ + currBestTeam = currTeam; + } + } + } + + System.out.println("Winner of the League is " + currBestTeam.getTeamName()); + + } + + public String getLeagueAnnouncement(Game[] theGames){ + + Period thePeriod = Period.between(theGames[0].getTheDateTime().toLocalDate(), + theGames[theGames.length - 1].getTheDateTime().toLocalDate()); + + return "The league is scheduled to run for " + + thePeriod.getMonths() + " month(s), and " + + thePeriod.getDays() + " day(s)\n"; + } + +} diff --git a/src/main/java/sofflix/practices/practice_12_usinginheritance_1/soccer/Player.java b/src/main/java/sofflix/practices/practice_12_usinginheritance_1/soccer/Player.java new file mode 100644 index 0000000..828805f --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_12_usinginheritance_1.soccer; + +/** + * + * @author Administrator + */ +public class Player { + + private String playerName; + + public Player(String playerName) { + this.playerName = playerName; + } + + public Player() {} + + /** + * @return the playerName + */ + public String getPlayerName() { + return playerName; + } + + /** + * @param playerName the playerName to set + */ + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + +} diff --git a/src/main/java/sofflix/practices/practice_12_usinginheritance_1/soccer/Team.java b/src/main/java/sofflix/practices/practice_12_usinginheritance_1/soccer/Team.java new file mode 100644 index 0000000..d0aa7eb --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_12_usinginheritance_1.soccer; + +/** + * + * @author Administrator + */ +public class Team { + + private String teamName; + private Player[] playerArray; + private int pointsTotal; + private int goalsTotal; + + public void incGoalsTotal(int goals){ + this.setGoalsTotal(this.getGoalsTotal() + goals); + } + + public void incPointsTotal(int points){ + this.pointsTotal += points; + } + + public Team(String teamName) { + this.teamName = teamName; + } + + public Team(String teamName, Player[] players) { + this(teamName); + this.playerArray = players; + } + + public Team() {} + + /** + * @return the teamName + */ + public String getTeamName() { + return teamName; + } + + /** + * @param teamName the teamName to set + */ + public void setTeamName(String teamName) { + this.teamName = teamName; + } + + /** + * @return the playerArray + */ + public Player[] getPlayerArray() { + return playerArray; + } + + /** + * @param playerArray the playerArray to set + */ + public void setPlayerArray(Player[] playerArray) { + this.playerArray = playerArray; + } + + /** + * @return the pointsTotal + */ + public int getPointsTotal() { + return pointsTotal; + } + + /** + * @param pointsTotal the pointsTotal to set + */ + public void setPointsTotal(int pointsTotal) { + this.pointsTotal = pointsTotal; + } + + /** + * @return the goalsTotal + */ + public int getGoalsTotal() { + return goalsTotal; + } + + /** + * @param goalsTotal the goalsTotal to set + */ + public void setGoalsTotal(int goalsTotal) { + this.goalsTotal = goalsTotal; + } + +} diff --git a/src/main/java/sofflix/practices/practice_12_usinginheritance_1/utility/PlayerDatabase.java b/src/main/java/sofflix/practices/practice_12_usinginheritance_1/utility/PlayerDatabase.java new file mode 100644 index 0000000..e92b90f --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_12_usinginheritance_1.utility; + +import java.util.*; +import sofflix.practices.practice_12_usinginheritance_1.soccer.Player; + + + +/** + * + * @author Administrator + */ +public class PlayerDatabase { + + private ArrayList players; + + public PlayerDatabase(){ + StringTokenizer authorTokens = new StringTokenizer(authorList, ","); + players = new ArrayList(); + while (authorTokens.hasMoreTokens()){ + players.add(new Player(authorTokens.nextToken())); + } + } + + public Player[] getTeam(int numberOfPlayers){ + Player[] teamPlayers = new Player[numberOfPlayers]; + for (int i = 0; i < numberOfPlayers; i++){ + int playerIndex = (int) (Math.random() * players.size()); + teamPlayers[i] = players.get(playerIndex); + players.remove(playerIndex); + } + return teamPlayers; + + } + + + +String authorList = +"Agatha Christie," + +"Alan Patton," + +"Alexander Solzhenitsyn," + +"Arthur Conan Doyle," + +"Anthony Trollope," + +"Baroness Orczy," + +"Brendan Behan," + +"Brian Moore," + +"Boris Pasternik," + +"Charles Dickens," + +"Charlotte Bronte," + +"Dorothy Parker," + +"Emile Zola," + +"Frank O'Connor," + +"Geoffrey Chaucer," + +"George Eliot," + +"G. K. Chesterton," + +"Graham Green," + +"Henry James," + +"James Joyce," + +"J. M. Synge," + +"J. R. Tolkien," + +"Jane Austin," + +"Leo Tolstoy," + +"Liam O'Flaherty," + +"Marcel Proust," + +"Mark Twain," + +"Oscar Wilde," + +"O. Henry," + +"Samuel Beckett," + +"Sean O'Casey," + +"William Shakespeare," + +"William Makepeace Thackeray," + +"W. B. Yeats," + +"Wilkie Collins"; + +} diff --git a/src/main/java/sofflix/practices/practice_12_usinginheritance_2/soccer/Game.java b/src/main/java/sofflix/practices/practice_12_usinginheritance_2/soccer/Game.java new file mode 100644 index 0000000..0692b4b --- /dev/null +++ b/src/main/java/sofflix/practices/practice_12_usinginheritance_2/soccer/Game.java @@ -0,0 +1,165 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package sofflix.practices.practice_12_usinginheritance_2.soccer; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; + +/** + * + * @author Administrator + */ + +public class Game { + + private Team homeTeam; + private Team awayTeam; + private GameEvent[] gameEvents; + private LocalDateTime theDateTime; + + public Game(Team homeTeam, Team awayTeam, LocalDateTime theDateTime) { + this.homeTeam = homeTeam; + this.awayTeam = awayTeam; + this.theDateTime = theDateTime; + } + + public void playGame() { + ArrayList eventList = new ArrayList(); + GameEvent currEvent; + for (int i = 1; i <=90; i++){ + + if (Math.random() > 0.95){ + currEvent = Math.random() > 0.6? new Goal(): new Possession(); + currEvent.setTheTeam(Math.random() > 0.5?homeTeam: awayTeam); + currEvent.setThePlayer(currEvent.getTheTeam(). + getPlayerArray()[(int)(Math.random() * currEvent.getTheTeam().getPlayerArray().length)]); + currEvent.setTheTime(i); + eventList.add(currEvent); + //System.out.println(i); + } + this.gameEvents = new GameEvent[eventList.size()]; + eventList.toArray(gameEvents); + + } + } + + public String getDescription() { + + int homeTeamGoals = 0; + int awayTeamGoals = 0; + StringBuilder returnString = new StringBuilder(); + + returnString.append(this.getHomeTeam().getTeamName() + " vs. " + + this.getAwayTeam().getTeamName() + "\n" + + "Date: " + this.getTheDateTime().format(DateTimeFormatter.ISO_LOCAL_DATE) + "\n"); + + for (GameEvent currEvent: this.getEvents()) { + + if (currEvent.getTheTeam()== homeTeam) { + homeTeamGoals++; + homeTeam.incGoalsTotal(1); + } else { + awayTeamGoals++; + awayTeam.incGoalsTotal(1); + } + + /* Practice 12-2. Modify the text printed */ + returnString.append(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 gameEvents + */ + public GameEvent[] getEvents() { + return gameEvents; + } + + /** + * @param gameEvents the gameEvents to set + */ + public void setEvents(Goal[] gameEvents) { + this.gameEvents = gameEvents; + } + + /** + * @return the localDateTime + */ + public LocalDateTime getLocalDateTime() { + return getTheDateTime(); + } + + /** + * @param theDateTime the localDateTime to set + */ + public void setLocalDateTime(LocalDateTime theDateTime) { + this.setTheDateTime(theDateTime); + } + + /** + * @return the theDateTime + */ + public LocalDateTime getTheDateTime() { + return theDateTime; + } + + /** + * @param theDateTime the theDateTime to set + */ + public void setTheDateTime(LocalDateTime theDateTime) { + this.theDateTime = theDateTime; + } + +} diff --git a/src/main/java/sofflix/practices/practice_12_usinginheritance_2/soccer/GameEvent.java b/src/main/java/sofflix/practices/practice_12_usinginheritance_2/soccer/GameEvent.java new file mode 100644 index 0000000..e5864f5 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_12_usinginheritance_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 sofflix.practices.practice_12_usinginheritance_2.soccer; + +/** + * + * @author Omar + */ +public abstract class GameEvent { + /* Practice 12-2. Start selection here for code in Goal to cut and paste to 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; + } + + /* Practice 12-2. End selection here for code in Goal to cut and paste to GameEvent */ + +} diff --git a/src/main/java/sofflix/practices/practice_12_usinginheritance_2/soccer/Goal.java b/src/main/java/sofflix/practices/practice_12_usinginheritance_2/soccer/Goal.java new file mode 100644 index 0000000..872fdb0 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_12_usinginheritance_2/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 sofflix.practices.practice_12_usinginheritance_2.soccer; + +/** + * + * @author Administrator + */ + +/* Practice 12-2. Make this class extend GameEvent */ +public class Goal extends GameEvent{ + + +} diff --git a/src/main/java/sofflix/practices/practice_12_usinginheritance_2/soccer/League.java b/src/main/java/sofflix/practices/practice_12_usinginheritance_2/soccer/League.java new file mode 100644 index 0000000..f89508c --- /dev/null +++ b/src/main/java/sofflix/practices/practice_12_usinginheritance_2/soccer/League.java @@ -0,0 +1,106 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package sofflix.practices.practice_12_usinginheritance_2.soccer; + +import java.time.LocalDateTime; +import java.time.Period; +import java.util.ArrayList; +import java.util.StringTokenizer; +import sofflix.practices.practice_12_usinginheritance_2.utility.PlayerDatabase; + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + League theLeague = new League(); + + Team[] theTeams = theLeague.createTeams("The Robins,The Crows,The Swallows", 5); + Game[] theGames = theLeague.createGames(theTeams); + + System.out.println(theLeague.getLeagueAnnouncement(theGames)); + for (Game currGame: theGames){ + currGame.playGame(); + //break; + System.out.println(currGame.getDescription()); + } + + theLeague.showBestTeam(theTeams); + + } + + public Team[] createTeams(String teamNames, int teamSize) { + + PlayerDatabase playerDB = new PlayerDatabase(); + + StringTokenizer teamNameTokens = new StringTokenizer(teamNames, ","); + Team[] theTeams = new Team[teamNameTokens.countTokens()]; + for (int i =0; i < theTeams.length; i++){ + theTeams[i] = new Team(teamNameTokens.nextToken(), playerDB.getTeam(teamSize)); + } + + + return theTeams; + } + + public Game[] createGames(Team[] theTeams) { + int daysBetweenGames = 0; + + ArrayList theGames = new ArrayList(); + + for (Team homeTeam: theTeams){ + for (Team awayTeam: theTeams){ + if (homeTeam != awayTeam) { + daysBetweenGames += 7; + theGames.add(new Game(homeTeam, awayTeam, LocalDateTime.now().plusDays(daysBetweenGames))); + } + + } + } + + + + return (Game[]) theGames.toArray(new Game[1]); + } + + public void showBestTeam(Team[] theTeams) { + Team currBestTeam = theTeams[0]; + System.out.println("\nTeam Points"); + + for (Team currTeam: theTeams){ + System.out.println(currTeam.getTeamName() + " : " + currTeam.getPointsTotal() + " : " + + currTeam.getGoalsTotal()); + currBestTeam = currTeam.getPointsTotal() > currBestTeam.getPointsTotal()?currTeam:currBestTeam; + if (currTeam.getPointsTotal() > currBestTeam.getPointsTotal()){ + currBestTeam = currTeam; + } else if (currTeam.getPointsTotal() == currBestTeam.getPointsTotal()){ + if (currTeam.getGoalsTotal() > currBestTeam.getGoalsTotal()){ + currBestTeam = currTeam; + } + } + } + + System.out.println("Winner of the League is " + currBestTeam.getTeamName()); + + } + + public String getLeagueAnnouncement(Game[] theGames){ + + Period thePeriod = Period.between(theGames[0].getTheDateTime().toLocalDate(), + theGames[theGames.length - 1].getTheDateTime().toLocalDate()); + + return "The league is scheduled to run for " + + thePeriod.getMonths() + " month(s), and " + + thePeriod.getDays() + " day(s)\n"; + } + +} diff --git a/src/main/java/sofflix/practices/practice_12_usinginheritance_2/soccer/Player.java b/src/main/java/sofflix/practices/practice_12_usinginheritance_2/soccer/Player.java new file mode 100644 index 0000000..2ab7276 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_12_usinginheritance_2.soccer; + +/** + * + * @author Administrator + */ +public class Player { + + private String playerName; + + public Player(String playerName) { + this.playerName = playerName; + } + + public Player() {} + + /** + * @return the playerName + */ + public String getPlayerName() { + return playerName; + } + + /** + * @param playerName the playerName to set + */ + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + +} diff --git a/src/main/java/sofflix/practices/practice_12_usinginheritance_2/soccer/Possession.java b/src/main/java/sofflix/practices/practice_12_usinginheritance_2/soccer/Possession.java new file mode 100644 index 0000000..a4f6558 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_12_usinginheritance_2.soccer; + +/** + * + * @author Omar + */ +public class Possession extends GameEvent { + +} diff --git a/src/main/java/sofflix/practices/practice_12_usinginheritance_2/soccer/Team.java b/src/main/java/sofflix/practices/practice_12_usinginheritance_2/soccer/Team.java new file mode 100644 index 0000000..bc4b908 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_12_usinginheritance_2.soccer; + +/** + * + * @author Administrator + */ +public class Team { + + private String teamName; + private Player[] playerArray; + private int pointsTotal; + private int goalsTotal; + + public void incGoalsTotal(int goals){ + this.setGoalsTotal(this.getGoalsTotal() + goals); + } + + public void incPointsTotal(int points){ + this.pointsTotal += points; + } + + public Team(String teamName) { + this.teamName = teamName; + } + + public Team(String teamName, Player[] players) { + this(teamName); + this.playerArray = players; + } + + public Team() {} + + /** + * @return the teamName + */ + public String getTeamName() { + return teamName; + } + + /** + * @param teamName the teamName to set + */ + public void setTeamName(String teamName) { + this.teamName = teamName; + } + + /** + * @return the playerArray + */ + public Player[] getPlayerArray() { + return playerArray; + } + + /** + * @param playerArray the playerArray to set + */ + public void setPlayerArray(Player[] playerArray) { + this.playerArray = playerArray; + } + + /** + * @return the pointsTotal + */ + public int getPointsTotal() { + return pointsTotal; + } + + /** + * @param pointsTotal the pointsTotal to set + */ + public void setPointsTotal(int pointsTotal) { + this.pointsTotal = pointsTotal; + } + + /** + * @return the goalsTotal + */ + public int getGoalsTotal() { + return goalsTotal; + } + + /** + * @param goalsTotal the goalsTotal to set + */ + public void setGoalsTotal(int goalsTotal) { + this.goalsTotal = goalsTotal; + } + +} diff --git a/src/main/java/sofflix/practices/practice_12_usinginheritance_2/utility/PlayerDatabase.java b/src/main/java/sofflix/practices/practice_12_usinginheritance_2/utility/PlayerDatabase.java new file mode 100644 index 0000000..5e07314 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_12_usinginheritance_2.utility; + +import java.util.*; +import sofflix.practices.practice_12_usinginheritance_2.soccer.Player; + + + +/** + * + * @author Administrator + */ +public class PlayerDatabase { + + private ArrayList players; + + public PlayerDatabase(){ + StringTokenizer authorTokens = new StringTokenizer(authorList, ","); + players = new ArrayList(); + while (authorTokens.hasMoreTokens()){ + players.add(new Player(authorTokens.nextToken())); + } + } + + public Player[] getTeam(int numberOfPlayers){ + Player[] teamPlayers = new Player[numberOfPlayers]; + for (int i = 0; i < numberOfPlayers; i++){ + int playerIndex = (int) (Math.random() * players.size()); + teamPlayers[i] = players.get(playerIndex); + players.remove(playerIndex); + } + return teamPlayers; + + } + + + +String authorList = +"Agatha Christie," + +"Alan Patton," + +"Alexander Solzhenitsyn," + +"Arthur Conan Doyle," + +"Anthony Trollope," + +"Baroness Orczy," + +"Brendan Behan," + +"Brian Moore," + +"Boris Pasternik," + +"Charles Dickens," + +"Charlotte Bronte," + +"Dorothy Parker," + +"Emile Zola," + +"Frank O'Connor," + +"Geoffrey Chaucer," + +"George Eliot," + +"G. K. Chesterton," + +"Graham Green," + +"Henry James," + +"James Joyce," + +"J. M. Synge," + +"J. R. Tolkien," + +"Jane Austin," + +"Leo Tolstoy," + +"Liam O'Flaherty," + +"Marcel Proust," + +"Mark Twain," + +"Oscar Wilde," + +"O. Henry," + +"Samuel Beckett," + +"Sean O'Casey," + +"William Shakespeare," + +"William Makepeace Thackeray," + +"W. B. Yeats," + +"Wilkie Collins"; + +} diff --git a/src/main/java/sofflix/practices/practice_13_usinginterfaces_1/soccer/Game.java b/src/main/java/sofflix/practices/practice_13_usinginterfaces_1/soccer/Game.java new file mode 100644 index 0000000..5ad5bb1 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_13_usinginterfaces_1/soccer/Game.java @@ -0,0 +1,169 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package sofflix.practices.practice_13_usinginterfaces_1.soccer; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; + + +/** + * + * @author Administrator + */ +public class Game { + + private Team homeTeam; + private Team awayTeam; + private GameEvent[] goals; + private LocalDateTime theDateTime; + + public Game(Team homeTeam, Team awayTeam, LocalDateTime theDateTime) { + this.homeTeam = homeTeam; + this.awayTeam = awayTeam; + this.theDateTime = theDateTime; + } + + public void playGame() { + ArrayList eventList = new ArrayList(); + GameEvent currEvent; + for (int i = 1; i <=90; i++){ + /* Practice 13-1. Change expression to Math.random > 0.8 */ + if (Math.random() > 0.8){ + currEvent = Math.random() > 0.8?new Goal():new Possession(); + /* Practice 13-1. Change first part of ternary operator toMath.random > 0.8 */ + currEvent.setTheTeam(Math.random() > 0.5?homeTeam: awayTeam); + currEvent.setThePlayer(currEvent.getTheTeam(). + getPlayerArray()[(int)(Math.random() * currEvent.getTheTeam().getPlayerArray().length)]); + currEvent.setTheTime(i); + eventList.add(currEvent); + //System.out.println(i); + } + this.goals = new GameEvent[eventList.size()]; + eventList.toArray(goals); + + } + } + + public String getDescription() { + + int homeTeamGoals = 0; + int awayTeamGoals = 0; + StringBuilder returnString = new StringBuilder(); + + returnString.append(this.getHomeTeam().getTeamName() + " vs. " + + this.getAwayTeam().getTeamName() + "\n" + + "Date: " + this.getTheDateTime().format(DateTimeFormatter.ISO_LOCAL_DATE) + "\n"); + + for (GameEvent currEvent: this.getEvents()) { + + /* Practice 13-1. Start if block here to determine if currEvent is a Goal */ + if(currEvent instanceof Goal){ + if (currEvent.getTheTeam()== homeTeam) { + homeTeamGoals++; + homeTeam.incGoalsTotal(1); + } else { + awayTeamGoals++; + awayTeam.incGoalsTotal(1); + } + } + /* Practice 13-1. End if block here to determine if currEvent is a Goal */ + + returnString.append(currEvent +" after " + + currEvent.getTheTime() + " mins by " + + currEvent.getThePlayer().getPlayerName() + " of " + + currEvent.getTheTeam().getTeamName() + + "\n"); + } + + if (homeTeamGoals == awayTeamGoals) { + returnString.append("It's a draw!"); + this.homeTeam.incPointsTotal(1); + this.awayTeam.incPointsTotal(1); + } else if (homeTeamGoals > awayTeamGoals) { + returnString.append(homeTeam.getTeamName() + " win"); + this.homeTeam.incPointsTotal(1); + } else { + returnString.append(awayTeam.getTeamName() + " win"); + this.awayTeam.incPointsTotal(1); + } + returnString.append(" (" + homeTeamGoals + " - " + awayTeamGoals + ") \n"); + + return returnString.toString(); + } + + /** + * @return the homeTeam + */ + public Team getHomeTeam() { + return homeTeam; + } + + /** + * @param homeTeam the homeTeam to set + */ + public void setHomeTeam(Team homeTeam) { + this.homeTeam = homeTeam; + } + + /** + * @return the awayTeam + */ + public Team getAwayTeam() { + return awayTeam; + } + + /** + * @param awayTeam the awayTeam to set + */ + public void setAwayTeam(Team awayTeam) { + this.awayTeam = awayTeam; + } + + /** + * @return the goals + */ + public GameEvent[] getEvents() { + return goals; + } + + /** + * @param goals the goals to set + */ + public void setEvents(GameEvent[] goals) { + this.goals = goals; + } + + /** + * @return the localDateTime + */ + public LocalDateTime getLocalDateTime() { + return getTheDateTime(); + } + + /** + * @param theDateTime the localDateTime to set + */ + public void setLocalDateTime(LocalDateTime theDateTime) { + this.setTheDateTime(theDateTime); + } + + /** + * @return the theDateTime + */ + public LocalDateTime getTheDateTime() { + return theDateTime; + } + + /** + * @param theDateTime the theDateTime to set + */ + public void setTheDateTime(LocalDateTime theDateTime) { + this.theDateTime = theDateTime; + } + +} diff --git a/src/main/java/sofflix/practices/practice_13_usinginterfaces_1/soccer/GameEvent.java b/src/main/java/sofflix/practices/practice_13_usinginterfaces_1/soccer/GameEvent.java new file mode 100644 index 0000000..8eaab0d --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.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/sofflix/practices/practice_13_usinginterfaces_1/soccer/Goal.java b/src/main/java/sofflix/practices/practice_13_usinginterfaces_1/soccer/Goal.java new file mode 100644 index 0000000..b794869 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_13_usinginterfaces_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 sofflix.practices.practice_13_usinginterfaces_1.soccer; + +/** + * + * @author Administrator + */ +public class Goal extends GameEvent { + + /* Practice 13-1. Add a toString() method */ + public String toString(){ + return "Goal scored"; + } +} diff --git a/src/main/java/sofflix/practices/practice_13_usinginterfaces_1/soccer/League.java b/src/main/java/sofflix/practices/practice_13_usinginterfaces_1/soccer/League.java new file mode 100644 index 0000000..af391d9 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_13_usinginterfaces_1/soccer/League.java @@ -0,0 +1,106 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package sofflix.practices.practice_13_usinginterfaces_1.soccer; + +import java.time.LocalDateTime; +import java.time.Period; +import java.util.ArrayList; +import java.util.StringTokenizer; +import sofflix.practices.practice_13_usinginterfaces_1.utility.PlayerDatabase; + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + League theLeague = new League(); + + Team[] theTeams = theLeague.createTeams("The Robins,The Crows,The Swallows", 5); + Game[] theGames = theLeague.createGames(theTeams); + + System.out.println(theLeague.getLeagueAnnouncement(theGames)); + for (Game currGame: theGames){ + currGame.playGame(); + //break; + System.out.println(currGame.getDescription()); + } + + theLeague.showBestTeam(theTeams); + + } + + public Team[] createTeams(String teamNames, int teamSize) { + + PlayerDatabase playerDB = new PlayerDatabase(); + + StringTokenizer teamNameTokens = new StringTokenizer(teamNames, ","); + Team[] theTeams = new Team[teamNameTokens.countTokens()]; + for (int i =0; i < theTeams.length; i++){ + theTeams[i] = new Team(teamNameTokens.nextToken(), playerDB.getTeam(teamSize)); + } + + + return theTeams; + } + + public Game[] createGames(Team[] theTeams) { + int daysBetweenGames = 0; + + ArrayList theGames = new ArrayList(); + + for (Team homeTeam: theTeams){ + for (Team awayTeam: theTeams){ + if (homeTeam != awayTeam) { + daysBetweenGames += 7; + theGames.add(new Game(homeTeam, awayTeam, LocalDateTime.now().plusDays(daysBetweenGames))); + } + + } + } + + + + return (Game[]) theGames.toArray(new Game[1]); + } + + public void showBestTeam(Team[] theTeams) { + Team currBestTeam = theTeams[0]; + System.out.println("\nTeam Points"); + + for (Team currTeam: theTeams){ + System.out.println(currTeam.getTeamName() + " : " + currTeam.getPointsTotal() + " : " + + currTeam.getGoalsTotal()); + currBestTeam = currTeam.getPointsTotal() > currBestTeam.getPointsTotal()?currTeam:currBestTeam; + if (currTeam.getPointsTotal() > currBestTeam.getPointsTotal()){ + currBestTeam = currTeam; + } else if (currTeam.getPointsTotal() == currBestTeam.getPointsTotal()){ + if (currTeam.getGoalsTotal() > currBestTeam.getGoalsTotal()){ + currBestTeam = currTeam; + } + } + } + + System.out.println("Winner of the League is " + currBestTeam.getTeamName()); + + } + + public String getLeagueAnnouncement(Game[] theGames){ + + Period thePeriod = Period.between(theGames[0].getTheDateTime().toLocalDate(), + theGames[theGames.length - 1].getTheDateTime().toLocalDate()); + + return "The league is scheduled to run for " + + thePeriod.getMonths() + " month(s), and " + + thePeriod.getDays() + " day(s)\n"; + } + +} diff --git a/src/main/java/sofflix/practices/practice_13_usinginterfaces_1/soccer/Player.java b/src/main/java/sofflix/practices/practice_13_usinginterfaces_1/soccer/Player.java new file mode 100644 index 0000000..ef10362 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_13_usinginterfaces_1.soccer; + +/** + * + * @author Administrator + */ +public class Player { + + private String playerName; + + public Player(String playerName) { + this.playerName = playerName; + } + + public Player() {} + + /** + * @return the playerName + */ + public String getPlayerName() { + return playerName; + } + + /** + * @param playerName the playerName to set + */ + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + +} diff --git a/src/main/java/sofflix/practices/practice_13_usinginterfaces_1/soccer/Possession.java b/src/main/java/sofflix/practices/practice_13_usinginterfaces_1/soccer/Possession.java new file mode 100644 index 0000000..9ae060d --- /dev/null +++ b/src/main/java/sofflix/practices/practice_13_usinginterfaces_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 sofflix.practices.practice_13_usinginterfaces_1.soccer; + +/** + * + * @author ksomervi + */ +public class Possession extends GameEvent { + + /* Practice 13-1. Add a toString() method */ + public String toString(){ + return "Possession"; + } +} diff --git a/src/main/java/sofflix/practices/practice_13_usinginterfaces_1/soccer/Team.java b/src/main/java/sofflix/practices/practice_13_usinginterfaces_1/soccer/Team.java new file mode 100644 index 0000000..d66ebe2 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_13_usinginterfaces_1.soccer; + +/** + * + * @author Administrator + */ +public class Team { + + private String teamName; + private Player[] playerArray; + private int pointsTotal; + private int goalsTotal; + + public void incGoalsTotal(int goals){ + this.setGoalsTotal(this.getGoalsTotal() + goals); + } + + public void incPointsTotal(int points){ + this.pointsTotal += points; + } + + public Team(String teamName) { + this.teamName = teamName; + } + + public Team(String teamName, Player[] players) { + this(teamName); + this.playerArray = players; + } + + public Team() {} + + /** + * @return the teamName + */ + public String getTeamName() { + return teamName; + } + + /** + * @param teamName the teamName to set + */ + public void setTeamName(String teamName) { + this.teamName = teamName; + } + + /** + * @return the playerArray + */ + public Player[] getPlayerArray() { + return playerArray; + } + + /** + * @param playerArray the playerArray to set + */ + public void setPlayerArray(Player[] playerArray) { + this.playerArray = playerArray; + } + + /** + * @return the pointsTotal + */ + public int getPointsTotal() { + return pointsTotal; + } + + /** + * @param pointsTotal the pointsTotal to set + */ + public void setPointsTotal(int pointsTotal) { + this.pointsTotal = pointsTotal; + } + + /** + * @return the goalsTotal + */ + public int getGoalsTotal() { + return goalsTotal; + } + + /** + * @param goalsTotal the goalsTotal to set + */ + public void setGoalsTotal(int goalsTotal) { + this.goalsTotal = goalsTotal; + } + +} diff --git a/src/main/java/sofflix/practices/practice_13_usinginterfaces_1/utility/PlayerDatabase.java b/src/main/java/sofflix/practices/practice_13_usinginterfaces_1/utility/PlayerDatabase.java new file mode 100644 index 0000000..7e37ae6 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_13_usinginterfaces_1.utility; + +import java.util.*; +import sofflix.practices.practice_13_usinginterfaces_1.soccer.Player; + + + +/** + * + * @author Administrator + */ +public class PlayerDatabase { + + private ArrayList players; + + public PlayerDatabase(){ + StringTokenizer authorTokens = new StringTokenizer(authorList, ","); + players = new ArrayList(); + while (authorTokens.hasMoreTokens()){ + players.add(new Player(authorTokens.nextToken())); + } + } + + public Player[] getTeam(int numberOfPlayers){ + Player[] teamPlayers = new Player[numberOfPlayers]; + for (int i = 0; i < numberOfPlayers; i++){ + int playerIndex = (int) (Math.random() * players.size()); + teamPlayers[i] = players.get(playerIndex); + players.remove(playerIndex); + } + return teamPlayers; + + } + + + +String authorList = +"Agatha Christie," + +"Alan Patton," + +"Alexander Solzhenitsyn," + +"Arthur Conan Doyle," + +"Anthony Trollope," + +"Baroness Orczy," + +"Brendan Behan," + +"Brian Moore," + +"Boris Pasternik," + +"Charles Dickens," + +"Charlotte Bronte," + +"Dorothy Parker," + +"Emile Zola," + +"Frank O'Connor," + +"Geoffrey Chaucer," + +"George Eliot," + +"G. K. Chesterton," + +"Graham Green," + +"Henry James," + +"James Joyce," + +"J. M. Synge," + +"J. R. Tolkien," + +"Jane Austin," + +"Leo Tolstoy," + +"Liam O'Flaherty," + +"Marcel Proust," + +"Mark Twain," + +"Oscar Wilde," + +"O. Henry," + +"Samuel Beckett," + +"Sean O'Casey," + +"William Shakespeare," + +"William Makepeace Thackeray," + +"W. B. Yeats," + +"Wilkie Collins"; + +} diff --git a/src/main/java/sofflix/practices/practice_13_usinginterfaces_2/soccer/Game.java b/src/main/java/sofflix/practices/practice_13_usinginterfaces_2/soccer/Game.java new file mode 100644 index 0000000..c7ba624 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_13_usinginterfaces_2.soccer; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; + + +/** + * + * @author Administrator + */ +public class Game { + + private Team homeTeam; + private Team awayTeam; + private GameEvent[] goals; + private LocalDateTime theDateTime; + + public Game(Team homeTeam, Team awayTeam, LocalDateTime theDateTime) { + this.homeTeam = homeTeam; + this.awayTeam = awayTeam; + this.theDateTime = theDateTime; + } + + public void playGame() { + ArrayList eventList = new ArrayList(); + GameEvent currEvent; + for (int i = 1; i <=90; i++){ + + if (Math.random() > 0.8){ + + currEvent = Math.random() > 0.8?new Goal():new Possession(); + currEvent.setTheTeam(Math.random() > 0.5?homeTeam: awayTeam); + currEvent.setThePlayer(currEvent.getTheTeam(). + getPlayerArray()[(int)(Math.random() * currEvent.getTheTeam().getPlayerArray().length)]); + currEvent.setTheTime(i); + eventList.add(currEvent); + //System.out.println(i); + } + this.goals = new GameEvent[eventList.size()]; + eventList.toArray(goals); + + } + } + + public String getDescription() { + + int homeTeamGoals = 0; + int awayTeamGoals = 0; + StringBuilder returnString = new StringBuilder(); + + returnString.append(this.getHomeTeam().getTeamName() + " vs. " + + this.getAwayTeam().getTeamName() + "\n" + + "Date: " + this.getTheDateTime().format(DateTimeFormatter.ISO_LOCAL_DATE) + "\n"); + + for (GameEvent currEvent: this.getEvents()) { + + if (currEvent instanceof Goal) { + if (currEvent.getTheTeam()== homeTeam) { + homeTeamGoals++; + homeTeam.incGoalsTotal(1); + } else { + awayTeamGoals++; + awayTeam.incGoalsTotal(1); + } + } + + returnString.append(currEvent +" after " + + currEvent.getTheTime() + " mins by " + + currEvent.getThePlayer().getPlayerName() + " of " + + currEvent.getTheTeam().getTeamName() + + "\n"); + } + + if (homeTeamGoals == awayTeamGoals) { + returnString.append("It's a draw!"); + this.homeTeam.incPointsTotal(1); + this.awayTeam.incPointsTotal(1); + } else if (homeTeamGoals > awayTeamGoals) { + returnString.append(homeTeam.getTeamName() + " win"); + this.homeTeam.incPointsTotal(1); + } else { + returnString.append(awayTeam.getTeamName() + " win"); + this.awayTeam.incPointsTotal(1); + } + returnString.append(" (" + homeTeamGoals + " - " + awayTeamGoals + ") \n"); + + return returnString.toString(); + } + + /** + * @return the homeTeam + */ + public Team getHomeTeam() { + return homeTeam; + } + + /** + * @param homeTeam the homeTeam to set + */ + public void setHomeTeam(Team homeTeam) { + this.homeTeam = homeTeam; + } + + /** + * @return the awayTeam + */ + public Team getAwayTeam() { + return awayTeam; + } + + /** + * @param awayTeam the awayTeam to set + */ + public void setAwayTeam(Team awayTeam) { + this.awayTeam = awayTeam; + } + + /** + * @return the goals + */ + public GameEvent[] getEvents() { + return goals; + } + + /** + * @param goals the goals to set + */ + public void setEvents(GameEvent[] goals) { + this.goals = goals; + } + + /** + * @return the localDateTime + */ + public LocalDateTime getLocalDateTime() { + return getTheDateTime(); + } + + /** + * @param theDateTime the localDateTime to set + */ + public void setLocalDateTime(LocalDateTime theDateTime) { + this.setTheDateTime(theDateTime); + } + + /** + * @return the theDateTime + */ + public LocalDateTime getTheDateTime() { + return theDateTime; + } + + /** + * @param theDateTime the theDateTime to set + */ + public void setTheDateTime(LocalDateTime theDateTime) { + this.theDateTime = theDateTime; + } + +} diff --git a/src/main/java/sofflix/practices/practice_13_usinginterfaces_2/soccer/GameEvent.java b/src/main/java/sofflix/practices/practice_13_usinginterfaces_2/soccer/GameEvent.java new file mode 100644 index 0000000..e8eaea8 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_13_usinginterfaces_2.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/sofflix/practices/practice_13_usinginterfaces_2/soccer/Goal.java b/src/main/java/sofflix/practices/practice_13_usinginterfaces_2/soccer/Goal.java new file mode 100644 index 0000000..ee8e08a --- /dev/null +++ b/src/main/java/sofflix/practices/practice_13_usinginterfaces_2/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 sofflix.practices.practice_13_usinginterfaces_2.soccer; + +/** + * + * @author Administrator + */ +public class Goal extends GameEvent { + + public String toString(){ + return "Goal scored"; + } +} diff --git a/src/main/java/sofflix/practices/practice_13_usinginterfaces_2/soccer/League.java b/src/main/java/sofflix/practices/practice_13_usinginterfaces_2/soccer/League.java new file mode 100644 index 0000000..42a2280 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_13_usinginterfaces_2/soccer/League.java @@ -0,0 +1,106 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package sofflix.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 sofflix.practices.practice_13_usinginterfaces_2.utility.PlayerDatabase; + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + League theLeague = new League(); + + Team[] theTeams = theLeague.createTeams("The Robins,The Crows,The Swallows", 5); + Game[] theGames = theLeague.createGames(theTeams); + + System.out.println(theLeague.getLeagueAnnouncement(theGames)); + for (Game currGame: theGames){ + currGame.playGame(); + //break; + System.out.println(currGame.getDescription()); + } + + theLeague.showBestTeam(theTeams); + + } + + public Team[] createTeams(String teamNames, int teamSize) { + + PlayerDatabase playerDB = new PlayerDatabase(); + + StringTokenizer teamNameTokens = new StringTokenizer(teamNames, ","); + Team[] theTeams = new Team[teamNameTokens.countTokens()]; + for (int i =0; i < theTeams.length; i++){ + theTeams[i] = new Team(teamNameTokens.nextToken(), playerDB.getTeam(teamSize)); + } + + + return theTeams; + } + + public Game[] createGames(Team[] theTeams) { + int daysBetweenGames = 0; + + ArrayList theGames = new ArrayList(); + + for (Team homeTeam: theTeams){ + for (Team awayTeam: theTeams){ + if (homeTeam != awayTeam) { + daysBetweenGames += 7; + theGames.add(new Game(homeTeam, awayTeam, LocalDateTime.now().plusDays(daysBetweenGames))); + } + + } + } + + + + return (Game[]) theGames.toArray(new Game[1]); + } + + public void showBestTeam(Team[] theTeams) { + + /* Practice 13-2. Add statement to sort theTeams here */ + Arrays.sort(theTeams); + Team currBestTeam = theTeams[0]; + System.out.println("\nTeam Points"); + + for (Team currTeam: theTeams){ + System.out.println(currTeam.getTeamName() + " : " + currTeam.getPointsTotal() + " : " + + currTeam.getGoalsTotal()); + + /* Practice 13-2. Remove all conditional code starting here */ + + /* Practice 13-2. Remove all conditional code between this point and above comment */ + } + + System.out.println("Winner of the League is " + currBestTeam.getTeamName()); + + } + + public String getLeagueAnnouncement(Game[] theGames){ + + Period thePeriod = Period.between(theGames[0].getTheDateTime().toLocalDate(), + theGames[theGames.length - 1].getTheDateTime().toLocalDate()); + + return "The league is scheduled to run for " + + thePeriod.getMonths() + " month(s), and " + + thePeriod.getDays() + " day(s)\n"; + } + +} diff --git a/src/main/java/sofflix/practices/practice_13_usinginterfaces_2/soccer/Player.java b/src/main/java/sofflix/practices/practice_13_usinginterfaces_2/soccer/Player.java new file mode 100644 index 0000000..50712d2 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_13_usinginterfaces_2.soccer; + +/** + * + * @author Administrator + */ +public class Player { + + private String playerName; + + public Player(String playerName) { + this.playerName = playerName; + } + + public Player() {} + + /** + * @return the playerName + */ + public String getPlayerName() { + return playerName; + } + + /** + * @param playerName the playerName to set + */ + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + +} diff --git a/src/main/java/sofflix/practices/practice_13_usinginterfaces_2/soccer/Possession.java b/src/main/java/sofflix/practices/practice_13_usinginterfaces_2/soccer/Possession.java new file mode 100644 index 0000000..eda3e86 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_13_usinginterfaces_2/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 sofflix.practices.practice_13_usinginterfaces_2.soccer; + +/** + * + * @author ksomervi + */ +public class Possession extends GameEvent { + + public String toString(){ + return "Possession"; + } + +} diff --git a/src/main/java/sofflix/practices/practice_13_usinginterfaces_2/soccer/Team.java b/src/main/java/sofflix/practices/practice_13_usinginterfaces_2/soccer/Team.java new file mode 100644 index 0000000..a3c7bd9 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_13_usinginterfaces_2/soccer/Team.java @@ -0,0 +1,113 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package sofflix.practices.practice_13_usinginterfaces_2.soccer; + +/** + * + * @author Administrator + */ + +/* Practice 13-2. Change class declaration so that it implements Comparable */ +public class Team implements Comparable{ + + private String teamName; + private Player[] playerArray; + private int pointsTotal; + private int goalsTotal; + + /* Practice 13-2. Add the compareTo() method here */ + @Override + 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/sofflix/practices/practice_13_usinginterfaces_2/utility/PlayerDatabase.java b/src/main/java/sofflix/practices/practice_13_usinginterfaces_2/utility/PlayerDatabase.java new file mode 100644 index 0000000..139edaf --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_13_usinginterfaces_2.utility; + +import java.util.*; +import sofflix.practices.practice_13_usinginterfaces_2.soccer.Player; + + + +/** + * + * @author Administrator + */ +public class PlayerDatabase { + + private ArrayList players; + + public PlayerDatabase(){ + StringTokenizer authorTokens = new StringTokenizer(authorList, ","); + players = new ArrayList(); + while (authorTokens.hasMoreTokens()){ + players.add(new Player(authorTokens.nextToken())); + } + } + + public Player[] getTeam(int numberOfPlayers){ + Player[] teamPlayers = new Player[numberOfPlayers]; + for (int i = 0; i < numberOfPlayers; i++){ + int playerIndex = (int) (Math.random() * players.size()); + teamPlayers[i] = players.get(playerIndex); + players.remove(playerIndex); + } + return teamPlayers; + + } + + + +String authorList = +"Agatha Christie," + +"Alan Patton," + +"Alexander Solzhenitsyn," + +"Arthur Conan Doyle," + +"Anthony Trollope," + +"Baroness Orczy," + +"Brendan Behan," + +"Brian Moore," + +"Boris Pasternik," + +"Charles Dickens," + +"Charlotte Bronte," + +"Dorothy Parker," + +"Emile Zola," + +"Frank O'Connor," + +"Geoffrey Chaucer," + +"George Eliot," + +"G. K. Chesterton," + +"Graham Green," + +"Henry James," + +"James Joyce," + +"J. M. Synge," + +"J. R. Tolkien," + +"Jane Austin," + +"Leo Tolstoy," + +"Liam O'Flaherty," + +"Marcel Proust," + +"Mark Twain," + +"Oscar Wilde," + +"O. Henry," + +"Samuel Beckett," + +"Sean O'Casey," + +"William Shakespeare," + +"William Makepeace Thackeray," + +"W. B. Yeats," + +"Wilkie Collins"; + +} diff --git a/src/main/java/sofflix/practices/practice_13_usinginterfaces_3/soccer/Game.java b/src/main/java/sofflix/practices/practice_13_usinginterfaces_3/soccer/Game.java new file mode 100644 index 0000000..41e246b --- /dev/null +++ b/src/main/java/sofflix/practices/practice_13_usinginterfaces_3/soccer/Game.java @@ -0,0 +1,170 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package sofflix.practices.practice_13_usinginterfaces_3.soccer; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; + + +/** + * + * @author Administrator + */ +public class Game { + + private Team homeTeam; + private Team awayTeam; + private GameEvent[] goals; + private LocalDateTime theDateTime; + + public Game(Team homeTeam, Team awayTeam, LocalDateTime theDateTime) { + this.homeTeam = homeTeam; + this.awayTeam = awayTeam; + this.theDateTime = theDateTime; + } + + public void playGame() { + ArrayList eventList = new ArrayList(); + GameEvent currEvent; + for (int i = 1; i <=90; i++){ + + if (Math.random() > 0.8){ + + currEvent = Math.random() > 0.8?new Goal():new Possession(); + currEvent.setTheTeam(Math.random() > 0.5?homeTeam: awayTeam); + currEvent.setThePlayer(currEvent.getTheTeam(). + getPlayerArray()[(int)(Math.random() * currEvent.getTheTeam().getPlayerArray().length)]); + currEvent.setTheTime(i); + eventList.add(currEvent); + //System.out.println(i); + } + this.goals = new GameEvent[eventList.size()]; + eventList.toArray(goals); + + } + } + + public String getDescription() { + + int homeTeamGoals = 0; + int awayTeamGoals = 0; + StringBuilder returnString = new StringBuilder(); + + returnString.append(this.getHomeTeam().getTeamName() + " vs. " + + this.getAwayTeam().getTeamName() + "\n" + + "Date: " + this.getTheDateTime().format(DateTimeFormatter.ISO_LOCAL_DATE) + "\n"); + + for (GameEvent currEvent: this.getEvents()) { + + if (currEvent instanceof Goal) { + if (currEvent.getTheTeam()== homeTeam) { + homeTeamGoals++; + homeTeam.incGoalsTotal(1); + } else { + awayTeamGoals++; + awayTeam.incGoalsTotal(1); + } + + /* Practice 13-3. Add the line to increment goals scored for a player */ + currEvent.getThePlayer().incGoalsScored(); + } + + returnString.append(currEvent +" after " + + currEvent.getTheTime() + " mins by " + + currEvent.getThePlayer().getPlayerName() + " of " + + currEvent.getTheTeam().getTeamName() + + "\n"); + } + + if (homeTeamGoals == awayTeamGoals) { + returnString.append("It's a draw!"); + this.homeTeam.incPointsTotal(1); + this.awayTeam.incPointsTotal(1); + } else if (homeTeamGoals > awayTeamGoals) { + returnString.append(homeTeam.getTeamName() + " win"); + this.homeTeam.incPointsTotal(1); + } else { + returnString.append(awayTeam.getTeamName() + " win"); + this.awayTeam.incPointsTotal(1); + } + returnString.append(" (" + homeTeamGoals + " - " + awayTeamGoals + ") \n"); + + return returnString.toString(); + } + + /** + * @return the homeTeam + */ + public Team getHomeTeam() { + return homeTeam; + } + + /** + * @param homeTeam the homeTeam to set + */ + public void setHomeTeam(Team homeTeam) { + this.homeTeam = homeTeam; + } + + /** + * @return the awayTeam + */ + public Team getAwayTeam() { + return awayTeam; + } + + /** + * @param awayTeam the awayTeam to set + */ + public void setAwayTeam(Team awayTeam) { + this.awayTeam = awayTeam; + } + + /** + * @return the goals + */ + public GameEvent[] getEvents() { + return goals; + } + + /** + * @param goals the goals to set + */ + public void setEvents(GameEvent[] goals) { + this.goals = goals; + } + + /** + * @return the localDateTime + */ + public LocalDateTime getLocalDateTime() { + return getTheDateTime(); + } + + /** + * @param theDateTime the localDateTime to set + */ + public void setLocalDateTime(LocalDateTime theDateTime) { + this.setTheDateTime(theDateTime); + } + + /** + * @return the theDateTime + */ + public LocalDateTime getTheDateTime() { + return theDateTime; + } + + /** + * @param theDateTime the theDateTime to set + */ + public void setTheDateTime(LocalDateTime theDateTime) { + this.theDateTime = theDateTime; + } + +} diff --git a/src/main/java/sofflix/practices/practice_13_usinginterfaces_3/soccer/GameEvent.java b/src/main/java/sofflix/practices/practice_13_usinginterfaces_3/soccer/GameEvent.java new file mode 100644 index 0000000..98d4417 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_13_usinginterfaces_3.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/sofflix/practices/practice_13_usinginterfaces_3/soccer/Goal.java b/src/main/java/sofflix/practices/practice_13_usinginterfaces_3/soccer/Goal.java new file mode 100644 index 0000000..5f1aee2 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_13_usinginterfaces_3/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 sofflix.practices.practice_13_usinginterfaces_3.soccer; + +/** + * + * @author Administrator + */ +public class Goal extends GameEvent { + + public String toString(){ + return "Goal scored"; + } +} diff --git a/src/main/java/sofflix/practices/practice_13_usinginterfaces_3/soccer/League.java b/src/main/java/sofflix/practices/practice_13_usinginterfaces_3/soccer/League.java new file mode 100644 index 0000000..2bc3373 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_13_usinginterfaces_3/soccer/League.java @@ -0,0 +1,115 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package sofflix.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 sofflix.practices.practice_13_usinginterfaces_3.utility.PlayerDatabase; + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + League theLeague = new League(); + + Team[] theTeams = theLeague.createTeams("The Robins,The Crows,The Swallows", 5); + Game[] theGames = theLeague.createGames(theTeams); + + System.out.println(theLeague.getLeagueAnnouncement(theGames)); + for (Game currGame: theGames){ + currGame.playGame(); + //break; + System.out.println(currGame.getDescription()); + } + + theLeague.showBestTeam(theTeams); + theLeague.showBestPlayers(theTeams); + } + + public Team[] createTeams(String teamNames, int teamSize) { + + PlayerDatabase playerDB = new PlayerDatabase(); + + StringTokenizer teamNameTokens = new StringTokenizer(teamNames, ","); + Team[] theTeams = new Team[teamNameTokens.countTokens()]; + for (int i =0; i < theTeams.length; i++){ + theTeams[i] = new Team(teamNameTokens.nextToken(), playerDB.getTeam(teamSize)); + } + + + return theTeams; + } + + public Game[] createGames(Team[] theTeams) { + int daysBetweenGames = 0; + + ArrayList theGames = new ArrayList(); + + for (Team homeTeam: theTeams){ + for (Team awayTeam: theTeams){ + if (homeTeam != awayTeam) { + daysBetweenGames += 7; + theGames.add(new Game(homeTeam, awayTeam, LocalDateTime.now().plusDays(daysBetweenGames))); + } + + } + } + + + + return (Game[]) theGames.toArray(new Game[1]); + } + + public void showBestTeam(Team[] theTeams) { + + Arrays.sort(theTeams); + Team currBestTeam = theTeams[0]; + System.out.println("\nTeam Points"); + + for (Team currTeam: theTeams){ + System.out.println(currTeam.getTeamName() + " : " + currTeam.getPointsTotal() + " : " + + currTeam.getGoalsTotal()); + + } + + System.out.println("Winner of the League is " + currBestTeam.getTeamName()); + + } + + public String getLeagueAnnouncement(Game[] theGames){ + + Period thePeriod = Period.between(theGames[0].getTheDateTime().toLocalDate(), + theGames[theGames.length - 1].getTheDateTime().toLocalDate()); + + return "The league is scheduled to run for " + + thePeriod.getMonths() + " month(s), and " + + thePeriod.getDays() + " day(s)\n"; + } + + public void showBestPlayers(Team[] theTeams){ + ArrayList thePlayers = new ArrayList(); + for (Team currTeam : theTeams) { + thePlayers.addAll(Arrays.asList(currTeam.getPlayerArray())); + } + 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/sofflix/practices/practice_13_usinginterfaces_3/soccer/Player.java b/src/main/java/sofflix/practices/practice_13_usinginterfaces_3/soccer/Player.java new file mode 100644 index 0000000..70deaa7 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_13_usinginterfaces_3/soccer/Player.java @@ -0,0 +1,56 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package sofflix.practices.practice_13_usinginterfaces_3.soccer; + +/** + * + * @author Administrator + */ +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; + } + + /** + * @return the goalsScored + */ + public int getGoalsScored() { + return goalsScored; + } + + /** + * @param goalsScored the goalsScored to set + */ + public void setGoalsScored(int goalsScored) { + this.goalsScored = goalsScored; + } + +} diff --git a/src/main/java/sofflix/practices/practice_13_usinginterfaces_3/soccer/Possession.java b/src/main/java/sofflix/practices/practice_13_usinginterfaces_3/soccer/Possession.java new file mode 100644 index 0000000..b81e2a0 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_13_usinginterfaces_3/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 sofflix.practices.practice_13_usinginterfaces_3.soccer; + +/** + * + * @author ksomervi + */ +public class Possession extends GameEvent { + + public String toString(){ + return "Possession"; + } + +} diff --git a/src/main/java/sofflix/practices/practice_13_usinginterfaces_3/soccer/Team.java b/src/main/java/sofflix/practices/practice_13_usinginterfaces_3/soccer/Team.java new file mode 100644 index 0000000..c844846 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_13_usinginterfaces_3.soccer; + +/** + * + * @author Administrator + */ +public class Team implements Comparable { + + private String teamName; + private Player[] playerArray; + private int pointsTotal; + private int goalsTotal; + + public int compareTo(Object theTeam){ + int returnValue = -1; + if (this.getPointsTotal()< ((Team)theTeam).getPointsTotal()) { + returnValue = 1; + } else if (this.getPointsTotal() == ((Team)theTeam).getPointsTotal()){ + if (this.getGoalsTotal()< ((Team)theTeam).getGoalsTotal()) { + returnValue = 1; + } + } + return returnValue; + } + + public void incGoalsTotal(int goals){ + this.setGoalsTotal(this.getGoalsTotal() + goals); + } + + public void incPointsTotal(int points){ + this.pointsTotal += points; + } + + public Team(String teamName) { + this.teamName = teamName; + } + + public Team(String teamName, Player[] players) { + this(teamName); + this.playerArray = players; + } + + public Team() {} + + /** + * @return the teamName + */ + public String getTeamName() { + return teamName; + } + + /** + * @param teamName the teamName to set + */ + public void setTeamName(String teamName) { + this.teamName = teamName; + } + + /** + * @return the playerArray + */ + public Player[] getPlayerArray() { + return playerArray; + } + + /** + * @param playerArray the playerArray to set + */ + public void setPlayerArray(Player[] playerArray) { + this.playerArray = playerArray; + } + + /** + * @return the pointsTotal + */ + public int getPointsTotal() { + return pointsTotal; + } + + /** + * @param pointsTotal the pointsTotal to set + */ + public void setPointsTotal(int pointsTotal) { + this.pointsTotal = pointsTotal; + } + + /** + * @return the goalsTotal + */ + public int getGoalsTotal() { + return goalsTotal; + } + + /** + * @param goalsTotal the goalsTotal to set + */ + public void setGoalsTotal(int goalsTotal) { + this.goalsTotal = goalsTotal; + } + +} diff --git a/src/main/java/sofflix/practices/practice_13_usinginterfaces_3/utility/PlayerDatabase.java b/src/main/java/sofflix/practices/practice_13_usinginterfaces_3/utility/PlayerDatabase.java new file mode 100644 index 0000000..3c4f58a --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_13_usinginterfaces_3.utility; + +import java.util.*; +import sofflix.practices.practice_13_usinginterfaces_3.soccer.Player; + + + +/** + * + * @author Administrator + */ +public class PlayerDatabase { + + private ArrayList players; + + public PlayerDatabase(){ + StringTokenizer authorTokens = new StringTokenizer(authorList, ","); + players = new ArrayList(); + while (authorTokens.hasMoreTokens()){ + players.add(new Player(authorTokens.nextToken())); + } + } + + public Player[] getTeam(int numberOfPlayers){ + Player[] teamPlayers = new Player[numberOfPlayers]; + for (int i = 0; i < numberOfPlayers; i++){ + int playerIndex = (int) (Math.random() * players.size()); + teamPlayers[i] = players.get(playerIndex); + players.remove(playerIndex); + } + return teamPlayers; + + } + + + +String authorList = +"Agatha Christie," + +"Alan Patton," + +"Alexander Solzhenitsyn," + +"Arthur Conan Doyle," + +"Anthony Trollope," + +"Baroness Orczy," + +"Brendan Behan," + +"Brian Moore," + +"Boris Pasternik," + +"Charles Dickens," + +"Charlotte Bronte," + +"Dorothy Parker," + +"Emile Zola," + +"Frank O'Connor," + +"Geoffrey Chaucer," + +"George Eliot," + +"G. K. Chesterton," + +"Graham Green," + +"Henry James," + +"James Joyce," + +"J. M. Synge," + +"J. R. Tolkien," + +"Jane Austin," + +"Leo Tolstoy," + +"Liam O'Flaherty," + +"Marcel Proust," + +"Mark Twain," + +"Oscar Wilde," + +"O. Henry," + +"Samuel Beckett," + +"Sean O'Casey," + +"William Shakespeare," + +"William Makepeace Thackeray," + +"W. B. Yeats," + +"Wilkie Collins"; + +} diff --git a/src/main/java/sofflix/practices/practice_14_exceptions_1/soccer/Game.java b/src/main/java/sofflix/practices/practice_14_exceptions_1/soccer/Game.java new file mode 100644 index 0000000..1666abd --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_14_exceptions_1.soccer; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; + + +/** + * + * @author Administrator + */ +public class Game { + + private Team homeTeam; + private Team awayTeam; + private GameEvent[] goals; + private LocalDateTime theDateTime; + + public Game(Team homeTeam, Team awayTeam, LocalDateTime theDateTime) { + this.homeTeam = homeTeam; + this.awayTeam = awayTeam; + this.theDateTime = theDateTime; + } + + public void playGame() { + ArrayList eventList = new ArrayList(); + GameEvent currEvent; + for (int i = 1; i <=90; i++){ + + if (Math.random() > 0.8){ + + currEvent = Math.random() > 0.8?new Goal():new Possession(); + currEvent.setTheTeam(Math.random() > 0.5?homeTeam: awayTeam); + currEvent.setThePlayer(currEvent.getTheTeam(). + getPlayerArray()[(int)(Math.random() * currEvent.getTheTeam().getPlayerArray().length)]); + currEvent.setTheTime(i); + eventList.add(currEvent); + //System.out.println(i); + } + this.goals = new GameEvent[eventList.size()]; + eventList.toArray(goals); + + } + } + + public String getDescription() { + + int homeTeamGoals = 0; + int awayTeamGoals = 0; + StringBuilder returnString = new StringBuilder(); + + returnString.append(this.getHomeTeam().getTeamName() + " vs. " + + this.getAwayTeam().getTeamName() + "\n" + + "Date: " + this.getTheDateTime().format(DateTimeFormatter.ISO_LOCAL_DATE) + "\n"); + + for (GameEvent currEvent: this.getEvents()) { + + if (currEvent instanceof Goal) { + if (currEvent.getTheTeam()== homeTeam) { + homeTeamGoals++; + homeTeam.incGoalsTotal(1); + } else { + awayTeamGoals++; + awayTeam.incGoalsTotal(1); + } + currEvent.getThePlayer().incGoalsScored(); + } + + returnString.append(currEvent +" after " + + currEvent.getTheTime() + " mins by " + + currEvent.getThePlayer().getPlayerName() + " of " + + currEvent.getTheTeam().getTeamName() + + "\n"); + } + + if (homeTeamGoals == awayTeamGoals) { + returnString.append("It's a draw!"); + this.homeTeam.incPointsTotal(1); + this.awayTeam.incPointsTotal(1); + } else if (homeTeamGoals > awayTeamGoals) { + returnString.append(homeTeam.getTeamName() + " win"); + this.homeTeam.incPointsTotal(1); + } else { + returnString.append(awayTeam.getTeamName() + " win"); + this.awayTeam.incPointsTotal(1); + } + returnString.append(" (" + homeTeamGoals + " - " + awayTeamGoals + ") \n"); + + return returnString.toString(); + } + + /** + * @return the homeTeam + */ + public Team getHomeTeam() { + return homeTeam; + } + + /** + * @param homeTeam the homeTeam to set + */ + public void setHomeTeam(Team homeTeam) { + this.homeTeam = homeTeam; + } + + /** + * @return the awayTeam + */ + public Team getAwayTeam() { + return awayTeam; + } + + /** + * @param awayTeam the awayTeam to set + */ + public void setAwayTeam(Team awayTeam) { + this.awayTeam = awayTeam; + } + + /** + * @return the goals + */ + public GameEvent[] getEvents() { + return goals; + } + + /** + * @param goals the goals to set + */ + public void setEvents(GameEvent[] goals) { + this.goals = goals; + } + + /** + * @return the localDateTime + */ + public LocalDateTime getLocalDateTime() { + return getTheDateTime(); + } + + /** + * @param theDateTime the localDateTime to set + */ + public void setLocalDateTime(LocalDateTime theDateTime) { + this.setTheDateTime(theDateTime); + } + + /** + * @return the theDateTime + */ + public LocalDateTime getTheDateTime() { + return theDateTime; + } + + /** + * @param theDateTime the theDateTime to set + */ + public void setTheDateTime(LocalDateTime theDateTime) { + this.theDateTime = theDateTime; + } + +} diff --git a/src/main/java/sofflix/practices/practice_14_exceptions_1/soccer/GameEvent.java b/src/main/java/sofflix/practices/practice_14_exceptions_1/soccer/GameEvent.java new file mode 100644 index 0000000..f3b6775 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.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/sofflix/practices/practice_14_exceptions_1/soccer/Goal.java b/src/main/java/sofflix/practices/practice_14_exceptions_1/soccer/Goal.java new file mode 100644 index 0000000..1182495 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_14_exceptions_1.soccer; + +/** + * + * @author Administrator + */ +public class Goal extends GameEvent { + + public String toString(){ + return "Goal scored"; + } +} diff --git a/src/main/java/sofflix/practices/practice_14_exceptions_1/soccer/League.java b/src/main/java/sofflix/practices/practice_14_exceptions_1/soccer/League.java new file mode 100644 index 0000000..b974cd2 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_14_exceptions_1/soccer/League.java @@ -0,0 +1,130 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package sofflix.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 sofflix.practices.practice_14_exceptions_1.utility.PlayerDatabase; +import sofflix.practices.practice_14_exceptions_1.utility.PlayerDatabaseException; + +/** + * + * @author Administrator + */ +public class League { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + + League theLeague = new League(); + + /* Practice 14-1, Step 3a. Start a try block here */ + try{ + + /* Practice 14-1, Step 2a. Modify the line below to add more teams and players */ + Team[] theTeams = theLeague.createTeams("The Robins,The Crows,The Swallows, The Owls", 11); + Game[] theGames = theLeague.createGames(theTeams); + + System.out.println(theLeague.getLeagueAnnouncement(theGames)); + for (Game currGame: theGames){ + currGame.playGame(); + //break; + System.out.println(currGame.getDescription()); + } + + theLeague.showBestTeam(theTeams); + theLeague.showBestPlayers(theTeams); + + /* Practice 14-1, Step 3b. Close the try block here */ + } + /* Practice 14-1, Step 3d. Add a catch block here */ + catch(Exception e){ + e.printStackTrace(System.err); + } + } + + /* Practice 14-1, Step 6e. Modify the signature to throw PlayerDatabaseException */ + public Team[] createTeams(String teamNames, int teamSize) throws PlayerDatabaseException { + + PlayerDatabase playerDB = new PlayerDatabase(); + + StringTokenizer teamNameTokens = new StringTokenizer(teamNames, ","); + Team[] theTeams = new Team[teamNameTokens.countTokens()]; + for (int i =0; i < theTeams.length; i++){ + theTeams[i] = new Team(teamNameTokens.nextToken(), playerDB.getTeam(teamSize)); + } + + + return theTeams; + } + + public Game[] createGames(Team[] theTeams) { + int daysBetweenGames = 0; + + ArrayList theGames = new ArrayList(); + + for (Team homeTeam: theTeams){ + for (Team awayTeam: theTeams){ + if (homeTeam != awayTeam) { + daysBetweenGames += 7; + theGames.add(new Game(homeTeam, awayTeam, LocalDateTime.now().plusDays(daysBetweenGames))); + } + + } + } + + + + return (Game[]) theGames.toArray(new Game[1]); + } + + public void showBestTeam(Team[] theTeams) { + + Arrays.sort(theTeams); + Team currBestTeam = theTeams[0]; + System.out.println("\nTeam Points"); + + for (Team currTeam: theTeams){ + System.out.println(currTeam.getTeamName() + " : " + currTeam.getPointsTotal() + " : " + + currTeam.getGoalsTotal()); + + } + + System.out.println("Winner of the League is " + currBestTeam.getTeamName()); + + } + + public String getLeagueAnnouncement(Game[] theGames){ + + Period thePeriod = Period.between(theGames[0].getTheDateTime().toLocalDate(), + theGames[theGames.length - 1].getTheDateTime().toLocalDate()); + + return "The league is scheduled to run for " + + thePeriod.getMonths() + " month(s), and " + + thePeriod.getDays() + " day(s)\n"; + } + + public void showBestPlayers(Team[] theTeams){ + ArrayList thePlayers = new ArrayList(); + for (Team currTeam: theTeams){ + thePlayers.addAll(Arrays.asList(currTeam.getPlayerArray())); + } + + Collections.sort(thePlayers, (p1, p2) -> Double.valueOf(p2.getGoalsScored()).compareTo(Double.valueOf(p1.getGoalsScored()))); + + System.out.println("\n\nBest Players"); + for (Player currPlayer: thePlayers){ + System.out.println(currPlayer.getPlayerName() + " : " + currPlayer.getGoalsScored()); + } + } + +} diff --git a/src/main/java/sofflix/practices/practice_14_exceptions_1/soccer/Player.java b/src/main/java/sofflix/practices/practice_14_exceptions_1/soccer/Player.java new file mode 100644 index 0000000..0ed5255 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_14_exceptions_1.soccer; + +/** + * + * @author Administrator + */ +public class Player { + + private String playerName; + private int goalsScored; + + public void incGoalsScored() { + this.goalsScored++; + } + + public Player(String playerName) { + this.playerName = playerName; + } + + public Player() {} + + /** + * @return the playerName + */ + public String getPlayerName() { + return playerName; + } + + /** + * @param playerName the playerName to set + */ + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + + /** + * @return the goalsScored + */ + public int getGoalsScored() { + return goalsScored; + } + +} diff --git a/src/main/java/sofflix/practices/practice_14_exceptions_1/soccer/Possession.java b/src/main/java/sofflix/practices/practice_14_exceptions_1/soccer/Possession.java new file mode 100644 index 0000000..b2d8630 --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_14_exceptions_1.soccer; + +/** + * + * @author ksomervi + */ +public class Possession extends GameEvent { + + public String toString(){ + return "Possession"; + } + +} diff --git a/src/main/java/sofflix/practices/practice_14_exceptions_1/soccer/Team.java b/src/main/java/sofflix/practices/practice_14_exceptions_1/soccer/Team.java new file mode 100644 index 0000000..370ee5c --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_14_exceptions_1.soccer; + +/** + * + * @author Administrator + */ +public class Team implements Comparable { + + private String teamName; + private Player[] playerArray; + private int pointsTotal; + private int goalsTotal; + + public int compareTo(Object theTeam){ + int returnValue = -1; + if (this.getPointsTotal()< ((Team)theTeam).getPointsTotal()) { + returnValue = 1; + } else if (this.getPointsTotal() == ((Team)theTeam).getPointsTotal()){ + if (this.getGoalsTotal()< ((Team)theTeam).getGoalsTotal()) { + returnValue = 1; + } + } + return returnValue; + } + + public void incGoalsTotal(int goals){ + this.setGoalsTotal(this.getGoalsTotal() + goals); + } + + public void incPointsTotal(int points){ + this.pointsTotal += points; + } + + public Team(String teamName) { + this.teamName = teamName; + } + + public Team(String teamName, Player[] players) { + this(teamName); + this.playerArray = players; + } + + public Team() {} + + /** + * @return the teamName + */ + public String getTeamName() { + return teamName; + } + + /** + * @param teamName the teamName to set + */ + public void setTeamName(String teamName) { + this.teamName = teamName; + } + + /** + * @return the playerArray + */ + public Player[] getPlayerArray() { + return playerArray; + } + + /** + * @param playerArray the playerArray to set + */ + public void setPlayerArray(Player[] playerArray) { + this.playerArray = playerArray; + } + + /** + * @return the pointsTotal + */ + public int getPointsTotal() { + return pointsTotal; + } + + /** + * @param pointsTotal the pointsTotal to set + */ + public void setPointsTotal(int pointsTotal) { + this.pointsTotal = pointsTotal; + } + + /** + * @return the goalsTotal + */ + public int getGoalsTotal() { + return goalsTotal; + } + + /** + * @param goalsTotal the goalsTotal to set + */ + public void setGoalsTotal(int goalsTotal) { + this.goalsTotal = goalsTotal; + } + +} diff --git a/src/main/java/sofflix/practices/practice_14_exceptions_1/utility/PlayerDatabase.java b/src/main/java/sofflix/practices/practice_14_exceptions_1/utility/PlayerDatabase.java new file mode 100644 index 0000000..d8d66bb --- /dev/null +++ b/src/main/java/sofflix/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 sofflix.practices.practice_14_exceptions_1.utility; + +import java.util.*; +import sofflix.practices.practice_14_exceptions_1.soccer.Player; + + + +/** + * + * @author Administrator + */ +public class PlayerDatabase { + + private ArrayList players; + + public PlayerDatabase(){ + StringTokenizer authorTokens = new StringTokenizer(authorList, ","); + players = new ArrayList(); + while (authorTokens.hasMoreTokens()){ + players.add(new Player(authorTokens.nextToken())); + } + } + /* Practice 14-1, Step 6d. Modify the signature to throw PlayerDatabaseException */ + public Player[] getTeam(int numberOfPlayers)throws PlayerDatabaseException{ + Player[] teamPlayers = new Player[numberOfPlayers]; + for (int i = 0; i < numberOfPlayers; i++){ + int playerIndex = (int) (Math.random() * players.size()); + + /* Practice 14-1, Step 6b. Start a try block here */ + try{ + teamPlayers[i] = players.get(playerIndex); + players.remove(playerIndex); + /* Practice 14-1, Step 6b. End the try block here */ + } + /* Practice 14-1, Step 6c. Add catch block here */ + catch(IndexOutOfBoundsException ie){ + throw new PlayerDatabaseException("Not enough players in the database for the teams requested."); + } + } + return teamPlayers; + + } + + + +String authorList = +"Agatha Christie," + +"Alan Patton," + +"Alexander Solzhenitsyn," + +"Arthur Conan Doyle," + +"Anthony Trollope," + +"Baroness Orczy," + +"Brendan Behan," + +"Brian Moore," + +"Boris Pasternik," + +"Charles Dickens," + +"Charlotte Bronte," + +"Dorothy Parker," + +"Emile Zola," + +"Frank O'Connor," + +"Geoffrey Chaucer," + +"George Eliot," + +"G. K. Chesterton," + +"Graham Green," + +"Henry James," + +"James Joyce," + +"J. M. Synge," + +"J. R. Tolkien," + +"Jane Austin," + +"Leo Tolstoy," + +"Liam O'Flaherty," + +"Marcel Proust," + +"Mark Twain," + +"Oscar Wilde," + +"O. Henry," + +"Samuel Beckett," + +"Sean O'Casey," + +"William Shakespeare," + +"William Makepeace Thackeray," + +"W. B. Yeats," + +"Wilkie Collins"; + +} diff --git a/src/main/java/sofflix/practices/practice_14_exceptions_1/utility/PlayerDatabaseException.java b/src/main/java/sofflix/practices/practice_14_exceptions_1/utility/PlayerDatabaseException.java new file mode 100644 index 0000000..ea6add3 --- /dev/null +++ b/src/main/java/sofflix/practices/practice_14_exceptions_1/utility/PlayerDatabaseException.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 sofflix.practices.practice_14_exceptions_1.utility; + +/** + * + * @author Omar + */ +public class PlayerDatabaseException extends Exception{ + + public PlayerDatabaseException(){} + public PlayerDatabaseException(String message){ + super(message); + } +}