-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewSystem.java
More file actions
64 lines (54 loc) · 2.14 KB
/
NewSystem.java
File metadata and controls
64 lines (54 loc) · 2.14 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
/**
* Name: Yueyihan Qi
* Course: CS-665 Software Designs & Patterns
* Date: 03/21/2024
* File Name: New.java
* Description: This class represents a new system that communicates over HTTPS to retrieve customer data.
*/
package UtilizingLegacySystems;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class NewSystem implements CustomerData_HTTPS {
@Override
public void printCustomer(int customerId) {
System.out.println("Customer ID(Via HTTPS): " + customerId);
}
@Override
public Customer getCustomer_HTTPS(int customerId) {
try {
// Create a URL object and change to servlet url
URL url = new URL("http://localhost:8080/customer");
// open connection
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
// get server response
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder response = new StringBuilder();
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
// parse customer id from response
int customerID = parseCustomerIdFromJson(response.toString());
// return customer object
return new Customer(customerID);
} catch (IOException e) {
e.printStackTrace();
return new Customer(-1); //return default value when error
}
}
// parses the customer id from json response
private int parseCustomerIdFromJson(String json) {
int customerId = -1; // set default as -1
try {
customerId = Integer.parseInt(json.split("\"customerId\":")[1].split("}")[0].trim());
} catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
return customerId;
}
}