4100COMP Tutorial 17: Meal Ordering System
VerifiedAdded on 2019/09/20
|15
|4518
|489
Practical Assignment
AI Summary
This document provides a detailed solution for the 4100COMP Introduction to Programming Tutorial 17, focusing on the design and implementation of a meal ordering system using Java. It walks through the process of identifying classes and methods from a problem specification, creating a UML class diagram, and developing the code step-by-step. The solution includes the implementation of the Meal and OrderingSystem classes, demonstrating how to load meal data from a file, display meals, handle orders, and cancel orders. The tutorial also emphasizes the importance of problem analysis and incremental development in software engineering.

4100COMP: Introduction to
Programming
Tutorial17: Coursework2- Design and
Implementation Exercise
Liverpool John Moores University Department of Computer Science
Programming
Tutorial17: Coursework2- Design and
Implementation Exercise
Liverpool John Moores University Department of Computer Science
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

4100COMP: Introduction to Programming Tutorial Solutions
Introduction
Up to the first coursework you have been writing Java programs that exist in a single
class and just recently you have seen how Java’s strengths lie in the use of classes to
build larger programs out of a series of individual classes, which at runtime create
objects which cooperate with one another. For this second coursework you are
required to build a larger program, which consists of two or more classes, which will
work together to provide an effective solution to a problem.
In the last week you have been using classes in lab sessions, but this has largely been
based on a precise description of how you should create the class and define its
structure, as shown in the example below
Write a class to represent a student according to the specification below:
Private members:
int id – id number for the student
String name – name of the student
String course – programme of study
int [] marks – an array of size 5 to store 5 marks (0-100)
Public methods:
Student(int id, String name, String course, int [] marks) – constructor which creates a
student according to the specified parameters
int average() – calculates and returns the average mark for the student,
void print() – prints student details
Write a main program to create 3 student objects as:
1234 Joe Bloggs, Computer Studies, {67, 55, 78, 72, 50}
2341 Sue White, Computer Science, {57, 85, 58, 49, 61}
3412 Ben Black, Software Engineering, {71, 45, 66, 70, 51}
For each student print the details and calculate the average.
For this tutorial we will walk through an example to show you how you can build a
program, based on two classes from a realistic system description. The classes will not
be quite as obvious as they were in the precise descriptions of the lab sessions, but
we will show you how to follow a systematic process to determine the classes and
then go on to develop a program based on these classes.
You should apply these techniques when tackling coursework 2.
Problem Specification
The description below describes the system that we will develop in this tutorial.
A restaurant requires an automated meal ordering system. The system is intended to provide
customers with a touchscreen at each table. The touchscreen will provide a main menu with 3
options:
- Display meals available
- Order a meal
- Cancel a meal
Liverpool John Moores University Department of Computer Science
Introduction
Up to the first coursework you have been writing Java programs that exist in a single
class and just recently you have seen how Java’s strengths lie in the use of classes to
build larger programs out of a series of individual classes, which at runtime create
objects which cooperate with one another. For this second coursework you are
required to build a larger program, which consists of two or more classes, which will
work together to provide an effective solution to a problem.
In the last week you have been using classes in lab sessions, but this has largely been
based on a precise description of how you should create the class and define its
structure, as shown in the example below
Write a class to represent a student according to the specification below:
Private members:
int id – id number for the student
String name – name of the student
String course – programme of study
int [] marks – an array of size 5 to store 5 marks (0-100)
Public methods:
Student(int id, String name, String course, int [] marks) – constructor which creates a
student according to the specified parameters
int average() – calculates and returns the average mark for the student,
void print() – prints student details
Write a main program to create 3 student objects as:
1234 Joe Bloggs, Computer Studies, {67, 55, 78, 72, 50}
2341 Sue White, Computer Science, {57, 85, 58, 49, 61}
3412 Ben Black, Software Engineering, {71, 45, 66, 70, 51}
For each student print the details and calculate the average.
For this tutorial we will walk through an example to show you how you can build a
program, based on two classes from a realistic system description. The classes will not
be quite as obvious as they were in the precise descriptions of the lab sessions, but
we will show you how to follow a systematic process to determine the classes and
then go on to develop a program based on these classes.
You should apply these techniques when tackling coursework 2.
Problem Specification
The description below describes the system that we will develop in this tutorial.
A restaurant requires an automated meal ordering system. The system is intended to provide
customers with a touchscreen at each table. The touchscreen will provide a main menu with 3
options:
- Display meals available
- Order a meal
- Cancel a meal
Liverpool John Moores University Department of Computer Science

4100COMP: Introduction to Programming Tutorial Solutions
- Quit
The Display Meal option will display a list of meals that can be ordered. Each meal is
defined by:
- An id number
- A description of the meal
- A vegetarian option (set as true if the ingredients are vegetarian)
- a vegan option (set as true if the ingredients are vegan)
The Order a Meal option will allow a customer to order a meal by input of their table
number and the meal number for that particular meal. A particular meal can have up to 10
orders (i.e. list of 10 table numbers)
The Cancel a Meal option will allow a customer to cancel a meal that was ordered
previously.
The Quit option will display a goodbye message and the touchscreen will hibernate.
You are required to produce an application that simulate the Meal Ordering Systems by
allowing a user to select the various menu options of displaying meals, ordering meals,
cancelling orders and quitting. On startup your application will need to load a series of
meals, which can be used for the simulation.
The first step to developing the application is working out what classes we need and
what shape and form these classes will take. These things will be considered in more
detail in Problem Solving modules in semester 2, but for now we can perform a basic
analysis by applying the “Nous and Verbs” approach to the system description.
Step 1
We have repeated the system description text below and highlighted the main nouns
and verbs in red and blue respectively. The idea is that the main Nouns will represent
the “things” or classes that make up our system and the main verbs will represent the
“operations” or methods that we need to include within the classes.
A restaurant requires an automated meal ordering system. The system is intended to provide
customers with a touchscreen at each table. The touchscreen will provide a main menu with 3
options:
- Display meals available
- Order a meal
- Cancel a meal
- Quit
The Display Meal option will display a list of meals that can be ordered. Each meal is
defined by:
- An id number
- A description of the meal
- A vegetarian option (set as true if the ingredients are vegetarian)
- a vegan option (set as true if the ingredients are vegan)
The Order a Meal option will allow a customer to order a meal by input of their table
number and the meal number for that particular meal. A particular meal can have up to 10
orders (i.e. list of 10 table numbers)
The Cancel a Meal option will allow a customer to cancel a meal that was ordered
previously.
The Quit option will display a goodbye message and the touchscreen will hibernate.
Liverpool John Moores University Department of Computer Science
- Quit
The Display Meal option will display a list of meals that can be ordered. Each meal is
defined by:
- An id number
- A description of the meal
- A vegetarian option (set as true if the ingredients are vegetarian)
- a vegan option (set as true if the ingredients are vegan)
The Order a Meal option will allow a customer to order a meal by input of their table
number and the meal number for that particular meal. A particular meal can have up to 10
orders (i.e. list of 10 table numbers)
The Cancel a Meal option will allow a customer to cancel a meal that was ordered
previously.
The Quit option will display a goodbye message and the touchscreen will hibernate.
You are required to produce an application that simulate the Meal Ordering Systems by
allowing a user to select the various menu options of displaying meals, ordering meals,
cancelling orders and quitting. On startup your application will need to load a series of
meals, which can be used for the simulation.
The first step to developing the application is working out what classes we need and
what shape and form these classes will take. These things will be considered in more
detail in Problem Solving modules in semester 2, but for now we can perform a basic
analysis by applying the “Nous and Verbs” approach to the system description.
Step 1
We have repeated the system description text below and highlighted the main nouns
and verbs in red and blue respectively. The idea is that the main Nouns will represent
the “things” or classes that make up our system and the main verbs will represent the
“operations” or methods that we need to include within the classes.
A restaurant requires an automated meal ordering system. The system is intended to provide
customers with a touchscreen at each table. The touchscreen will provide a main menu with 3
options:
- Display meals available
- Order a meal
- Cancel a meal
- Quit
The Display Meal option will display a list of meals that can be ordered. Each meal is
defined by:
- An id number
- A description of the meal
- A vegetarian option (set as true if the ingredients are vegetarian)
- a vegan option (set as true if the ingredients are vegan)
The Order a Meal option will allow a customer to order a meal by input of their table
number and the meal number for that particular meal. A particular meal can have up to 10
orders (i.e. list of 10 table numbers)
The Cancel a Meal option will allow a customer to cancel a meal that was ordered
previously.
The Quit option will display a goodbye message and the touchscreen will hibernate.
Liverpool John Moores University Department of Computer Science
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

4100COMP: Introduction to Programming Tutorial Solutions
You are required to produce an application that simulate the Meal Ordering Systems by
allowing a user to select the various menu options of displaying meals, ordering meals,
cancelling orders and quitting. On startup your application will need to load a series of
meals, which can be used for the simulation.
The process takes a bit of getting used to as there are lots of unnecessary nouns and
verbs that we don’t actually need. For example we don’t actually need restaurant, or
touchscreen, but we do need meal ordering system and meal. There is also a lot of
repetition, so meal is repeated many times but we only need to consider one
occurrence.
After going through this process once or twice we can draw up an initial list which we
can then refine to something like below.
Main Nouns
----------
1. Restaurant
2. Automated Meal Ordering System
3. Customer
4. Main menu
5. Meal
6. Vegetarian Option
7. Vegan Option
8. Table Number
9. List of Table Numbers
Main Verb Phrases
-----------------
1. Provide a Main Menu
2. Display a List of Meals
3. Order a Meal
3. Cancel a Meal
4. Load a Series of Meals
5. Input a Meal Number
6. Input Table Number
7. Quit
Step 2
Liverpool John Moores University Department of Computer Science
You are required to produce an application that simulate the Meal Ordering Systems by
allowing a user to select the various menu options of displaying meals, ordering meals,
cancelling orders and quitting. On startup your application will need to load a series of
meals, which can be used for the simulation.
The process takes a bit of getting used to as there are lots of unnecessary nouns and
verbs that we don’t actually need. For example we don’t actually need restaurant, or
touchscreen, but we do need meal ordering system and meal. There is also a lot of
repetition, so meal is repeated many times but we only need to consider one
occurrence.
After going through this process once or twice we can draw up an initial list which we
can then refine to something like below.
Main Nouns
----------
1. Restaurant
2. Automated Meal Ordering System
3. Customer
4. Main menu
5. Meal
6. Vegetarian Option
7. Vegan Option
8. Table Number
9. List of Table Numbers
Main Verb Phrases
-----------------
1. Provide a Main Menu
2. Display a List of Meals
3. Order a Meal
3. Cancel a Meal
4. Load a Series of Meals
5. Input a Meal Number
6. Input Table Number
7. Quit
Step 2
Liverpool John Moores University Department of Computer Science
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

4100COMP: Introduction to Programming Tutorial Solutions
We can then refine this further to group things together, such that we can determine
that the List of Meals is part of the Meal Ordering System, whereas Vegetarian Option
and Vegan Option are part of Meal. So we can put this information together to
produce a more structured view of the system, as below:
Meal Ordering System
- List of Meals
Meal
- Number
- Description
- Vegetarian Option
- Vegan Option
- List of Tables
Now we have to think a bit further into the problem and note that the Display Meal
Option mentions a List of Meals, which suggests that the Meal Ordering System should
store some kind of collection. So we need to think of an appropriate data structure for
this. We could use either a fixed sized array or a variable sized ArrayList. In this
example we will use a fixed sized array.
Now we have built up a good picture of the classes we need and the structure of the
classes (shown in red), so it’s time to think about the behaviour (shown in blue).
Meal Ordering System
- List of Meals
- Display List of Meals
- Load a Series of Meals
- Order Meal
- Cancel Meal
- Quit
Meal
- Number
- Description
- Vegetarian Option
- Vegan Option
- List of Tables
- Order
- Cancel
Step 3
Liverpool John Moores University Department of Computer Science
We can then refine this further to group things together, such that we can determine
that the List of Meals is part of the Meal Ordering System, whereas Vegetarian Option
and Vegan Option are part of Meal. So we can put this information together to
produce a more structured view of the system, as below:
Meal Ordering System
- List of Meals
Meal
- Number
- Description
- Vegetarian Option
- Vegan Option
- List of Tables
Now we have to think a bit further into the problem and note that the Display Meal
Option mentions a List of Meals, which suggests that the Meal Ordering System should
store some kind of collection. So we need to think of an appropriate data structure for
this. We could use either a fixed sized array or a variable sized ArrayList. In this
example we will use a fixed sized array.
Now we have built up a good picture of the classes we need and the structure of the
classes (shown in red), so it’s time to think about the behaviour (shown in blue).
Meal Ordering System
- List of Meals
- Display List of Meals
- Load a Series of Meals
- Order Meal
- Cancel Meal
- Quit
Meal
- Number
- Description
- Vegetarian Option
- Vegan Option
- List of Tables
- Order
- Cancel
Step 3
Liverpool John Moores University Department of Computer Science

4100COMP: Introduction to Programming Tutorial Solutions
Based on what we have so far we can put the information into a UML class diagram.
UML will be covered in your second year of study, but for this coursework you are only
required to produce a class diagram, as shown below, based on the information we
have determined above. This can be done using a standard diagramming software like
Visio (preferred), or just simply drawn using shapes in Powerpoint or Word.
Note that for the Meal class we have added a method toString to provide a string
representation of a meal and its orders and getMealNo to return the number for a
meal. Also we don’t need a Quit method in the OrderingSystem class. These are
minor refinements from the lists we build up previously, which often occur as we build
up a clearer picture of the system.
OrderingSystem
- meals : Meal [ ]
+ loadMeals ( ) : void
+ displayMeals ( ) : void
Meal
- no: int
- desc: String
- tables: int [ ]
+ toString ( ) : String
+ order (int tableNo) : void
5 1- veggie: boolean
- vegan: boolean
+ cancel(int tableNo) : boolean
+ getMealNo( ) : int
+ orderMeal ( ) : void
+ cnacelMeal( ): void
Step 4
We are now in a position to start writing the code. We can begin by doing a skeleton
outline of the main classes, as shown below, which contains the member variables we
determined previously. The skeleton outline also contains the definitions or stubs for
the methods.
package cw2_ex;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
public class OrderingSystem {
private static Scanner input = new Scanner(System.in);
private static final Meal[] meals = new Meal[5];
public static void main(String[] args) throws Exception {
}
private static void loadMeals() throws FileNotFoundException {
Liverpool John Moores University Department of Computer Science
Based on what we have so far we can put the information into a UML class diagram.
UML will be covered in your second year of study, but for this coursework you are only
required to produce a class diagram, as shown below, based on the information we
have determined above. This can be done using a standard diagramming software like
Visio (preferred), or just simply drawn using shapes in Powerpoint or Word.
Note that for the Meal class we have added a method toString to provide a string
representation of a meal and its orders and getMealNo to return the number for a
meal. Also we don’t need a Quit method in the OrderingSystem class. These are
minor refinements from the lists we build up previously, which often occur as we build
up a clearer picture of the system.
OrderingSystem
- meals : Meal [ ]
+ loadMeals ( ) : void
+ displayMeals ( ) : void
Meal
- no: int
- desc: String
- tables: int [ ]
+ toString ( ) : String
+ order (int tableNo) : void
5 1- veggie: boolean
- vegan: boolean
+ cancel(int tableNo) : boolean
+ getMealNo( ) : int
+ orderMeal ( ) : void
+ cnacelMeal( ): void
Step 4
We are now in a position to start writing the code. We can begin by doing a skeleton
outline of the main classes, as shown below, which contains the member variables we
determined previously. The skeleton outline also contains the definitions or stubs for
the methods.
package cw2_ex;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
public class OrderingSystem {
private static Scanner input = new Scanner(System.in);
private static final Meal[] meals = new Meal[5];
public static void main(String[] args) throws Exception {
}
private static void loadMeals() throws FileNotFoundException {
Liverpool John Moores University Department of Computer Science
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

4100COMP: Introduction to Programming Tutorial Solutions
}
private static void displayMeals() {
private static void orderMeal() {
}
private static void cancelMeal() {
}
}
package cw2_ex;
public class Meal {
private int no;
private String desc;
private boolean veggie;
private boolean vegan;
private int [] table;
// constructors must do everything as set methods are not necessary here
public Meal(int no, String desc, boolean veggie, boolean vegan, int[] table) {
this.no = no;
this.desc = desc;
this.veggie = veggie;
this.vegan = vegan;
this.table = table;
}
public String toString() {
}
public void order(int tableNo) {
}
public boolean cancel(int tableNo) {
}
public int getMealNo() {
}
}
Step 5
At this stage the solution is far from complete, but it’s important to realize that you
would still get some marks for this as you have analysed the problem and provided an
outline solution. However, with just a bit more effort you would achieve a better mark
Liverpool John Moores University Department of Computer Science
}
private static void displayMeals() {
private static void orderMeal() {
}
private static void cancelMeal() {
}
}
package cw2_ex;
public class Meal {
private int no;
private String desc;
private boolean veggie;
private boolean vegan;
private int [] table;
// constructors must do everything as set methods are not necessary here
public Meal(int no, String desc, boolean veggie, boolean vegan, int[] table) {
this.no = no;
this.desc = desc;
this.veggie = veggie;
this.vegan = vegan;
this.table = table;
}
public String toString() {
}
public void order(int tableNo) {
}
public boolean cancel(int tableNo) {
}
public int getMealNo() {
}
}
Step 5
At this stage the solution is far from complete, but it’s important to realize that you
would still get some marks for this as you have analysed the problem and provided an
outline solution. However, with just a bit more effort you would achieve a better mark
Liverpool John Moores University Department of Computer Science
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

4100COMP: Introduction to Programming Tutorial Solutions
by attempting to implement the methods, so let’s start by implementing the Meal
class as below.
package cw2_ex;
public class Meal {
private int no;
private String desc;
private boolean veggie;
private boolean vegan;
private int [] table;
// constructors must do everything as set methods are not necessary here
public Meal(int no, String desc, boolean veggie, boolean vegan, int[] table) {
this.no = no;
this.desc = desc;
this.veggie = veggie;
this.vegan = vegan;
this.table = table;
}
public String toString() {
String result = no + " " + desc + " Vegetarian " + veggie + " Vegan " + vegan
+ " Table orders: ";
for (int i=0; i<table.length; i++) {
result += " " + table[i];
}
return result;
}
public void order(int tableNo) {
for (int i=0; i<table.length; i++) {
if (table[i] == 0) {
table[i] = tableNo;
return;
}
}
}
public boolean cancel(int tableNo) {
for (int i= 0; i<table.length; i++) {
if (table[i] == tableNo) {
table[i] = 0;
return true;
}
}
return false;
}
public int getMealNo() {
return this.no;
}
}
We’ve added the code for the toString method, which displays the meal data and
then joins it with the elements of the array of table orders. We’ve added the code for
order and cancel which involves working through the array of table numbers and
Liverpool John Moores University Department of Computer Science
by attempting to implement the methods, so let’s start by implementing the Meal
class as below.
package cw2_ex;
public class Meal {
private int no;
private String desc;
private boolean veggie;
private boolean vegan;
private int [] table;
// constructors must do everything as set methods are not necessary here
public Meal(int no, String desc, boolean veggie, boolean vegan, int[] table) {
this.no = no;
this.desc = desc;
this.veggie = veggie;
this.vegan = vegan;
this.table = table;
}
public String toString() {
String result = no + " " + desc + " Vegetarian " + veggie + " Vegan " + vegan
+ " Table orders: ";
for (int i=0; i<table.length; i++) {
result += " " + table[i];
}
return result;
}
public void order(int tableNo) {
for (int i=0; i<table.length; i++) {
if (table[i] == 0) {
table[i] = tableNo;
return;
}
}
}
public boolean cancel(int tableNo) {
for (int i= 0; i<table.length; i++) {
if (table[i] == tableNo) {
table[i] = 0;
return true;
}
}
return false;
}
public int getMealNo() {
return this.no;
}
}
We’ve added the code for the toString method, which displays the meal data and
then joins it with the elements of the array of table orders. We’ve added the code for
order and cancel which involves working through the array of table numbers and
Liverpool John Moores University Department of Computer Science

4100COMP: Introduction to Programming Tutorial Solutions
setting the appropriate value. Notice that cancel returns a boolean value of true if we
can cancel the order and false otherwise.
Step 6
Next we can move on to implement the OrderingSystem class. This requires a bit
more work as we have to implement a menu (not a meal menu – a user selection
menu ) and load the meals data from a file, before we can move on to implement
the orderMeal and cancleMeal methods. If we develop this bit by bit we can first
add the method loadMeals to load the meals from file and implement the user
selection menu as below.
package cw2_ex;
package cw2_ex;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
public class OrderingSystem {
private static Scanner input = new Scanner(System.in);
private static final Meal[] meals = new Meal[5];
public static void main(String[] args) throws Exception {
loadMeals();
String choice = "";
do {
System.out.println("\n-- MAIN MENU --");
System.out.println("1 - Display Menu");
System.out.println("2 - Order Meal");
System.out.println("3 - Cancel Meal");
System.out.println("Q - Quit");
System.out.print("Pick : ");
choice = input.next().toUpperCase();
switch (choice) {
case "1" : {
displayMeals();
break;
}
case "2" : {
orderMeal();
break;
}
case "3" : {
cancelMeal();
break;
}
Liverpool John Moores University Department of Computer Science
setting the appropriate value. Notice that cancel returns a boolean value of true if we
can cancel the order and false otherwise.
Step 6
Next we can move on to implement the OrderingSystem class. This requires a bit
more work as we have to implement a menu (not a meal menu – a user selection
menu ) and load the meals data from a file, before we can move on to implement
the orderMeal and cancleMeal methods. If we develop this bit by bit we can first
add the method loadMeals to load the meals from file and implement the user
selection menu as below.
package cw2_ex;
package cw2_ex;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
public class OrderingSystem {
private static Scanner input = new Scanner(System.in);
private static final Meal[] meals = new Meal[5];
public static void main(String[] args) throws Exception {
loadMeals();
String choice = "";
do {
System.out.println("\n-- MAIN MENU --");
System.out.println("1 - Display Menu");
System.out.println("2 - Order Meal");
System.out.println("3 - Cancel Meal");
System.out.println("Q - Quit");
System.out.print("Pick : ");
choice = input.next().toUpperCase();
switch (choice) {
case "1" : {
displayMeals();
break;
}
case "2" : {
orderMeal();
break;
}
case "3" : {
cancelMeal();
break;
}
Liverpool John Moores University Department of Computer Science
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

4100COMP: Introduction to Programming Tutorial Solutions
}
} while (!choice.equals("Q"));
System.out.println("-- GOODBYE --");
}
private static void loadMeals() throws FileNotFoundException {
Scanner file = new Scanner(new FileReader("meals"));
int index = 0;
while (file.hasNext()) {
int no = Integer.parseInt(file.nextLine());
System.out.println(no);
String desc = file.nextLine();
boolean veggie = Boolean.parseBoolean(file.nextLine());
boolean vegan = Boolean.parseBoolean(file.nextLine());
// read the line of tables as a String and split into ints
String tableData = file.nextLine();
String[] sTables = tableData.split(" ");
int [] tables = new int[10];
for (int i=0; i<sTables.length; i++) {
tables[i] = Integer.parseInt(sTables[i]);
}
meals[index] = new Meal(no, desc, veggie, vegan, tables);
index++;
}
file.close();
}
private static void displayMeals() {
}
private static void orderMeal() {
}
private static void cancelMeal() {
}
}
If we focus attention on the loadMeals method you can see that we have declared
the Scanner to access the file as a local object within this method, as it doesn’t need
to be accessed elsewhere in this program. We then have a familiar while loop similar
to the one used for Coursework 1, which reads the lines from the file. Note that we
have read the lines as Strings and use Integer.parseInt and
Boolean.prarseBoolean to convert to int and boolean respectively. We also read the
list of tables in as one String and then split this String into individual components,
which we then convert into ints which we store in an array.
The line to focus attention on in the while loop is:
meals[index] = new Meal(no, desc, veggie, vegan, tables);
Liverpool John Moores University Department of Computer Science
}
} while (!choice.equals("Q"));
System.out.println("-- GOODBYE --");
}
private static void loadMeals() throws FileNotFoundException {
Scanner file = new Scanner(new FileReader("meals"));
int index = 0;
while (file.hasNext()) {
int no = Integer.parseInt(file.nextLine());
System.out.println(no);
String desc = file.nextLine();
boolean veggie = Boolean.parseBoolean(file.nextLine());
boolean vegan = Boolean.parseBoolean(file.nextLine());
// read the line of tables as a String and split into ints
String tableData = file.nextLine();
String[] sTables = tableData.split(" ");
int [] tables = new int[10];
for (int i=0; i<sTables.length; i++) {
tables[i] = Integer.parseInt(sTables[i]);
}
meals[index] = new Meal(no, desc, veggie, vegan, tables);
index++;
}
file.close();
}
private static void displayMeals() {
}
private static void orderMeal() {
}
private static void cancelMeal() {
}
}
If we focus attention on the loadMeals method you can see that we have declared
the Scanner to access the file as a local object within this method, as it doesn’t need
to be accessed elsewhere in this program. We then have a familiar while loop similar
to the one used for Coursework 1, which reads the lines from the file. Note that we
have read the lines as Strings and use Integer.parseInt and
Boolean.prarseBoolean to convert to int and boolean respectively. We also read the
list of tables in as one String and then split this String into individual components,
which we then convert into ints which we store in an array.
The line to focus attention on in the while loop is:
meals[index] = new Meal(no, desc, veggie, vegan, tables);
Liverpool John Moores University Department of Computer Science
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

4100COMP: Introduction to Programming Tutorial Solutions
This is a constructor call to create a series of Meal objects. So we loop and read the
data from the file and use it in a constructor call to create Meal objects. The result
when this method finishes is that we have loaded into memory a series of Meal
objects that our program can use to perform order and cancel operations. Note that
when the program finishes all the data will be lost, so we would normally arrange for
the data to be saved when the program exits, which we will discuss later.
(remember to close the file once you’ve finished with it).
We’ve also added the user selection menu, which combines a do while loop with a
switch case statement to allow the user to select an option. Note that each case block
involves a call to a method to carry out the appropriate operation. The methods, like
the data, in OrderSystem are declared as static, which is more convenient as they
can be used class-wise (without creating an object)
Step 7
We can now move on to implement the displayMeals, orderMeal and cancelMeal
methods
package cw2_ex;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
public class OrderingSystem {
private static Scanner input = new Scanner(System.in);
private static final Meal[] meals = new Meal[5];
public static void main(String[] args) throws Exception {
loadMeals();
String choice = "";
do {
System.out.println("\n-- MAIN MENU --");
System.out.println("1 - Display Menu");
System.out.println("2 - Order Meal");
System.out.println("3 - Cancel Meal");
System.out.println("Q - Quit");
System.out.print("Pick : ");
choice = input.next().toUpperCase();
switch (choice) {
case "1" : {
displayMeals();
break;
}
Liverpool John Moores University Department of Computer Science
This is a constructor call to create a series of Meal objects. So we loop and read the
data from the file and use it in a constructor call to create Meal objects. The result
when this method finishes is that we have loaded into memory a series of Meal
objects that our program can use to perform order and cancel operations. Note that
when the program finishes all the data will be lost, so we would normally arrange for
the data to be saved when the program exits, which we will discuss later.
(remember to close the file once you’ve finished with it).
We’ve also added the user selection menu, which combines a do while loop with a
switch case statement to allow the user to select an option. Note that each case block
involves a call to a method to carry out the appropriate operation. The methods, like
the data, in OrderSystem are declared as static, which is more convenient as they
can be used class-wise (without creating an object)
Step 7
We can now move on to implement the displayMeals, orderMeal and cancelMeal
methods
package cw2_ex;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
public class OrderingSystem {
private static Scanner input = new Scanner(System.in);
private static final Meal[] meals = new Meal[5];
public static void main(String[] args) throws Exception {
loadMeals();
String choice = "";
do {
System.out.println("\n-- MAIN MENU --");
System.out.println("1 - Display Menu");
System.out.println("2 - Order Meal");
System.out.println("3 - Cancel Meal");
System.out.println("Q - Quit");
System.out.print("Pick : ");
choice = input.next().toUpperCase();
switch (choice) {
case "1" : {
displayMeals();
break;
}
Liverpool John Moores University Department of Computer Science

4100COMP: Introduction to Programming Tutorial Solutions
case "2" : {
orderMeal();
break;
}
case "3" : {
cancelMeal();
break;
}
}
} while (!choice.equals("Q"));
System.out.println("-- GOODBYE --");
}
private static void loadMeals() throws FileNotFoundException {
Scanner file = new Scanner(new FileReader("meals"));
int index = 0;
while (file.hasNext()) {
int no = Integer.parseInt(file.nextLine());
System.out.println(no);
String desc = file.nextLine();
boolean veggie = Boolean.parseBoolean(file.nextLine());
boolean vegan = Boolean.parseBoolean(file.nextLine());
// read the line of tables as a String and split into ints
String tableData = file.nextLine();
String[] sTables = tableData.split(" ");
int [] tables = new int[10];
for (int i=0; i<sTables.length; i++) {
tables[i] = Integer.parseInt(sTables[i]);
}
meals[index] = new Meal(no, desc, veggie, vegan, tables);
index++;
}
file.close();
}
private static void displayMeals() {
System.out.println("\n-- MEALS ON MENU --");
for (int i=0; i<meals.length; i++) {
System.out.println(meals[i].toString());
}
}
private static void orderMeal() {
displayMeals();
System.out.print("Enter meal number: ");
int mealNo = input.nextInt();
System.out.print("Enter table number: ");
int tableNo = input.nextInt();
meals[mealNo-1].order(tableNo);
}
private static void cancelMeal() {
System.out.println("\n-- CANCEL A MEAL --");
Liverpool John Moores University Department of Computer Science
case "2" : {
orderMeal();
break;
}
case "3" : {
cancelMeal();
break;
}
}
} while (!choice.equals("Q"));
System.out.println("-- GOODBYE --");
}
private static void loadMeals() throws FileNotFoundException {
Scanner file = new Scanner(new FileReader("meals"));
int index = 0;
while (file.hasNext()) {
int no = Integer.parseInt(file.nextLine());
System.out.println(no);
String desc = file.nextLine();
boolean veggie = Boolean.parseBoolean(file.nextLine());
boolean vegan = Boolean.parseBoolean(file.nextLine());
// read the line of tables as a String and split into ints
String tableData = file.nextLine();
String[] sTables = tableData.split(" ");
int [] tables = new int[10];
for (int i=0; i<sTables.length; i++) {
tables[i] = Integer.parseInt(sTables[i]);
}
meals[index] = new Meal(no, desc, veggie, vegan, tables);
index++;
}
file.close();
}
private static void displayMeals() {
System.out.println("\n-- MEALS ON MENU --");
for (int i=0; i<meals.length; i++) {
System.out.println(meals[i].toString());
}
}
private static void orderMeal() {
displayMeals();
System.out.print("Enter meal number: ");
int mealNo = input.nextInt();
System.out.print("Enter table number: ");
int tableNo = input.nextInt();
meals[mealNo-1].order(tableNo);
}
private static void cancelMeal() {
System.out.println("\n-- CANCEL A MEAL --");
Liverpool John Moores University Department of Computer Science
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide
1 out of 15
Related Documents

Your All-in-One AI-Powered Toolkit for Academic Success.
+13062052269
info@desklib.com
Available 24*7 on WhatsApp / Email
Unlock your academic potential
Copyright © 2020–2025 A2Z Services. All Rights Reserved. Developed and managed by ZUCOL.