Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import staticArray.ArrayList; import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Scanner; import javax.swing.text.html.HTMLDocument.Iterator; import java.io.*; public class Restaurant { static String [] names = {Jones, Smith,

import staticArray.ArrayList; import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Scanner;

import javax.swing.text.html.HTMLDocument.Iterator;

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 myOrders = new ArrayList();

static final String FILENAME = "lunchOrders.dat"; static final String FILENAME2 = "alllunchOrders.dat"; /* * create two Iterators * verify the work independently * => create iterator #1 * display half of the objects using next() * => create iterator #2 * display all the objects using next() * => display the 2nd half of iterator #1 objects using next() */ public static void testIterators() { //to do } public static void mainMenu() { Scanner input = new Scanner(System.in); while(true) { System.out.println("Menu"); System.out.println(" 1 ..create new Array of LunchOrders"); System.out.println(" 2 ..add 10 entries to the Array of LunchOrders"); System.out.println(" 3 ..display array"); 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 individual objects to a file"); System.out.println(" 8 ..restore individual objects from a from file"); System.out.println(" 9 ..save ArrayList object to a file"); System.out.println("10 ..restore ArrayList object from a from file"); System.out.println("11 ..test clone"); System.out.println("12 ..test iterators"); System.out.println("20 ..exit"); int choice=input.nextInt(); if (choice == 1 || choice == 2) { if (choice == 1) myOrders = new ArrayList(); for (int k=0; k<10; k++) { String name = names[ (int)(Math.random() * names.length)]; int numBurgers = (int) (Math.random() * 10 + 1); int numFries = (int) (Math.random() * 10 ); int numSodas = (int) (Math.random() * 10 ); LunchOrder order = new LunchOrder(name, numBurgers, numFries, numSodas); myOrders.add(order); } } else if (choice == 3) { displayOrders(); } else if (choice == 4) { LunchOrder.setCompareOrder(0); myOrders.sort(); displayOrders(); } else if (choice == 5) { LunchOrder.setCompareOrder(1); myOrders.sort(); displayOrders(); } else if (choice == 6) { LunchOrder.setCompareOrder(2); myOrders.sort(); displayOrders(); } else if (choice == 7) { try{ ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(FILENAME)); /* * TO DO: traverse the ArrayList and save each LunchOrder object to the file */ out.close(); } catch (Exception e) { System.out.println("error opening file: " + e); } } else if (choice == 8) { try{ FileInputStream fileStream = new FileInputStream(FILENAME); ObjectInputStream in = new ObjectInputStream( fileStream); myOrders.clear(); /* * TO DO: restore the ArrayList of LunchOrder objects from the file * note: use fileStream.available() to determine if another object * can be read from the file. */ System.out.println("in.avail: " + fileStream.available()); in.close(); } catch (Exception e) { System.out.println("error opening file: " + e); } } else if (choice == 9) { try{ /* * TO DO: write the entire ArrayList to the file * note, this can be done with 1 write. */ } catch (Exception e) { System.out.println("error opening file: " + e); } } else if (choice == 10) { try{ /* * TO DO: restore the entire ArrayList from the file * note, this can be done with 1 read. */ } catch (Exception e) { System.out.println("error opening file: " + e); } } else if (choice == 11) { try{ /* * TODO: test your LunchOrder's clone method */ } catch(Exception e) { System.out.println("error with clone: " + e); } } else if (choice == 12) { testIterators(); } else if (choice == 20) { System.exit(0); } } }

public static void main(String[] args) { mainMenu(); } public static void displayOrders() { System.out.println(LunchOrder.getHdr()); for (LunchOrder ord : myOrders) System.out.println(ord); }

}

how to finish the code where comments " to do"?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 33rd International Conference Dexa 2022 Vienna Austria August 22 24 2022 Proceedings Part 1 Lncs 13426

Authors: Christine Strauss ,Alfredo Cuzzocrea ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

3031124227, 978-3031124228

More Books

Students also viewed these Databases questions

Question

=+3. Explain the process of elaboration and its importance.

Answered: 1 week ago