/*
* Program: ArrayListExample (Snippet 8b)
* Programmer: [Ray Holguin]
* Date:[April 15,2019]
* Description: This program will ask the user to enter the 3 dimensions of a
* room, then store the room object in a list rooms. This program will also
* store the data in an external file.Each time the program is started, it
* will load the data from the external file.
*/
package arraylistexample;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList; // Needed to use ArrayLists
import javax.swing.JOptionPane; // Needed to use JOptionPane methods
public class ArrayListExample {
// Globel varaible
static String fn = "Room_Data.txt"; // File that will store the data
public static void main(String[] args){
// initialization
String depth, width, length; // The 3 dimisions of the room
ArrayList<BoxClass> rooms = new ArrayList(); // A list of rooms (box)
// Load the data into the Array List from the external file
try {
} catch (Exception e) {
}
// Loop until user press the "No" button
do {
// Ask the user for the depth of the room
depth = JOptionPane.showInputDialog(null,
"Please enter the depth of the office",
"Room Database", JOptionPane.QUESTION_MESSAGE);
// Ask the user for the width of the room
width = JOptionPane.showInputDialog(null,
"Please enter the width of the office",
"Room Database", JOptionPane.QUESTION_MESSAGE);
// Ask the user for the length of the room
length = JOptionPane.showInputDialog(null,
"Please enter the length of the office",
"Room Database", JOptionPane.QUESTION_MESSAGE);
// Use the Box Class constructor to build the room (Object)
Box room = new Box(depth, length, width);
// Add room to the arrayList
boolean add;
add = rooms.add(room);
// Save the room's data to the external file
writeData(room);
// Ask the user if they want to enter a new room. The dialog box
// will include a Yes/No Button - If Yes is clicked, loop
} while(JOptionPane.showConfirmDialog(null,
"You have " + rooms.size() + " rooms stored\n" +
"Do you want to enter another room?", "Room Database",
JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION);
// Print the list of rooms
showList(rooms);
} // End of 'main'
private static void showList(ArrayList<Box> list) {
// Print the list of rooms using a collection loop. The ArrayList is
// passed from the main method.
int roomNum; // will hold the number for each room
String output = "List of Rooms:\n"; // Output string of all rooms
// For each box (i) in the ArrayList rooms.
for (Box i : list) {
// Take the index number of the current room (i), but since indexes
// start with 0, you ave to add 1.
roomNum = list.indexOf(i) + 1;
// concatenate one line room dimensions per room
output += "Room " + roomNum +
"> D: "+ i.depth +
" L: " + i.length +
" W: " + i.width +"\n";
}
JOptionPane.showMessageDialog(null, output, "Room Database",
JOptionPane.INFORMATION_MESSAGE);
} // End of ShowList method
private static ArrayList<Box> loadData(ArrayList)<Box> rooms) {
// Read the data from the external file and load the ArrayList rooms
String fileData = ""; // Will hold all of the data from the file
String line; // Will hold one line of the file at a time
String[] fArray; // Will hold the dimensions from one line of data
// Create a trap if the I/O has an error
try{
// Open the file and read the first line
BufferedReader br = new BufferedReader(new FileReader(fn))) {
// Check to see that there is fdata on the current line in the file
while ((line = br.readLine()) !=null) {
// Load string into the array, comma delimiter for the data
fArray = line.split(",");
// Create a new room based on the dimensions that were store.
// Since we know that the first number stored is the depth and
// had the array index of 0, we are just use the index number
// to reference the values in the array
Box room = new Box(fArray[0], fArray[1], fArray[2]);
// Add the room to the room list - rooms
rooms.add(room);
}
// Once done with the file, destroy the thread to the file
br.close();
} catch (Exception e){
// Send the exception to the output psane. 'e' will be the exception
// created when an error occurs in the 'try' section.
System.out.println(e);
} // End of Try/Catch
// Return the Room list back to the main method
return rooms ;
} // End of loadData method
private static void writeData(Box room) {
// Save the room data into an external file. Data from each room will
// be stored on one line.
// Create a trap if the I/O has an error
try{
// Convert the data into a String and add comma as a delimiter.
// Append the data into the file ending with carage return.
try ( // Create a FileWriter - Append = true
FileWriter fw = new FileWriter(fn ) {
// Convert the data into a String and add comma as a delimiter.
// Append the data into the file ending with carage return.
fw.append(Double.toString(room.depth) + ','+
Double.toString(room.length) + ',' +
Double.toString(room.width) + "\r\n");
// Once done with the file, close the tread to the file
}
'
}catch (Exception e){
// Send the exception to the output pane. 'e' will be the exception
// created when an error occurs in the 'try' section. Errpr message
// can only be seen by the programmer, not the user.
System.out.println(e);
} // End of Try/Catch
} // End of writeData method
private static void showList(ArrayList<BoxClass> rooms) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
private static class Box {
private String depth;
private String length;
private String width;
public Box() {
}
private Box(String depth, String length, String width) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
} // End of Class
/*
* Program: BoxClass (Snippet 7b)
* Programmer: [Ray Holguin]
* Date: [April 9,2019]
* Description: This class is used to create a box object. Class include area,
* volumn and one display methods. This class has two constructor, so now the
* user can create objects by passing number or strings.
*/
package arraylistexample;
class BoxClass {
// Initialization
double depth; // Depth of the box
double length; // Height of the box
double width; // width of the box
// Constructor - user provides the values to create the box object
public BoxClass(double d, double l, double w){
// Take the values that are passed from the user to set the local var.
this.depth = d;
this.length = l;
this.width = w;
} // End of contructor
// Overloaded constructor
public BoxClass (String d, String l, String w){
// Convert the passed String to number and set the Box dimensions
this.depth = Double.parseDouble(d);
this.length = Double.parseDouble(l);
this.width = Double.parseDouble(w);
} // End of constructor
// Calculation methods
public double volumn(){
// Multiply the 3 dimensions to calculate the volumn of the box
return depth * length * width;
} // End of method
public double area(){
// Multiply the length and width to calculate the floor area of the box
return length * width;
} // End of method
// Display method
public String dimensions(){
// Return a string that contain the 3 dimensions and summary
return "Depth = " + depth + "\n" +
"Length = " + length + "\n" +
"Width = " + width + "\n" +
"Floor area = "+ this.area() + "\n" +
"Volumn = " + this.volumn();
} // End of Method
} // End of Class
For those who are reading this message.I'm a beginners at this java assignment.I'm struggling at understanding it.If anyones willing to solve the code and to explain how you solved it.Im all ears and eyes.I looked all over websites and it is hard to find someone who is willing to teach me how to solve this code.I made a ArrayListExample and a Boxclass.I get some errors in the code.Someone taught me at the college and they were too in hurry to explain.If you have the time i have the time to look this over and to learn it.