Selection sort not working
I can not figure out what is wrong with my code I have looked over it many times and cant find anything wrong with it. When I run the program it doesn't sort the list by the Item Id.
this is my tester class
import java.util.*;
public class Tester
{
public static void printInvetory(List<Item> a)
{
System.out.println("ItemID ItemName InStoreID Price");
System.out.println("-------------------------------------------");
for(int i = 0; i < a.size(); i++)
{
System.out.println(a.get(i));
}
}
public static void sortID(List<Item> a)
{
int i;
int k;
int posmax;
Item temp;
for(i = a.size() - 1; i >= 0; i--)
{
posmax = 0;
for(k = 0 ; k <= i ; k++)
{
if(a.get(k).getItemID() > a.get(posmax).getItemID())
posmax = k;
}
temp = a.get(i);
a.set(i, a.get(posmax));
a.set(posmax, temp);
}
}
public static void main(String[] args)
{
List<Item> myStore = new ArrayList<Item>();
myStore.add(new Item(0004, 200, 10.5, "Air Filters"));
myStore.add(new Item(0002, 60, 21.5, "Door Knobs"));
myStore.add(new Item(0006, 90, 9.99, "Hammers "));
myStore.add(new Item(0001, 80, 19.99, "Levels "));
myStore.add(new Item(0005, 100, 59, "Ceiling fans"));
myStore.add(new Item(0003, 55, 80, "Wrench Sets"));
printInvetory(myStore);
System.out.println();
printInvetory(myStore);
}
}
this is my constructor class
public class Item
{
private int itemID;
private int inStore;
private double Price;
private String itemName;
public Item(int id, int is, double p, String in)
{
itemID = id;
inStore = is;
Price = p;
itemName = in;
}
public String getName()
{
return itemName;
}
public int getItemID()
{
return itemID;
}
public int getInStore()
{
return inStore;
}
public double getPrice()
{
return Price;
}
public String toString()
{
return itemID + "\t" + itemName + "\t" + inStore + "\t" + "$ " + Price;
}
}
PLZ HELP.
Re: Selection sort not working
When posting code, please use the highlight tags to preserve formatting.
You're going to have to debug your code to figure out where its execution differs from your expectations. Recommended reading: http://www.javaprogrammingforums.com...t-println.html
Re: Selection sort not working
Your sortID method is never called... main method should look like this (assuming you only want to print the list once)...
Code :
public static void main(String[] args) {
List<Item> myStore = new ArrayList<Item>();
myStore.add(new Item(0004, 200, 10.5, "Air Filters"));
myStore.add(new Item(0002, 60, 21.5, "Door Knobs"));
myStore.add(new Item(0006, 90, 9.99, "Hammers "));
myStore.add(new Item(0001, 80, 19.99, "Levels "));
myStore.add(new Item(0005, 100, 59, "Ceiling fans"));
myStore.add(new Item(0003, 55, 80, "Wrench Sets"));
sortID(myStore);
printInvetory(myStore);
System.out.println();
}
Re: Selection sort not working
I am gonna print it every time I sort it.
Re: Selection sort not working
Wow I just looked back a noticed I never called the method ...... I feel Kinda dumb but thanks for the help.
Re: Selection sort not working
Quote:
Originally Posted by
killer3p0
Wow I just looked back a noticed I never called the method ...... I feel Kinda dumb but thanks for the help.
We all have those kinds of days... :)