Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: HUGE PROBLEM - PLEASE HELP! ArrayList issues

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default HUGE PROBLEM - PLEASE HELP! ArrayList issues

    Okay. Strap on your seat belts, this isn't exactly going to be a fun ride. But I thank anyone who's willing to help or at least offer assistance. Basically I have been giving the assignment of making a program that allows a user to keep track of a "PixFile" (Picture files) We present them a menu, they can add, look at all of them, see the biggest and smallest, and search for a specific file, and search for a specific photographer.

    My teacher wants us to add a class called PixFileList where we utilize an ArrayList full of PixFile objects. My problem is, I can't figure out how to add a PixFile object (From the class PixFileDriver) into the PixFileList (Array List)

    It's an object, right? So do I add the full object into PixFileList? I'm so confused...He's a very good teacher but likes to give the smallest amount of detail when teaching. He showed us how to do ArrayList work inside of the same class. But not when working inbetween three separate classes.

    I'm including all three classes along with his instructions for the project. Thank you all for everything...

    Your program needs to maintain an ArrayList of photographic files. After the startup screen and the prompt for the user’s first and last names, display a menu showing the following options and prompt for the user’s choice:
    1. Enter a new Pixfile
    2. Display all information about one Pixfile based on the Pixfile’s name
    3. Display all information about one Pixfile based on the photographer’s name
    4. Display the names of *all* Pixfiles, sorted by Pixfile name
    5. Display the names and sizes of the smallest and largest Pixfiles
    6. Quit the program
    For choice 1, input the data about a Pixfile as you did in Project 1. The other choices require that data for some Pixfiles has already been entered before they can do their jobs (i.e., choice 1 will have to be selected one or more times before there is any data for the other choices to use). For choice 2, prompt the user for the name of the Pixfile, then find that particular photograph and display all of the information about it. For choice 3, prompt the user for the photographer’s name, then display all the information about the first Pixfile from that photographer. For choice 4, show just the names of the stored Pixfiles, in sorted order. For choice 5, display the name and size of the smallest Pixfile and the name and size of the largest Pixfile, based on the size of the file. For choices 2 through 5, be sure your code works even if no Pixfile data has been entered yet.
    RESTRICTIONS
    • The design must contain an ArrayList of objects of type Pixfile. Use a separate class (say PixFileList) that contains the ArrayList as member data. Test the program with at least 10 Pixfiles.
    • Please use the JOptionPane class for all input and output. You should also use the Menu class posted on the website. The posting includes instructions with an example.



    import java.text.DecimalFormat; // To give our cost two decimal places
     
    /**
     * ---------------------------------------------------------------------------
     * File name: PixFile.java<br/>
     * Project name: PhotoTracker<br/>
     * ---------------------------------------------------------------------------
     * Creator's name and email: Austin Stanley, Stanleyar@Goldmail.etsu.edu<br/>
     * Course: CSCI1260-002<br/>
     * Creation Date: Jan 28, 2013<br/>
     * Date of Last Modification: Jan 28, 2013
     * ---------------------------------------------------------------------------
     */
     
    /**
     * The calculate the user input, a background class<br>
     * 
     * <hr>
     * Date created: Jan 28, 2013<br>
     * Date last modified: Jan 28, 2013<br>
     * <hr>
     * 
     * @author Austin Stanley
     */
    public class PixFile // All of our attributes
    {
    	// Class Attributes
    	private String	userFirstName;
    	private String	userLastName;
    	private String	fileName;
    	private String	photographerName;
    	private String	fileType	= "jpeg";
    	private double	fileSize	= 0;
     
    	/**
    	 * Default Constructor <br>
    	 * 
    	 * <hr>
    	 * Date created: Jan 28, 2013 <br>
    	 * Date last modified: Jan 28, 2013 <br>
    	 * 
    	 * <hr>
    	 */
    	public PixFile( ) // Initialize all variables
    	{
    		String userFirstName = "";
    		String userLastName = "";
    		String fileName = "";
    		String photographerName = "";
    		String fileType = "jpeg";
    		double fileSize = 0;
    	}
     
    	/**
    	 * Constructor <br>
    	 * 
    	 * <hr>
    	 * Date created: Jan 28, 2013 <br>
    	 * Date last modified: Jan 28, 2013 <br>
    	 * 
    	 * <hr>
    	 * 
    	 * @param userFirstName
    	 * @param userLastName
    	 * @param fileName
    	 * @param photographerName
    	 * @param fileType
    	 * @param fileSize
    	 */
    	public PixFile(String userFirstName, String userLastName, String fileName,
    										String photographerName, String fileType, double fileSize)
    	{ // More initializing
    		setUserFirstName(userFirstName);
    		setUserLastName(userLastName);
    		setFileName(fileName);
    		setPhotographerName(photographerName);
    		setFileType(fileType);
    		setFileSize(fileSize);
    	}
     
    	// GETTERS AND SETTERS
    	/**
    	 * @return userFirstName
    	 */
    	public String getUserFirstName ( )
    	{
    		return userFirstName;
    	}
     
    	/**
    	 * @param userFirstName
    	 *            the userFirstName to set
    	 */
    	public void setUserFirstName (String userFirstName)
    	{
    		this.userFirstName = userFirstName;
    	}
     
    	/**
    	 * @return userLastName
    	 */
    	public String getUserLastName ( )
    	{
    		return userLastName;
    	}
     
    	/**
    	 * @param userLastName
    	 *            the userLastName to set
    	 */
    	public void setUserLastName (String userLastName)
    	{
    		this.userLastName = userLastName;
    	}
     
    	/**
    	 * @return fileName
    	 */
    	public String getFileName ( )
    	{
    		return fileName;
    	}
     
    	/**
    	 * @param fileName
    	 *            the fileName to set
    	 */
    	public void setFileName (String fileName)
    	{
    		this.fileName = fileName;
    	}
     
    	/**
    	 * @return photographerName
    	 */
    	public String getPhotographerName ( )
    	{
    		return photographerName;
    	}
     
    	/**
    	 * @param photographerName
    	 *            the photographerName to set
    	 */
    	public void setPhotographerName (String photographerName)
    	{
    		this.photographerName = photographerName;
    	}
     
    	/**
    	 * @return fileType
    	 */
    	public String getFileType ( )
    	{
    		return fileType;
    	}
     
    	/**
    	 * @param fileType
    	 *            the fileType to set
    	 */
    	public void setFileType (String fileType)
    	{
    		if (fileType.equals (""))
    		{
    			fileType = "jpeg";
    		}
    		else
    			this.fileType = fileType;
    	}
     
    	/**
    	 * @return fileSize
    	 */
    	public double getFileSize ( )
    	{
    		return fileSize;
    	}
     
    	/**
    	 * @param fileSize
    	 *            the fileSize to set
    	 */
    	public void setFileSize (double fileSize)
    	{
    		this.fileSize = fileSize;
    	}
     
    	// Created Methods
    	// Welcome Message to welcome the user
    	/**
    	 * The welcome screen presented to the user <br>
    	 * 
    	 * <hr>
    	 * Date created: Jan 28, 2013 <br>
    	 * Date last modified: Jan 28, 2013 <br>
    	 * 
    	 * <hr>
    	 * 
    	 * @param none
    	 */
    	public String welcomeMessage ( )
    	{
    		return ("Welcome to the Photo Tracker!" + "\n  Created By: Austin Stanley");
    	}
     
    	//  End Message to thank the user
    	/**
    	 * The end screen presented and thanking the user <br>
    	 * 
    	 * <hr>
    	 * Date created: Jan 28, 2013 <br>
    	 * Date last modified: Jan 28, 2013 <br>
    	 * 
    	 * <hr>
    	 * 
    	 * @param none
    	 */
    	public String endMessage ( )
    	{
    		return ("Thanks for using the Photo Tracker!" + "\n  Created By: Austin Stanley");
    	}
     
    	// Calculate the cost of the storage method
    	/**
    	 * Calculate the cost of the storage method <br>
    	 * 
    	 * <hr>
    	 * Date created: Jan 28, 2013 <br>
    	 * Date last modified: Jan 28, 2013 <br>
    	 * 
    	 * <hr>
    	 * 
    	 * @param none
    	 */
    	public double calcStorageCost ( )
    	{
    		double storageCost = fileSize * 0.25;
    		return storageCost;
    	}
     
    	// calculate the total download time
    	/**
    	 * Calculate the total download time <br>
    	 * 
    	 * <hr>
    	 * Date created: Jan 28, 2013 <br>
    	 * Date last modified: Jan 28, 2013 <br>
    	 * 
    	 * <hr>
    	 * 
    	 * @param none
    	 */
    	public double calcDownloadTime ( )
    	{
    		double downloadTime = fileSize / 3;
    		return downloadTime;
    	}
     
    	// A file report to present back to the user once they applied input
    	/**
    	 * The generated filed report of the users in formation<br>
    	 * 
    	 * <hr>
    	 * Date created: Jan 28, 2013 <br>
    	 * Date last modified: Jan 28, 2013 <br>
    	 * 
    	 * <hr>
    	 * 
    	 * @param none
    	 */
    	public String fileReport ( )
    	{
    		DecimalFormat format = new DecimalFormat ("#,###.##");
    		return ("User's Name: " + userFirstName + " " + userLastName + "\nPhoto Title: " +
    						fileName + "\nPhoto Type: " + fileType + "\nPhotographer's Name: " +
    						photographerName + "\nSize of the Photo: " + fileSize +
    						"\nCost for file storage on our server: $" +
    						format.format (calcStorageCost ( )) + "\nApproximate download time: " +
    						format.format (calcDownloadTime ( )) + " secs");
    	}
     
    }// End PixFile

    /**
     * ---------------------------------------------------------------------------
     * File name: PixFileDriver.java<br/>
     * Project name: PhotoTracker<br/>
     * ---------------------------------------------------------------------------
     * Creator's name and email: Austin Stanley, Stanleyar@Goldmail.etsu.edu<br/>
     * Course: CSCI1260-002<br/>
     * Creation Date: Jan 28, 2013<br/>
     * Date of Last Modification: Jan 28, 2013
     * ---------------------------------------------------------------------------
     */
    import javax.swing.JOptionPane; //import JOptionPane
    import util.Menu; 				//import Menu class
    //import java.util.ArrayList;		//import ArrayList
     
    /**
     * Collect and display picture file data<br>
     *
     * <hr>
     * Date created: Feb 6, 2013<br>
     * Date last modified: Feb 6, 2013<br>
     * <hr>
     * @author Elizabeth Turbyfill
     */
    public class PixFileDriver
    {
    	/**
    	 * Driver for the card class <br>        
    	 *
    	 * <hr>
    	 * Date created: Feb 6, 2013 <br>
    	 * Date last modified: Feb 6, 2013 <br>
    	 *
    	 * <hr>
    	 * @param args
    	 */
    	public static void main (String [ ] args)
    	{
    		PixFile photo = new PixFile();
    		//declare the menu and menu choices
    		Menu menu = new Menu("Pixfile Menu");
    		menu.addChoice("Enter a new Pixfile");
    		menu.addChoice("Display information about one Pixfile based on the Pixfile's name");
    		menu.addChoice("Display information about one Pixfile based on photographer's name");
    		menu.addChoice("Display the names of all pixfiles");
    		menu.addChoice("Display the names and sizes of the smallest and largest Pixfiles");
    		menu.addChoice("Quit the program");
    		int choice = 0;		//the user's menu choice
     
    		//display welcome screen
    		JOptionPane.showMessageDialog(null, photo.welcomeMessage());
     
    		//display the menu
    		while ((choice = menu.getChoiceDialog()) != 6)
    		{
    			switch (choice)
    			{
    				case 1: //enter a new Pixfile
    					PixFile newPixfile = new PixFile();
    					//newPixfile.setUserName(JOptionPane.showInputDialog("Please enter your " +
    							//"first and last name."));
    					newPixfile.setFileName(JOptionPane.showInputDialog("What is the title of the " +
    							"file?"));
    					newPixfile.setPhotographerName(JOptionPane.showInputDialog("Who is the photographer " +
    							"of the file?"));
    					newPixfile.setFileType(JOptionPane.showInputDialog("Please enter the file type. (.JPG, " +
    							".PNG, etc.)"));
    					newPixfile.setFileSize(Double.parseDouble(JOptionPane.showInputDialog("What is the " +
    							"file size in MB?")));
    					PixFileList.add(newPixfile); 	//add the pixfile to the ArrayList
    					break;
     
    				case 2: //display info of one Pixfile based on name
    					//JOptionPane.showMessageDialog(null, (pixfileList.findName(JOptionPane.showInputDialog
    						//("What is the name of the file?")).toString());
    					//pixfileList.remove(0);
    					break;
     
    				case 3: //display info of one Pixfile based on photographer
    					//JOptionPane.showMessageDialog(null, (pixfileList.findPhotographer(JOptionPane.
    						//showInputDialog("Who is the photographer of the file?")).toString());
    					//pixfileList.remove(0);
    					break;
     
    				case 4: //display names of all pixfiles
    					//pixfileList.allpixfiles();
    					break;
     
    				case 5: //display names and sizes of largest and smallest pixfiles
    					//JOptionPane.showMessageDialog(null, "The largest pixfile: " + pixfileList.largest());
    					//JOptionPane.showMessageDialog(null, "\nThe smallest pixfile: " + pixfileList.smallest());
    					break;
    			} //end switch statement
    		} //end while loop
     
    		System.exit(0); //end the program
     
    	} //end main
    } //end PhotoTracker

    import java.util.ArrayList;
     
    /**
     * ---------------------------------------------------------------------------
     * File name: pixList.java<br/>
     * Project name: PhotoTracker<br/>
     * ---------------------------------------------------------------------------
     * Creator's name and email: Austin Stanley, Stanleyar@Goldmail.etsu.edu<br/>
     * Course:  CSCI1260-002<br/>
     * Creation Date: Feb 15, 2013<br/>
     * Date of Last Modification: Feb 15, 2013
     * ---------------------------------------------------------------------------
     */
     
    /**
     * Enter type purpose here<br>
     *
     * <hr>
     * Date created: Feb 15, 2013<br>
     * Date last modified: Feb 15, 2013<br>
     * <hr>
     * @author Austin Stanley
     */
    public class pixList
    {
    	ArrayList<PixFile> pixList= new ArrayList<PixFile> ();
    }
     
    /**
    	 * Find a Pixfile based on file name <br>        
    	 *
    	 * <hr>
    	 * Date created: Feb 22, 2013 <br>
    	 * Date last modified: Feb 22, 2013 <br>
    	 *
    	 * <hr>
    	 * @param fileName
    	 * @return
    	 */
    	public Pixfile sortFileName(String fileName)
    	{
    		//create default Pixfile
    		Pixfile defaultPixfile = new Pixfile();
    		pixList.add(0, defaultPixfile);
     
    		//variable declarations
    		int index = 1;			//loop counter and index
    		boolean found = false;	//indicates if result is found
     
    		while (!found && index < pixList.size())
    		{
    			if (pixList.get(index).getTitle().equalsIgnoreCase(fileName))
    			{
    				found = true;
    				return pixList.get(index);
    			} //end if statement
    			index++;
    		} //end while loop
    		if (!found)
    		{
    			index = 0;
    		} //end if statement
    		return pixList.get(index);
    	} //end sortFileName
     
    	/**
    	 * Find a Pixfile based on the photographer <br>        
    	 *
    	 * <hr>
    	 * Date created: Feb 22, 2013 <br>
    	 * Date last modified: Feb 22, 2013 <br>
    	 *
    	 * <hr>
    	 * @param photographer
    	 * @return
    	 */
    	public Pixfile sortPhotographer(String photographer)
    	{
    		//create default Pixfile
    		Pixfile defaultPixfile = new Pixfile();
    		pixList.add(0, defaultPixfile);
     
    		//variable declarations
    		int index = 1;			//loop counter and index
    		boolean found = false;	//indicates if result is found
     
    		while (!found && index < pixList.size())
    		{
    			if (pixList.get(index).getPhotographerName().equalsIgnoreCase(photographerName))
    			{
    				found = true;
    				return pixList.get(index);
    			} //end if statement
    			index++;
    		} //end while loop
    		if (!found)
    		{
    			index = 0;
    		} //end if statement
    		return pixList.get(index);
    	} //end sortPhotographer
     
    	/**
    	 * Display the names of all Pixfiles <br>        
    	 *
    	 * <hr>
    	 * Date created: Feb 22, 2013 <br>
    	 * Date last modified: Feb 22, 2013 <br>
    	 *
    	 * <hr>
    	 * @return
    	 */
    	public String sortFileName()
    	{
    		//create a new StringBuilder
    		StringBuilder sb = new StringBuilder();
     
    		for (int i = 0; i < pixList.size(); i++)
    		{	
    			sb.append(pixList.get(i).getFileName());
    		} //end for loop
    		return sb.toString();	//convert sb to a String
    	} //end sortFileName
     
    	/**
    	 * Find the largest Pixfile <br>        
    	 *
    	 * <hr>
    	 * Date created: Feb 22, 2013 <br>
    	 * Date last modified: Feb 22, 2013 <br>
    	 *
    	 * <hr>
    	 * @return
    	 */
    	public Pixfile getHighestSize()
    	{	
    		double largest = 0; //storage for highest number
    		int i = 0;			//index number
    		for (i = 1; i < pixList.size(); i++)
    		{
    			if (pixList.get(i).getSize() > largest)
    				largest = pixList.get(i).getSize();
    		}//end for loop
    		return pixList.get(i);
    	} //end getHighestSize
     
    	/**
    	 * Find the smallest Pixfile <br>        
    	 *
    	 * <hr>
    	 * Date created: Feb 22, 2013 <br>
    	 * Date last modified: Feb 22, 2013 <br>
    	 *
    	 * <hr>
    	 * @return
    	 */
    	public Pixfile getLowestSize()
    	{
    		double smallest = 0; //storage for highest number
    		int i = 0;			//index number
    		for (i = 1; i < pixList.size(); i++)
    		{
    			if (pixList.get(i).getSize() > smallest)
    				smallest = pixList.get(i).getSize();
    		}//end for loop
    		return pixList.get(i);
    	} //end getLowestSize
    } //end pixList


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: HUGE PROBLEM - PLEASE HELP! ArrayList issues

    I can't figure out how to add a PixFile object (From the class PixFileDriver) into the PixFileList (Array List)
    Can you tell us where this problem is in the code? Add a comment like: //<<<<<<<<<< PROBLEM HERE
    and then tell us what comment to look for to find the code with the problem.

    PixFile is used over 88 times in this post.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help simplifying huge nested if statement?
    By racecar333 in forum Loops & Control Statements
    Replies: 6
    Last Post: November 1st, 2011, 01:03 AM
  2. Replies: 1
    Last Post: April 7th, 2011, 01:32 PM
  3. Streaming Raw Data to a Client - huge message loss
    By MarkusTandy in forum Java Networking
    Replies: 0
    Last Post: February 19th, 2011, 07:41 PM
  4. switch and math problem issues
    By emartino in forum Java Theory & Questions
    Replies: 1
    Last Post: January 19th, 2010, 05:34 PM