Commits

synthomat  committed 8280991

Thread-Klassen ausgelagert

  • Participants
  • Parent commits 6ed25cd

Comments (0)

Files changed (5)

File src/controller/ControllerCustomer.java

 import view.ViewCustomer;
 import view.ViewLogin;
 import model.ModelShop;
+import utils.OrderThread;
+import utils.TimeThread;
 
 public class ControllerCustomer implements ActionListener {
 
 		
 		view.addActionListner(this);
 
-		new Thread(new TimeThread()).start();
+		new Thread(new TimeThread(view)).start();
 	}
 
 	@Override
 		}
 		
 		System.out.println("Gesammtpreis für Bestellung: " + order.getSum() + "€");
-	}
 
-	class TimeThread implements Runnable {
-		DatagramSocket clientSocket;
-		InetAddress IPAddress;
-		byte[] buffer;
-		DatagramPacket packet;
-
-		TimeThread() {
-
-			try {
-				clientSocket = new DatagramSocket();
-				IPAddress = InetAddress.getByName("localhost");
-			} catch (final SocketException e) {
-				e.printStackTrace();
-			} catch (final UnknownHostException e) {
-				e.printStackTrace();
-			}
-
-			buffer = new byte[1024];
-			packet = new DatagramPacket(buffer, buffer.length, IPAddress, 6667);
-		}
-
-		@Override
-		public void run() {
-			while (true) {
-				try {
-					// time-Befehl absenden
-					buffer = "time".getBytes();
-					DatagramPacket dp = new DatagramPacket(buffer, buffer.length, IPAddress, 6667);
-					clientSocket.send(dp);
-
-					// Uhrzeit empfangen und anzeigen
-					clientSocket.receive(packet);
-					view.setTimeLabel(new String(packet.getData()));
-
-					// Warten
-					Thread.sleep(1000);
-				} catch (InterruptedException e) {
-					e.printStackTrace();
-				} catch (IOException e) {
-					e.printStackTrace();
-				}
-			}
-		}
+		new Thread(new OrderThread(order)).start();
 	}
 }

File src/hs/HardwareShop.java

 		ControllerCustomer controllerCustomer = new ControllerCustomer(model, viewCustomer);
 	}
 
-}
+}

File src/utils/OrderThread.java

+package utils;
+
+import java.io.BufferedReader;
+import java.io.ObjectInputStream;
+import java.net.Socket;
+
+import model.Order;
+
+public class OrderThread implements Runnable {
+
+	Order order;
+	Socket sock = null;
+	BufferedReader br = null;
+	ObjectInputStream ois = null;
+	
+	
+	public OrderThread(Order order) {
+		this.order = order;
+	}
+
+	@Override
+	public void run() {
+		// TODO Auto-generated method stub
+		System.out.println(order);
+	}
+}

File src/utils/TimeThread.java

+package utils;
+
+import java.io.IOException;
+import java.net.DatagramPacket;
+import java.net.DatagramSocket;
+import java.net.InetAddress;
+import java.net.SocketException;
+import java.net.UnknownHostException;
+
+import view.ViewCustomer;
+
+public class TimeThread implements Runnable {
+	DatagramSocket clientSocket;
+	InetAddress IPAddress;
+	byte[] buffer;
+	DatagramPacket packet;
+	ViewCustomer view;
+	
+	public TimeThread(ViewCustomer view) {
+		this.view = view;
+		try {
+			clientSocket = new DatagramSocket();
+			IPAddress = InetAddress.getByName("localhost");
+		} catch (final SocketException e) {
+			e.printStackTrace();
+		} catch (final UnknownHostException e) {
+			e.printStackTrace();
+		}
+
+		buffer = new byte[1024];
+		packet = new DatagramPacket(buffer, buffer.length, IPAddress, 6667);
+	}
+
+	@Override
+	public void run() {
+		while (true) {
+			try {
+				// time-Befehl absenden
+				buffer = "time".getBytes();
+				DatagramPacket dp = new DatagramPacket(buffer, buffer.length, IPAddress, 6667);
+				clientSocket.send(dp);
+
+				// Uhrzeit empfangen und anzeigen
+				clientSocket.receive(packet);
+				view.setTimeLabel(new String(packet.getData()));
+
+				// Warten
+				Thread.sleep(1000);
+			} catch (InterruptedException e) {
+				e.printStackTrace();
+			} catch (IOException e) {
+				e.printStackTrace();
+			}
+		}
+	}
+}

File src/view/ViewShop.java

 		JPanel controls = new JPanel();
 
 		// Hilfslayout für Eingabefelder / Beschriftungen
-		controls.setLayout(new GridLayout(2, 2));
+		GridLayout gl = new GridLayout(2, 2);
+		gl.setHgap(5);
+		controls.setLayout(gl);
 		panel.add(controls, BorderLayout.SOUTH);
 
 		GridBagConstraints c = new GridBagConstraints();