Commits

synthomat committed 0274ead

Serveruhrzeit wird geholt. Testserver eingerichtet. Order-Objekt wird erstellt

  • Participants
  • Parent commits ae81897

Comments (0)

Files changed (6)

File products.ser

Binary file added.

File src/controller/ControllerCustomer.java

 package controller;
 
+import fpt.com.Product;
+import hs.Order;
+
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.io.BufferedReader;
+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 java.util.Iterator;
 
 import view.ViewCustomer;
 import model.ModelShop;
 
-public class ControllerCustomer implements ActionListener{
+public class ControllerCustomer implements ActionListener {
 
-		private ModelShop model;
-		private ViewCustomer view;
+	private ModelShop model;
+	private ViewCustomer view;
+
+	public ControllerCustomer(ModelShop model, ViewCustomer view) {
+		this.model = model;
+		this.view = view;
+
+		view.addActionListner(this);
+
+		new Thread(new TimeThread()).start();
+	}
+
+	@Override
+	public void actionPerformed(ActionEvent arg0) {
+		if (arg0.getSource() == view.button)
+			order();
+
+	}
+
+	public void order() {
+		Order order = new Order();
 		
-		public ControllerCustomer(ModelShop model, ViewCustomer view) {
-			this.model = model;
-			this.view = view;
+		for(int i=0; i<view.model.getProducts().size(); i++) {
+			Product prod = view.model.getProducts().get(i);
+			prod.setQuantity((Integer) view.mod.getValueAt(i, 3));
 			
-			view.addActionListner(this);
+			order.add(prod);		
+		}
+		System.out.println(order.getSum());
+		/*
+		while( iter.hasNext()) {
+			Product prod = (Product)iter.next();
+			
+			order.add(prod);
+		}
+		*/
+		
+	}
+
+	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 actionPerformed(ActionEvent arg0) {
-			// TODO Auto-generated method stub
-			
+		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/controller/UDPTimeServer.java

+package controller;
+
+import java.io.*;
+import java.net.*;
+import java.nio.charset.Charset;
+import java.util.Date;
+
+public class UDPTimeServer {
+	public static void main(String[] Args) {
+		byte[] inhalt = new byte[1024];
+		DatagramPacket paket;
+		DatagramSocket dSocket = null;
+		try {
+			dSocket = new DatagramSocket(6667);
+			while (System.in.available() == 0) {
+
+				paket = new DatagramPacket(inhalt, inhalt.length);
+				dSocket.receive(paket);
+				String recv = new String(paket.getData()).substring(0,
+						paket.getLength());
+
+				// Kommando überprüfen. Wird die Uhrzeit abgefragt?
+				if (!recv.equals("time"))
+					continue;
+
+				// IP und Adresse des Clients ausgeben
+				System.out.println("[IP]: " + paket.getAddress()
+						+ " | [Port]: " + paket.getPort());
+
+				inhalt = (new Date(System.currentTimeMillis())).toString()
+						.getBytes();
+				paket = new DatagramPacket(inhalt, inhalt.length,
+						paket.getSocketAddress());
+				dSocket.send(paket);
+			}
+			//System.in.read(new byte[System.in.available()]);
+			dSocket.close();
+		} catch (Exception e) {
+		}
+	}
+}

File src/hs/HardwareShop.java

 		ModelShop model = new ModelShop();
 		
 		ViewShop viewShop = new ViewShop(model);
-		//ViewCustomer viewCustomer = new ViewCustomer(model);
+		ViewCustomer viewCustomer = new ViewCustomer(model);
 		
 		ControllerShop controllerShop = new ControllerShop(model, viewShop);
-		//ControllerCustomer controllerCustomer = new ControllerCustomer(model, viewCustomer);
+		ControllerCustomer controllerCustomer = new ControllerCustomer(model, viewCustomer);
 	}
 
 }

File src/hs/Order.java

 	public boolean add(Product e) {
 		if (super.add(e)) {
 			sum+=e.getPrice();
-			return true;	
+			return true;
 		}
 
 		return false;

File src/view/ViewCustomer.java

 
 import javax.swing.JButton;
 import javax.swing.JFrame;
+import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.JScrollPane;
 import javax.swing.JTable;
 	JPanel panel;
 	JPanel panel2;
 	JTable table;
-	JButton button;
+	public JButton button;
 	
-	ModelShop model;
+	public ModelShop model;
+	JLabel l_time;
+	public DefaultTableModel mod;
 	
 	public ViewCustomer(ModelShop model){
 			super("View Customer");
 			
 			button = new JButton("Bestellen");
 			panel2.add(button, BorderLayout.EAST);
-			
+			l_time = new JLabel("warte auf Serverzeit...");
+			panel2.add(l_time, BorderLayout.WEST);
 			panel.add(panel2,BorderLayout.SOUTH);
 
 			// update aufrufen um die Spalten darzustellen (noch keine Daten!)
 	@Override
 	public void update(Observable arg0, Object arg1) {
 		// Temporäres Model erstellen
-		DefaultTableModel mod = new DefaultTableModel();
+		mod = new DefaultTableModel();
 
 		// Spalten erstellen, Spaltenname
 		mod.addColumn("Name");
 	}	
 	
 	public void addActionListner(ActionListener e) {
-		// TODO Auto-generated method stub
+		button.addActionListener(e);
 		
 	}
-
+	
+	public void setTimeLabel(String time) {
+		l_time.setText("Zeit: " + time);
+	}
 }