Question: I am working on a 2D array program. I have been provided information from my instructor that I am NOT ALLOWED TO CHANGE. Here is
I am working on a 2D array program.
I have been provided information from my instructor that I am NOT ALLOWED TO CHANGE.
Here is the information given:
//*** This program will read some information from a file and ***//
//** some information from a keyboard. ***//
import java.io.*;
public class Assignment7 {
public static void main(String[] args) throws IOException {
MovieSeating theatreSeating;
Customer tempCustomer;
int row, col, rowNum, columnNum;
String line, fileName;
// create InputStreamReader and BufferedReader object
// to read input form a KEYBOARD.
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader stdin = new BufferedReader(isr);
// Ask a user to enter a number of rows for a movie theatre seating from a KEYBOARD.
System.out.println("Please enter a number of rows for a movie theatre seating: ");
rowNum = Integer.parseInt(stdin.readLine());
// Ask a user to enter a number of columns for a movie theatre seating from a KEYBOARD.
System.out.println("Please enter a number of columns for a movie theatre seating: ");
columnNum = Integer.parseInt(stdin.readLine());
// instantiate a MovieSeating object
theatreSeating = new MovieSeating(rowNum, columnNum);
// get a file name read from a KEYBOARD.
System.out.println("Please enter a file name ");
fileName = stdin.readLine();
//create FileReader and BufferedReader object to
// read from a file.
FileReader fr = new FileReader (fileName);
BufferedReader inFile = new BufferedReader (fr);
/*** reading a customer's information from a FILE ***/
line = inFile.readLine();
/*** we will read line by line until we read the end of a given file ***/
while (line != null) {
System.out.println(" A customer information is read from a file.");
// printing information read from a file.
System.out.println(line);
// creating a customer object using information from a file
tempCustomer = new Customer(line);
// Ask a user to decide where to seat a customer by asking for row and column of a seat
System.out.println("Please enter a row number where the customer wants to sit.");
row = Integer.parseInt(stdin.readLine());
System.out.println("Please enter a column number where the customer wants to sit.");
col = Integer.parseInt(stdin.readLine());
// Checking if the row number and column number are valid (exist in the theatre that we created.)
if (theatreSeating.checkBoundaries(row, col) == false) {
System.out.println(" row or column numer is not valid.");
System.out.println("A customer " + tempCustomer.getFirstName() + " " + tempCustomer.getLastName() + " is not assigned a seat.");
}
else
{
// Assigning a seat for a customer
if (theatreSeating.assignCustomerAt(row, col, tempCustomer) == true) {
System.out.println(" The seat at row " + row + " and column " + col + " is assigned to the customer " + tempCustomer.toString());
System.out.println(" The seat is taken.");
}
}
// Read next line in a FILE
line = inFile.readLine();
}
// Closing the file
inFile.close();
}
}
Second part:
import java.util.StringTokenizer; import java.text.NumberFormat;
public class Customer { private String lastName; private String firstName; private int customerID; private int matineeTickets; private int normalTickets; private double totalCost; // This constructor sets the first name and last name to "???", customer ID, // the number of matinee tickets, and the number of normal tickets to 0, // and the total cost to 0.0 public Customer() { lastName = "???"; firstName = "???"; customerID = 0; matineeTickets = 0; normalTickets = 0; totalCost = 0.0; } // This constructor constructs a Customer object given the last name, // first name, customer ID, the number of matinee tickets, the number // of normal tickets. public Customer(String customerInfo) { String[]tokenizer = customerInfo.split(" "); firstName = tokenizer[0].trim(); lastName = tokenizer[1].trim(); customerID = Integer.parseInt(tokenizer[2].trim()); matineeTickets = Integer.parseInt(tokenizer[3].trim()); normalTickets = Integer.parseInt(tokenizer[4].trim()); totalCost = 0.0; computeTotalCost(); } // This constructor constructs a Customer object using the string containing customer's info. // It uses the StringTokenizer to extract first name, last name, ID, the number of matinee tickets, // and the number of normal tickets. public Customer(String lName, String fName, int id, int matineeNum, int normalNum) { lastName = lName; firstName = fName; customerID = id; matineeTickets = matineeNum; normalTickets = normalNum; totalCost = 0.0; computeTotalCost(); } // This method sets the last name. public void setLastName(String lName) { lastName = lName; } // This method sets the first name. public void setFirstName(String fName) { firstName = fName; } // This method sets the customer ID. public void setCustomerID(int id) { customerID = id; } // This method sets the value of number of matineeTickets to have its parameter value. // And it re-computes total cost. public void setMatineeTickets(int matinee) { matineeTickets = matinee; computeTotalCost(); } // This method sets the value of number of normalTickets to have its parameter value. // And it re-computes total cost. public void setNormalTickets(int normal) { normalTickets = normal; computeTotalCost(); } // This method returns the last name. public String getLastName() { return lastName; } // This method returns the first name. public String getFirstName() { return firstName; } // This method returns the customer Id. public int getCustomerID() { return customerID; } // This method returns the number of matinee tickets. public int getMatineeTickets() { return matineeTickets; } // This method returns the number of normal tickets. public int getNormalTickets() { return normalTickets; } // This method returns the total cost. public double getTotalCost() { return totalCost; } // This method computes the total cost based on the number of matinee tickets and normal tickets. private void computeTotalCost() { totalCost = (5.00)*matineeTickets + (7.50)*normalTickets; } // This method checks if a customer object passed as a parameter and itself (customer object) // are same using their last names, first names, and customerIDs. public boolean equals(Customer other) { if (lastName.equals(other.lastName) && firstName.equals(other.firstName) && (customerID == other.customerID) ) return true; else return false; } // This method compares a customer object passed as a parameter to itself (customer object) // are same using their total costs. public Customer hasMore(Customer other) { if (totalCost >= other.totalCost) return this; else return other; } // This method returns a string containing a customer's initials // (first characters of firstName and lastName.) public String toString() { String result = firstName.charAt(0) + "." + lastName.charAt(0) + "."; return result; }
} // end of class Customer
And then I was given this file:
Bob Sponge 111111111 3 4
Pika Chu 222222222 6 5
Charlie Brown 333333333 4 3
Donald Trump 555666777 4 5
Thomas Jefferson 666888999 16 5
Snow White 777777777 43 23
George Washington 888888888 2 4
This is supposed to work into my code but I have no idea where...
I finished my part of the program:
public class MovieSeating {
// 2D array of customer objects
Customer seating[][];
// 2-argument constructor takes rows and columns to initialize
// the two dimensional seating aray
public MovieSeating(int rowNum, int columnNum) {
seating = new Customer[rowNum][columnNum];
for (int i = 0; i < rowNum; i++) {
for (int j = 0; j< columnNum; j++)
seating[i][j] = new Customer();
}
}
// returns customer at given row and column position
private Customer getCustomerAt(int row, int col) {
return seating[row][col];
}
// assigns the seat specified by row and column to the tempCustomer
// if the seat is available
public boolean assignCustomerAt(int row, int col, Customer tempCustomer) {
Customer cur = seating[row][col];
if (cur.getLastName().equals("???") && cur.getFirstName().equals("???")) {
seating[row][col] = tempCustomer;
return true;
}
else
return false;
}
// checks the boundaries of given row and column against the
// array dimensions
public boolean checkBoundaries(int row, int col) {
if (row < 0 || col < 0 || row >= seating.length
|| col >= seating[0].length)
return false;
else
return true;
}
// returns current seating
public String toString() {
String seats = " The current seating";
seats += " -----------------";
for (int i = 0; i < seating.length; i++) {
for (int j = 0; j < seating[i].length; j++)
seats += seating[i][j].getFirstName().charAt(0) + "."
+ seating[i][j].getLastName().charAt(0) + "." + " ";
seats += " ";
}
return seats;
}
}
This is an example of how the program is supposed to run:
Sample Output
Make sure that your program works at least with this scenario (the inputs entered by a user are shown in bold).
Please enter a number of rows for a movie theatre seating
3
Please enter a number of columns for a movie theatre seating.
3
Please enter a file name
customerData.txt <-------This is where I'm having trouble. How do I get the info???? I'm using Eclipse file on a Mac. Please help!
A customer information is read from a file. George Bush/111111111/3/4 Please enter a row number where the customer wants to sit. 1 Please enter a column number where the customer wants to sit. 2
The seat at row 1 and column 2 is assigned to the customer G.B. The current seating ?.?. ?.?. ?.?.
?.?. ?.?. G.B. ?.?. ?.?. ?.?.
A customer information is read from a file. Bill Clinton/222222222/6/5 Please enter a row number where the customer wants to sit. 2 Please enter a column number where the customer wants to sit. 0
The seat at row 2 and column 0 is assigned to the customer B.C. The current seating ?.?. ?.?. ?.?.
?.?. ?.?. G.B. B.C. ?.?. ?.?.
A customer information is read from a file. Hilary Clinton/444444444/5/3 Please enter a row number where the customer wants to sit. 2 Please enter a column number where the customer wants to sit. 1
The seat at row 2 and column 1 is assigned to the customer H.C. The current seating ?.?. ?.?. ?.?.
?.?. ?.?. G.B.
B.C. H.C. ?.?.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
