Miyerkules, Pebrero 15, 2012

Data Structure Searching

import utils.*;
import java.io.*;
public class Orders {
    static Order orders[];
    public static void main(String [] args)throws Exception{
        FileInputStream fstream= new FileInputStream("data2.txt");
        DataInputStream in= new DataInputStream(fstream);
        BufferedReader br= new BufferedReader(new InputStreamReader(in));
        String strLine;
        String str[]= new String[5];
        Input input= new Input();
        String item;
        int count=0;
       
        while ((strLine= br.readLine())!= null)
               {
                   count++;
               }
        
        orders= new Order[count];
        for(int x=0; x<count; x++)
               {
                   orders[x]= new Order();
               }
       
        fstream= new FileInputStream("data2.txt");
        in= new DataInputStream(fstream);
        br= new BufferedReader(new InputStreamReader(in));
       
        for(int x=0; ((strLine= br.readLine())!= null); x++)
               {
                    str= strLine.split(",");
                    orders[x].setRegion(str[0]);
                    orders[x].setRep(str[1]);
                    orders[x].setItem(str[2]);
                    orders[x].setUnit(Integer.parseInt(str[3]));
                    orders[x].setCost(Float.parseFloat(str[4]));
               }


        sortOrders();       
        System.out.println("Enter item to search ");
        item= input.getString();
        linearSearch(item);       
    }
   
    public static void sortOrders()
        {
            int locOfSmallest;
            Order temp;

            for(int step=0; step<orders.length; step++)
                {
                    //find the location of the smallest element from the array[step] to the end of the array
                    locOfSmallest= step;
                        for(int loc=step; loc<orders.length; loc++)
                            {
                                if(orders[loc].getRegion().compareTo(orders[locOfSmallest].getRegion())<0)
                                {
                                    locOfSmallest= loc;
                                }
                            }
                    //exchange orders[step] with orders[locOfSmallest]
                    temp= orders[step];
                    orders[step]= orders[locOfSmallest];
                    orders[locOfSmallest]=temp;

                }
        }

    public static void linearSearch(String item)
    {
        boolean found= false;
        int loc=0;
        int[] array= new int[50];
        int x=0;

        for(int i=0; i<orders.length; i++)
        {
            if(item.equalsIgnoreCase(orders[i].getRegion()))
            {
                found= true;
                loc= i;
                array[x]= i;
                x++;
            }
        }


            if(!found)
            {
                System.out.println("Item not found");
            }

            else

            {
                System.out.print("FOund at locations ");
                for(int i=0; i<x; i++)
                {
                    System.out.print(array[i]+ " ");
                }
            }

    }




}

Walang komento:

Mag-post ng isang Komento