import java.text.*;
import java.util.*;
public class TestItem
{
private static List<Item> hardware = new ArrayList<Item>();
public static void printInventory(List<Item> a)
{
System.out.println("itemID itemName Stock Price");
System.out.println("------------------------------------------");
for(Item i : a)
{
System.out.println(i);
}
System.out.println();
}
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;
Item gmaxobj = a.get(posmax);
int gmax = Integer.parseInt(gmaxobj.getID());
for ( k = 0 ; k <= i ; k++ )
{
Item gobj = a.get(k);
int g = Integer.parseInt(gobj.getID());
if (g > gmax)
posmax = k;
}
temp = a.get(i);
a.remove(i);
a.add(i, a.get(posmax));
a.remove(posmax);
a.add(posmax, temp);
}
System.out.println("Sorting by ID..");
System.out.println();
printInventory(hardware);
System.out.println();
}
public static void sortName(List<Item> a)
{
int i;
int k;
int posmax;
Item temp;
for ( i = a.size() - 1 ; i >= 0 ; i-- )
{
posmax = 0;
Item gmax = a.get(posmax);
for ( k = 0 ; k <= i ; k++ )
{
Item g = a.get(k);
if (gmax.getName().compareTo(g.getName()) < 0)
posmax = k;
}
temp = a.get(i);
a.set(i, a.get(posmax));
a.set(posmax, temp);
}
System.out.println("Sorting by Name..");
System.out.println();
printInventory(hardware);
System.out.println();
}
public static void sortInStore(List<Item> a)
{
int i;
int k;
int posmax;
Item temp;
for ( i = a.size() - 1 ; i >= 0 ; i-- )
{
posmax = 0;
Item gmax = a.get(posmax);
for ( k = 0 ; k <= i ; k++ )
{
Item g = a.get(k);
if (g.getStock() > gmax.getStock() )
posmax = k;
}
temp = a.get(i);
a.remove(i);
a.add(i, a.get(posmax));
a.remove(posmax);
a.add(posmax, temp);
}
System.out.println("Sorting by Stock..");
System.out.println();
printInventory(hardware);
System.out.println();
}
public static void sortPrice(List<Item> a)
{
int i;
int k;
int posmax;
Item temp;
for ( i = a.size() - 1 ; i >= 0 ; i-- )
{
posmax = 0;
Item gmax = a.get(posmax);
for ( k = 0 ; k <= i ; k++ )
{
Item g = a.get(k);
if (g.getPrice() > gmax.getPrice() )
posmax = k;
}
temp = a.get(i);
a.set(i, a.get(posmax));
a.set(posmax, temp);
}
System.out.println("Sorting by Price..");
System.out.println();
printInventory(hardware);
System.out.println();
}
public static void main(String [] args)
{
hardware.add(0, new Item("100", "Air Filters", 200, 10.5));
hardware.add(1, new Item("101", "Door Knobs", 60, 21.5));
hardware.add(2, new Item("102", "Sledge Hammers", 90, 9.99));
hardware.add(3, new Item("103", "Laser Levels", 80, 19.99));
hardware.add(4, new Item("104", "Ceiling Fans", 100, 59));
hardware.add(5, new Item("105", "Wrench Sets", 55, 80));
printInventory(hardware);
sortInStore(hardware);
sortID(hardware);
sortName(hardware);
//sortPrice(hardware, (int)Math.floor(findHigh(hardware)), (int)Math.floor(findLow(hardware)));
sortPrice(hardware);
}
}