aznishboy

Store

Jan 8th, 2012
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.34 KB | None | 0 0
  1. http://pastebin.com/1HCRsgC3
  2. import chn.util.*;
  3. import apcslib.*;
  4. import java.util.*;
  5.  
  6. public class Store{
  7.     private Item[] myStore;
  8.     public Store(String fileName){
  9.         FileInput inFile = new FileInput(fileName);
  10.         int size = inFile.readInt();
  11.         myStore = new Item[size];
  12.         int a = 0;
  13.         while(inFile.hasMoreTokens()){
  14.             int id = inFile.readInt();
  15.             int inv = inFile.readInt();
  16.             myStore[a] = new Item(id, inv);
  17.             a++;
  18.         }
  19.     }
  20.     public void displayStore(){
  21.         int count = 0;
  22.         System.out.println('\t' + "Id" + '\t' + "Inv");
  23.         for(int i = 0; i < myStore.length; i++){
  24.             System.out.println((i+1) + "    " + myStore[i].toString());
  25.             count++;
  26.             if(count == 10){
  27.                 System.out.println();
  28.                 count = 0;
  29.             }
  30.         }
  31.     }
  32.     public void doSort(){
  33.         quickSort(myStore, 0, myStore.length - 1);
  34.     }
  35.     private void quickSort(Item[] list, int first, int last){
  36.         int g = first, h = last;
  37.         int midIndex = (first + last) / 2;
  38.         Item dividingValue = list[midIndex];
  39.         do{
  40.             while (list[g].compareTo(dividingValue) < 0){
  41.                 g++;
  42.             }
  43.             while (list[h].compareTo(dividingValue) > 0){
  44.                 h--;
  45.             }
  46.             if (g <= h){
  47.                 Item temp = list[g];
  48.                 list[g] = list[h];
  49.                 list[h] = temp;
  50.                 g++;
  51.                 h--;
  52.             }
  53.         }
  54.         while (g < h);
  55.         if (h > first){
  56.             quickSort (list,first,h);
  57.         }
  58.         if (g < last){
  59.             quickSort (list,g,last);
  60.         }
  61.     }
  62.     public void testSearch(){
  63.         int idToFind;
  64.         int invReturn;
  65.         int index;
  66.         ConsoleIO console = new ConsoleIO();
  67.         System.out.println("Testing search algorithm\n");
  68.         System.out.print("Enter Id value to search for (-1 to quit) ---> ");
  69.         idToFind = console.readInt();
  70.         while (idToFind >= 0){
  71.             index = bsearch(new Item(idToFind, 0));
  72.             System.out.print("Iterative binary search: Id # " + idToFind);
  73.             if (index == -1)
  74.                 System.out.println("     No such part in stock");
  75.             else
  76.                 System.out.println("     Inventory = " + myStore[index].getInv());
  77.             index = bsearch (new Item(idToFind, 0), 0, myStore.length-1);
  78.             System.out.print("Recursive binary search: Id # " + idToFind);
  79.             if (index == -1)
  80.                 System.out.println("     No such part in stock");
  81.             else
  82.                 System.out.println("     Inventory = " + myStore[index].getInv());     
  83.             System.out.print("\nEnter Id value to search for (-1 to quit) ---> ");
  84.             idToFind = console.readInt();
  85.         }
  86.     }
  87.     int bsearch(Item idToSearch){
  88.         int first = 0;
  89.         int last = myStore.length - 1;
  90.         while (first <= last){
  91.             int mid = (first + last)/2;
  92.             if (myStore[mid].compareTo(idToSearch) == 0)
  93.                 return mid;
  94.             else if (myStore[mid].compareTo(idToSearch) > 0)
  95.                 last = mid - 1;
  96.             else first = mid + 1;  
  97.         }
  98.         return -1;
  99.     }
  100.     int bsearch (Item idToSearch, int first, int last){
  101.         int mid = (first + last)/2;
  102.         if(first > last)
  103.             return -1;
  104.         else if(myStore[mid].equals(idToSearch))
  105.             return mid;
  106.         else{
  107.             if(myStore[mid].compareTo(idToSearch) > 0){
  108.                 return bsearch(idToSearch, first, mid - 1);
  109.             }
  110.             else
  111.                 return bsearch(idToSearch, mid + 1, last);
  112.         }
  113.     }
  114.  
  115.     public static void main(String[] args) {
  116.         Store miniMart = new Store("file50.txt");
  117.         System.out.println("Database before sorted: ");
  118.         System.out.println();
  119.         miniMart.displayStore();
  120.         miniMart.doSort();
  121.         System.out.println();
  122.         System.out.println("Database after sorted by id: ");
  123.         System.out.println();
  124.         miniMart.displayStore();
  125.         miniMart.testSearch();
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment