-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuctionModel.java
More file actions
233 lines (209 loc) · 5.62 KB
/
AuctionModel.java
File metadata and controls
233 lines (209 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
import javax.swing.JFrame;
public class AuctionModel {
public ArrayList<String> Auction_data;
public ArrayList<String> Users_data;
public Controller controller;
EditElement temp_el;
public Socket socket;
DataInputStream s_in = null;
DataOutputStream s_out = null;
public AuctionModel(Controller controller, Socket socket, DataInputStream s_in, DataOutputStream s_out) {
this.controller = controller;
this.socket = socket;
this.s_in = s_in;
this.s_out = s_out;
Auction_data = new ArrayList<String>();
Users_data = new ArrayList<String>();
init_data();
//print_data();
}
private void init_data() {
String line;
try {
s_out.writeUTF("Initialization");
while((line = s_in.readUTF()) != null) {
if (line.equals("End"))
break;
Auction_data.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
File file_in = null;
FileReader fr = null;
BufferedReader br = null;
try {
file_in = new File("Users.txt");
fr = new FileReader(file_in);
br = new BufferedReader(fr);
while((line = (String)br.readLine()) != null)
Users_data.add(line);
} catch (IOException e) {
e.printStackTrace();
} finally {
try{
if(fr != null) {
fr.close();
}
} catch(IOException ee) {ee.printStackTrace();}
}
}
public void print_data() {
System.out.println(Auction_data);
System.out.println(Users_data);
}
public ArrayList<String> get_Auction_data() {
return Auction_data;
}
public void push_sign_botton() {
controller.view.run_sign_in_dialog();
}
public void push_sign_botton_in_dialog(String name, String password) {
String[] arr = new String[2];
boolean autho_flag = false;
for (int i = 0; i < Users_data.size(); i++) {
arr = Users_data.get(i).split(" ");
if (arr[0].equals(name) == true && arr[1].equals(password) == true) {
autho_flag = true;
break;
}
}
if (autho_flag == true) {
controller.user.name = name;
controller.user.authorization_flag = true;
controller.view.sign_succeed_messadge();
}
else {
controller.view.sign_failed_messadge();
}
}
public void registration(String name, String password) {
String[] arr = new String[2];
boolean registr_flag = true;
for (int i = 0; i < Users_data.size(); i++) {
arr = Users_data.get(i).split(" ");
if (arr[0].equals(name) == true) {
registr_flag = false;
break;
}
}
if (registr_flag == true) {
FileWriter fw = null;
try {
fw = new FileWriter("Users.txt", true);
fw.write(name);
fw.write(" ");
fw.write(password);
fw.write("\n");
} catch (IOException e) {e.printStackTrace();}
finally {
try{
if(fw != null) {
fw.close();
}
} catch(IOException ee) {ee.printStackTrace();}
}
Users_data.add(name + " " + password);
controller.view.registration_succeed_message();
}
else
controller.view.registration_failed_message();
}
public void push_done_button_in_dialog(String new_price_str) {
temp_el.new_price_str = new_price_str;
temp_el.new_price = Integer.parseInt(new_price_str);
if (temp_el.new_price > temp_el.current_price) {
send_new_info_to_server(controller.user.name);
}
}
public void push_table_button(int editing_row, int current_price) {
if (!controller.user.isAuthorizated()) {
controller.view.sign_attention_messadge();
}
else {
temp_el = new EditElement(editing_row, current_price);
controller.view.making_offer();
}
}
private void send_new_info_to_server(String new_name) {
String[] arr = new String[4];
arr = Auction_data.get(temp_el.editing_row).split(" ");
String lot = new String(arr[0] + " " + temp_el.new_price_str + " " + new_name);
StringBuilder sb = new StringBuilder();
sb.append(temp_el.editing_row);
String row = sb.toString();
System.out.println(lot);
System.out.println(row);
try {
s_out.writeUTF("Update");
s_out.writeUTF(lot);
s_out.writeUTF(row);
} catch(IOException ee) {ee.printStackTrace();}
}
private void update_file(String new_price, int editing_row, String new_name) {
System.out.println("update");
FileWriter fw = null;
String[] arr = new String[4];
try {
fw = new FileWriter("Auction.txt", false);
for (int i = 0; i < Auction_data.size(); i++) {
if (i == editing_row) {
arr = Auction_data.get(i).split(" ");
String s = new String(arr[0] + " " + new_price + " " + new_name);
Auction_data.set(i, s);
}
fw.write(Auction_data.get(i));
fw.write("\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try{
if(fw != null) {
fw.close();
}
} catch(IOException ee) {ee.printStackTrace();}
}
}
public void close_connection() {
try {
s_out.writeUTF("Exit");
socket.close();
} catch (IOException e) {e.printStackTrace();}
}
public void update_table() {
String line;
Auction_data = new ArrayList<String>() ;
System.out.println("update_table");
try {
s_out.writeUTF("Update_table");
while((line = s_in.readUTF()) != null) {
if (line.equals("End"))
break;
Auction_data.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("update_table 2");
System.out.println(Auction_data);
controller.view.update_table(Auction_data);
}
}
class EditElement {
public int current_price;
public int new_price;
public String new_price_str;
public int editing_row;
public EditElement(int editing_row, int current_price) {
this.editing_row = editing_row;
this.current_price = current_price;
}
}