package shapes;

public class Square {

    private final int sides = 4;

    private String fillColor;

    private String lineColor;

     

    public Square() {}

     

    public void draw() {

        System.out.println("I am drawing a square");

        System.out.printf("I have %d sides", sides); 

    }

    public int getSides() {

        return sides;

    }

    public String getFillColor() {

        return fillColor;

    }

    public void setFillColor(String fillColor) {

        this.fillColor = fillColor;

    }

    public String getLineColor() {

        return lineColor;

    }

    public void setLineColor(String lineColor) {

        this.lineColor = lineColor;

    }

}

 

package shapes;

public class Triangle {

    private final int sides = 3;

    private String fillColor;

    private String lineColor;

     

    public Triangle() {}

     

    public void draw() {

        System.out.println("I am drawing a triangle");

        System.out.printf("I have %d sides", sides); 

    }

    public int getSides() {

        return sides;

    }

    public String getFillColor() {

        return fillColor;

    }

    public void setFillColor(String fillColor) {

        this.fillColor = fillColor;

    }

    public String getLineColor() {

        return lineColor;

    }

    public void setLineColor(String lineColor) {

        this.lineColor = lineColor;

    }

}

 

package shapes;

public class Octagon {

    private final int sides = 8;

    private String fillColor;

    private String lineColor;

     

    public Octagon() {}

     

    public void draw() {

        System.out.println("I am drawing a octagon");

        System.out.printf("I have %d sides", sides); 

    }

    public int getSides() {

        return sides;

    }

    public String getFillColor() {

        return fillColor;

    }

    public void setFillColor(String fillColor) {

        this.fillColor = fillColor;

    }

    public String getLineColor() {

        return lineColor;

    }

    public void setLineColor(String lineColor) {

        this.lineColor = lineColor;

    }

}

 

 

 

 

 

package shapes;

public class Shape {

    private String fillColor;

    private String lineColor;

 

    public Shape() {}

     

    public String getFillColor() {

        return fillColor;

    }

    public void setFillColor(String fillColor) {

        this.fillColor = fillColor;

    }

    public String getLineColor() {

        return lineColor;

    }

    public void setLineColor(String lineColor) {

        this.lineColor = lineColor;

    }

}

 

 

 

 

 

package shapes;

public class Square extends Shape {

    private final int sides = 4;

     

    public Square() {}

     

    public void draw() {

        System.out.println("I am drawing a square");

        System.out.printf("I have %d sides\n", sides);     

        System.out.printf("My fill color is %s\n", getFillColor());

        System.out.printf("My line color is %s\n", getLineColor());

    }

    public int getSides() {

        return sides;

    }

}

 

 

 

 

 

package shapes;

public class Main {

    public static void main(String[] args) {

        Square s = new Square();

        s.setFillColor("Blue");

        s.setLineColor("Red");

        s.draw();

    }

}

 

 

 

 

 

package shapes;

public abstract class Shape {

    private String fillColor;

    private String lineColor;

 

    public Shape() {}

     

    public abstract int getSides();

     

    public abstract String getType();

     

    public void draw() {

        System.out.printf("I am drawing a %s\n", getType());

         System.out.printf("I have %d sides\n", getSides());     

         System.out.printf("My fill color is %s\n", getFillColor());

         System.out.printf("My line color is %s\n", getLineColor());

    }

    protected String getFillColor() {

        return fillColor;

    }

    public void setFillColor(String fillColor) {

        this.fillColor = fillColor;

    }

    protected String getLineColor() {

        return lineColor;

    }

    public void setLineColor(String lineColor) {

        this.lineColor = lineColor;

    }

}

 

 

 

 

 

package shapes;

 

public final class Square extends Shape {

    private final int sides = 4;

   

    public Square() {}

    @Override

    public int getSides() {

        return sides;

    }

    @Override

    public String getType() {

        return "Square";

    }

}

 

 

 

 

package shapes;

 

public class Main {

 

    public static void main(String[] args) {

        Square s = new Square();

         s.setFillColor("Blue");

         s.setLineColor("Red");

         s.draw();

         Triangle t = new Triangle();

         t.setFillColor("Green");

         t.setLineColor("Orange");

         t.draw();

    }

}

 

 

 

 

 

package shapes;

public class Main {

 

    public static void main(String[] args) {

        Square s = new Square();

        assignFillColor(s);

        s.setLineColor("Red");

        s.draw();

        Triangle t = new Triangle();

        assignFillColor(t);

        t.setLineColor("Orange");

        t.draw();

    }

     

    private static String[] colors = {"Red", "Blue",

        "Green", "Black", "Orange", "Pink"};

 

    private static void assignFillColor(Shape shape) {

        int num = (int)(Math.random() * colors.length);

        shape.setFillColor(colors[num]);

    }

}

 

 

 

 

 

package shapes;

 

public class ChangingShape {

 

    public static void main(String[] args) {

        Shape s = null;

        s = new Square();

        s.setLineColor("Red");

        s.setFillColor("Red");

        s.draw();

        s = new Triangle();

        s.setLineColor("Orange");

        s.draw();

    }

}

 

 

 

 

 

package shapes;

public class Main2 {

    public static void main(String[] args) {

        Object o1 = "Hello world";

        receiveObject(o1);

        o1 = new Square();

        ((Shape)o1).setFillColor("Red");

        ((Shape)o1).setLineColor("Blue");

        receiveObject(o1);

    }

    public static void receiveObject(Object o) {

        if (o instanceof Shape) {

            Shape s = (Shape)o;

            s.draw();

         } else {

             System.out.println(o);

         }

    }

}

 

 

 

 

 

package shapes;

public class Main3 {

 

    public static void main(String[] args) {

        Object o1 = "Hello world";

        ((Shape)o1).setFillColor("Red");

    }

}

 

 

 

 

 

package shapes;

 

public class Text implements Drawable {

    public String text;

    public String color;

    public Text(String text, String color) {

        this.text = text;

        this.color = color;

    }

    @Override

    public void draw() {

        System.out.printf("I am drawing a %s text string with the value %s\n", color, text);

    }

}

 

 

 

 

 

package shapes;

public class Text implements Drawable, Colorable {

    public String text;

    public String color;

    public Text(String text, String color) {

        this.text = text;

        this.color = color;

    }

 

    @Override

    public void draw() {

        System.out.printf("I am drawing a %s text string with the value %s\n", color, text);

    }

 

    @Override

    public String getColor() {

        return color;

    }

}

 

 

 

 

 

package shapes;

public class UsingInterfaces {

    public static void main(String[] args) {

        Shape s = new Square();

        receiveDrawable(s);

        receiveColorable(s);

        Text t = new Text("Hello World", "Red");

        receiveDrawable(t);

        receiveColorable(t);

    }

     

    public static void receiveDrawable(Drawable d) {

        System.out.printf("I have been passed a Drawable: it is really a %s\n", d.getClass().getSimpleName());

        d.draw();

    }

 

    public static void receiveColorable(Colorable d) {

        System.out.printf("I have been passed a Colorable that is %s: it is really a %s\n", d.getColor(), d.getClass().getSimpleName());

    }

}

 

 

 

 

 

package template;

 

public class ExchangeRate {

    private String fromCurrency;

    private String toCurrency;

    private Double rate;

     

    public ExchangeRate(String fromCurrency, String toCurrency, Double rate) {

        this.fromCurrency = fromCurrency;

        this.toCurrency = toCurrency;

        this.rate = rate;

    }

 

    public String getFromCurrency() {

        return fromCurrency;

    }

    public String getToCurrency() {

        return toCurrency;

    }

    public Double getRate() {

        return rate;

    }

}

 

 

 

 

 

 

package template;

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

 

public abstract class FileProcessor {

    private String file;

    private int lineNumber = 0;

    BufferedReader br = null;

    public FileProcessor(String file) {

        this.file = file;

    }

     

    public final void process() {

        try {

            openFile();

            processLines();

            closeFile();

        } catch (IOException e ) {

            e.printStackTrace(System.out);

        }

    }

 

    protected void openFile() throws FileNotFoundException {

        br = new BufferedReader(new FileReader(file));

    }

 

    private void processLines() throws IOException {

        String line;

        while ((line = br.readLine()) != null) {

            ExchangeRate result = processLine(lineNumber, line);

            if (result != null) {

                printLine(result);

            }

            lineNumber++;

        }

    }

 

    protected void closeFile() throws IOException {

        br.close();

    }

 

    protected abstract ExchangeRate processLine

        (int lineNumber, String line);

     

    protected void printLine(ExchangeRate rate) {

        System.out.printf("The exchange rate from %s to %s is %f, you will get $%f for each $100\n",

             rate.getFromCurrency(), rate.getToCurrency(),

             rate.getRate(), rate.getRate()*100);                

    }

}

 

 

 

 

 

package template;

import java.util.StringTokenizer;

 

public class CSVFileProcessor extends FileProcessor {

 

    public CSVFileProcessor(String file) {

        super(file);

    }

 

    @Override

    protected ExchangeRate processLine(int lineNumber, String line) {

        StringTokenizer st = new StringTokenizer(line, ",");

        int i = 0;

        String fromCurrency = null;

        String toCurrency = null;

        Double rate = null;

        while (st.hasMoreTokens()) {

           String s = st.nextToken();

           s = s.replaceAll("\"", "");

           switch (i) {

               case 0 :

                   fromCurrency = s;

                   break;

               case 1 :

                   toCurrency = s;

                   break;

               case 2 :

                   rate = Double.valueOf(s);

                   break;

            }

            i++;

        }

        return new ExchangeRate(fromCurrency, toCurrency, rate);

    }

}

 

 

 

 

 

package template;

public class Main {

    public static void main(String[] args) {

        CSVFileProcessor csv1 = new CSVFileProcessor("file1.csv");

        csv1.process();

    }

}

 

 

 

 

 

package template;

import java.util.StringTokenizer;

public class PipeFileProcessor extends FileProcessor {

    public PipeFileProcessor(String file) {

        super(file);

    }

 

    @Override

    protected ExchangeRate processLine(int lineNumber, String line) {

        if (lineNumber == 0) {

            return null;

        }  

        String fromCurrency = null;

        String toCurrency = null;

        Double rate = null;

        String[] tokens = line.split("\\|");

        for (int i = 0; i < tokens.length; i++) {

            String s = tokens[i];

            s = s.replaceAll("\"", "");

                switch (i) {

                    case 0 :

                         fromCurrency = s;

                         break;

                    case 1 :

                        toCurrency = s;

                        break;

                    case 2 :

                        rate = Double.valueOf(s);

                        break;

            }

            i++;

        }

        return new ExchangeRate(fromCurrency, toCurrency, rate);

    }

}

 

 

 

 

 

package template;

public class Main {

 

    public static void main(String[] args) {

        FileProcessor csv1 = new CSVFileProcessor("file1.csv");

        csv1.process();

        csv1 = new PipeFileProcessor("file2.csv");

        csv1.process();

    }

}

 

 

 

 

 

package abstractfactory;

public interface TextField {

}

 

class WindowsTextField implements TextField {

}

 

class OSXTextField implements TextField {

}

 

 

 

 

 

package abstractfactory;

public interface Label {

}

 

class WindowsLabel implements Label {

}

 

class OSXLabel implements Label {

}

 

 

 

 

 

package abstractfactory;

public interface ComboBox {

}

 

class WindowsComboBox implements ComboBox {

}

 

class OSXComboBox implements ComboBox {  

}

 

 

 

 

 

package abstractfactory;

public abstract class UIFactory {

      public abstract TextField createTextField();

      public abstract Label createLabel();

      public abstract ComboBox createComboBox();

}

 

 

 

 

 

package abstractfactory;

public class WindowsUIFactory extends UIFactory {

    public TextField createTextField() {

         return new WindowsTextField();

    }

    public Label createLabel() {

         return new WindowsLabel();

    }

    public ComboBox createComboBox() {

        return new WindowsComboBox();

    }

}

 

 

 

 

 

package abstractfactory;

public class OSXUIFactory extends UIFactory {

    public TextField createTextField() {

        return new OSXTextField();

    }

    public Label createLabel() {

        return new OSXLabel();

    }

    public ComboBox createComboBox() {

        return new OSXComboBox();

    }

}

 

 

 

 

 

package abstractfactory;

public class Main {

    public static void main(String[] args) {

        UIFactory factory = getUIFactory();

        TextField tf = factory.createTextField();

        Label l = factory.createLabel();

        ComboBox cb = factory.createComboBox();

        System.out.println(tf.getClass().getSimpleName());

        System.out.println(l.getClass().getSimpleName());

        System.out.println(cb.getClass().getSimpleName());

    }

     

    public static UIFactory getUIFactory() {

        if (System.getProperty("os.name").equals("Mac OS X")) {

            return new OSXUIFactory();

        } else {

             return new WindowsUIFactory();

        }

    }

}