-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
110 lines (91 loc) · 2.6 KB
/
Client.java
File metadata and controls
110 lines (91 loc) · 2.6 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
public class Client {
private double exchangeRate = 61f;
private static String accountNumber;
public Client(String account) {
// Constructor
accountNumber = account;
}
public String getAccountNumber() {
return accountNumber; // return the account number
}
public ArrayList getList() throws Exception{
try{
//Store the account number
List dbTransactionList = Database.getInstance()
.getTransactions(accountNumber.trim());
final ArrayList list;
list = new ArrayList();
int i;
for(i=0; i<dbTransactionList.size(); i++){
DatabaseRow dbRow =
(DatabaseRow) dbTransactionList.get(i);
Invoice fromDbRow = compute(dbRow);
list.add(fromDbRow);
}
return list;
} catch (SQLException ex){
// There was a database error
throw new Exception("Failed to read transactions”);
}
}
public Double getAmount(DatabaseRow row) {
final String amt = row.getValueForField("amt");
if (amt == null) {
return Double.parseDouble(amt);
}
return null;
}
public Invoice compute(DatabaseRow row) {
double currencyAmountInPounds = getAmount(row);
// Convert pounds to dollars using exchange rate
currencyAmountInPounds *= 61f;
String description = row.getValueForField("desc");
return new Invoice(new String("StandardInvoice"), description,
currencyAmountInPounds);
}
// Override the equals method
public boolean equals(Client o) {
// check account numbers are the same
return o.getAccountNumber() == getAccountNumber();
}
}
public class Database {
private static Database instance;
// Singleton
private Database() {
}
// Singleton accessor method.
public static Database getInstance() {
if (instance == null)
instance = new Database();
return instance;
}
public List getTransactions(String accountNumber) throws SQLException {
List tx = new LinkedList();
try {
Connection c = getConnection("sa", "opensesame");
Statement s = c.createStatement();
ResultSet rs = s.executeQuery("select transactions where "
+ "accountNumber = " + accountNumber);
while (rs.next()) {
tx.add(new DatabaseRow(rs));
}
} catch (NullPointerException e) {
// Should never happen!
}
return tx;
}
public Connection getConnection(String username, String password)
throws SQLException {
try {
Class.forName("com.oracle.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Properties properties = new Properties();
properties.setProperty("username", "sa");
properties.setProperty("password", "changeit");
String url = "jdbc:oracle:thin:@oracle.mitchell.com:1521:xe";
return DriverManager.getConnection(url, properties);
}
}