Skip to content

Commit a08037e

Browse files
committed
Java : Task 2 & 3 WIP
1 parent d8a851c commit a08037e

File tree

8 files changed

+211
-10
lines changed

8 files changed

+211
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
5
2+
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
150101053, Roopansh Bansal, roopansh@iitg.ac.in, 100, TA1
2+
150101002, Abhishek Goyal, a.goyal@iitg.ac.in, 100, TA2

JavaProgramming/src/task2/distributedsystem/Constants.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ public class Constants {
44
public static final String CC = "CC";
55
public static final String TA1 = "TA1";
66
public static final String TA2 = "TA2";
7-
public static final String STUD_INFO = "D:\\Studies\\Semester 7\\CS431 Programming Languages Lab\\Programming-Lab\\JavaProgramming\\src\\task2\\distributedsystem\\stud_info.txt";
7+
public static final String STUD_INFO = "/home/cse/IdeaProjects/Programming-Lab/JavaProgramming/src/task2/distributedsystem/stud_info.txt";
88

99
}

JavaProgramming/src/task2/distributedsystem/MarkEvaluationSystem.java

+1-8
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@ private MarkEvaluationSystem() {
2424

2525
Data = new HashMap<>();
2626
InputBuffer = new ArrayList<>();
27-
28-
ArrayList<String> temp = new ArrayList<>();
29-
temp.add("Abhishek");
30-
temp.add("agoyal");
31-
temp.add("100");
32-
temp.add("CC");
33-
Data.put("002", temp);
3427
}
3528

3629
private void UpdateStudentMarks() {
@@ -215,7 +208,7 @@ public static void main(String[] args) throws IOException {
215208
markEvaluationSystem.writeFinalData();
216209

217210
/*Todo
218-
* Generate the remaning files - Sorted by name and sorted by roll number */
211+
* Generate the remaning files - Sorted by name and sorted by roll number */
219212
return;
220213
case 1:
221214
markEvaluationSystem.UpdateStudentMarks();
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
150101053, Roopansh Bansal, roopansh@iitg.ac.in, 100, TA1
2-
150101002, Abhishek Goyal, a.goyal@iitg.ac.in, 100, TA2
2+
150101002, Abhishek Goyal, a.goyal@iitg.ac.in, 100, TA2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package task3.teastall;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class Constants {
7+
public static final String MESSAGE_END = "END";
8+
public static final String SERVER_ADDRESS = "127.0.0.1";
9+
public static final int SERVER_PORT = 5000;
10+
11+
public static Map<String, Integer> getInitialItems() {
12+
Map<String, Integer> items = new HashMap<>();
13+
items.put("Coffee", 0);
14+
items.put("Tea", 0);
15+
items.put("Snacks", 100);
16+
items.put("Chips", 100);
17+
return items;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package task3.teastall.client;
2+
3+
import task3.teastall.Constants;
4+
5+
import javax.swing.*;
6+
import javax.swing.table.DefaultTableModel;
7+
import java.io.BufferedInputStream;
8+
import java.io.DataInputStream;
9+
import java.io.IOException;
10+
import java.net.Socket;
11+
import java.util.ArrayList;
12+
import java.util.HashMap;
13+
import java.util.List;
14+
import java.util.Map;
15+
16+
import static java.lang.System.out;
17+
18+
public class Client {
19+
private Map<String, Integer> OrderItemList;
20+
private List<String> Items;
21+
private Socket socket = null;
22+
private DataInputStream dataInputStream = null;
23+
24+
private Client() {
25+
OrderItemList = new HashMap<>();
26+
Items = new ArrayList<>();
27+
establishConnection(Constants.SERVER_ADDRESS, Constants.SERVER_PORT);
28+
generateGui();
29+
}
30+
31+
private void establishConnection(String address, int port) {
32+
// establish a connection
33+
try {
34+
socket = new Socket(address, port);
35+
out.println("Connected to server");
36+
37+
// takes input from terminal
38+
dataInputStream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
39+
} catch (IOException i) {
40+
System.out.println(i);
41+
}
42+
43+
// string to read message from input
44+
String line = "";
45+
// keep reading until "End" is input
46+
while (!line.equals(Constants.MESSAGE_END)) {
47+
try {
48+
this.Items.add(line);
49+
line = dataInputStream.readUTF();
50+
} catch (IOException i) {
51+
System.out.println(i);
52+
}
53+
}
54+
}
55+
56+
private void generateGui() {
57+
JFrame frame = new JFrame(); //creating instance of JFrame
58+
59+
JLabel itemLabel = new JLabel("Select the item to order");
60+
JLabel quantityLabel = new JLabel("Select the quantity of item to order.");
61+
JButton orderButton = new JButton("Order");//creating instance of JButton
62+
JButton addButton = new JButton("Add");//creating instance of JButton
63+
SpinnerModel spinnerNumberModel = new SpinnerNumberModel(1, //initial value
64+
1, //minimum value
65+
10, //maximum value
66+
1); //step
67+
JSpinner quantitySpinner = new JSpinner(spinnerNumberModel);
68+
JList itemList = new JList(Items.toArray());
69+
DefaultTableModel orderDetailsTable = new DefaultTableModel(new String[]{"S.No.", "Item", "Qty"}, 0);
70+
JTable orderTable = new JTable(orderDetailsTable);
71+
JScrollPane orderDetails = new JScrollPane(orderTable);
72+
73+
74+
// x axis, y axis, width, height
75+
itemLabel.setBounds(50, 50, 250, 30);
76+
itemList.setBounds(350, 50, 100, 150);
77+
quantityLabel.setBounds(50, 250, 250, 30);
78+
quantitySpinner.setBounds(350, 250, 50, 30);
79+
addButton.setBounds(250, 400, 100, 40);
80+
orderDetails.setBounds(50, 500, 500, 200);
81+
orderButton.setBounds(250, 750, 100, 40);
82+
83+
addButton.addActionListener(actionEvent -> {
84+
String item = Items.get(itemList.getSelectedIndex());
85+
Integer quantity = OrderItemList.getOrDefault(item, 0) + (Integer) quantitySpinner.getValue();
86+
out.println(quantity);
87+
OrderItemList.put(item, quantity);
88+
orderDetailsTable.setRowCount(0);
89+
OrderItemList.forEach((key, value) -> orderDetailsTable.addRow(new Object[]{orderDetailsTable.getRowCount() + 1, key, value}));
90+
if (OrderItemList.size() > 0) {
91+
orderButton.setEnabled(true);
92+
} else {
93+
orderButton.setEnabled(false);
94+
}
95+
});
96+
orderButton.setEnabled(false);
97+
98+
frame.add(itemLabel);
99+
frame.add(itemList);
100+
frame.add(quantityLabel);
101+
frame.add(quantitySpinner);
102+
frame.add(addButton);//adding addButton in JFrame
103+
frame.add(orderDetails);
104+
frame.add(orderButton);//adding orderButton in JFrame
105+
106+
107+
frame.setSize(600, 800);//600 width and 800 height
108+
109+
frame.setLayout(null);//using no layout managers
110+
111+
frame.setVisible(true);//making the frame visible
112+
}
113+
114+
public static void main(String[] args) {
115+
new Client();
116+
}
117+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package task3.teastall.server;
2+
3+
import task3.teastall.Constants;
4+
5+
import java.io.BufferedInputStream;
6+
import java.io.DataInputStream;
7+
import java.io.DataOutputStream;
8+
import java.io.IOException;
9+
import java.net.ServerSocket;
10+
import java.net.Socket;
11+
import java.util.ArrayList;
12+
import java.util.HashMap;
13+
import java.util.List;
14+
import java.util.Map;
15+
16+
public class Server {
17+
private Map<String, Integer> Items; // Item, Stock
18+
private List<Map<String, Integer>> OrdersRecieved;
19+
20+
// constructor with port
21+
public Server(int port) {
22+
Items = Constants.getInitialItems();
23+
OrdersRecieved = new ArrayList<>();
24+
25+
// starts server and waits for a connection
26+
try {
27+
ServerSocket server = new ServerSocket(port);
28+
System.out.println("Tea Stall Server started");
29+
30+
// initialize socket and input stream
31+
Socket socket = server.accept();
32+
33+
// takes input from the client socket
34+
DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
35+
DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
36+
37+
String line = "";
38+
39+
// Send message
40+
System.out.println("Sending available items");
41+
for (String item : Items.keySet()) {
42+
dataOutputStream.writeUTF(item);
43+
}
44+
dataOutputStream.writeUTF(Constants.MESSAGE_END);
45+
dataOutputStream.close();
46+
47+
System.out.println("Receiving Orders");
48+
// reads message from client until "END" is sent
49+
while (!line.equals(Constants.MESSAGE_END)) {
50+
try {
51+
line = dataInputStream.readUTF();
52+
System.out.println(line);
53+
} catch (IOException i) {
54+
System.out.println(i);
55+
}
56+
}
57+
// close connection
58+
socket.close();
59+
dataInputStream.close();
60+
} catch (IOException i) {
61+
System.out.println(i);
62+
}
63+
}
64+
65+
public static void main(String[] args) {
66+
new Server(Constants.SERVER_PORT);
67+
}
68+
}

0 commit comments

Comments
 (0)