Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- http://pastebin.com/1HCRsgC3
- import chn.util.*;
- import apcslib.*;
- import java.util.*;
- public class Store{
- private Item[] myStore;
- public Store(String fileName){
- FileInput inFile = new FileInput(fileName);
- int size = inFile.readInt();
- myStore = new Item[size];
- int a = 0;
- while(inFile.hasMoreTokens()){
- int id = inFile.readInt();
- int inv = inFile.readInt();
- myStore[a] = new Item(id, inv);
- a++;
- }
- }
- public void displayStore(){
- int count = 0;
- System.out.println('\t' + "Id" + '\t' + "Inv");
- for(int i = 0; i < myStore.length; i++){
- System.out.println((i+1) + " " + myStore[i].toString());
- count++;
- if(count == 10){
- System.out.println();
- count = 0;
- }
- }
- }
- public void doSort(){
- quickSort(myStore, 0, myStore.length - 1);
- }
- private void quickSort(Item[] list, int first, int last){
- int g = first, h = last;
- int midIndex = (first + last) / 2;
- Item dividingValue = list[midIndex];
- do{
- while (list[g].compareTo(dividingValue) < 0){
- g++;
- }
- while (list[h].compareTo(dividingValue) > 0){
- h--;
- }
- if (g <= h){
- Item temp = list[g];
- list[g] = list[h];
- list[h] = temp;
- g++;
- h--;
- }
- }
- while (g < h);
- if (h > first){
- quickSort (list,first,h);
- }
- if (g < last){
- quickSort (list,g,last);
- }
- }
- public void testSearch(){
- int idToFind;
- int invReturn;
- int index;
- ConsoleIO console = new ConsoleIO();
- System.out.println("Testing search algorithm\n");
- System.out.print("Enter Id value to search for (-1 to quit) ---> ");
- idToFind = console.readInt();
- while (idToFind >= 0){
- index = bsearch(new Item(idToFind, 0));
- System.out.print("Iterative binary search: Id # " + idToFind);
- if (index == -1)
- System.out.println(" No such part in stock");
- else
- System.out.println(" Inventory = " + myStore[index].getInv());
- index = bsearch (new Item(idToFind, 0), 0, myStore.length-1);
- System.out.print("Recursive binary search: Id # " + idToFind);
- if (index == -1)
- System.out.println(" No such part in stock");
- else
- System.out.println(" Inventory = " + myStore[index].getInv());
- System.out.print("\nEnter Id value to search for (-1 to quit) ---> ");
- idToFind = console.readInt();
- }
- }
- int bsearch(Item idToSearch){
- int first = 0;
- int last = myStore.length - 1;
- while (first <= last){
- int mid = (first + last)/2;
- if (myStore[mid].compareTo(idToSearch) == 0)
- return mid;
- else if (myStore[mid].compareTo(idToSearch) > 0)
- last = mid - 1;
- else first = mid + 1;
- }
- return -1;
- }
- int bsearch (Item idToSearch, int first, int last){
- int mid = (first + last)/2;
- if(first > last)
- return -1;
- else if(myStore[mid].equals(idToSearch))
- return mid;
- else{
- if(myStore[mid].compareTo(idToSearch) > 0){
- return bsearch(idToSearch, first, mid - 1);
- }
- else
- return bsearch(idToSearch, mid + 1, last);
- }
- }
- public static void main(String[] args) {
- Store miniMart = new Store("file50.txt");
- System.out.println("Database before sorted: ");
- System.out.println();
- miniMart.displayStore();
- miniMart.doSort();
- System.out.println();
- System.out.println("Database after sorted by id: ");
- System.out.println();
- miniMart.displayStore();
- miniMart.testSearch();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment