diff --git a/src/main/java/rosario/exercises/exercise_08_methods/ex08_1_exercise/Item.java b/src/main/java/rosario/exercises/exercise_08_methods/ex08_1_exercise/Item.java new file mode 100644 index 0000000..18841ad --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_08_methods/ex08_1_exercise/Item.java @@ -0,0 +1,15 @@ + +package rosario.exercises.exercise_08_methods.ex08_1_exercise; + +public class Item { + char color; + public boolean setColor(char colorCode){ + if(colorCode == ' '){ + return false; + }else{ + this.color = colorCode; + return true; + } + } + +} diff --git a/src/main/java/rosario/exercises/exercise_08_methods/ex08_1_exercise/ShoppingCart.java b/src/main/java/rosario/exercises/exercise_08_methods/ex08_1_exercise/ShoppingCart.java new file mode 100644 index 0000000..b71ff7e --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_08_methods/ex08_1_exercise/ShoppingCart.java @@ -0,0 +1,13 @@ + +package rosario.exercises.exercise_08_methods.ex08_1_exercise; + +public class ShoppingCart { + public static void main(String[] args){ + Item item1 = new Item(); + if(/*item1.setColor(' ')*/item1.setColor('U')){ + System.out.println(item1.color); + }else{ + System.out.println("Invalid color."); + } + } +} diff --git a/src/main/java/rosario/exercises/exercise_08_methods/ex08_2_exercise/Item.java b/src/main/java/rosario/exercises/exercise_08_methods/ex08_2_exercise/Item.java new file mode 100644 index 0000000..02cb678 --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_08_methods/ex08_2_exercise/Item.java @@ -0,0 +1,32 @@ + +package rosario.exercises.exercise_08_methods.ex08_2_exercise; + + +public class Item { + String desc; + int quantity; + double price; + char colorCode = 'U'; //'U' = Undetermined + + public void displayItem() { + System.out.println("Item: " + desc + ", " + quantity + ", " + + price + ", "+colorCode); + } + + public void setItemFields(String desc, int quantity, double price) { + this.desc = desc; + this.quantity = quantity; + this.price = price; + } + + public int setItemFields(String desc, int quantity, double price, char colorCode) { + if(colorCode == ' '){ + return -1; + }else{ + this.setItemFields(desc, quantity, price); + this.colorCode = colorCode; + return 1; + } + } + +} diff --git a/src/main/java/rosario/exercises/exercise_08_methods/ex08_2_exercise/ShoppingCart.java b/src/main/java/rosario/exercises/exercise_08_methods/ex08_2_exercise/ShoppingCart.java new file mode 100644 index 0000000..7204128 --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_08_methods/ex08_2_exercise/ShoppingCart.java @@ -0,0 +1,27 @@ + +package rosario.exercises.exercise_08_methods.ex08_2_exercise; + + +public class ShoppingCart { + public static void main(String[] args) { + Item item1 = new Item(); + item1.setItemFields("hola",2,50.0); + item1.displayItem(); + + // case valid + if(item1.setItemFields("adios", 3, 100.0,'A') < 0){ + System.out.println("Invalid Color"); + }else{ + item1.displayItem(); + } + + // Case invalid + //if(item1.setItemFields("adios", 3, 100.0,' ') < 0){ + //System.out.println("Invalid Color"); + //}else{ + // item1.displayItem(); + //} + + } + +} diff --git a/src/main/java/rosario/exercises/exercise_09_encapconstructors/ex09_1_exercise/ShoppingCart.java b/src/main/java/rosario/exercises/exercise_09_encapconstructors/ex09_1_exercise/ShoppingCart.java new file mode 100644 index 0000000..5e3a8e3 --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_09_encapconstructors/ex09_1_exercise/ShoppingCart.java @@ -0,0 +1,21 @@ +package rosario.exercises.exercise_09_encapconstructors.ex09_1_exercise; + +/** + * Created by rosario + */ +public class ShoppingCart { + private String name; + private String ssn; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSsn() { + return ssn; + } +} diff --git a/src/main/java/rosario/exercises/exercise_09_encapconstructors/ex09_2_exercise/Customer.java b/src/main/java/rosario/exercises/exercise_09_encapconstructors/ex09_2_exercise/Customer.java new file mode 100644 index 0000000..f23dbf7 --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_09_encapconstructors/ex09_2_exercise/Customer.java @@ -0,0 +1,25 @@ +package rosario.exercises.exercise_09_encapconstructors.ex09_2_exercise; + +/** + * Created by rosario + */ +public class Customer { + private String name; + private String ssn; + + public Customer(String name, String ssn){ + this.name = name; + this.ssn = ssn; + } + + public String getName(){ + return name; + } + public void setName(String n){ + name = n; + } + + public String getSSN(){ + return ssn; + } +} diff --git a/src/main/java/rosario/exercises/exercise_09_encapconstructors/ex09_2_exercise/ShoppingCart.java b/src/main/java/rosario/exercises/exercise_09_encapconstructors/ex09_2_exercise/ShoppingCart.java new file mode 100644 index 0000000..426c758 --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_09_encapconstructors/ex09_2_exercise/ShoppingCart.java @@ -0,0 +1,13 @@ +package rosario.exercises.exercise_09_encapconstructors.ex09_2_exercise; + +/** + * Created by rosario + */ +public class ShoppingCart { + public static void main(String args[]) { + Customer customer = new Customer("Rosario", "CUER931007MTLRSS08"); + System.out.println(customer.getName()); + System.out.println(customer.getSSN()); + + } +} diff --git a/src/main/java/rosario/exercises/exercise_10_conditions/ex10_1_exercise/TestClass.java b/src/main/java/rosario/exercises/exercise_10_conditions/ex10_1_exercise/TestClass.java new file mode 100644 index 0000000..02482ce --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_10_conditions/ex10_1_exercise/TestClass.java @@ -0,0 +1,21 @@ + +package rosario.exercises.exercise_10_conditions.ex10_1_exercise; + + +public class TestClass { + + public static void main(String args[]) { + int x = 4, y = 9; + if (y / x < 3) { + x += y; + } else { + x *= y; + } + System.out.println("After if stmt, x = " + x); + + //Operator ternary + x = y/x<3?(x += y):(x *= y); + System.out.println("Operator ternary, x = " + x); + + } +} diff --git a/src/main/java/rosario/exercises/exercise_10_conditions/ex10_2_exercise/Order.java b/src/main/java/rosario/exercises/exercise_10_conditions/ex10_2_exercise/Order.java new file mode 100644 index 0000000..cef1663 --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_10_conditions/ex10_2_exercise/Order.java @@ -0,0 +1,47 @@ + +package rosario.exercises.exercise_10_conditions.ex10_2_exercise; + +public class Order { + + static final char CORP = 'C'; + static final char PRIVATE = 'P'; + static final char NONPROFIT = 'N'; + String name; + double total; + String stateCode; + double discount; + char custType; + + public Order(String name, double total, String state, char custType) { + this.name = name; + this.total = total; + this.stateCode = state; + this.custType = custType; + calcDiscount(); + } + + public String getDiscount() { + return Double.toString(discount) + "%"; + } + + // Code the calcDiscount method. + public void calcDiscount() { + if (total > 900) { + if (NONPROFIT == custType) { + discount = 10.0; + } else if (PRIVATE == custType) { + discount = 7.0; + } + } else if (CORP == custType && total < 500) { + discount = 8.0; + } else if (total < 900) { + if (NONPROFIT == custType) { + discount = 5.0; + } else if (PRIVATE == custType) { + discount = 0.0; + } else if (CORP == custType && total > 500) { + discount = 5.0; + } + } + } +} diff --git a/src/main/java/rosario/exercises/exercise_10_conditions/ex10_2_exercise/ShoppingCart.java b/src/main/java/rosario/exercises/exercise_10_conditions/ex10_2_exercise/ShoppingCart.java new file mode 100644 index 0000000..6fdc86f --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_10_conditions/ex10_2_exercise/ShoppingCart.java @@ -0,0 +1,9 @@ + +package rosario.exercises.exercise_10_conditions.ex10_2_exercise; + +public class ShoppingCart { + public static void main(String args[]){ + Order order = new Order("Rick Wilson", 1000, "VA", Order.CORP); + System.out.println("Discount is: "+ order.getDiscount()); + } +} diff --git a/src/main/java/rosario/exercises/exercise_10_conditions/ex10_3_exercise/Order.java b/src/main/java/rosario/exercises/exercise_10_conditions/ex10_3_exercise/Order.java new file mode 100644 index 0000000..da6a7e6 --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_10_conditions/ex10_3_exercise/Order.java @@ -0,0 +1,55 @@ + +package rosario.exercises.exercise_10_conditions.ex10_3_exercise; + +public class Order { + static final char CORP = 'C'; + static final char PRIVATE = 'P'; + static final char NONPROFIT = 'N'; + String name; + double total; + String stateCode; + double discount; + char custType; + + public Order(String name, double total, String state, char custType) { + this.name = name; + this.total = total; + this.stateCode = state; + this.custType = custType; + calcDiscount(); + } + + public String getDiscount(){ + return Double.toString(discount) + "%"; + } + + public void calcDiscount() { + // Replace the if logic with a switch statement. + switch (custType){ + case NONPROFIT: + if (total > 900) { + discount = 10.00; + } else { + discount = 5.00; + } + break; + case PRIVATE: + if (total > 900) { + discount = 7.00; + } else { + discount = 0.00; + } + break; + case CORP: + if (total < 500) { + discount = 8.00; + } else { + discount = 5.00; + } + break; + default: + discount = 0.0; + break; + } + } +} diff --git a/src/main/java/rosario/exercises/exercise_10_conditions/ex10_3_exercise/ShoppingCart.java b/src/main/java/rosario/exercises/exercise_10_conditions/ex10_3_exercise/ShoppingCart.java new file mode 100644 index 0000000..acbddd5 --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_10_conditions/ex10_3_exercise/ShoppingCart.java @@ -0,0 +1,9 @@ + +package rosario.exercises.exercise_10_conditions.ex10_3_exercise; + +public class ShoppingCart { + public static void main(String args[]){ + Order order = new Order("Rick Wilson", 910.00, "VA", Order.NONPROFIT); + System.out.println("Discount is: "+ order.getDiscount()); + } +} diff --git a/src/main/java/rosario/exercises/exercise_11_arraysloopsdates/ex11_1_exercise/ShoppingCart.java b/src/main/java/rosario/exercises/exercise_11_arraysloopsdates/ex11_1_exercise/ShoppingCart.java new file mode 100644 index 0000000..6f89079 --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_11_arraysloopsdates/ex11_1_exercise/ShoppingCart.java @@ -0,0 +1,17 @@ +package rosario.exercises.exercise_11_arraysloopsdates.ex11_1_exercise; + +import java.time.*; +import java.time.format.DateTimeFormatter; + +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. + System.out.print(orderDate.format(DateTimeFormatter.ISO_LOCAL_DATE)); + + } +} \ No newline at end of file diff --git a/src/main/java/rosario/exercises/exercise_11_arraysloopsdates/ex11_2_exercise/ShoppingCart.java b/src/main/java/rosario/exercises/exercise_11_arraysloopsdates/ex11_2_exercise/ShoppingCart.java new file mode 100644 index 0000000..f0f8cff --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_11_arraysloopsdates/ex11_2_exercise/ShoppingCart.java @@ -0,0 +1,17 @@ + +package rosario.exercises.exercise_11_arraysloopsdates.ex11_2_exercise; + +public class ShoppingCart { + public static void main(String[] args) { + String name; + int age; + if(args.length <2){ + System.out.println("2 arguments are required."); + }else{ + name = args[0]; + age = Integer.parseInt(args[1]); + System.out.println("name: "+name); + System.out.println("age: "+age); + } + } +} diff --git a/src/main/java/rosario/exercises/exercise_11_arraysloopsdates/ex11_3_exercise/Item.java b/src/main/java/rosario/exercises/exercise_11_arraysloopsdates/ex11_3_exercise/Item.java new file mode 100644 index 0000000..cca332f --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_11_arraysloopsdates/ex11_3_exercise/Item.java @@ -0,0 +1,57 @@ +package rosario.exercises.exercise_11_arraysloopsdates.ex11_3_exercise; + +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/rosario/exercises/exercise_11_arraysloopsdates/ex11_3_exercise/ShoppingCart.java b/src/main/java/rosario/exercises/exercise_11_arraysloopsdates/ex11_3_exercise/ShoppingCart.java new file mode 100644 index 0000000..2ec8c80 --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_11_arraysloopsdates/ex11_3_exercise/ShoppingCart.java @@ -0,0 +1,29 @@ + +package rosario.exercises.exercise_11_arraysloopsdates.ex11_3_exercise; + +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("El total de articulos es: "+ total); + } +} + diff --git a/src/main/java/rosario/exercises/exercise_11_arraysloopsdates/ex11_4_exercise/ShoppingCart.java b/src/main/java/rosario/exercises/exercise_11_arraysloopsdates/ex11_4_exercise/ShoppingCart.java new file mode 100644 index 0000000..029f2a9 --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_11_arraysloopsdates/ex11_4_exercise/ShoppingCart.java @@ -0,0 +1,24 @@ +package rosario.exercises.exercise_11_arraysloopsdates.ex11_4_exercise; + +import java.util.ArrayList; + +public class ShoppingCart { + public static void main(String[] args){ + // Declare, instantiate, and initialize an ArrayList of Strings. Print and test your code. + ArrayList arrayList = new ArrayList<>(); + arrayList.add("Rosario"); + arrayList.add("Cruz"); + arrayList.add("Espinoza"); + System.out.println(arrayList); + // add (insert) another element at a specific index + arrayList.add(0,"My name is: "); + System.out.println(arrayList); + + // Check for the existence of a specific String element. + // If it exists, remove it. + if(arrayList.contains("Espinoza")){ + arrayList.remove("Espinoza"); + } + System.out.println(arrayList); + } +} diff --git a/src/main/java/rosario/exercises/exercise_12_inheritance/ex12_1_exercise/Item.java b/src/main/java/rosario/exercises/exercise_12_inheritance/ex12_1_exercise/Item.java new file mode 100644 index 0000000..ee96b2a --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_12_inheritance/ex12_1_exercise/Item.java @@ -0,0 +1,51 @@ +package rosario.exercises.exercise_12_inheritance.ex12_1_exercise; + +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/rosario/exercises/exercise_12_inheritance/ex12_1_exercise/Shirt.java b/src/main/java/rosario/exercises/exercise_12_inheritance/ex12_1_exercise/Shirt.java new file mode 100644 index 0000000..65b7ce3 --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_12_inheritance/ex12_1_exercise/Shirt.java @@ -0,0 +1,11 @@ +package rosario.exercises.exercise_12_inheritance.ex12_1_exercise; + +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/rosario/exercises/exercise_12_inheritance/ex12_1_exercise/ShoppingCart.java b/src/main/java/rosario/exercises/exercise_12_inheritance/ex12_1_exercise/ShoppingCart.java new file mode 100644 index 0000000..3440ed9 --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_12_inheritance/ex12_1_exercise/ShoppingCart.java @@ -0,0 +1,11 @@ +package rosario.exercises.exercise_12_inheritance.ex12_1_exercise; + +public class ShoppingCart { + public static void main(String[] args){ + + // instantiate a Shirt object and call display() on the object reference + Shirt shirt = new Shirt(150.00,'M','U'); + shirt.display(); + + } +} diff --git a/src/main/java/rosario/exercises/exercise_12_inheritance/ex12_2_exercise/Item.java b/src/main/java/rosario/exercises/exercise_12_inheritance/ex12_2_exercise/Item.java new file mode 100644 index 0000000..376e6b0 --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_12_inheritance/ex12_2_exercise/Item.java @@ -0,0 +1,54 @@ +package rosario.exercises.exercise_12_inheritance.ex12_2_exercise; + +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/rosario/exercises/exercise_12_inheritance/ex12_2_exercise/Shirt.java b/src/main/java/rosario/exercises/exercise_12_inheritance/ex12_2_exercise/Shirt.java new file mode 100644 index 0000000..24ad514 --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_12_inheritance/ex12_2_exercise/Shirt.java @@ -0,0 +1,20 @@ +package rosario.exercises.exercise_12_inheritance.ex12_2_exercise; + +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("\tSize: "+ size); + System.out.println("\tColorCode: "+ colorCode); + } +} diff --git a/src/main/java/rosario/exercises/exercise_12_inheritance/ex12_2_exercise/ShoppingCart.java b/src/main/java/rosario/exercises/exercise_12_inheritance/ex12_2_exercise/ShoppingCart.java new file mode 100644 index 0000000..3d1775d --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_12_inheritance/ex12_2_exercise/ShoppingCart.java @@ -0,0 +1,10 @@ +package rosario.exercises.exercise_12_inheritance.ex12_2_exercise; + +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/rosario/exercises/exercise_12_inheritance/ex12_3_exercise/Item.java b/src/main/java/rosario/exercises/exercise_12_inheritance/ex12_3_exercise/Item.java new file mode 100644 index 0000000..5f3efec --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_12_inheritance/ex12_3_exercise/Item.java @@ -0,0 +1,53 @@ +package rosario.exercises.exercise_12_inheritance.ex12_3_exercise; + +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/rosario/exercises/exercise_12_inheritance/ex12_3_exercise/Shirt.java b/src/main/java/rosario/exercises/exercise_12_inheritance/ex12_3_exercise/Shirt.java new file mode 100644 index 0000000..0f21b24 --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_12_inheritance/ex12_3_exercise/Shirt.java @@ -0,0 +1,42 @@ +package rosario.exercises.exercise_12_inheritance.ex12_3_exercise; + +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("\tColorCode: "+ 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(colorCode){ + case 'R': + color ="Rosa"; + break; + case 'V': + color="Vino"; + break; + case 'A': + color="Azul"; + break; + case 'B': + color="Blanco"; + break; + default: + color="No existe"; + } + return color; + } + +} diff --git a/src/main/java/rosario/exercises/exercise_12_inheritance/ex12_3_exercise/ShoppingCart.java b/src/main/java/rosario/exercises/exercise_12_inheritance/ex12_3_exercise/ShoppingCart.java new file mode 100644 index 0000000..26e30a5 --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_12_inheritance/ex12_3_exercise/ShoppingCart.java @@ -0,0 +1,18 @@ +package rosario.exercises.exercise_12_inheritance.ex12_3_exercise; + +public class ShoppingCart { + public static void main(String[] args) { + // declare and instantiate a Shirt object using an Item reference type + Shirt shirt = new Shirt(500.0, 'M', ' '); + + // call the display method on the object, then the getColor method + shirt.display(); + if(shirt instanceof Shirt){ + Shirt shirt1 = shirt; + String color = shirt1.getColor(); + System.out.println("\tColor the shirt: "+ color); + }else{ + System.out.println("\tThe object is not a Shirt"); + } + } +} diff --git a/src/main/java/rosario/exercises/exercise_13_interfaces/ex13_1_exercise/ShoppingCart.java b/src/main/java/rosario/exercises/exercise_13_interfaces/ex13_1_exercise/ShoppingCart.java new file mode 100644 index 0000000..af310d7 --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_13_interfaces/ex13_1_exercise/ShoppingCart.java @@ -0,0 +1,26 @@ +package rosario.exercises.exercise_13_interfaces.ex13_1_exercise; + + +import java.util.*; + +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/rosario/exercises/exercise_13_interfaces/ex13_2_exercise/Item.java b/src/main/java/rosario/exercises/exercise_13_interfaces/ex13_2_exercise/Item.java new file mode 100644 index 0000000..6bf1176 --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_13_interfaces/ex13_2_exercise/Item.java @@ -0,0 +1,69 @@ +package rosario.exercises.exercise_13_interfaces.ex13_2_exercise; + +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/rosario/exercises/exercise_13_interfaces/ex13_2_exercise/Shirt.java b/src/main/java/rosario/exercises/exercise_13_interfaces/ex13_2_exercise/Shirt.java new file mode 100644 index 0000000..3a40e63 --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_13_interfaces/ex13_2_exercise/Shirt.java @@ -0,0 +1,23 @@ +package rosario.exercises.exercise_13_interfaces.ex13_2_exercise; + +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/rosario/exercises/exercise_13_interfaces/ex13_2_exercise/ShoppingCart.java b/src/main/java/rosario/exercises/exercise_13_interfaces/ex13_2_exercise/ShoppingCart.java new file mode 100644 index 0000000..2920ea8 --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_13_interfaces/ex13_2_exercise/ShoppingCart.java @@ -0,0 +1,40 @@ +package rosario.exercises.exercise_13_interfaces.ex13_2_exercise; + +import java.util.ArrayList; +import java.util.function.Predicate; + +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/rosario/exercises/exercise_13_interfaces/ex13_2_exercise/Trousers.java b/src/main/java/rosario/exercises/exercise_13_interfaces/ex13_2_exercise/Trousers.java new file mode 100644 index 0000000..82fd02f --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_13_interfaces/ex13_2_exercise/Trousers.java @@ -0,0 +1,45 @@ +package rosario.exercises.exercise_13_interfaces.ex13_2_exercise; + +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/rosario/exercises/exercise_14_exceptions/ex14_1_exercise/Calculator.java b/src/main/java/rosario/exercises/exercise_14_exceptions/ex14_1_exercise/Calculator.java new file mode 100644 index 0000000..68d9fff --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_14_exceptions/ex14_1_exercise/Calculator.java @@ -0,0 +1,11 @@ +package rosario.exercises.exercise_14_exceptions.ex14_1_exercise; + +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){ + return x / y; + } +} diff --git a/src/main/java/rosario/exercises/exercise_14_exceptions/ex14_1_exercise/ShoppingCart.java b/src/main/java/rosario/exercises/exercise_14_exceptions/ex14_1_exercise/ShoppingCart.java new file mode 100644 index 0000000..c4973ed --- /dev/null +++ b/src/main/java/rosario/exercises/exercise_14_exceptions/ex14_1_exercise/ShoppingCart.java @@ -0,0 +1,18 @@ +package rosario.exercises.exercise_14_exceptions.ex14_1_exercise; + +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, 5); + System.out.println("Division Result: " + divResult); + }catch(ArithmeticException e){ + e.printStackTrace(System.err); + } + } +}