Results 1 to 4 of 4
  1. #1
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Is there a less complicated way of doing this?

    My main, if I continue this, is going to reach hundreds of lines, and will probably take hours to finish.
    I feel like I'm hardcoding.
    What I'm supposed to be doing is making it so the program can accept multiple filter and sort commands at one time, and each should be separated by a whitespace.
    I was thinking about parsing the input again, using whitespace as the delimiter, then normally progressing with each token, as though there was only one command.

    However, coding this the way I'm doing it will firstly probably take hours, and secondly, it's likely not even right. I don't have any real way to determine which token contains which data from the Song objects.

    Any assistance you can provide would be helpful.

    These are the specific requirements for this portion:
    A sort/filter command consists of one or more of the following options:
    -year:<year(s)>
    -rank:<rank(s)>
    -artist:<artist>
    -title:<title>
    -sortBy:<field>
    Any number of these options may be given, and they may be given in any order. If multiple options are specified, they will be separated by whitespace.

    GazillionSongs Class (the main)
    Java Code:
     
    import java.util.*;
    import java.io.*;
    
    public class GazillionSongs {
    	public static void main(String[] args) throws FileNotFoundException {
    		System.out.println("Welcome to Java Song Collection!"); //greets the user
    		System.out
    		.println("This program sorts and filters large databases of popular songs."); //explains purpose of program
    		System.out.println("This program is able to filter and sort by year, artist, title and rank.");
    		System.out 
    		.println("Please enter a file that contains a database you wish to filter or sort. (i.e, alistofsongs.txt)"); // sample
    		// file
    		// =
    		// agazillionsongs.txt
    		Scanner input2 = new Scanner(System.in);
    		String filename = input2.nextLine();
    
    		File f = new File(filename);
    		Scanner tokens = new Scanner(f);
    
    		ArrayList<Song> songs = new ArrayList<Song>();
    		while ((tokens.hasNextLine())) {
    			songs.add(Song.parse(tokens.nextLine()));
    		}
    		
    		
    		System.out.println("Please select which commands you would like to use for the program.");
    		System.out.println("Please format your command like the following example: year:<year(s)> rank:<rank(s)> artist:<artist> title:<title> sortBy:<field>");
    		System.out.println();
    		System.out.println("You may pick any number of commands you want.");
    		System.out.println("For years and rank, you may select a range of years or ranks.");
    		System.out.println("For artists and titles, you may enter a partial name or title.");
    		System.out.println("i.e, year:1983 rank:1");
    		Scanner input = new Scanner(System.in);
    		SongCollection collection = new SongCollection(songs);
    				if (input.hasNextLine()){
    					String otherToken = null;
    					String otherToken2 = null;
    					String otherToken3 = null;
    					String inputType = input.nextLine();
    					StringTokenizer parseInput = new StringTokenizer(inputType, " ");
    					if (inputType.contains(" ")){
    					while (parseInput.hasMoreTokens());{
    						otherToken = parseInput.nextToken();
    						inputType = inputType.replace(otherToken, "");
    						otherToken2 = parseInput.nextToken();
    						inputType = inputType.replace(otherToken2, "");
    						otherToken3 = parseInput.nextToken();
    						inputType = inputType.replace(otherToken3, ""); 
    						}
    					}
    					
    					if (inputType.contains("year:")){
    						System.out.println(inputType);
    						if(inputType.contains(" ")){
    						collection.filterYear(Range.parse(inputType));
    						if (!((otherToken).equals(null))){
    							collection.filterYear(Range.parse(otherToken));
    							}
    						}
    						System.out.println(collection.toString());
    					}//end of year loop
    					
    					
    					
    					else if (inputType.contains("rank:")){
    					collection.filterRank(Range.parse(inputType));	
    					System.out.println(collection.toString());
    					}//end of rank
    					else if (inputType.contains("artist:")){
    						System.out.println("Please enter an artist that you wish to see songs from.");
    					System.out.println("(i.e, Paramore or Ashley Tisdale)");
    					String artistName = input.nextLine();
    					collection.filterArtist(artistName);	
    					System.out.println(collection.toString());
    					}//end of artist
    					else if (inputType.contains("title:")){
    						System.out.println("Please enter a song title you wish to see.");
    					System.out.println("(i.e, Misery Business)");
    					String songTitle = input.nextLine();
    					collection.filterTitle(songTitle);	
    					System.out.println(collection.toString());
    					}//end of title
    				}//end of if-has-next-line
    			}//end of filter loop 
    					//end of year loop
    				//end of if loop for if input exists			
    		
    	}//end of main
    //end of class
    Song class:
    Java Code:
    import java.util.Scanner;
    import java.util.StringTokenizer;
    
    public class Song  {
    	public int year;
    	public int rank;
    	public String artist;
    	public String title;
    
    	public static Song parse(String s) {
    		Song instance = new Song();
    		StringTokenizer tokenizer = new StringTokenizer(s, "\t");
    		instance.year = Integer.parseInt(tokenizer.nextToken());
    		instance.rank = Integer.parseInt(tokenizer.nextToken());
    		instance.artist = (tokenizer.nextToken());
    		instance.title = (tokenizer.nextToken());
    		return instance;
    	}
    
    	public int getYear() {
    		return year;
    	}
    
    	public int getRank() {
    		return rank;
    	}
    
    	public String getArtist() {
    		return artist;
    	}
    
    	public String getTitle() {
    		return title;
    	}
    
    	public String toString() {
    		String output = "\n\nYear = " + year + "\nRank = " + rank + "\nArtist = "
    				+ artist + "\nTitle = " + title;
    		return output;
    	}
    	//public int compareByYear(Song current) {
    		//return year.compareTo(current.year);
    		//}
    	
    	//public int compareByRank(Song current) {
    		//return rank.compareTo(current.rank);
    		//}	
    	
    	public int compareByArtist(Song current) {
    		return artist.compareTo(current.artist);
    		}
    	
    
    	public int compareByTitle(Song current) {
    		return title.compareTo(current.title);
    		}
    	}
    SongCollections class:
    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    import java.util.*;
    
    public class SongCollection {
    	ArrayList<Song> songs2;
    	ArrayList<Song> itemsToRemove = new ArrayList<Song>(); // second collection
    															// for items to
    															// remove
    
    	public SongCollection(ArrayList<Song> songs) { // constructor for
    													// SongCollection
    		this.songs2 = songs;
    	}
    
    	public void filterYear(Range r) {
    		for (Song song1 : songs2) {
    			if (r.contains(song1.getYear())){
    				itemsToRemove.add(song1);
    		}
    	}
    		songs2.removeAll(itemsToRemove);
    		itemsToRemove.clear();
    }
    
    	public void filterRank(Range r) {
    		for (Song song1 : songs2) {
    			if (r.contains(song1.getRank())){
    				itemsToRemove.add(song1);
    			}
    		}
    		songs2.removeAll(itemsToRemove);
    		itemsToRemove.clear();
    }
    
    	public void filterArtist(String s) {
    		for (Song song1 : songs2) {
    			if ((!(((song1.getArtist()).contains(s))))){
    				itemsToRemove.add(song1);
    		}
    	}
    		songs2.removeAll(itemsToRemove);
    		itemsToRemove.clear();
    }
    	public void filterTitle(String s) {
    		for (Song song1 : songs2) {
    			if ((!(((song1.getTitle()).contains(s))))){
    				itemsToRemove.add(song1);
    		}
    	}
    		songs2.removeAll(itemsToRemove);
    		itemsToRemove.clear();
    }
    	public void sortTitle() {
    		for (int i = 0; i != songs2.size(); i++) {
    			int minIndex = i;
    			Song min = songs2.get(minIndex);
    
    			for (int j = i + 1; j != songs2.size(); j++) {
    				Song current = songs2.get(j);
    				if (min.compareByTitle(current) < 0) {
    					minIndex = j;
    					min = current;
    				}
    			}
    
    			Song temp = songs2.get(i);
    			songs2.set(minIndex, temp);
    			songs2.set(i, min);
    
    		}
    	}
    
    	public void sortRank() {
    
    	}
    
    	public void sortArtist() {
    
    	}
    
    	public void sortYear() {
    
    	}
    
    	public String toString() {
    		String result = "";
    		for (int i = 0; i < songs2.size(); i++) {
    			result += " " + songs2.get(i);
    		}
    
    		return result;
    
    	}
    
    }
    Range class:
    Java Code:
    import java.util.StringTokenizer;
    
    public class Range {
    	private int min;
    	private int max;
    
    	public Range() {
    		System.out.println("Please wait.");
    	}
    
    	public static Range parse(String s) {
    		Range instance = new Range(); // instance is created here so object
    										// variables may be accessed
    		String field; // String to contain deleted part of user input
    		StringTokenizer tokenizer = new StringTokenizer(s, "-");
    		StringTokenizer tokenizer2 = new StringTokenizer(s, ":");// for separating "field:" from the
    																	// other part of the String
    		if (s.contains(":")) { // this deletes the "field:" of the user input so
    								// it does not interfere with the parsing
    			field = (tokenizer2.nextToken());
    			s = s.replace(field, "");
    			s = s.replace(":", "");
    		}
    		if (s.contains("-")) {
    			instance.min = Integer.parseInt(tokenizer.nextToken());
    			instance.max = Integer.parseInt(tokenizer.nextToken());
    
    		} else if (!(s.contains("-"))) {
    			{
    				instance.min = Integer.parseInt(s);
    				instance.max = Integer.parseInt(s);
    			}
    		}
    		return instance;
    	}
    
    	public boolean contains(int n) {
    		if (n > min && n < max) { //if the number is contained in the range, method returns true.
    			return true;
    		} else if (n == min && n == max) {
    			return true;
    		} else {
    			return false;
    		}
    	}
    
    	public int getMin() {
    		return min;
    	}
    
    	public int getMax() {
    		return max;
    	}
    }
    Last edited by Propinquity; 03-09-2014 at 04:48 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,661
    Rep Power
    5

    Default Re: Is there a less complicated way of doing this?

    Is there a less complicated way of doing this?
    Probably (and I haven't even looked at your code!). But you have said in other posts that you are restricted from using other methods not approved by your professor. Even if they are home grown. So it would be hard to help you stream line your code with that restriction. Anything that is recommend may turn out not to be permitted.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: Is there a less complicated way of doing this?

    Quote Originally Posted by jim829 View Post
    Probably (and I haven't even looked at your code!). But you have said in other posts that you are restricted from using other methods not approved by your professor. Even if they are home grown. So it would be hard to help you stream line your code with that restriction. Anything that is recommend may turn out not to be permitted.

    Regards,
    Jim
    Whatever you suggest should be fine as long as you don't change the modifiers of the methods that are already there. Like I said in the other thread, the requirements say,
    "The remaining classes should include the following methods, along with any other methods or fields you find necessary or useful."

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,661
    Rep Power
    5

    Default Re: Is there a less complicated way of doing this?

    One thing I would do right off is to change your design slightly.

    You Song class simply creates a single instance of a song.
    Your SongCollection class holds a collection of songs.

    So instead of adding them to an ArrayList<Song>, simply add them to a SongCollection.

    Then you can do something like the following:

    Java Code:
    public SongCollection filterRank(min, max) { 
       SongCollection songs = new SongCollection();
       for (Song song1 : songs2) {
         // check rank and add to songs.
       }
       return songs; // return the filtered collection.
    }
    The you can do something like this;
    Java Code:
    SongCollection rankCollection = filterRank(min, max);
    This allows several advantages.

    1. You don't need to maintain a separate list in SongCollection
    2. You can then filter the filtered lists even more.

    I would also try and have a central controller do all the input processing and parsing instead of doing it in separate classes. So simply
    figure out the min and max of your range and just pass that info to the filtering method.

    Of course, to make these changes at this time may all be a bit overwhelming so just think about the concepts and use what you want.

    Regards,
    Jim
    Last edited by jim829; 03-10-2014 at 02:56 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Java got complicated for me :( Need Help!
    By ram alday in forum New To Java
    Replies: 5
    Last Post: 01-26-2014, 02:19 PM
  2. Complicated for loop's
    By JordiLaForge in forum New To Java
    Replies: 6
    Last Post: 05-27-2013, 02:44 PM
  3. replacement for complicated if else?
    By TopNFalvors in forum New To Java
    Replies: 12
    Last Post: 04-09-2011, 07:05 PM
  4. Complicated Draw
    By Desdenova in forum New To Java
    Replies: 9
    Last Post: 05-27-2010, 08:44 PM
  5. Complicated Method
    By Desmond in forum New To Java
    Replies: 5
    Last Post: 03-17-2010, 11:31 AM

Posting Permissions

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