package innerclasses;

public class Product {

    private String name;

    private Double price;

     

    public Product(String name, Double price) {

        this.name = name;

        this.price = price;

    }

 

    public String getName() {

        return name;

    }

    public Double getPrice() {

        return price;

    }

    @Override

    public int hashCode() {

        final int prime = 31;

        int result = 1;

         result = prime * result + ((name == null) ? 0 :

            name.hashCode());

         result = prime * result + ((price == null) ? 0 :

             price.hashCode());

         return result;

    }

    @Override

    public boolean equals(Object obj) {

        if (this == obj)

            return true;

        if (obj == null)

            return false;

        if (getClass() != obj.getClass())

            return false;

        Product other = (Product) obj;

        if (name == null) {

            if (other.name != null)

                return false;

        } else if (!name.equals(other.name))

            return false;

        if (price == null) {

            if (other.price != null)

                return false;

        } else if (!price.equals(other.price))

            return false;

        return true;

    }

}

 

 

 

 

 

package innerclasses;

 

import java.util.ArrayList;

import java.util.List;

 

public class ShoppingCart {

    private List<Item> items = new ArrayList<>();

    public ShoppingCart() {}

     

    public void addProduct(Product p) {

        boolean found = false;

        for (Item item : items) {

            if (item.matches(p)) {

                item.increaseTotal();

                found = true;

            }

        }

        if (!found) {

            items.add(new Item(p));

        }

    }

    public void removeProduct(Product p) {

        for (Item item : items) {

            if (item.matches(p)) {

                item.decreaseTotal();

            }

        }

    }

    public void printCart() {

        double total = 0;

        for (Item item : items) {

            total += item.getTotalPrice();

            System.out.println(item.getDisplay());

        }

        System.out.printf("The total is : $%f\n\n", total);

    }

    private class Item {

        private Product product;

        private Integer qty;

           

        Item(Product product) {

            this.product = product;

            this.qty = 1;

        }

        void increaseTotal() {

            qty += 1;

        }  

        void decreaseTotal() {

            qty = Math.max(0, qty-1);

        }

        boolean matches(Product p) {

            return this.product.equals(p);

        }

        Double getTotalPrice() {

            return qty*product.getPrice();

        }

        String getDisplay() {

            if (qty == 0) {

                return product.getName() + " has been removed";

            } else {

                return qty + " x " + product.getName() +

                   " ($" + (product.getPrice() * qty) + ")";

            }

        }              

    }

}

 

 

 

 

 

package innerclasses;

public class Main {

    public static void main(String[] args) {

        Product i1 = new Product("The Sun Also Rises", 9.99);

        Product i2 = new Product("The Old Man and the Sea", 7.99);

        Product i3 = new Product("For Whom the Bell Tolls", 7.99);

        ShoppingCart cart = new ShoppingCart();

        cart.addProduct(i1);

        cart.addProduct(i2);

        cart.addProduct(i2);

        cart.addProduct(i3);

        cart.printCart();

        cart.removeProduct(i3);

        cart.printCart();

    }

}

 

 

 

 

 

package innerclasses;

 

public class StaticInner {

    public StaticInner() {}

     

    public static class Helper {

        public void printHello() {

             System.out.println("Hello");

        }  

    }

}

 

 

 

 

 

package innerclasses;

public class Main2 {

    public static void main(String[] args) {

        StaticInner.Helper helper = new StaticInner.Helper();

        helper.printHello();

    }

}

 

 

 

 

 

public void printCart() {

    class PrintAdaptor {

        int count = 1;

        double total = 0;

        PrintAdaptor() {}                

        void processItem(Item item) {

            System.out.println("Item "+ (count++));

            System.out.println(item.getDisplay());

            System.out.println("------------------------");

            total += item.getTotalPrice();

        }

        double getTotal() {

            return total;

        }

    }

    PrintAdaptor pa = new PrintAdaptor();

    for (Item item : items) {

        pa.processItem(item);

    }

    System.out.printf("The total is : $%f\n\n", pa.getTotal());

}

 

 

 

 

 

package localclasses;

 

import java.util.ArrayList;

import java.util.List;

 

public class Collections2 {

    public static <T> List<T> filter(

           List<T> input, Filter<T> filter) {

        List<T> result = new ArrayList<T>();

        for (T t : input) {

            if (filter.filter(t)) {

                result.add(t);

            }

        }

        return result;

    }

}

 

 

 

 

 

package localclasses;

import java.util.List;

import java.util.Arrays;

public class Main {

    public static void main(String[] args) {

        List<Integer> numbers = Arrays.asList(

                          1,5,4,2,7,9,6,4,2,6,5,8);

        List result = Collections2.filter(numbers,

            new Filter<Integer>() {

                @Override

                public boolean filter(Integer t) {

                    return t%2 == 1;

                }            

            });

        System.out.println(result);

    }

}