Add excercise 5.1

This commit is contained in:
Simon Giesel 2019-05-07 12:37:25 +02:00
parent 795bb4ac63
commit a61dc4e7f2
4 changed files with 91 additions and 11 deletions

View file

@ -0,0 +1,3 @@
Test#LARGE#CHEESE#TOMATO#HAM#
Hello World#FAMILY#CHEESE#TOMATO#HAM#MUSHROOM#
Test#SMALL#HAM#

View file

@ -5,11 +5,9 @@ import java.awt.Color;
import java.awt.Component; import java.awt.Component;
import java.awt.Container; import java.awt.Container;
import java.awt.GridLayout; import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.List; import java.util.List;
@ -23,8 +21,11 @@ import javax.swing.JMenuBar;
import javax.swing.JMenuItem; import javax.swing.JMenuItem;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JTextField; import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants; import javax.swing.WindowConstants;
import app.io.PizzaIOHandler;
public class MyPizzaShop extends JFrame { public class MyPizzaShop extends JFrame {
private static final long serialVersionUID = -5421020006317002203L; private static final long serialVersionUID = -5421020006317002203L;
@ -114,24 +115,42 @@ public class MyPizzaShop extends JFrame {
private CenterPanel() { private CenterPanel() {
this.setLayout(new GridLayout(5, 2)); this.setLayout(new GridLayout(5, 2));
this.add(wrap(new JLabel("Name:"))); 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:"))); 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; int i = 0;
for (PizzaSize size : EnumSet.allOf(PizzaSize.class)) { for (PizzaSize size : EnumSet.allOf(PizzaSize.class)) {
pizzaSizes[i++] = size.getName(); availPizzaSizes[i++] = size.getName();
} }
this.add(wrap(new JList<String>(pizzaSizes))); JList<String> pizzaSize = new JList<String>(availPizzaSizes);
this.add(wrap(pizzaSize));
this.add(wrap(new JLabel("Belag:"))); 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; i = 0;
for (Topping topping : EnumSet.allOf(Topping.class)) { for (Topping topping : EnumSet.allOf(Topping.class)) {
toppings[i++] = topping.getName(); availToppings[i++] = topping.getName();
} }
this.add(wrap(new JList<String>(toppings))); JList<String> toppings = new JList<String>(availToppings);
toppings.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
this.add(wrap(toppings));
this.add(wrap(new JPanel())); this.add(wrap(new JPanel()));
this.add(wrap(priceLabel)); 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<Pizza> pizzas = PizzaIOHandler.read(null);
List<Topping> toppingList = new ArrayList<Topping>();
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"))); this.add(wrap(new JButton("Ausdrucken")));
setPrice(price); setPrice(price);
} }

View file

@ -40,7 +40,8 @@ public class Pizza {
@Override @Override
public String toString() { public String toString() {
return "Test"; // TODO: return "{\n name: " + this.name + ",\n size: " + this.size + ",\n toppings: " + this.toppings.toString()
+ ",\n}";
} }
} }

View file

@ -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<Pizza> read(String fileName) {
if (fileName == null)
fileName = "pizza.txt";
List<Pizza> pizzas = new ArrayList<Pizza>();
try {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line = null;
while ((line = reader.readLine()) != null) {
String[] val = line.split("#");
List<Topping> toppings = new ArrayList<Topping>();
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<Pizza> 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)");
}
}
}