Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The goal of this lab is to: • Create an object which supports the Comparable, Se

ID: 3726012 • Letter: T

Question

The goal of this lab 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, customer’s 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 customer’s 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 Object’s 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 LunchOrder’s clone method to just call the Object’s 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 LunchOrder’s clone method so that it creates a new Date object with the same value of the original order. Verify when you modify the clone’s Date field, it does not change the original object’s 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 LunchOrder’s static method, setCompareOrder(1), is called to compare orders based on the cutomer’s 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 LunchOrder’s static method, setCompareOrder(1), is called to compare orders based on the customer’s 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 LunchOrder’s static method, setCompareOrder(1), is called to compare orders based on the customer’s 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 customer’s 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 object’s dateCreated object is modified, the original object’s 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

Explanation / Answer

import java.io.Serializable;

import java.util.Date;

public class LunchOrder implements Comparable,Cloneable,Serializable {

private static final long serialVersionUID = 1L;

int orderNumber;

String name;

int numberOfBurgers;

int numberOfFries;

int numberOfSodas;

Date dateCreated;

double orderCost;

static int OrderType;

public static void setCompareOrder(int value){

OrderType = value;

}

static int nextOrderNumber;

public LunchOrder(int aOrderNumber, String aName, int aNumberOfBurgers, int aNumberOfFries, int aNumberOfSodas,

Date aDateCreated) {

super();

orderNumber = aOrderNumber;

name = aName;

numberOfBurgers = aNumberOfBurgers;

numberOfFries = aNumberOfFries;

numberOfSodas = aNumberOfSodas;

dateCreated = aDateCreated;

}

private static final double COST_OF_BURGER = 2.75;

private static final double COST_OF_FRIES = 0.75;

private static final double COST_OF_SODA = 1.5;

@Override

public int compareTo(Object obj) {

if(OrderType==1){

Integer i1 = ((LunchOrder)obj).getOrderNumber();

Integer i2 = this.getOrderNumber();

return i1.compareTo(i2);

}

else

if(OrderType==2){

String i1 = ((LunchOrder)obj).getName();

String i2 = this.getName();

return i1.compareTo(i2);

}

if(OrderType==3){

Double i1 = ((LunchOrder)obj).getOrderCost();

Double i2 = this.getOrderCost();

return i1.compareTo(i2);

}

return 0;

}

double computeOrderCost(double tax,double tips){

double total = (numberOfBurgers*COST_OF_BURGER)+(numberOfFries*COST_OF_FRIES)+(numberOfSodas*COST_OF_SODA);

total = (total *(tax/100))+total;

total+=tips;

return total;

}

public void basedOnID(){

}

public void basedOnCustmerName(){

}

public void basedOnCostOrOrder(){

}

@Override

public String toString() {

return " "+getName()+" "+getOrderNumber()+" "+getNumberOfBurgers()+" "+getNumberOfFries()+" "+getNumberOfSodas()+" "+getOrderCost();

}

public static String getHdr(){

return " customer order# #Burgs Fries Sodas cost";

}

public int getOrderNumber() {

return orderNumber;

}

public void setOrderNumber(int aOrderNumber) {

orderNumber = aOrderNumber;

}

public String getName() {

return name;

}

public void setName(String aName) {

name = aName;

}

public int getNumberOfBurgers() {

return numberOfBurgers;

}

public void setNumberOfBurgers(int aNumberOfBurgers) {

numberOfBurgers = aNumberOfBurgers;

}

public int getNumberOfFries() {

return numberOfFries;

}

public void setNumberOfFries(int aNumberOfFries) {

numberOfFries = aNumberOfFries;

}

public int getNumberOfSodas() {

return numberOfSodas;

}

public void setNumberOfSodas(int aNumberOfSodas) {

numberOfSodas = aNumberOfSodas;

}

public Date getDateCreated() {

return dateCreated;

}

public void setDateCreated(Date aDateCreated) {

dateCreated = aDateCreated;

}

public double getOrderCost() {

return orderCost;

}

public void setOrderCost(double aOrderCost) {

orderCost = aOrderCost;

}

public static int getNextOrderNumber() {

return nextOrderNumber;

}

public static void setNextOrderNumber(int aNextOrderNumber) {

nextOrderNumber = aNextOrderNumber;

}

public static long getSerialversionuid() {

return serialVersionUID;

}

public static double getCostOfBurger() {

return COST_OF_BURGER;

}

public static double getCostOfSoda() {

return COST_OF_SODA;

}

public Object clone()throws CloneNotSupportedException{  

LunchOrder lo = null;

lo = (LunchOrder) super.clone();  

//lo.setDateCreated(new Date()); uncomment to enable deep cloning

return lo;

//Deep Cloning : For all the references we need to create the new objects

//Shadow Cloning : Just it will copies the old value into new object

}  

}

MyRestaurantClass Start:

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Date;

import java.util.Iterator;

import java.util.Scanner;

public class Restaurant {

private static final double tax = 0;

private static final double tips = 0;

static int orderID=100;

ArrayList<LunchOrder> orders = null;

public static void main(String[] args) {

Restaurant restaurant = new Restaurant();

Scanner input = new Scanner(System.in);

int ch=0;

while(true){

System.out.println("1 Create LunchOrders");

System.out.println("2 Add Lunch Orders ");

System.out.println("3 Display ");

System.out.println("4 Sort on Order ");

System.out.println("5 Sort on Customer ");

System.out.println("6 Sort on Cost ");

System.out.println("7 Save Objects into file ");

System.out.println("8 Restore Indivisual Objects from a file ");

System.out.println("9 Save Array List Objet into File");

System.out.println("9 Restore Array List From File");

System.out.println("11 Test Clone");

System.out.println("12 Test Iterators");

System.out.println("13 Exit");

ch = input.nextInt();

switch(ch){

case 1:

restaurant.orders = new ArrayList<LunchOrder>(10);

break;

case 2 :

try {

restaurant.takeOrders();

} catch (IOException e1) {

e1.printStackTrace();

}

break;

case 3:

restaurant.displayOrders();

break;

case 4:restaurant.sortOnOrderId();

break;

case 5:

restaurant.sortOnCustomer();

break;

case 6:

restaurant.sortOnCost();

break;

case 7:

try {

restaurant.saveObjectsIntoFile();

} catch (IOException e) {

e.printStackTrace();

}

break;

case 8:

try {

restaurant.restoreObjectsFromFile();

} catch (FileNotFoundException e) {

} catch (ClassNotFoundException e) {

} catch (IOException e) {

}

break;

case 9:

try {

restaurant.saveArrayListIntoFile();

} catch (IOException e) {

e.printStackTrace();

}

break;

case 10:

try {

restaurant.restoreArraylistObjectFromFile();

} catch (ClassNotFoundException | IOException e) {

e.printStackTrace();

}

break;

case 11:

restaurant.testClone();

break;

case 12:

restaurant.testIterators();

break;

case 13:

input.close();

break;

}

}

}

private void testClone() {

for(LunchOrder temp : orders){

try {

temp = (LunchOrder) temp.clone();

} catch (CloneNotSupportedException e) {

e.printStackTrace();

}

}

}

private void restoreArraylistObjectFromFile() throws FileNotFoundException, IOException, ClassNotFoundException {

ObjectInputStream in=new ObjectInputStream(new FileInputStream("lunchOrder.dat"));  

orders = (ArrayList<LunchOrder>)in.readObject();

}

private void saveArrayListIntoFile() throws IOException {

FileOutputStream fout=new FileOutputStream("arrayLunchOrderObjects.dat");  

ObjectOutputStream out=new ObjectOutputStream(fout);  

out.writeObject(orders);  

out.flush();

out.close();

}

private void restoreObjectsFromFile() throws FileNotFoundException, IOException, ClassNotFoundException {

ObjectInputStream in=new ObjectInputStream(new FileInputStream("lunchOrder.dat"));  

orders = new ArrayList<LunchOrder>();

LunchOrder temp=(LunchOrder)in.readObject();  

orders.add(temp);

while(temp!=null){

temp=(LunchOrder)in.readObject();

orders.add(temp);

}

}

private void saveObjectsIntoFile() throws IOException {

FileOutputStream fout=new FileOutputStream("lunchOrder.dat");  

ObjectOutputStream out=new ObjectOutputStream(fout);  

for(LunchOrder temp:orders){

out.writeObject(temp);  

out.flush();

}

out.close();

}

private void sortOnCost() {

LunchOrder.setCompareOrder(3);

sortOrders();

}

private void sortOrders() {

Collections.sort(orders);

}

private void sortOnCustomer() {

LunchOrder.setCompareOrder(2);

sortOrders();

}

private void sortOnOrderId() {

LunchOrder.setCompareOrder(1);

sortOrders();

}

private void displayOrders() {

System.out.println(LunchOrder.getHdr());

for(LunchOrder tempOrder:orders){

System.out.println(tempOrder.toString());

}

}

private void takeOrders() throws IOException {

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

String name ="";

int friesCount = 0;

int sodasCount = 0;

int burgerCount = 0;

orders = new ArrayList<LunchOrder>();

for(int i=1;i<=3;i++){

System.out.println("Enter Order - "+i+" Details ");

System.out.println("Enter Your Name");

name = reader.readLine();

System.out.println("Enter Number Of Burgers");

burgerCount = Integer.parseInt(reader.readLine());

System.out.println("Enter Number Of Sodas");

sodasCount =Integer.parseInt(reader.readLine());

System.out.println("Enter Number Of Fries");

friesCount = Integer.parseInt(reader.readLine());

LunchOrder temp = new LunchOrder(orderID++,name,burgerCount,friesCount,sodasCount,new Date());

temp.setOrderCost(temp.computeOrderCost(tax, tips));

orders.add(temp);

}

}

public void testIterators(){

Iterator<LunchOrder> itr1 = orders.iterator();

Iterator<LunchOrder> itr2 = orders.iterator();

int i=1;

while(itr1.hasNext()){

System.out.println(itr1.next().toString());

if(i==3){

System.out.println("Displaying with Iterator 2");

while(itr2.hasNext()){

System.out.println(itr2.next().toString());

}

}

i++;

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote