In JAVA: You have been asked to create a customer service program for a new phon
ID: 3805007 • Letter: I
Question
In JAVA: You have been asked to create a customer service program for a new phone store that will be opening soon. Since this company anticipates being the agent for a rising new product that will be able to host the most popular video games always released at midnight on the eve of the anticipated announcement date they want a customer service module that will help them manage the customers as they arrive. Customers will be added to the queue as they arrive, removed once they have been served. Sometimes the queue will be search to determine who is there. As a prototype for this system, you are to use the linked list created in Assignment 3.1 to create a queue class and a test program that can handle this new system. The queue class must include a method to insert elements into the queue, remove elements from the queue, look at the first and last element of the queue without removing the elements from the queue, and search for an element in the queue. Use the following algorithm to simulate a simple version of the new system. 1.Choose a random integer between 1 and 5 to determine the minutes at which a customer arrives. 2.When a customer arrives, choose a random integer between 1 and 5 to determine the number of minutes that the customer must remain in the checkout line. 3.Repeat the two steps for a 12 hour (720 minute) simulation. 4.Run the simulation again with a random integer of 1 and 3 to compare the number of customers in the checkout line against the original 12 hour simulation.
Explanation / Answer
Customer Service Program
@Path("/Customer")
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")
public Response createCustomer(CustomerType 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(CustomerType customer) throws
IOException {
File dataFile = new File(DATA_STORE);
if (!dataFile.exists()) {
dataFile.createNewFile();}
long customerId = customer.getId();
Properties properties = new Properties();
properties.load(new FileInputStream(dataFile));
properties.setProperty(String.valueOf(customerId),
customer.getFirstName() + "," + customer.getLastName() + "," + customer.getCity() + "," + customer.getCountry());
properties.store(new FileOutputStream(DATA_STORE),null);
return customerId;
}
The CustomerService class has a createCustomer method that creates a customer resource
based on CustomerType and returns a URI for the new resource. The persist method emulates
the behavior of the JPA entity manager. This example uses a java.util.Properties file to store
data. If you are using the default configuration of GlassFish, you should see this properties file
created under /as-install/glassfish/domains/domain1/CustomerDATA.txt
The response that is returned to the client has a URI to the newly created resource. The return
type is an entity body mapped from the property of the response with the status code specified
by the status property of the response. The WebApplicationException is a RuntimeException
that is used to wrap the appropriate HTTP error status code, such as 404, 406, 415, or 500.
The @Consumes (“application/xml”) and @Produces (“application/xml”) annotations set the
request and response media types to use the appropriate MIME client. These annotations can
be applied to a resource method, a resource class, or even to an entity provider. If you do not use
these annotations, JAX-RS uses the default media type, (?*/*?).
The following code snippet shows the implementation of the following methods: getCustomer
and findbyId. The getCustomer method uses the @Produces annotation and returns a
JAXBElement with a CustomerType object, which is a JAXB XML based entity, generated
through the xjc binding compiler.
@GET
@Path("{id}")
@Produces("application/xml")
public JAXBElement<CustomerType>
getCustomer(@PathParam("id") String customerId)
{
CustomerType customer = null;
try {
customer = findById(customerId);
}
catch (Exception ex) {
logger.log(Level.SEVERE,
"Error calling searchCustomer() for customerId {0}. {1}", new Object[]{customerId, ex.getMessage()});
}
return new ObjectFactory().createCustomer(customer);
}
private CustomerType findById(String customerId) throws IOException {
properties properties = new Properties();
properties.load(new FileInputStream(DATA_STORE));
String rawData = properties.getProperty(customerId);
if (rawData != null) {
final String[] field = rawData.split(",");
ObjectFactory objFactory = new ObjectFactory();
CustomerType customer = objFactory.createCustomerType();
customer.setFirstName(field[0]);
customer.setLastName(field[1]);
customer.setCity(field[2]);
customer.setCountry(field[3]);
customer.setId(Integer.parseInt(customerId));
return customer;
}
return null;
}
elementFormDefault="qualified"
xmlns:ora="http://examples.oracle.com"> <xs:element name="customer" type="ora:CustomerType"/>
<xs:complexType name="CustomerType">
<xs:sequence>
<xs:element name="id" type="xs:int"/>
<xs:element name="firstName" type="xs:string"/>
<xs:element name="lastName" type="xs:string"/>
<xs:element name="email" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.