Results 1 to 13 of 13
  1. #1
    Crypts is offline Member
    Join Date
    May 2010
    Posts
    6
    Rep Power
    0

    Default One big arraylist and static mess

    ok, so i have an assignment that starts with an empty arraylist and using user input, call various methods to do things to the arraylist. but im having trouble with static methods and static variables or just the general layout. heres my code so far:

    Java Code:
    import java.util.ArrayList; 
    import java.util.Scanner;
    import java.io.*;
    import java.lang.String;
    
     
    public class Part2
    {
    
    	private ArrayList<Collection> collections = new ArrayList<Collection>();
    	
    	private boolean quit = false
    		
    	public static void main(String[] args)
    	{
    			
    		while(quit == false)
    			mainMenu();
    			selection();
    		
    	}
    
    	
    	public static void mainMenu()
    	{
    		System.out.println("Please make a selection, by entering the letter which corresponds to the desired action:");
    		System.out.println("");
    		System.out.println("[P] Print out details of list");
    		System.out.println("[E] Is the list empty?");
    		System.out.println("[N] Number of elements in list");
    		System.out.println("[C] Create empty list");
    		System.out.println("[I] Insert element into list");
    		System.out.println("[D] Remove element from list with certain variable");
    		System.out.println("[R] Remove element from list with a certain index");
    		System.out.println("[F] Read data from file");
    		System.out.println("[W] Write current list to file");
    		System.out.println("[A] Print out average collection size");
    		System.out.println("[M] Print out maximum collection size");
    		System.out.println("[V] Print elements in list with a collection of a certain size");
    		System.out.println("[U] Find whether a certain name owns a collection");
    		System.out.println("[H] Change collection size of a certain person");
    		System.out.println("[Q] Quit program ");
    				
    	}
    	
    	public boolean selection()
    	{
    		Scanner in = new Scanner(System.in);
    		String selection = in.next();
    		selection = selection.toUpperCase();
    		
    		if(selection.length() > 1)
    		{
    			System.out.println("That is not a valid input. Please make sure you enter only one character. Try again:");
    			selection();
    		}
    		
    		if(selection.equals("I"))
    			insertElement();
    		else if(selection.equals("P"))
    			printListDetails();
    		else if(selection.equals("E"))
    			listEmpty();
    		else if(selection.equals("N"))
    			numberOfCollections();
    		else if(selection.equals("C"))
    			createEmptyList();
    		else if(selection.equals("D"))
    			removeElementCertainVariable();
    		else if(selection.equals("R"))
    			removeElementCertainIndex();
    		else if(selection.equals("F"))
    			readFile();
    		else if(selection.equals("W"))
    			WriteCurrentList();
    		else if(selection.equals("A"))
    			System.out.println(calculateAverage());
    		else if(selection.equals("M"))
    			System.out.println(findGreatestCollection());
    		else if(selection.equals("V"))
    			findElementsCertainSize();
    		else if(selection.equals("U"))
    			findElementCertainName();
    		else if(selection.equals("H"))
    			changeCollectionSize();
    		else if(selection.equals("Q"))
    			quit = true;
    		else
    			System.out.println("That is not a valid input. Please make sure you enter a character that corresponds to an action. Try again:");
    		
    		quit = false;	
    	}
    	
    	public void insertElement()
    	{
    		System.out.println("Who is the collection owned by?");
    		Scanner in = new Scanner(System.in);
    		String name = in.next();
    		for(int i=0; i<collections.size(); i++)
    		{
    			Collection a = collections.get(i);
    			if(name.equals(a.getOwner()))
    			{
    				System.out.println("There is already an account owned by that name. Try a different name next time");
    			}
    		}
    		System.out.println("How many CD's are in the collection?");
    		Scanner in2 = new Scanner(System.in);
    		int quantity = in2.nextInt();
    		Collection c1 = new Collection(name);
    		c1.addCD(quantity);
    		System.out.println(c1.getOwner());
    		
    	}
    it says that non-static variable quit cannot be referenced from a static context, and that the non static method selection() cannot be referenced from a static context, and i dont understand how to fix it
    if i can get all that working, i should be able to do all the methods that manipulate the arraylist without too much hassle

    EDIT: also, any hints on better layout/design are more than welcome

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    4

    Default

    So in my mind, I think that you have to ask yourself, is this project going to be a completely static project, or is it going to be an OOP-based project, and you'll have to make this decision first before trying to make any changes to this program.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by curmudgeon View Post
    So in my mind, I think that you have to ask yourself, is this project going to be a completely static project, or is it going to be an OOP-based project, and you'll have to make this decision first before trying to make any changes to this program.
    Yep. Actually think about the OOP-based approach is more and more appropriate. In later time, extensibility to the application is very high.

  4. #4
    Crypts is offline Member
    Join Date
    May 2010
    Posts
    6
    Rep Power
    0

    Default

    so what is OOP-based? and how is it different from the static one?

  5. #5
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    OOP - object oriented programming. It relies on objects doing certain tasks to solve the problem at hand. A static aproach instead has a few functions that take parameters and return the results. Both aproaches solve a problem, it isn't really a question of what's right and what's wrong. It more of an organisational decision you have to make. I myself swear by OOP, since the programs I write now are actually legible and easy to maintain.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  6. #6
    Crypts is offline Member
    Join Date
    May 2010
    Posts
    6
    Rep Power
    0

    Default

    ahk, so how would i change my code to create an OOP project?

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,608
    Rep Power
    23

    Default

    Remove all static methods except for main(). Have main() create an object or objects that has the methods you need. Call the methods as needed using object references:
    object.method()

  8. #8
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

  9. #9
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    You could use a switch statement instead of that giant else if group also. It doesn't change much but makes it a little easier to read.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by xcallmejudasx View Post
    You could use a switch statement instead of that giant else if group also. It doesn't change much but makes it a little easier to read.
    Yeah that's 100% correct. Too many if-else conditions make really hard to read the code. And in most of the cases it's easy to handle different conditions.

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,608
    Rep Power
    23

    Default

    One problem with converting to switch is that the expression is a primitive like int. The users values are Strings. Looks like a one for one conversion from a single letter String to an int however.

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. Replies: 4
    Last Post: 05-12-2010, 11:05 AM
  2. Replies: 3
    Last Post: 02-11-2010, 09:59 AM
  3. Replies: 5
    Last Post: 11-04-2009, 02:25 AM
  4. Replies: 1
    Last Post: 08-07-2007, 05:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •