Derive a SortedArrayList
that the items of a sorted ArrayList are sorted in ascending order. This subclass of the
ArrayList
You only need to provide your new insertion method in the SortedArrayList
not need to consider the other methods from the ArrayList
You are asked to write a program to help a Holiday Resort ticket office clerk in their work. The
Holiday Resort offers various activities to its customers. Customers can book tickets to take
part in activities, but the number of tickets available for each activity is limited.
Submission Notes
You should submit a .zip file containing four Java files and three text files as follows:
• SortedArrayList.java, which contains your SortedArrayList class for Task 1.
• Activity.java, which contains your Activity class for Task 2.
• Customer.java, which contains your Customer class for Task 2.
• MainProgram.java, which contains your MainProgram driver class for Task 2; your main()
method should be in this class.
• input.txt, which is the input file for your program (this can be the same as the provided
input.txt file; your program should only read this file in, not change it).
• clerk.txt should contain a record of the clerk’s interactions with the program displayed in
the Terminal Window (both what the clerk inputs and what your program prints out; copy
and paste this from the Terminal Window in IntelliJ; your program should not write to this
file).
• letters.txt should be the output file created by your program, which contains the letters to
customers from when there are not enough tickets available.
Your text files should show that you have tested your program.
Aims
• To gain experience in designing an interactive system of practical importance.
• To reinforce your knowledge about the standard java.util.ArrayList class.
• To gain experience at using the java.lang.Comparable interface, inheritance and generic
classes in Java.
Background
Sometimes we want to keep the items in a list in sorted order, which we can achieve with a
sorted list. The fundamental difference between a sorted list and an unsorted one is the
“adding an item”/insertion method. Having a definition of a class for lists, we can obtain a
sorted list class by altering the existing one or adding a new insertion method.
Task 1
Derive a SortedArrayList class as a subclass of the java.util.ArrayList class in such a way
that the items of a sorted ArrayList are sorted in ascending order. This subclass of the
ArrayList class will be needed to complete Task 2 (see Additional Assumptions (2) below).
You only need to provide your new insertion method in the SortedArrayList class. You do
not need to consider the other methods from the ArrayList class that can modify the list.
Task 2
You are asked to write a program to help a Holiday Resort ticket office clerk in their work. The
Holiday Resort offers various activities to its customers. Customers can book tickets to take
part in activities, but the number of tickets available for each activity is limited. Your program
should read a list of available activities and a list of registered customers from a file. The
content of the input file should have the following form: The first line contains an integer
representing the number of available activities, and is followed by the information about the
activities (two lines for every activity: one line containing the name of the activity and the
second one containing the number of tickets available for this activity). The next line contains
an integer representing the number of registered customers, followed by the information
about the customers (one line for every customer with their first name and surname).
Example file content is given below (same as the input.txt file available from Canvas), but your
program should run correctly on any file in the correct format:
6
Hiking
4
Bowling
8
Cycling
10
Sailing
3
Football
22
Table Tennis
4
3
Michael Smith
Anna Smith
Ted Jones
The program should be able to store information about activity and customers:
1. For each activity, the information required is: the name of the activity and the number of
tickets left for this activity.
2. For each customer, the office should know their first name, surname and the chosen
activity together with the number of tickets bought for each of the activities. A customer can
hold tickets for at most three different activities at a time. We also assume that no two
customers share both their first name and their surname.
After the initial information has been read from the file, the clerk will work with the program
interactively. The program should display a menu on the screen offering a choice of possible
operations, each represented by a lower case letter:
• f – to finish running the program.
• a – to display on the screen information about all the activities.
• c – to display on the screen information about all the customers.
• t – to update the stored data when tickets are bought by one of the registered customers.
• r – to update the stored data when a registered customer cancels tickets for a booking.
You should implement all the above operations.
Additional Assumptions
1. When f is selected, the program stops running and all the data is lost. The program could
be extended to save all the data to a file, but this is not part of the project and you must not
do this!
2. To store activities and customers you should use your SortedArrayList class. Activities
should be sorted in the ascending (lexicographic) order of names of activities. Customers
should be sorted in the ascending (lexicographic) order of their surnames. If two customers
have the same surname, then their first names should decide their order. You may assume
that each customer has exactly one first name and exactly one surname.
3. When tickets for an activity are ordered by a customer, your program must check whether
the customer is a valid customer, and that the activity is on the list of the available activities.
If not, an appropriate message should be displayed on the screen. If the request is a valid one,
the program should check whether there are enough tickets left for the ordered activity. You
may assume that in one transaction a customer orders tickets for one activity. If the tickets
are not available (or there are not enough tickets), a note (in the form of a letter) should be
printed to a file informing the customer that the tickets are not available; no other errors
should be logged to letters.txt. You may assume that the order is either fully satisfied or not
carried out at all. If the ordered number of tickets is available, the transaction should be
processed and the stored information should be updated accordingly.
4. When tickets (a ticket) are (is) cancelled by a customer, your program must check whether
the customer is a valid customer, whether the activity specified by the tickets is on the list of
activities and whether the tickets have been purchased by this customer. If not, an
appropriate message should be displayed on the screen. If the request is a valid one, the
stored information should be updated accordingly. You may assume that a customer can
cancel tickets for only one activity per one transaction. If a customer has tickets for an activity,
they should be able to cancel one, some or all of their tickets for that activity in a single
transaction (your program should ask them how many tickets they want to cancel).
5. We assume that the ticket office only sells the tickets initially allocated to it, serves only its
registered customers and processes transactions sequentially, one after the other.
6. When the program first starts, you should assume that no tickets have been sold so far.
Marking Scheme
The total number of marks which can be awarded for this coursework is 50. Marks will be
awarded as follows:
• Activity, Customer, SortedArrayList classes: 15 marks
• Input and Output: 6 marks
• Implementation of operations: 15 marks
• Readability of code: 8 marks
• Evidence of testing: 6 marks
Guidance Notes for the Assessed Coursework
Task 1 of the specification says that you need to develop a SortedArrayList class as a subclass of the standard library class ArrayList from the java.util package. Observe that your
SortedArrayList class should be implemented as a generic class that uses a type variable
E. This class should contain only one method for inserting a new element into a sorted array
list. This insertion method must not use the Collections.sort() method or any other built-in
Java sorting method. Furthermore, this method should be a non-static method and therefore
work as an operation on sorted array lists. We know that a “future” sorted array list, on which
this method will be invoked, is represented inside the method by the this reference variable.
Into this sorted array list, the method should to insert a new element of type E in the right
place, so the ascending order of the elements in the list is preserved. You need to think
carefully about the heading of your SortedArrayList class. Will it work with any type
substituted for E, or should its type variable be constrained in any way? You should be able
to find some clues to help you answer this question by looking at slide 41 of the second set of
lecture notes. (If you cannot get Task 1 to work without using a built-in sorting method then
do so. You’ll lose some marks for doing this, but can still get marks for the rest of the
coursework. If you can’t get Task 1 to work at all, then use an ArrayList for Task 2, rather than
a SortedArrayList. Again, you’ll lose some marks for doing this, but can still get marks for the
rest of the coursework.)
Task 2 of the specification indicates that a solution to this coursework should include two
further classes: one for activity objects and the second one for customer objects. Each of
these classes should work as a new type for activity objects and customer objects. Each of
these classes should have fields, constructor(s) and methods (non-static methods as they
should work as operations on objects of a given type/class). You need to decide first on the
fields of each class: the attributes of objects of a given class. The clues for the fields for both
classes can be found at the top of page 3 of the specification (see points 1 and 2). Then, you
need to decide on types of your fields. Sometimes the types are obvious. For example, the
field to store the name of an activity should be of String type. However, for some other fields,
you might need to make a choice between a few possible types. Further important
information for implementing the classes for activities and customers can be found in the
point 2 of the “Additional assumptions” on page 3 of the specification, where it is mentioned
that these objects should be stored in sorted array lists. This point also explains how the
objects of both classes should be ordered. You are advised to look at slide 38 in the second
set of lecture notes to see an example of a class whose objects can be ordered (the Person
class). Its compareTo method defines how the Person objects are ordered.
Apart from these three classes, your program should contain a driver class, the class that does
not work as a new type for objects in your program, but provides a framework for your static
main method representing your sequential program that you will run at the testing stage of
this project. For some ideas on how to structure your main method, look at the first set of
lecture notes at the interactive program about a sports club (slides 31–49). Your experience
of developing a solution for the Formative Coursework should also be helpful here.
You might decide to have additional classes in your program, but this is up to you. What about
having a TicketOffice class, for example? Would this help with your overall design? As a
software engineer you need to make such decisions. In the first instance, your solution should
satisfy the specification. However, this shouldn’t be your only concern. You want your
program to be readable and maintainable. So, its overall structure is important.
Reading Marking Scheme provided at the end of the coursework specification, you can see
that up to 8 marks will be assigned for readability of code. Make sure to use meaningful names
for fields, variables, classes etc. as appropriate and include appropriate comments and
javadoc entries.
Reading the Marking Scheme provided at the end of the coursework specification, you can
see that up to 6 marks out of 50 will be assigned for the input and output operations. You
have learned how to use I/O files in Java programs by doing the Formative Coursework. The
specification of the Assessed Coursework tells you that your program should use input and
output files. On the second page of the specification there is an example content of the input
file (also downloadable from Canvas). You are advised to use it for your program. It follows
from the specification that the lists of customers and activities won’t change after the input
file has been read by the program (new customers or activities won’t be added; no activities
or customers will be removed). Furthermore, as an interactive program, it should retrieve the
information that the clerk will type on the keyboard and display some of the information on
the computer screen for the clerk to see, when they are talking to the customers booking and
cancelling tickets for activities. The customers will need to tell the clerk their names and
surnames and the details of the activities they want to buy tickets for/cancel. The clerk will
be entering these details by using the keyboard. See the descriptions of the t and r operations
in points 3 and 4 of the “Additional assumptions” on page 3 of the specification. In the
Submission Notes, on page 1 of the specification, it is written that you should submit “the
output file created by your program”. Most information produced by the program should be
displayed on the computer’s screen. The only information that will be directed to the
program’s letters.txt output file is described in point 3 of the “Additional assumptions”: “If
the tickets are not available (or there are not enough tickets), a note (in the form of a letter)
should be printed to a file informing the customer that the tickets are not available; no other
errors should be logged to letters.txt.” If you create only one object of the PrintWriter type in
your program, all such notes will be appended to the same output file, which will contain the
full record of printed notes at the end of the program’s execution. This record should be
consistent with the content of the second required text file containing the record of clerk’s
interactions with the program displayed in the Terminal Window/Console. These two text
files will be assessed in the “Evidence of testing” section of the Marking Scheme. You will test
your program by running the main method of your program.
6
Hiking
4
Bowling
8
Cycling
10
Sailing
3
Football
22
Table Tennis
4
3
Michael Smith
Anna Smith
Ted Jones
Submission Notes
You should submit a .zip file containing four Java files and three text files as follows:
• SortedArrayList.java, which contains your SortedArrayList class for Task 1.
• Activity.java, which contains your Activity class for Task 2.
• Customer.java, which contains your Customer class for Task 2.
• MainProgram.java, which contains your MainProgram driver class for Task 2; your main()
method should be in this class.
• input.txt, which is the input file for your program (this can be the same as the provided
input.txt file; your program should only read this file in, not change it).
• clerk.txt should contain a record of the clerk’s interactions with the program displayed in
the Terminal Window (both what the clerk inputs and what your program prints out; copy
and paste this from the Terminal Window in IntelliJ; your program should not write to this
file).
• letters.txt should be the output file created by your program, which contains the letters to
customers from when there are not enough tickets available.
Your text files should show that you have tested your program.
Aims
• To gain experience in designing an interactive system of practical importance.
• To reinforce your knowledge about the standard java.util.ArrayList class.
• To gain experience at using the java.lang.Comparable interface, inheritance and generic
classes in Java.
Background
Sometimes we want to keep the items in a list in sorted order, which we can achieve with a
sorted list. The fundamental difference between a sorted list and an unsorted one is the
“adding an item”/insertion method. Having a definition of a class for lists, we can obtain a
sorted list class by altering the existing one or adding a new insertion method.
Task 1
Derive a SortedArrayList class as a subclass of the java.util.ArrayList class in such a way
that the items of a sorted ArrayList are sorted in ascending order. This subclass of the
ArrayList class will be needed to complete Task 2 (see Additional Assumptions (2) below).
You only need to provide your new insertion method in the SortedArrayList class. You do
not need to consider the other methods from the ArrayList class that can modify the list.
Task 2
You are asked to write a program to help a Holiday Resort ticket office clerk in their work. The
Holiday Resort offers various activities to its customers. Customers can book tickets to take
part in activities, but the number of tickets available for each activity is limited. Your program
should read a list of available activities and a list of registered customers from a file. The
content of the input file should have the following form: The first line contains an integer
representing the number of available activities, and is followed by the information about the
activities (two lines for every activity: one line containing the name of the activity and the
second one containing the number of tickets available for this activity). The next line contains
an integer representing the number of registered customers, followed by the information
about the customers (one line for every customer with their first name and surname).
Example file content is given below (same as the input.txt file available from Canvas), but your
program should run correctly on any file in the correct format:
6
Hiking
4
Bowling
8
Cycling
10
Sailing
3
Football
22
Table Tennis
4
3
Michael Smith
Anna Smith
Ted Jones
The program should be able to store information about activity and customers:
1. For each activity, the information required is: the name of the activity and the number of
tickets left for this activity.
2. For each customer, the office should know their first name, surname and the chosen
activity together with the number of tickets bought for each of the activities. A customer can
hold tickets for at most three different activities at a time. We also assume that no two
customers share both their first name and their surname.
After the initial information has been read from the file, the clerk will work with the program
interactively. The program should display a menu on the screen offering a choice of possible
operations, each represented by a lower case letter:
• f – to finish running the program.
• a – to display on the screen information about all the activities.
• c – to display on the screen information about all the customers.
• t – to update the stored data when tickets are bought by one of the registered customers.
• r – to update the stored data when a registered customer cancels tickets for a booking.
You should implement all the above operations.
Additional Assumptions
1. When f is selected, the program stops running and all the data is lost. The program could
be extended to save all the data to a file, but this is not part of the project and you must not
do this!
2. To store activities and customers you should use your SortedArrayList class. Activities
should be sorted in the ascending (lexicographic) order of names of activities. Customers
should be sorted in the ascending (lexicographic) order of their surnames. If two customers
have the same surname, then their first names should decide their order. You may assume
that each customer has exactly one first name and exactly one surname.
3. When tickets for an activity are ordered by a customer, your program must check whether
the customer is a valid customer, and that the activity is on the list of the available activities.
If not, an appropriate message should be displayed on the screen. If the request is a valid one,
the program should check whether there are enough tickets left for the ordered activity. You
may assume that in one transaction a customer orders tickets for one activity. If the tickets
are not available (or there are not enough tickets), a note (in the form of a letter) should be
printed to a file informing the customer that the tickets are not available; no other errors
should be logged to letters.txt. You may assume that the order is either fully satisfied or not
carried out at all. If the ordered number of tickets is available, the transaction should be
processed and the stored information should be updated accordingly.
4. When tickets (a ticket) are (is) cancelled by a customer, your program must check whether
the customer is a valid customer, whether the activity specified by the tickets is on the list of
activities and whether the tickets have been purchased by this customer. If not, an
appropriate message should be displayed on the screen. If the request is a valid one, the
stored information should be updated accordingly. You may assume that a customer can
cancel tickets for only one activity per one transaction. If a customer has tickets for an activity,
they should be able to cancel one, some or all of their tickets for that activity in a single
transaction (your program should ask them how many tickets they want to cancel).
5. We assume that the ticket office only sells the tickets initially allocated to it, serves only its
registered customers and processes transactions sequentially, one after the other.
6. When the program first starts, you should assume that no tickets have been sold so far.
Marking Scheme
The total number of marks which can be awarded for this coursework is 50. Marks will be
awarded as follows:
• Activity, Customer, SortedArrayList classes: 15 marks
• Input and Output: 6 marks
• Implementation of operations: 15 marks
• Readability of code: 8 marks
• Evidence of testing: 6 marks