1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package Workers;
21
22 import Utilities.Customer;
23 import Utilities.Geolocation;
24 import Utilities.Order;
25 import Utilities.formattedProductProps;
26 import javafx.concurrent.Task;
27 import javafx.scene.control.TableView;
28
29 import java.math.BigDecimal;
30
31
32
33
34
35
36 public class AddCustomerWorker extends Task<Customer> {
37 private final Integer ID;
38
39 private final String Address;
40 private final String Town;
41 private final String State;
42 private final String year;
43 private final TableView<formattedProductProps> ProductTable;
44 private final String Name;
45 private final String ZipCode;
46 private final String Phone;
47 private final String Email;
48 private final String DonationsT;
49 private final String NameEditCustomer;
50 private final String uName;
51 private final BigDecimal Paid;
52 private final Boolean Delivered;
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 public AddCustomerWorker(Integer id, String Address, String Town, String State, String year, TableView ProductTable, String name, String zipCode, String phone, String email, String donationsT, String nameEditCustomer, String paid, Boolean delivered, String uName) {
69 ID = id;
70 this.Address = Address;
71 this.Town = Town;
72 this.State = State;
73 this.year = year;
74 this.ProductTable = ProductTable;
75 Name = name;
76 ZipCode = zipCode;
77 Phone = phone;
78 Email = email;
79 DonationsT = donationsT;
80 NameEditCustomer = nameEditCustomer;
81 Paid = new BigDecimal(paid);
82 Delivered = delivered;
83 this.uName = uName;
84 }
85
86 private static void failIfInterrupted() throws InterruptedException {
87 if (Thread.currentThread().isInterrupted()) {
88 throw new InterruptedException("Interrupted while Adding Order");
89 }
90 }
91
92 private int IntegerLength(int n) {
93 if (n < 100000) {
94
95 if (n < 100) {
96
97 if (n < 10) { return 1; } else { return 2; }
98 } else {
99
100 if (n < 1000) { return 3; } else {
101
102 if (n < 10000) { return 4; } else { return 5; }
103 }
104 }
105 } else {
106
107 if (n < 10000000) {
108
109 if (n < 1000000) { return 6; } else { return 7; }
110 } else {
111
112 if (n < 100000000) { return 8; } else {
113
114 if (n < 1000000000) { return 9; } else { return 10; }
115 }
116 }
117 }
118
119 }
120
121
122
123
124
125
126
127 @Override
128 protected Customer call() throws Exception {
129 String address = String.format("%s, %s, %s, %s", Address, Town, State, ZipCode);
130 updateMessage("Analyzing Address");
131 Utilities.Coords coords;
132 coords = Geolocation.GetCoords(address);
133
134
135 int numRows = ProductTable.getItems().size();
136
137 double lat = coords.getLat();
138 double lon = coords.getLon();
139 AddCustomerWorker.failIfInterrupted();
140 updateProgress(0, 100);
141 Customer customer = new Customer(ID, NameEditCustomer, year, Address, Town, State, ZipCode, lat, lon, Phone, Paid, Delivered, Email,
142 Name, new BigDecimal(DonationsT), uName);
143 customer.progressProperty().addListener(((observableValue, oldProgress, newProgress) -> {
144 updateProgress(newProgress.doubleValue(), 100);
145 }));
146 customer.messageProperty().addListener(((observableValue, oldMessage, newMessage) -> {
147 updateMessage(newMessage);
148 }));
149 Integer custID = customer.updateValues(() -> AddCustomerWorker.failIfInterrupted());
150 Order order = new Order(ProductTable.getItems(), year, custID, Paid, Delivered, uName);
151 order.progressProperty().addListener(((observableValue, oldProgress, newProgress) -> {
152 updateProgress(newProgress.doubleValue(), 100);
153 }));
154 order.messageProperty().addListener(((observableValue, oldMessage, newMessage) -> {
155 updateMessage(newMessage);
156 }));
157 String Id = order.updateOrder(() -> AddCustomerWorker.failIfInterrupted());
158
159 updateProgress(100, 100);
160
161
162 updateMessage("Done");
163
164
165
166
167 return customer;
168 }
169 }