100% Guaranteed Results


LAB 03: PROGRAMMING 2 Solved
$ 24.99
Category:

Description

5/5 – (1 vote)

Lab 03: AppointmentApp

In this lab, you will:

• Work with and complete an ‘Appointment’ application.
• Further to lab 2, you will work with more examples of file-based data persistence.
• Add and cancel appointment, as the program user, and store them back to a text file.
• Extend a class by implementing a subclass of Appointment (PromoAppointment)

… which will result in the following outcomes:

• Further practice on the use of Readers and Writers.
• Practice the use of java time classes (LocalTime).
• Knowledge of the use of ArrayLists as field variables.
• Knowledge of subclassing and adapting a controller type to determine at run time the kind of Appointment object created.

Table of Contents
1.1 Preliminary 3
1.2 Exercise 1 3
1.3 Exercise 2 4
1.4 Exercise 3 4

Figure 1: AppointmentAppStarter ………………………………………………………………………………… 3

1.1 Preliminary

• Download the lab 3 zip file <Lab3_AppointmentAppStarter.zip> from GCULearn and unzip it.

• Open the Lab3_AppointmentAppStarter project in NetBeans:

Figure 1: AppointmentAppStarter

1.2 Exercise 1

a. Open the Appointment class, in the model folder, and note the attributes:
customerName, appointmentTime and appointmentLength. Constructor, getter and setter methods are defined as well as the overridden method toString().
Note, times are specified as LocalTime objects.

b. Now open the appointments20161001.txt file and note the format:
“Lynn Kelly”,”10:00″,30
“Ciara O’Donnell”,”11:15″,45
“Louise Kelly”,”13:15″,60

private void loadAppointmentsFromTextFile(String fileName) { char DELIMITER=’,’; try (BufferedReader br =
new BufferedReader(new FileReader(fileName))) {

String[] temp;
String line = br.readLine();
temp=line.split(Character.toString(DELIMITER)); customerName = stripQuotes(temp[0]); localTimeStr = stripQuotes(temp[1]); appointmentTime = LocalTime.parse(localTimeStr); appointmentLength = Integer.parseInt(temp[2]);

A stream is opened to the text file and the readLine() method used to read a line from the text file into a String variable called line. This string is then split up an array of Strings – temp – using the split() method. Each element of the array holds a piece of text which has been delimited using, in this case, a comma – CSV file. Each element is accessed in turn, and, where necessary, converted into a variable of an appropriate type, e.g. int, LocalTime etc. which are then used to create an Appointment object.

Appointment appointment = new Appointment(customerName, appointmentTime, appointmentLength);

The appointment is then added to the appointments ArrayList object.

c. Run the AppointmentApp class and test that the appointments can be successfully loaded and displayed. Add appointments and cancel appointments and store them back to a text file. Ensure you can reload the file correctly.
1.3 Exercise 2
We are now going to extend the app to include persistence using object streams.
a. Open the AppointmentController class and implement the loadAppointmentsFromObjectFile() method using a similar approach to
Lab 2.

b. Implement the storeAppointmentsToObjectFile() method using a similar approach to Lab 2.

b. Run the AppointmentApp class and test that a new appointments file can be created and stored as an object file and then successfully loaded and displayed.
1.4 Exercise 3
We are now going to implement Promo Appointments which are specialized versions of Appointment which add a string attribute promoCode and an integer discountPercentage.

a. Implement the PromoAppointment subclass making the necessary changes to the Appointment class.

b. Adjust the controller class method loadAppointmentsFromTextFile() to be able to determine when a PromoAppointment object should be created rather than an Appointment object.

c. Adjust the controller method addAppointment() to ask the user if a
PromoAppointment is required, and, if so, ask for the promo code and discount percentage, create a PromoAppointment object and add it to the appointments attribute.

d. Run the AppointmentApp class and test you can load and store appointments, including promo appointments, from both text files and object files; and, add and cancel appointments of both types.

Reviews

There are no reviews yet.

Be the first to review “LAB 03: PROGRAMMING 2 Solved”

Your email address will not be published. Required fields are marked *

Related products