Skip to content

Commit

Permalink
Repository update with Object related code
Browse files Browse the repository at this point in the history
  • Loading branch information
kon3ktor committed Jan 25, 2021
1 parent 3e30f09 commit 681f2aa
Show file tree
Hide file tree
Showing 20 changed files with 745 additions and 9 deletions.
5 changes: 1 addition & 4 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions src/com/advanced/collections/arraylist/Product.java
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 + '\'' +
'}';
}
}
56 changes: 56 additions & 0 deletions src/com/advanced/collections/arraylist/ToDoListApp.java
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);
}
}
}
32 changes: 32 additions & 0 deletions src/com/advanced/collections/arraylist/WebShopApp.java
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.");

}
}
10 changes: 5 additions & 5 deletions src/com/advanced/debugging/RockPaperScissorsApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public static void main(String[] args) {
System.out.print("Your move: ");
String userMove = inputScanner.nextLine().toLowerCase();

if (userMove.equals("quit")){
if (userMove.equals("quit")) {
break;
}

if (isValidInput(userMove)){
if (isValidInput(userMove)) {
String opponentMove = generateOpponentMove();
System.out.println("Your opponent's move: " + opponentMove);
whoWon(userMove, opponentMove);
Expand All @@ -31,13 +31,13 @@ public static void main(String[] args) {
}
}

private static boolean isValidInput(String userInput){
private static boolean isValidInput(String userInput) {
return userInput.equals("rock") || userInput.equals("scissors") || userInput.equals("paper");
}

private static String generateOpponentMove(){
private static String generateOpponentMove() {
Random randomNumberGenerator = new Random();
switch (randomNumberGenerator.nextInt(13)){
switch (randomNumberGenerator.nextInt(3)){
case 0:
default:
return "rock";
Expand Down
46 changes: 46 additions & 0 deletions src/com/advanced/objectclone/Pizza.java
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();
}

}
25 changes: 25 additions & 0 deletions src/com/advanced/objectclone/PizzaApp.java
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());
}

}
15 changes: 15 additions & 0 deletions src/com/advanced/objectclone/PizzaSize.java
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;
}
}
61 changes: 61 additions & 0 deletions src/com/advanced/objectequals/Pizza.java
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;

}
}
29 changes: 29 additions & 0 deletions src/com/advanced/objectequals/PizzaApp.java
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));

}

}
15 changes: 15 additions & 0 deletions src/com/advanced/objectequals/PizzaSize.java
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;
}
}
Loading

0 comments on commit 681f2aa

Please sign in to comment.