Write, document (internally) and test a Java program to do the following: A nati
ID: 3776882 • Letter: W
Question
Write, document (internally) and test a Java program to do the following:
A national company records the following information about each of its customers:
-name
-address
-customer number (9 digits)
-date (year) when the customer last placed an order
a. Define a class Customer to represent this information.
b. Create a singly-linked list of Customer objects. The information for each customer is to be input. Output the contents of the list after it has been created.
c Write a method which, given a customer number, returns the customer information. Output the customer information after it is returned.
d. Output the name and address of each customer who placed an order last year (2015).
e. Delete from the list those Customer records for which an order has not been placed in the last five years (i.e., 2012-2016).
Explanation / Answer
public class CustomerService {
public static final String DATA_STORE = "CustomerDATA.txt";
public static final Logger logger =
Logger.getLogger(CustomerService.class.getCanonicalName());
...
@POST
@Consumes({"application/xml", "application/json"})
public Response createCustomer(Customer customer) {
try {
long customerId = persist(customer);
return Response.created(URI.create("/" + customerId)).build();
} catch (Exception e) {
throw new WebApplicationException(e,
Response.Status.INTERNAL_SERVER_ERROR);
}
}
...
private long persist(Customer customer) throws IOException {
File dataFile = new File(DATA_STORE);
if (!dataFile.exists()) {
dataFile.createNewFile();
}
long customerId = customer.getId();
Address address = customer.getAddress();
Properties properties = new Properties();
properties.load(new FileInputStream(dataFile));
properties.setProperty(String.valueOf(customerId),
customer.getFirstname() + ","
+ customer.getLastname() + ","
+ address.getNumber() + ","
+ address.getStreet() + ","
+ address.getCity() + ","
+ address.getState() + ","
+ address.getZip() + ","
+ address.getCountry() + ","
+ customer.getEmail() + ","
+ customer.getPhone());
properties.store(new FileOutputStream(DATA_STORE),null);
return customerId;
}
...
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.