Hello. My task is import data from a text file into an array, and then sort the array and print out the sorted array. Each element of the array represents a clothing item and has name,category,quantity, and price values. I have an Item class to represent a single clothing item, a sorting class with multiple sorting methods, and a class to test the sort and print the array (with the main method). Category values are either "C","W", or "M" (child, women, or men), and i have to sort the array in the order of C, then W, then M. How can i edit my compareTo method to do so? since M comes before W, i do not know how to write my compareTo method to sort the items of category W before the ones of category M.
the data file looks as follows: (if theres a better way to type this out here on these forums, please let me know)
Suit C 12 12.99
Shirt C 25 12.99
Blazer W 16 45.79
Suit M 12 299.00
Suit W 17 127.99
Suit C 12 12.99
Pants W 33 54.99
Pants C 22 10.59
Pants M 12 10.59
Skirt W 17 39.99
Sweater C 25 10.27
Sweater W 17 47.99
heres the code that i have so far:
/**
* Class ItemTester
* Creates and prints an array of items
* Asks user to choose how to sort array
* Prints sorted array of items
*/
import java.util.Scanner;
import java.io.*;
public class ItemTester
{
public static void main(String[] args) throws IOException
{
String line;
Scanner scanToken;
Scanner scan = new Scanner(new File("Indata.txt"));
Item[] itemList = new Item[12];
String name;
String category;
int quantity;
double price;
int size = 0;
while (scan.hasNext())
{
line = scan.nextLine();
scanToken = new Scanner(line);
scanToken.useDelimiter(" ");
name = scanToken.next();
category = scanToken.next();
quantity = scanToken.nextInt();
price = scanToken.nextDouble();
itemList[size] = new Item(name,category,quantity,price);
size++;
}
System.out.println("Printout of the array");
System.out.println();
System.out.println("Name Category Quantity Price");
System.out.println();
for ( int i = 0; i < size; i++)
{
System.out.println(itemList[i] + "\t");
}
System.out.println();
System.out.print("Array has " + size + " items");
System.out.println();
System.out.println();
System.out.println();
System.out.println();
Sorting.selectionSort(itemList);
for ( int i = 0; i < size; i++)
{
System.out.println(itemList[i] + "\t");
}
}
}
/**
* Class Item
* Represents A Single Item
*/
import java.text.NumberFormat;
public class Item implements Comparable
{
// instance variables
public String name;
public String category;
public int quantity;
public double price;
/**
* Constructor for objects of class Item
*/
public Item(String n, String c, int q, double p)
{
// initialise instance variables
name = n;
category = c;
quantity = q;
price = p;
}
/**
*
* Methods of class Item
*/
public String toString()
{
NumberFormat fmt =NumberFormat.getCurrencyInstance();
return name + "\t\t" + category + "\t\t" + quantity + "\t\t" + fmt.format(price);
}
public String getName()
{
return name;
}
public String getCategory()
{
return category;
}
public int getQuantity()
{
return quantity;
}
public double getPrice()
{
return price;
}
public int compareTo(Object other)
{
int result;
String otherName = ((Item)other).getName();
String otherCategory = ((Item)other).getCategory();
int otherQuantity = ((Item)other).getQuantity();
double otherPrice = ((Item)other).getPrice();
if (category.equals(otherCategory))
result = name.compareTo(otherName);
else
result = category.compareTo(otherCategory);
return result;
}
}
/**
* Class Sorting
* Contains 3 different sort methods
*/
public class Sorting
{
public static void selectionSort (Comparable[] iArray)
{
int min;
Comparable temp;
for (int index = 0; index < iArray.length-1; index++)
{
min = index;
for (int scan = index+1; scan < iArray.length; scan++)
if (iArray[scan].compareTo(iArray[min]) < 0)
min = scan;
// Swap the values
temp = iArray[min];
iArray[min] = iArray[index];
iArray[index] = temp;
}
}
public static void insertionSort (Comparable[] iArray)
{
for (int index = 1; index < iArray.length; index ++)
{
Comparable key = iArray[index];
int position = index;
// Shift larger values to the right
while (position > 0 && key.compareTo(iArray[position-1]) < 0)
{
iArray[position] = iArray[position -1];
position--;
}
iArray[position] = key;
}
}
public static void bubbleSort(Comparable[] iArray)
{
boolean changed = false;
for(int a = 0; a < iArray.length - 2; a++)
{
if(iArray[a].compareTo(iArray[a + 1]) > 0)
{
Comparable tmp = iArray[a];
iArray[a] = iArray[a + 1];
iArray[a + 1] = tmp;
changed = true;
}
}
}
}
If anyone could give me suggestions on how to revise my compareTo() method, it would be greatly appreciated. I need to design it in such a way that items are compared based on category first. (all C before W and all W before M). If two items are in the same category then they are based on quantity. If quantity is also the same, then items are compared based on their name.
Thanks again for any help