Question
LunchOrder The goal of this is to: Create an object which supports the Comparable, Serializable, and Cloneable interfaces. Be able to sort an ArrayList of
LunchOrder
The goal of this is to:
Create an object which supports the Comparable, Serializable, and Cloneable interfaces.
Be able to sort an ArrayList of objects implementing the Comparable interface.
Be able to clone LunchOrder objects and to understand the difference between a reference copy and a full copy.
Explore how to save and restore Objects to/from a file.
Create and use multiple Iterators.
ArrayList
Modify the ArrayList class in staticArray:
implement Iterable
private Iterator class
implement Serializable
add clear() : set all elements to null and reset the next field.
implement a sort() method
selection sort (Sorting method will fall later in the semester)
LunchOrder
Create a LunchOrder class with the following fields:
String name
int orderNumber
int number of burgers
int number of fries
int number of sodas
Date created; reference to a java.util.Date object.
static int nextOrderNumber
static int comparisonMethod
In addition, define constants for the cost of a burger (2.75), fries (.75), and soda (1.50).
Note: all fields should be defined as private or protected unless there is a good reason to do otherwise.
The LunchOrder will implement the following interfaces: Comparable, Cloneable, and Serializable.
We will be able to support sorting LunchOrders based on 3 different criteria: order number, customers name, and cost of the order. To do so, the LunchOrder will need the following constants to support different ways to compare LunchOrders:
based on ID
based on customers name
based on the cost of an order
LunchOrder methods
double computeOrderCost(): you will be passed two doubles for the tax and tip rates. Compute the cost based on the Cost per item, the number of items, the tax and tip based on the subtotal.
int compareTo(): LunchOrder objects will be compared based on the comparison indicator (ID, name, cost of order). This method is defined in the Comparable interface.
clone(): copy of item using the Objects clone method and then make of a copy of the Date object reference by the dateCreated field. This method is defined in the Cloneable interface.
toString(): display the name, id, number of items, order cost, and the date Created field.
a static method to display the header for the toString() method.
accessors where needed.
Restaurant application.
Modify Restaurant.java, this is the application code.
Complete the following test methods:
testIterators(): create 2 Iterators. Display the first 3 orders of the 1st iterator, then display all the orders of the 2nd iterator, and then display the rest of the elements with the 1st iterators. This is to show the independence of each iterator.
testCloneMethod(): first implement the LunchOrders clone method to just call the Objects clone method. After cloning a specific object, verify when you modify the Date field (using getTime() and setTime()), it changes both the original and cloned objects. Then modify the LunchOrders clone method so that it creates a new Date object with the same value of the original order. Verify when you modify the clones Date field, it does not change the original objects Date field.
In addition, you will modify code within the mainMenu() to:
save and restore all LunchOrder objects to a file called lunchOrder.dat
save and restore the ArrayList of LunchOrder objects to a filed called arrayLunchOrderObjects.dat.
Display of Application:
Menu
1 ..create new Array of LunchOrders
2 ..add 10 entries to the Array of LunchOrders
3 ..display array
4 ..sort on order#
5 ..sort on customer
6 ..sort on cost
7 ..save individual objects to a file
8 ..restore individual objects from a from file
9 ..save ArrayList object to a file
10 ..restore ArrayList object from a from file
11 ..test clone
12 ..test iterators
20 ..exit
1
Menu
1 ..create new Array of LunchOrders
2 ..add 10 entries to the Array of LunchOrders
3 ..display array
4 ..sort on order#
5 ..sort on customer
6 ..sort on cost
7 ..save individual objects to a file
8 ..restore individual objects from a from file
9 ..save ArrayList object to a file
10 ..restore ArrayList object from a from file
11 ..test clone
12 ..test iterators
20 ..exit
3
NOTE: We created 10 LunchOrders after we created the array. Each element is displayed via a call to the toString() method. The static method, getHdr() returns the corresponding header for the display.
customer order# #Burgs #Fries #Sodas cost
Jordan 100 9 7 9 52.20 Fri Oct 14 22:45:56 EDT 2016
Porter 101 1 5 2 11.40 Fri Oct 14 22:45:56 EDT 2016
Xung 102 5 6 0 21.90 Fri Oct 14 22:45:56 EDT 2016
Zeaoma 103 3 9 0 18.00 Fri Oct 14 22:45:56 EDT 2016
McDougal 104 7 2 1 26.70 Fri Oct 14 22:45:56 EDT 2016
Powell 105 4 1 7 26.70 Fri Oct 14 22:45:56 EDT 2016
Hayes 106 5 6 0 21.90 Fri Oct 14 22:45:56 EDT 2016
Xung 107 1 7 3 15.00 Fri Oct 14 22:45:56 EDT 2016
McDougal 108 3 2 2 15.30 Fri Oct 14 22:45:56 EDT 2016
McDougal 109 1 9 0 11.40 Fri Oct 14 22:45:56 EDT 2016
Menu
1 ..create new Array of LunchOrders
2 ..add 10 entries to the Array of LunchOrders
3 ..display array
4 ..sort on order#
5 ..sort on customer
6 ..sort on cost
7 ..save individual objects to a file
8 ..restore individual objects from a from file
9 ..save ArrayList object to a file
10 ..restore ArrayList object from a from file
11 ..test clone
12 ..test iterators
20 ..exit
5
Note: The LunchOrders static method, setCompareOrder(1), is called to compare orders based on the cutomers name.
customer order# #Burgs #Fries #Sodas cost
Hayes 106 5 6 0 21.90 Fri Oct 14 22:45:56 EDT 2016
Jordan 100 9 7 9 52.20 Fri Oct 14 22:45:56 EDT 2016
McDougal 104 7 2 1 26.70 Fri Oct 14 22:45:56 EDT 2016
McDougal 108 3 2 2 15.30 Fri Oct 14 22:45:56 EDT 2016
McDougal 109 1 9 0 11.40 Fri Oct 14 22:45:56 EDT 2016
Porter 101 1 5 2 11.40 Fri Oct 14 22:45:56 EDT 2016
Powell 105 4 1 7 26.70 Fri Oct 14 22:45:56 EDT 2016
Xung 102 5 6 0 21.90 Fri Oct 14 22:45:56 EDT 2016
Xung 107 1 7 3 15.00 Fri Oct 14 22:45:56 EDT 2016
Zeaoma 103 3 9 0 18.00 Fri Oct 14 22:45:56 EDT 2016
Menu
1 ..create new Array of LunchOrders
2 ..add 10 entries to the Array of LunchOrders
3 ..display array
4 ..sort on order#
5 ..sort on customer
6 ..sort on cost
7 ..save individual objects to a file
8 ..restore individual objects from a from file
9 ..save ArrayList object to a file
10 ..restore ArrayList object from a from file
11 ..test clone
12 ..test iterators
20 ..exit
6
Note: The LunchOrders static method, setCompareOrder(1), is called to compare orders based on the customers order cost.
customer order# #Burgs #Fries #Sodas cost
McDougal 109 1 9 0 11.40 Fri Oct 14 22:45:56 EDT 2016
Porter 101 1 5 2 11.40 Fri Oct 14 22:45:56 EDT 2016
Xung 107 1 7 3 15.00 Fri Oct 14 22:45:56 EDT 2016
McDougal 108 3 2 2 15.30 Fri Oct 14 22:45:56 EDT 2016
Zeaoma 103 3 9 0 18.00 Fri Oct 14 22:45:56 EDT 2016
Hayes 106 5 6 0 21.90 Fri Oct 14 22:45:56 EDT 2016
Xung 102 5 6 0 21.90 Fri Oct 14 22:45:56 EDT 2016
McDougal 104 7 2 1 26.70 Fri Oct 14 22:45:56 EDT 2016
Powell 105 4 1 7 26.70 Fri Oct 14 22:45:56 EDT 2016
Jordan 100 9 7 9 52.20 Fri Oct 14 22:45:56 EDT 2016
Menu
1 ..create new Array of LunchOrders
2 ..add 10 entries to the Array of LunchOrders
3 ..display array
4 ..sort on order#
5 ..sort on customer
6 ..sort on cost
7 ..save individual objects to a file
8 ..restore individual objects from a from file
9 ..save ArrayList object to a file
10 ..restore ArrayList object from a from file
11 ..test clone
12 ..test iterators
20 ..exit
4
Note: The LunchOrders static method, setCompareOrder(1), is called to compare orders based on the customers order number.
customer order# #Burgs #Fries #Sodas cost
Jordan 100 9 7 9 52.20 Fri Oct 14 22:45:56 EDT 2016
Porter 101 1 5 2 11.40 Fri Oct 14 22:45:56 EDT 2016
Xung 102 5 6 0 21.90 Fri Oct 14 22:45:56 EDT 2016
Zeaoma 103 3 9 0 18.00 Fri Oct 14 22:45:56 EDT 2016
McDougal 104 7 2 1 26.70 Fri Oct 14 22:45:56 EDT 2016
Powell 105 4 1 7 26.70 Fri Oct 14 22:45:56 EDT 2016
Hayes 106 5 6 0 21.90 Fri Oct 14 22:45:56 EDT 2016
Xung 107 1 7 3 15.00 Fri Oct 14 22:45:56 EDT 2016
McDougal 108 3 2 2 15.30 Fri Oct 14 22:45:56 EDT 2016
McDougal 109 1 9 0 11.40 Fri Oct 14 22:45:56 EDT 2016
Menu
1 ..create new Array of LunchOrders
2 ..add 10 entries to the Array of LunchOrders
3 ..display array
4 ..sort on order#
5 ..sort on customer
6 ..sort on cost
7 ..save individual objects to a file
8 ..restore individual objects from a from file
9 ..save ArrayList object to a file
10 ..restore ArrayList object from a from file
11 ..test clone
12 ..test iterators
20 ..exit
7
Note: All LunchOrders are saved via an enhanced for loop to a file named lunchOrder.dat.
Menu
1 ..create new Array of LunchOrders
2 ..add 10 entries to the Array of LunchOrders
3 ..display array
4 ..sort on order#
5 ..sort on customer
6 ..sort on cost
7 ..save individual objects to a file
8 ..restore individual objects from a from file
9 ..save ArrayList object to a file
10 ..restore ArrayList object from a from file
11 ..test clone
12 ..test iterators
20 ..exit
2
Note: additional entries are added to the ArrayList we want to modify the Array list before restoring it to the original contents we saved to a file.
Menu
1 ..create new Array of LunchOrders
2 ..add 10 entries to the Array of LunchOrders
3 ..display array
4 ..sort on order#
5 ..sort on customer
6 ..sort on cost
7 ..save individual objects to a file
8 ..restore individual objects from a from file
9 ..save ArrayList object to a file
10 ..restore ArrayList object from a from file
11 ..test clone
12 ..test iterators
20 ..exit
5
Note: ArrayList is sorted based on customers name
customer order# #Burgs #Fries #Sodas cost
Albertson 118 8 3 3 34.50 Fri Oct 14 22:47:35 EDT 2016
Brady 112 8 3 7 41.70 Fri Oct 14 22:47:35 EDT 2016
Grevey 113 7 2 0 24.90 Fri Oct 14 22:47:35 EDT 2016
Hayes 106 5 6 0 21.90 Fri Oct 14 22:45:56 EDT 2016
Hayes 110 8 2 6 39.00 Fri Oct 14 22:47:35 EDT 2016
Hayes 117 9 7 4 43.20 Fri Oct 14 22:47:35 EDT 2016
Jones 114 1 7 5 18.60 Fri Oct 14 22:47:35 EDT 2016
Jones 115 5 7 9 39.00 Fri Oct 14 22:47:35 EDT 2016
Jordan 100 9 7 9 52.20 Fri Oct 14 22:45:56 EDT 2016
McDonald 119 2 8 3 19.20 Fri Oct 14 22:47:35 EDT 2016
McDougal 104 7 2 1 26.70 Fri Oct 14 22:45:56 EDT 2016
McDougal 108 3 2 2 15.30 Fri Oct 14 22:45:56 EDT 2016
McDougal 109 1 9 0 11.40 Fri Oct 14 22:45:56 EDT 2016
Porter 101 1 5 2 11.40 Fri Oct 14 22:45:56 EDT 2016
Powell 105 4 1 7 26.70 Fri Oct 14 22:45:56 EDT 2016
Smith 116 8 1 7 39.90 Fri Oct 14 22:47:35 EDT 2016
Walton 111 7 8 9 46.50 Fri Oct 14 22:47:35 EDT 2016
Xung 102 5 6 0 21.90 Fri Oct 14 22:45:56 EDT 2016
Xung 107 1 7 3 15.00 Fri Oct 14 22:45:56 EDT 2016
Zeaoma 103 3 9 0 18.00 Fri Oct 14 22:45:56 EDT 2016
Menu
1 ..create new Array of LunchOrders
2 ..add 10 entries to the Array of LunchOrders
3 ..display array
4 ..sort on order#
5 ..sort on customer
6 ..sort on cost
7 ..save individual objects to a file
8 ..restore individual objects from a from file
9 ..save ArrayList object to a file
10 ..restore ArrayList object from a from file
11 ..test clone
12 ..test iterators
20 ..exit
8
ArrayList is restored from a file, therefore we go back to the original 10 items we saved in the order that they were in (based on order number).
Note: you need to repeat the save/restore saving the ArrayList as an object. 1 call to writeObject, as opposed to 10 calls above.
Menu
1 ..create new Array of LunchOrders
2 ..add 10 entries to the Array of LunchOrders
3 ..display array
4 ..sort on order#
5 ..sort on customer
6 ..sort on cost
7 ..save individual objects to a file
8 ..restore individual objects from a from file
9 ..save ArrayList object to a file
10 ..restore ArrayList object from a from file
11 ..test clone
12 ..test iterators
20 ..exit
3
customer order# #Burgs #Fries #Sodas cost
Jordan 100 9 7 9 52.20 Fri Oct 14 22:45:56 EDT 2016
Porter 101 1 5 2 11.40 Fri Oct 14 22:45:56 EDT 2016
Xung 102 5 6 0 21.90 Fri Oct 14 22:45:56 EDT 2016
Zeaoma 103 3 9 0 18.00 Fri Oct 14 22:45:56 EDT 2016
McDougal 104 7 2 1 26.70 Fri Oct 14 22:45:56 EDT 2016
Powell 105 4 1 7 26.70 Fri Oct 14 22:45:56 EDT 2016
Hayes 106 5 6 0 21.90 Fri Oct 14 22:45:56 EDT 2016
Xung 107 1 7 3 15.00 Fri Oct 14 22:45:56 EDT 2016
McDougal 108 3 2 2 15.30 Fri Oct 14 22:45:56 EDT 2016
McDougal 109 1 9 0 11.40 Fri Oct 14 22:45:56 EDT 2016
Menu
1 ..create new Array of LunchOrders
2 ..add 10 entries to the Array of LunchOrders
3 ..display array
4 ..sort on order#
5 ..sort on customer
6 ..sort on cost
7 ..save individual objects to a file
8 ..restore individual objects from a from file
9 ..save ArrayList object to a file
10 ..restore ArrayList object from a from file
11 ..test clone
12 ..test iterators
20 ..exit
11
Note: The 1st element of the ArrayList is cloned and displayed. Then modify the clone method so that a new Date object is created in the clone method. When the cloned objects dateCreated object is modified, the original objects field is not modified.
get(0) clone: Jordan 100 9 7 9 52.20 Fri Oct 14 22:45:56 EDT 2016
get(0) order1: Jordan 100 9 7 9 52.20 Fri Oct 14 22:45:56 EDT 2016
get(0) order2: Jordan 100 9 7 9 52.20 Thu Oct 13 22:45:56 EDT 2016
Menu
1 ..create new Array of LunchOrders
2 ..add 10 entries to the Array of LunchOrders
3 ..display array
4 ..sort on order#
5 ..sort on customer
6 ..sort on cost
7 ..save individual objects to a file
8 ..restore individual objects from a from file
9 ..save ArrayList object to a file
10 ..restore ArrayList object from a from file
11 ..test clone
12 ..test iterators
20 ..exit
12
Note: 2 Iterators are created. We display the first 3 elements of the first Iterator, then we display all items of the 2nd iterator, and finally, we display the rest of the elements of the 1st iterator. The Iterators are independent of each other.
Jordan 100 9 7 9 52.20 Fri Oct 14 22:45:56 EDT 2016
Porter 101 1 5 2 11.40 Fri Oct 14 22:45:56 EDT 2016
Xung 102 5 6 0 21.90 Fri Oct 14 22:45:56 EDT 2016
-------------------
Jordan 100 9 7 9 52.20 Fri Oct 14 22:45:56 EDT 2016
Porter 101 1 5 2 11.40 Fri Oct 14 22:45:56 EDT 2016
Zeaoma 103 3 9 0 18.00 Fri Oct 14 22:45:56 EDT 2016
McDougal 104 7 2 1 26.70 Fri Oct 14 22:45:56 EDT 2016
Powell 105 4 1 7 26.70 Fri Oct 14 22:45:56 EDT 2016
Hayes 106 5 6 0 21.90 Fri Oct 14 22:45:56 EDT 2016
Xung 107 1 7 3 15.00 Fri Oct 14 22:45:56 EDT 2016
McDougal 108 3 2 2 15.30 Fri Oct 14 22:45:56 EDT 2016
McDougal 109 1 9 0 11.40 Fri Oct 14 22:45:56 EDT 2016
-------------------
Zeaoma 103 3 9 0 18.00 Fri Oct 14 22:45:56 EDT 2016
McDougal 104 7 2 1 26.70 Fri Oct 14 22:45:56 EDT 2016
Powell 105 4 1 7 26.70 Fri Oct 14 22:45:56 EDT 2016
Hayes 106 5 6 0 21.90 Fri Oct 14 22:45:56 EDT 2016
Xung 107 1 7 3 15.00 Fri Oct 14 22:45:56 EDT 2016
McDougal 108 3 2 2 15.30 Fri Oct 14 22:45:56 EDT 2016
McDougal 109 1 9 0 11.40 Fri Oct 14 22:45:56 EDT 2016
Menu
1 ..create new Array of LunchOrders
2 ..add 10 entries to the Array of LunchOrders
3 ..display array
4 ..sort on order#
5 ..sort on customer
6 ..sort on cost
7 ..save individual objects to a file
8 ..restore individual objects from a from file
9 ..save ArrayList object to a file
10 ..restore ArrayList object from a from file
11 ..test clone
12 ..test iterators
20 ..exit
20
LunchOrder.java
import java.io.Serializable; import java.util.Date; public class LunchOrder implements Comparable, Cloneable, Serializable { static final double COST_BURGERS = 2.75; static final double COST_FRIES = .75; static final double COST_SODAS = 1.50; private int numBurgers; private int numFries; private int numSodas; private String customer; private int orderNum; private Date dateCreated; static int nextOrderNum = 100; //3 ways to sort LunchOrders: by orderNum, customer, and cost private static int sortOrder = 0; static final int SORT_ID = 0; static final int SORT_CUSTOMER = 1; static final int SORT_COST = 2; public LunchOrder(String customer, int numBurgers, int numFries, int numSodas) { super(); this.customer = customer; this.numBurgers = numBurgers; this.numFries = numFries; this.numSodas = numSodas; orderNum = nextOrderNum++; dateCreated = new Date(); } public int compareTo(Object order) { //todo } public static void setCompareOrder( int num ) { sortOrder = num; } public Object clone() throws CloneNotSupportedException { //todo } public double computeOrderCost( double tax, double tip) { double subtotal = numBurgers * COST_BURGERS + numFries * COST_FRIES + numSodas * COST_SODAS; double orderTip = subtotal * tip; double orderTax = subtotal * tax; return subtotal + orderTip + orderTax; } public Date getDateCreated() { return dateCreated; } public static String getHdr() { String str = String.format(" %10s %6s %6s %6s %6s %6s", "customer", "order#", "#Burgs", "#Fries", "#Sodas", "cost"); return str; } public String toString() { String str = String.format(" %10s %6d %6d %6d %6d %6.2f %s", customer, orderNum, numBurgers, numFries, numSodas, computeOrderCost(.05, .15), dateCreated); return str; } } |
Restaurant.java
import staticArray.ArrayList; import java.util.Date; import java.util.Scanner; import java.io.*; public class Restaurant { static String [] names = {"Jones", "Smith", "Brady", "Albertson", "McDougal", "McDonald", "Adams", "Billington", "Grevey", "Hayes", "Porter", "Walton", "Woods", "Jordan", "Powell", "Zeaoma", "Xung" }; static ArrayList |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started