package com.sepoe.wbitdd.pos;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Optional;
import java.util.function.Consumer;

/**
 * Class to handle input from command line and output to commandline.
 * It is just meant as POC.
 *
 * @author lumiha
 * @since 08/05/15.
 */
public class PointOfSaleApp {


    public static void main(String[] args) {

        System.out.println("Started PointOfSaleApp");
        final PointOfSaleApp app = new PointOfSaleApp();

        final PointOfSale pointOfSale = new PointOfSale(app.itemRepository, app.outputDevice);

        queryForInput(pointOfSale::onBarcode);
    }

    // the real application would provide the price data here.
    final ItemRepository itemRepository = new ItemRepository() {
        private HashMap<String, Double> prices = new HashMap<>();

        {
            prices.put("12345", 42.2);
            prices.put("123456", 142.2);
            prices.put("1234567", 2142.2);
        }

        @Override
        public Optional<Double> lookupPrice(final String barcode) {
            return Optional.ofNullable(prices.get(barcode));
        }
    };


    // the real application would route this to the display here.
    final OutputDevice outputDevice = new DefaultOutputDevice() {

        protected void write(final String message) {
            System.out.println(message);
        }
    };

    private static void queryForInput(final Consumer<String> inputHandler) {
        try (final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in))) {

            String barcode;
            System.out.print("Enter 'quit' or barcode: ");
            barcode = bufferedReader.readLine();

            while (!barcode.equals("quit")) {
                inputHandler.accept(barcode);

                System.out.print("Enter 'quit' or barcode: ");

                barcode = bufferedReader.readLine();
            }

            System.out.println("Ended PointOfSaleApp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

@@ -59,36 +59,8 @@
 er;%0A
-import java.io.IOException;%0A
 impo
@@ -656,16 +656,38 @@
 nBarcode
+, pointOfSale::onTotal
 );%0A    %7D
@@ -1511,16 +1511,39 @@
 tHandler
+, Callback totalHandler
 ) %7B%0A    
@@ -1850,36 +1850,172 @@
    i
-nputHandler.accept(barcode);
+f (!barcode.equals(%22total%22)) %7B%0A                    inputHandler.accept(barcode);%0A                %7D else %7B%0A                    totalHandler.call();%0A                %7D
 %0A%0A  
@@ -2221,10 +2221,8 @@
 ch (
-IO
 Exce
@@ -2280,11 +2280,70 @@
  %7D%0A    %7D
+%0A%0A    public interface Callback%7B%0A        void call();%0A    %7D
 %0A%7D%0A

call total in POC