1. (12) Suppose you are automating operations for a store selling PCs. All PCs of a certain model number are equivalent as far as buyers are concerned, but the store wants to sell them in FIFO (first-in, first-out) order so that no box gets too worn-looking. Each PC has a model number (int), serial number (10-char string), vendor id (int), and delivery date (int day number.)
All long-term changeable knowledge should be in the DB (except per-user data). This means we need a table to hold the PC information:
create table pcs(model int, serial_no int, vendor int,
delivery_date int);
(normally we would have a PK set up, probably serial_no, but it's not required by the problem statement)
select serial_no from pcs
where model=1234 and delivery_date = (select
min(delivery_date) from pcs where model=1234)
This may result in several serial_nos. To make it unique we could select for min(serial_no) instead of just serial_no.
c. Design a domain class and DAO API (method signatures only) for this app that supports insert as well as the retrieval as described above.
domain class PC: just a POJO with the attributes as fields: you don’t have to write out the getters and setters, just indicate them
public class PC {
private String serialNo;
private int model;
private int vendor;
private int deliveryDate;
// getters and setters for the 4 fields
}
DAO API:
void initializeDB() throws SQLException;
void insertPC(PC pc) throws SQLException;
PC findOldestPC(int model) throws SQLException;
(also close() is a good idea, to close the Connection)
2. (7) In the same scenario as problem 1, once a PC is sold, we need to keep track of the customer for each individual PC. Each PC has a unique customer but a single customer can purchase multiple PCs. Each customer has a string name and a customer id, custid (a unique integer).
create table customer( name char(20),
custid int primary key);
b. How do we specify the customer for each PC?
We need to add a custid
column to table pcs.
The FK needs to go from PCs to customer, because a PC has a unique customer, the target of the FK:
Answer: Add FK from custid of pcs to custid of customer.
3. (12) Web Background
-------------
a.html
<html>
<head>
<title>Page A</title>
</head>
<body>
<a href="b.html">Link to
B</A><br>
</body>
</html>
b.html
<html>
<head>
<title>Page B</title>
</head>
<body>
<form method='POST'>
<input type="text" name="msg">
<input type="submit">
</form>
</body>
</html>
Since headers were not asked for, you can omit them--
GET /testmsg/a.html HTTP/1.1
response body: text of a.html
GET /testmsg/b.html HTTP/1.1
response body: text of b.html
POST /testmsg/b.html HTTP/1.1
request body has msg=hello
response body: not clear: may depend on web server or be b.html
Note: b.html does not know how to process the msg=hello information, so
all that happens is that the text of b.html gets returned again here. We
need a servlet or JSP (which compiles to a servlet) to actually process
form data.
4. (7) Maven and command line tools.
Answer: Maven projects share one repository (in ~/.m2) for downloaded jars, so once one project downloads a jar, other projects can use it without downloading.
Answer: mvn package runs the JUnit tests before building the main jar. In this case the failing JUnit test will fail the whole command, so no jar will be built.
javac JdbcCheckup.java
java -cp ojdbc6.jar:. JdbcCheckup
(12) System implementation. In pizza1
consider the service method listed here:
// return all orders for this
room, for today, in order by id
public List<PizzaOrderData>
getOrderStatus(int roomNumber) throws ServiceException {
List<PizzaOrder>
pizzaOrders = null;
List<PizzaOrderData>
pizzaOrders1 = new ArrayList<PizzaOrderData>();
try {
pizzaOrders =
pizzaOrderDAO.findOrdersByRoom(roomNumber,
adminDAO.findCurrentDay());
for
(PizzaOrder order: pizzaOrders) {
pizzaOrders1.add(new PizzaOrderData(order));
}
} catch (SQLException e) {
throw new
ServiceException("Error in getting status" + e, e);
}
return pizzaOrders1;
}
findCurrentDay (called first as arguments are evaluated)
findOrdersByRoom
It has a method parameter in its signature. A proper POJO getter is a
no-args method like int getId()
.
This is a layered system, with no upcalls, that is, calls from a lower layer to an upper layer. This service-layer code only calls down to the DAO and also to the domain object methods (plus library methods). The domain objects are all self-contained, or at worst, calling other related domain objects, and thus never calling any app methods outside of their package. The DAO never calls up the layers, so in particular not to the top layer, the presentation layer, which has all the UI code. Thus there is no way for execution to reach the presentation code while executing this method.
new PizzaOrderData(order);
Presentation
Answer. No, here we have a new
PizzaOrderData object and the only other object involved is order
,
a PizzaOrder object. But the PizzaOrderData object does not end up with
a ref to the PizzaOrder object. Instead, the various fields of the
PizzaOrder are just copied to the new PizzaOrderData object, and no
other object is created in the PizzaOrderData constructor. So this is
not an object dependency problem.