-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Repository update with Object related code
- Loading branch information
Showing
20 changed files
with
745 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.advanced.collections.arraylist; | ||
|
||
import java.util.Objects; | ||
|
||
public class Product { | ||
|
||
private String name; | ||
private double price; | ||
private String description; | ||
|
||
public Product(String name, double price, String description) { | ||
this.name = name; | ||
this.price = price; | ||
this.description = description; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public double getPrice() { | ||
return price; | ||
} | ||
|
||
public void setPrice(double price) { | ||
this.price = price; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Product product = (Product) o; | ||
return Double.compare(product.price, price) == 0 && Objects.equals(name, product.name) && Objects.equals(description, product.description); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(name, price, description); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Product{" + | ||
"name='" + name + '\'' + | ||
", price=" + price + | ||
", description='" + description + '\'' + | ||
'}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.advanced.collections.arraylist; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
|
||
public class ToDoListApp { | ||
|
||
public static void main(String[] args) { | ||
|
||
ArrayList<String> urgentTasks = new ArrayList<>(); | ||
ArrayList<String> allTasks = new ArrayList<>(); | ||
|
||
|
||
urgentTasks.add("Repairing the car"); | ||
urgentTasks.add("Paying the checks"); | ||
|
||
allTasks.add("Cleaning the bathroom"); | ||
allTasks.add("Mowing the lawn"); | ||
allTasks.add("Going to the grocery store"); | ||
|
||
Collections.swap(urgentTasks, 0, 1); | ||
//urgentTasks.clear(); | ||
if (!urgentTasks.isEmpty()) { | ||
System.out.println("Urgent tasks:"); | ||
for (String task : urgentTasks) { | ||
System.out.println("- " + task); | ||
} | ||
} | ||
|
||
allTasks.addAll(urgentTasks); | ||
Collections.sort(allTasks, Collections.reverseOrder()); | ||
|
||
System.out.println("All tasks:"); | ||
for (String task : allTasks) { | ||
System.out.println("- " + task); | ||
} | ||
|
||
System.out.println("--------------------------------------"); | ||
if (allTasks.contains("Cleaning the bathroom")){ | ||
System.out.println("I haven't cleaned the bathroom"); | ||
} | ||
|
||
System.out.println("--------------------------------------"); | ||
System.out.println("The first 3 most important tasks:"); | ||
for (String task : allTasks.subList(0, 3)) { | ||
System.out.println("- " + task); | ||
} | ||
|
||
Collections.shuffle(allTasks); | ||
System.out.println("--------------------------------------"); | ||
System.out.println("Shuffled tasks:"); | ||
for (String task : allTasks) { | ||
System.out.println("- " + task); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.advanced.collections.arraylist; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class WebShopApp { | ||
|
||
public static void main(String[] args) { | ||
ArrayList<Product> products = new ArrayList<>(); | ||
|
||
products.add(new Product("Fidget spinner", 3.99, "It's fun.")); | ||
products.add(new Product("Earbud", 39.99, "Extremely clear sound. Waterproof.")); | ||
products.add(new Product("Earbud", 39.99, "Extremely clear sound. Waterproof.")); | ||
|
||
products.add(1, new Product("36 AAA batteries", 10.99, "High-performance alkaline batteries.")); | ||
|
||
products.remove(2); | ||
//products.remove(new Product("Earbud", 39.99, "Extremely clear sound. Waterproof.")); | ||
|
||
products.set(0, new Product("Rubber duck", 5.99, "You can't have bath without this.")); | ||
|
||
//System.out.println(products); | ||
for (Product product : products) { | ||
System.out.println(product); | ||
} | ||
|
||
System.out.println("The third element in the list:"); | ||
System.out.println(products.get(2)); | ||
|
||
System.out.println(products.size() + " products can be found in the list."); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.advanced.objectclone; | ||
|
||
import java.util.Objects; | ||
|
||
public class Pizza implements Cloneable{ | ||
|
||
private String name; | ||
private PizzaSize pizzaSize; | ||
private double price; | ||
|
||
public Pizza(String name, PizzaSize pizzaSize) { | ||
this.name = name; | ||
this.pizzaSize = pizzaSize; | ||
this.price = calculatePrice(); | ||
} | ||
|
||
private double calculatePrice() { | ||
switch (pizzaSize) { | ||
case SMALL: | ||
default: | ||
return 5.99; | ||
case MEDIUM: | ||
return 7.99; | ||
case LARGE: | ||
return 10.99; | ||
} | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public PizzaSize getPizzaSize() { | ||
return pizzaSize; | ||
} | ||
|
||
public double getPrice() { | ||
return price; | ||
} | ||
|
||
@Override | ||
protected Object clone() throws CloneNotSupportedException { | ||
return super.clone(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.advanced.objectclone; | ||
|
||
public class PizzaApp { | ||
|
||
public static void main(String[] args) throws CloneNotSupportedException { | ||
System.out.println("Available sizes:"); | ||
for (PizzaSize pizzaSize : PizzaSize.values()) { | ||
System.out.println("- " + pizzaSize.getPizzaSizeText()); | ||
} | ||
System.out.println(); | ||
|
||
Pizza pizzaOrder = new Pizza("Margareta", PizzaSize.MEDIUM); | ||
System.out.println("I ordered the following pizza:"); | ||
System.out.println("Name: " + pizzaOrder.getName()); | ||
System.out.println("Size: " + pizzaOrder.getPizzaSize().getPizzaSizeText()); | ||
System.out.println("Price: $" + pizzaOrder.getPrice()); | ||
|
||
Pizza samePizza = (Pizza) pizzaOrder.clone(); | ||
System.out.println("I ordered another pizza:"); | ||
System.out.println("Name: " + samePizza.getName()); | ||
System.out.println("Size: " + samePizza.getPizzaSize().getPizzaSizeText()); | ||
System.out.println("Price: $" + samePizza.getPrice()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.advanced.objectclone; | ||
|
||
public enum PizzaSize { | ||
SMALL("Small size"), MEDIUM("Medium size"), LARGE("Large size"); | ||
|
||
private String pizzaSizeText; | ||
|
||
PizzaSize(String pizzaSizeText) { | ||
this.pizzaSizeText = pizzaSizeText; | ||
} | ||
|
||
public String getPizzaSizeText() { | ||
return pizzaSizeText; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.advanced.objectequals; | ||
|
||
import java.util.Arrays; | ||
import java.util.Objects; | ||
|
||
public class Pizza implements Cloneable{ | ||
|
||
private String name; | ||
private PizzaSize pizzaSize; | ||
private double price; | ||
|
||
public Pizza(String name, PizzaSize pizzaSize) { | ||
this.name = name; | ||
this.pizzaSize = pizzaSize; | ||
this.price = calculatePrice(); | ||
} | ||
|
||
private double calculatePrice() { | ||
switch (pizzaSize) { | ||
case SMALL: | ||
default: | ||
return 5.99; | ||
case MEDIUM: | ||
return 7.99; | ||
case LARGE: | ||
return 10.99; | ||
} | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public PizzaSize getPizzaSize() { | ||
return pizzaSize; | ||
} | ||
|
||
public double getPrice() { | ||
return price; | ||
} | ||
|
||
@Override | ||
protected Object clone() throws CloneNotSupportedException { | ||
return super.clone(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (!(o instanceof Pizza)) { | ||
return false; | ||
} | ||
Pizza pizza = (Pizza) o; | ||
return Objects.equals(pizza.name, name) && | ||
price == pizza.price && | ||
pizzaSize == pizza.pizzaSize; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.advanced.objectequals; | ||
|
||
public class PizzaApp { | ||
|
||
public static void main(String[] args) throws CloneNotSupportedException, NoSuchMethodException { | ||
System.out.println("Available sizes:"); | ||
for (PizzaSize pizzaSize : PizzaSize.values()) { | ||
System.out.println("- " + pizzaSize.getPizzaSizeText()); | ||
} | ||
System.out.println(); | ||
|
||
Pizza pizzaOrder = new Pizza("Margareta", PizzaSize.MEDIUM); | ||
System.out.println("I ordered the following pizza:"); | ||
System.out.println("Name: " + pizzaOrder.getName()); | ||
System.out.println("Size: " + pizzaOrder.getPizzaSize().getPizzaSizeText()); | ||
System.out.println("Price: $" + pizzaOrder.getPrice()); | ||
|
||
Pizza samePizza = (Pizza) pizzaOrder.clone(); | ||
System.out.println("I ordered another pizza:"); | ||
System.out.println("Name: " + samePizza.getName()); | ||
System.out.println("Size: " + samePizza.getPizzaSize().getPizzaSizeText()); | ||
System.out.println("Price: $" + samePizza.getPrice()); | ||
|
||
Pizza anotherPizza = new Pizza("Margareta", PizzaSize.LARGE); | ||
System.out.println(pizzaOrder.equals(anotherPizza)); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.advanced.objectequals; | ||
|
||
public enum PizzaSize { | ||
SMALL("Small size"), MEDIUM("Medium size"), LARGE("Large size"); | ||
|
||
private String pizzaSizeText; | ||
|
||
PizzaSize(String pizzaSizeText) { | ||
this.pizzaSizeText = pizzaSizeText; | ||
} | ||
|
||
public String getPizzaSizeText() { | ||
return pizzaSizeText; | ||
} | ||
} |
Oops, something went wrong.