This file contains hidden or 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
| public class Main { | |
| public static void main(String[] args) { | |
| Car car = CarFactory.getCar("to work"); | |
| car.drive(); | |
| System.out.println("-------"); | |
| car = CarFactory.getCar("to city"); | |
| car.drive(); | |
| } |
This file contains hidden or 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
| import java.util.ArrayList; | |
| import java.util.Arrays; | |
| public class Main { | |
| public static void main(String[] args) { | |
| PizzaBuilder pizzaBuilder = new PizzaBuilder(); | |
| // use method chaining to build properties | |
| pizzaBuilder.setName("Veggie").setPrice(15.99).setToppings(new ArrayList<>(Arrays.asList("broccoli", "squash", "tomatoes"))); | |
| pizzaBuilder.setSize(11).setTips(4.00); |
This file contains hidden or 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
| import java.util.ArrayList; | |
| import java.util.Arrays; | |
| public class Main { | |
| public static void main(String[] args) { | |
| Pizza pizza = new Pizza("Veggie", 15.99, new ArrayList<>(Arrays.asList("broccoli", "squash", "tomatoes"))); | |
| // after creating an object, there is no way to change created object | |
| System.out.println(pizza.getName()); | |
| System.out.println(pizza.getPrice()); | |
| System.out.println(pizza.getToppings()); |
This file contains hidden or 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
| public class Main { | |
| public static void main(String[] args) { | |
| // Balance obj = new Balance(); doesn't compile - constructor is private | |
| Balance husband = Balance.getInstance(); | |
| System.out.println("Available balance: " + husband.getBalanceAvailable()); | |
| husband.spend(250.00); // husband goes for shopping | |
| System.out.println("Available balance: " + husband.getBalanceAvailable()); | |
| System.out.println("---------"); |
This file contains hidden or 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
| public class Main { | |
| public static void main(String[] args) { | |
| // Balance obj = new Balance(); doesn't compile - constructor is private | |
| Balance husband = Balance.getInstance(); | |
| System.out.println("Available balance: " + husband.getBalanceAvailable()); | |
| husband.spend(250.00); // husband goes for shopping | |
| System.out.println("Available balance: " + husband.getBalanceAvailable()); | |
| System.out.println("---------"); |
This file contains hidden or 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
| import java.util.ArrayList; | |
| import java.util.Arrays; | |
| import java.util.List; | |
| public class Main { | |
| public static void main(String[] args) { | |
| List<Integer> list = new ArrayList<>(Arrays.asList(2, 1, 3, 4, 8, 7, 6, 5, 9, 10)); | |
| // forEach method accepts Consumer functional interface | |
| // forEach(Consumer<? super T> action) |
This file contains hidden or 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
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.function.Predicate; | |
| public class Main { | |
| public static void main(String[] args) { | |
| List<Car> cars = new ArrayList<>(); | |
| cars.add(new Car("Tesla", "white", 2019)); | |
| cars.add(new Car("Tesla", "black", 2021)); | |
| cars.add(new Car("BMW", "white", 2018)); |
This file contains hidden or 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
| import java.util.ArrayList; | |
| import java.util.List; | |
| public class Main { | |
| public static void main(String[] args) { | |
| List<Car> cars = new ArrayList<>(); | |
| cars.add(new Car("Tesla", "white", 2019)); | |
| cars.add(new Car("Tesla", "black", 2021)); | |
| cars.add(new Car("BMW", "white", 2018)); | |
| cars.add(new Car("BMW", "red", 2017)); |
This file contains hidden or 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
| import java.util.ArrayList; | |
| import java.util.List; | |
| public class Main { | |
| public static void main(String[] args) { | |
| List<Car> cars = new ArrayList<>(); | |
| cars.add(new Car("Tesla", "white", 2019)); | |
| cars.add(new Car("Tesla", "black", 2021)); | |
| cars.add(new Car("BMW", "white", 2018)); | |
| cars.add(new Car("BMW", "red", 2017)); |
This file contains hidden or 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
| import java.util.Map; | |
| import java.util.TreeMap; | |
| public class Main { | |
| public static void main(String[] args) { | |
| Map<Integer, Double> items = new TreeMap<>(); | |
| items.put(1, 1.05); | |
| items.put(2, 0.50); | |
| items.put(3, 0.76); | |
| items.put(4, 1.47); |