-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInvoiceEntry.java
More file actions
227 lines (210 loc) · 7.87 KB
/
Copy pathInvoiceEntry.java
File metadata and controls
227 lines (210 loc) · 7.87 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
package ch24;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.io.IOException;
import java.io.File;
import java.util.Scanner;
/**
* Enters an invoice into the database. Be sure to add Customer.sql,
* Product.sql, Invoice.sql, and LineItem.sql to the database before running
* this program.
*/
public class InvoiceEntry {
public static void main(String args[]) {
if (args.length == 0) {
System.out.println(
"Usage: java -classpath driver_class_path" + File.pathSeparator +
". InvoiceEntry propertiesFile");
return;
}
try {
SimpleDataSource.init(args[0]);
try (Connection conn = SimpleDataSource.getConnection();
Scanner in = new Scanner(System.in)) {
addInvoice(in, conn);
}
} catch (SQLException ex) {
System.out.println("Database error");
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
System.out.println("Error loading database driver");
ex.printStackTrace();
} catch (IOException ex) {
System.out.println("Error loading database properties");
ex.printStackTrace();
}
}
public static void addInvoice(Scanner in, Connection conn) throws SQLException {
int customerNumber = newCustomer(conn, in);
int id = getNewId(conn, "Invoice");
try (PreparedStatement stat = conn.prepareStatement("INSERT INTO Invoice VALUES (?, ?, 0)")) {
stat.setInt(1, id);
stat.setInt(2, customerNumber);
stat.executeUpdate();
}
boolean done = false;
while (!done) {
String productCode = nextLine(in, "Product code (D=Done, L=List)");
if (productCode.equals("D")) {
done = true;
} else if (productCode.equals("L")) {
listProducts(conn);
} else if (findProduct(conn, productCode)) {
int quantity = nextInt(in, "Quantity");
addLineItem(conn, id, productCode, quantity);
} else {
System.out.println("Invalid product code.");
}
}
showInvoice(conn, id);
}
/**
* Prompts the user for the customer information and creates a new customer.
*
* @param conn the database connection
* @param in the scanner
* @return the ID of the new customer
*/
private static int newCustomer(Connection conn, Scanner in) throws SQLException {
String name = nextLine(in, "Name");
String address = nextLine(in, "Street address");
String city = nextLine(in, "City");
String state = nextLine(in, "State");
String zip = nextLine(in, "Zip");
int id = getNewId(conn, "Customer");
try (PreparedStatement stat = conn.prepareStatement("INSERT INTO Customer VALUES (?, ?, ?, ?, ?, ?)")) {
stat.setInt(1, id);
stat.setString(2, name);
stat.setString(3, address);
stat.setString(4, city);
stat.setString(5, state);
stat.setString(6, zip);
stat.executeUpdate();
}
return id;
}
/**
* Finds a product in the database.
*
* @param conn the database connection
* @param code the product code to search
* @return true if there is a product with the given code
*/
private static boolean findProduct(Connection conn, String code) throws SQLException {
boolean found = false;
try (PreparedStatement stat = conn.prepareStatement("SELECT * FROM Product WHERE Product_Code = ?")) {
stat.setString(1, code);
ResultSet result = stat.executeQuery();
found = result.next();
}
return found;
}
/**
* Adds a line item to the database
*
* @param conn the database connection
* @param id the invoice ID
* @param code the product code
* @param quantity the quantity to order
*/
private static void addLineItem(Connection conn, int id, String code, int quantity)
throws SQLException {
try (PreparedStatement stat = conn.prepareStatement("INSERT INTO LineItem VALUES (?, ?, ?)")) {
stat.setInt(1, id);
stat.setString(2, code);
stat.setInt(3, quantity);
stat.executeUpdate();
}
}
/**
* Lists all products in the database.
*
* @param conn the database connection
*/
private static void listProducts(Connection conn) throws SQLException {
try (Statement stat = conn.createStatement()) {
ResultSet result = stat.executeQuery("SELECT Product_Code, Description FROM Product");
while (result.next()) {
String code = result.getString(1);
String description = result.getString(2);
System.out.println(code + " " + description);
}
}
}
/**
* Gets a new ID for a table. This method should be called from inside a
* transaction that also creates the new row with this ID. The ID field should
* have name table_Number and type INTEGER.
*
* @param table the table name
* @return a new ID that has not yet been used.
*/
private static int getNewId(Connection conn, String table) throws SQLException {
int max = -1;
try (Statement stat = conn.createStatement()) {
ResultSet result = stat.executeQuery("SELECT max(" + table + "_Number) FROM " + table);
result.next();
max = result.getInt(1) + 1;
}
return max;
}
/**
* Shows an invoice.
*
* @param conn the database connection
* @param id the invoice ID
*/
private static void showInvoice(Connection conn, int id) throws SQLException {
try (PreparedStatement stat = conn.prepareStatement("SELECT Customer.Name, Customer.Address, "
+ "Customer.City, Customer.State, Customer.Zip " + "FROM Customer, Invoice "
+ "WHERE Customer.Customer_Number = Invoice.Customer_Number "
+ "AND Invoice.Invoice_Number = ?")) {
stat.setInt(1, id);
ResultSet result = stat.executeQuery();
result.next();
System.out.println(result.getString(1));
System.out.println(result.getString(2));
System.out.println(result.getString(3).trim() + ", " + result.getString(4)
+ " " + result.getString(5));
}
try (PreparedStatement stat = conn.prepareStatement(
"SELECT Product.Product_Code, Product.Description, LineItem.Quantity " + "FROM Product, LineItem "
+ "WHERE Product.Product_Code = LineItem.Product_Code " + "AND LineItem.Invoice_Number = ?")) {
stat.setInt(1, id);
ResultSet result = stat.executeQuery();
while (result.next()) {
String code = result.getString(1);
String description = result.getString(2).trim();
int qty = result.getInt(3);
System.out.println(qty + " x " + code + " " + description);
}
}
}
/**
* Prompts the user and reads a line from a scanner.
*
* @param in the scanner
* @param prompt the prompt
* @return the string that the user entered
*/
private static String nextLine(Scanner in, String prompt) {
System.out.print(prompt + ": ");
return in.nextLine();
}
/**
* Prompts the user and reads an integer from a scanner.
*
* @param in the scanner
* @param prompt the prompt
* @return the integer that the user entered
*/
private static int nextInt(Scanner in, String prompt) {
System.out.print(prompt + ": ");
int result = in.nextInt();
in.nextLine(); // Consume newline
return result;
}
}