From a61dc4e7f2ea58f77522aaeebe7682734473a26c Mon Sep 17 00:00:00 2001 From: Simon Giesel Date: Tue, 7 May 2019 12:37:25 +0200 Subject: [PATCH] Add excercise 5.1 --- de.hhn.ai.prog2.blatt3.pizzashop/pizza.txt | 3 + .../src/app/MyPizzaShop.java | 39 +++++++++---- .../src/app/Pizza.java | 3 +- .../src/app/io/PizzaIOHandler.java | 57 +++++++++++++++++++ 4 files changed, 91 insertions(+), 11 deletions(-) create mode 100644 de.hhn.ai.prog2.blatt3.pizzashop/pizza.txt create mode 100644 de.hhn.ai.prog2.blatt3.pizzashop/src/app/io/PizzaIOHandler.java diff --git a/de.hhn.ai.prog2.blatt3.pizzashop/pizza.txt b/de.hhn.ai.prog2.blatt3.pizzashop/pizza.txt new file mode 100644 index 0000000..6c4446c --- /dev/null +++ b/de.hhn.ai.prog2.blatt3.pizzashop/pizza.txt @@ -0,0 +1,3 @@ +Test#LARGE#CHEESE#TOMATO#HAM# +Hello World#FAMILY#CHEESE#TOMATO#HAM#MUSHROOM# +Test#SMALL#HAM# diff --git a/de.hhn.ai.prog2.blatt3.pizzashop/src/app/MyPizzaShop.java b/de.hhn.ai.prog2.blatt3.pizzashop/src/app/MyPizzaShop.java index 2a1d85e..a80be79 100644 --- a/de.hhn.ai.prog2.blatt3.pizzashop/src/app/MyPizzaShop.java +++ b/de.hhn.ai.prog2.blatt3.pizzashop/src/app/MyPizzaShop.java @@ -5,11 +5,9 @@ import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.GridLayout; -import java.awt.Panel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; -import java.util.Arrays; import java.util.EnumSet; import java.util.List; @@ -23,8 +21,11 @@ import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JTextField; +import javax.swing.ListSelectionModel; import javax.swing.WindowConstants; +import app.io.PizzaIOHandler; + public class MyPizzaShop extends JFrame { private static final long serialVersionUID = -5421020006317002203L; @@ -114,24 +115,42 @@ public class MyPizzaShop extends JFrame { private CenterPanel() { this.setLayout(new GridLayout(5, 2)); this.add(wrap(new JLabel("Name:"))); - this.add(wrap(new JTextField(20))); + JTextField name = new JTextField(20); + this.add(wrap(name)); this.add(wrap(new JLabel("Größe:"))); - String[] pizzaSizes = new String[EnumSet.allOf(PizzaSize.class).size()]; + String[] availPizzaSizes = new String[EnumSet.allOf(PizzaSize.class).size()]; int i = 0; for (PizzaSize size : EnumSet.allOf(PizzaSize.class)) { - pizzaSizes[i++] = size.getName(); + availPizzaSizes[i++] = size.getName(); } - this.add(wrap(new JList(pizzaSizes))); + JList pizzaSize = new JList(availPizzaSizes); + this.add(wrap(pizzaSize)); this.add(wrap(new JLabel("Belag:"))); - String[] toppings = new String[EnumSet.allOf(Topping.class).size()]; + String[] availToppings = new String[EnumSet.allOf(Topping.class).size()]; i = 0; for (Topping topping : EnumSet.allOf(Topping.class)) { - toppings[i++] = topping.getName(); + availToppings[i++] = topping.getName(); } - this.add(wrap(new JList(toppings))); + JList toppings = new JList(availToppings); + toppings.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); + this.add(wrap(toppings)); this.add(wrap(new JPanel())); this.add(wrap(priceLabel)); - this.add(wrap(new JButton("Abschicken"))); + JButton sendBtn = new JButton("Abschicken"); + sendBtn.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + List pizzas = PizzaIOHandler.read(null); + List toppingList = new ArrayList(); + for (int t : toppings.getSelectedIndices()) { + toppingList.add(Topping.values()[t]); + } + pizzas.add( + new Pizza(name.getText(), PizzaSize.values()[pizzaSize.getSelectedIndex()], toppingList)); + PizzaIOHandler.write(pizzas, null); + } + }); + this.add(wrap(sendBtn)); this.add(wrap(new JButton("Ausdrucken"))); setPrice(price); } diff --git a/de.hhn.ai.prog2.blatt3.pizzashop/src/app/Pizza.java b/de.hhn.ai.prog2.blatt3.pizzashop/src/app/Pizza.java index 226ba82..2c7df41 100644 --- a/de.hhn.ai.prog2.blatt3.pizzashop/src/app/Pizza.java +++ b/de.hhn.ai.prog2.blatt3.pizzashop/src/app/Pizza.java @@ -40,7 +40,8 @@ public class Pizza { @Override public String toString() { - return "Test"; // TODO: + return "{\n name: " + this.name + ",\n size: " + this.size + ",\n toppings: " + this.toppings.toString() + + ",\n}"; } } \ No newline at end of file diff --git a/de.hhn.ai.prog2.blatt3.pizzashop/src/app/io/PizzaIOHandler.java b/de.hhn.ai.prog2.blatt3.pizzashop/src/app/io/PizzaIOHandler.java new file mode 100644 index 0000000..3696348 --- /dev/null +++ b/de.hhn.ai.prog2.blatt3.pizzashop/src/app/io/PizzaIOHandler.java @@ -0,0 +1,57 @@ +package app.io; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import app.Pizza; +import app.PizzaSize; +import app.Topping; + +public class PizzaIOHandler { + public static List read(String fileName) { + if (fileName == null) + fileName = "pizza.txt"; + List pizzas = new ArrayList(); + try { + BufferedReader reader = new BufferedReader(new FileReader(fileName)); + String line = null; + while ((line = reader.readLine()) != null) { + String[] val = line.split("#"); + List toppings = new ArrayList(); + for (int i = 2; i < val.length; i++) { + toppings.add(Topping.valueOf(val[i])); + } + pizzas.add(new Pizza(val[0], PizzaSize.valueOf(val[1]), toppings)); + } + reader.close(); + System.out.println(pizzas); + return pizzas; + } catch (IOException ex) { + System.out.println("###ERROR### (IOException)"); + } + return null; + } + + public static void write(List pizzas, String fileName) { + if (fileName == null) + fileName = "pizza.txt"; + try (PrintWriter writer = new PrintWriter(new FileWriter(fileName))) { + for (Pizza pizza : pizzas) { + String line = pizza.getName(); + line += "#" + pizza.getSize() + "#"; + for (Topping topping : pizza.getToppings()) { + line += topping + "#"; + } + writer.println(line); + } + } catch (IOException ex) { + System.out.println("###ERROR### (IOException)"); + } + } +} \ No newline at end of file