Sorting printed ArrayList of user inputted strings.
My program asks for 3 parameters for a virtual portfolio; song title, date, and number of instrumental/miscellaneous parts.
After these are inputted, the program prints them onto the screen and asks if you wish to continue "composing" ie; adding more songs. If not, the program should say how many songs are in the tracklist and print out the ArrayList of tracks in alphabetical order.
I have every aspect completed in the program except for the alphabetical sorting. This is what i've tried.
You can compile this in bluej or dr java or whatever you use if you want to see how it works.
import java.util.*;
public class Portfolio implements Comparable
{
private String title;
private String date;
private int parts;
// -------------------------------------------------------
// Creates a new portfolio with the given information.
// -------------------------------------------------------
public Portfolio (String songTitle, String songDate, int songParts)
{
title = songTitle;
date = songDate;
parts = songParts;
}
// -------------------------------------------------------
// Returns a string with the information about the item
// -------------------------------------------------------
public String toString ()
{
return ("title: " + title + "\t\t" + "Date composed: " + (date) + "\t Instruments: " + parts + "\t");
// + (date*quantity));
}
// -------------------------------------------------
// Returns the date the song was created
// -------------------------------------------------
public String getDate()
{
return date;
}
// -------------------------------------------------
// Returns the title of the composition
// -------------------------------------------------
public String getTitle()
{
return title;
}
// ----------------------------------------------------------------------------
// Returns the number of instrumental/miscellaneous parts in the composition
// ----------------------------------------------------------------------------
public int getParts()
{
return parts;
}
// -------------------------------------------------
// Sorts the compositions alphabetically by title
// -------------------------------------------------
public int compareTo(Object other)
{
int result;
if (title.equals(((Portfolio)other).title))
result = date.compareTo(((Portfolio)other).date);
else
result = title.compareTo(((Portfolio)other).title);
return result;
}
}
____________________
Now the main program:
____________________
import java.util.Collections;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.ListIterator;
public class Track
{
public static void main (String[] args)
{
Scanner scan = new Scanner (System.in);
ArrayList<Portfolio> tracks = new ArrayList<Portfolio>();
//Defines the elements in the array
String songTitle;
String songDate;
int songParts;
int total = 0;
//Defines the user input necessary to run the program. Action is in the do-while loop.
String keepWriting = "y";
//Do while loop
do
{
//Asks for the name of the composition and goes to next line.
System.out.print ("Enter the name of the song or composition: ");
songTitle = scan.nextLine();
//Asks for the date at which the composition was created and goes to next line.
System.out.print ("Enter the date: ");
songDate = scan.nextLine();
//Asks for the number of instrumentla or miscellaneous parts in the song.
System.out.print ("Enter the number of parts in the song: ");
songParts = scan.nextInt();
scan.nextLine();
// Creates a new song and adds it to the tracklist
Portfolio portfolio = new Portfolio(songTitle, songDate, songParts);
tracks.add (portfolio);
// Prints the contents of the tracks object using println
total += (portfolio.getParts());
ListIterator iterator = tracks.listIterator();
while (iterator.hasNext())
System.out.println(iterator.next());
//Asks if the user wishes to continue programming.
System.out.print ("Continue composing (y/n)? ");
keepWriting = scan.nextLine();
}
//End of the do while loop. Continues the programming if "y" was inputed. Otherwise, prints out
//the current contents of the array.
while (keepWriting.equals("y"));
System.out.println("There are " + tracks.size() + " songs in your tracklist" + "\n" + tracks);
System.out.println("Keep the tunes a'comin!");
}
}
The compareTo method in the portfolio class does not work for me. I want to sort the ArrayList ONLY if the user no longer wants to add more songs to the tracklist.
THANKS A MILLION to anyone who goes through my code and helps me out!! :p