Results 1 to 3 of 3

Thread: Searching got problem....Help needed

  1. #1

    Thread Starter
    New Member raggy_man's Avatar
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    12

    Searching got problem....Help needed

    My lecturer gave me this question n somehow I have completed most of this portion. But still my Searching method is not working properly in the class Transaction.

    Here is the question:
    1. Create a class Counter having counterID, cashierID, openingBalance and closingBalance, date, and shiftNumber( range 1-3 )
    2. Create an ADT to create records, sort & search scale between any 2 dat, sales on the date (I did only sales on the date). Store records in object file or any database.
    3. Client class can call methods of ADT using its methods

    Check the codes below:
    Date class:

    public class Date implements java.io.Serializable {

    private int month ; // 1-12
    private int year ; // any year
    private int day ; // 1-31

    public Date() {

    setData( 0, 0 , 0 ) ;
    }


    public Date( int theMonth , int theYear , int theDay ) {

    setData( theMonth , theYear , theDay ) ;

    }

    private void setData( int theMonth , int theYear , int theDay ) {

    month = checkMonth( theMonth ) ;
    year = theYear ;
    day = checkDay( theDay ) ;

    System.out.printf( "Date object constructor for date %s\n" , this ) ;
    }

    // Utility method to confirm proper month value
    private int checkMonth( int testMonth ) {

    if( testMonth > 0 && testMonth <= 12 ) // valid month
    return testMonth ;
    // month is invalid
    else{
    System.out.printf( "Invalid month (%d) set to 1." , testMonth );
    return 1 ;
    } // end else
    } // end method checkMonth

    // Utility method to confirm proper day value based on month and year
    private int checkDay( int testDay ) {

    int daysPerMonth[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } ;

    //check if day in range for month
    if( testDay > 0 && testDay <= daysPerMonth[ month ] )
    return testDay ;

    //check for leap year
    if( month == 2 && testDay == 29 && ( year % 400 == 0 || ( year % 4 == 0 && year
    % 100 != 0 ) ) )
    return testDay ;

    System.out.printf( "Invalid day(%d) set to 1." , testDay ) ;
    return 1 ;
    } // end of method checkDay

    public int getMonth() {

    return month ;
    } // end method getMonth

    public int getDay() {

    return day ;
    } // end of method getDay

    public int getYear(){

    return year ;
    } // end of method getYear

    public boolean equals( Date that ) {

    return day == that.getDay() && month == that.getMonth() && year == that.getYear() ;
    }

    public int compareTo( Date that ) {

    return day > that.getDay() ? 1 : day < that.getDay() ? -1 :
    month > that.getMonth() ? 1 : month < that.getMonth() ? -1 :
    year > that.getYear() ? 1 : year < that.getYear() ? -1 : 0 ;
    }

    public String toString() {

    return String.format("%d/%d/%d ", month , day , year) ;
    }// end of toString method
    }

    Counter class:


    public class Counter implements java.io.Serializable {

    private String counterID, cashierID ;
    private int openingBal, closingBal ;
    private int shiftNumber ;
    private Date date ;

    public Counter() {

    setCounter( null, null, 0, 0, 0, null ) ;
    }

    public Counter( String countID, String cashID, int openBal, int cloBal,
    int shiftNum, Date d ) {

    setCounter( countID, cashID, openBal, cloBal, shiftNum, d ) ;
    }

    private void setCounter( String countID, String cashID, int openBal, int cloBal,
    int shiftNum, Date d ) {

    counterID = countID; cashierID = cashID; openingBal = openBal; closingBal = cloBal;
    shiftNumber = shiftNum; date = d;
    }

    public String getCounterID() {

    return counterID ;
    }

    public String getCashierID() {

    return cashierID ;
    }

    public int getOpeningBal() {

    return openingBal ;
    }

    public int getClosingBal() {

    return closingBal ;
    }

    public int getShiftNumber() {

    return shiftNumber ;
    }

    public Date getDate() {

    return date ;
    }

    public String toString() {

    return counterID + "\t" + cashierID + "\t" + openingBal + "\t" + closingBal
    + "\t" + shiftNumber + "\t" + date ;
    }

    }

    Transaction Class( used to create ADT ) :

    import java.util.* ;
    import java.io.* ;
    import javax.swing.* ;
    import static javax.swing.JOptionPane.* ;

    public class Transaction implements java.io.Serializable {

    private ArrayList list ;
    private Counter counter[] ;
    private int many ;
    private Counter count ;

    public Transaction() {

    list = new ArrayList() ;
    count = new Counter() ;
    many = 0 ;
    }

    public void createRecord() {
    int more = 0;
    do{
    String cId = showInputDialog( "Counter ID" ) ;
    String cashId = showInputDialog( "Cashir ID" ) ;
    int openBal = Integer.parseInt( showInputDialog( "Opeining Balance" ) ) ;
    int closeBal = Integer.parseInt( showInputDialog( "Closing Balance" ) ) ;

    int day = Integer.parseInt( showInputDialog( "Date" ) ) ;
    int month = Integer.parseInt( showInputDialog( "Month" ) ) ;
    int year = Integer.parseInt( showInputDialog( "Year" ) ) ;

    Date d = new Date( month, year, day ) ;
    int shiftNum = Integer.parseInt( showInputDialog( "shift" + "1-3" ) ) ;
    Counter c = new Counter( cId, cashId, openBal, closeBal, shiftNum, d ) ;
    list.add( c ) ; many++ ;
    more = Integer.parseInt( showInputDialog( "more-1 , Exit-0" ) ) ;

    }while( more != 0 ) ;

    Iterator iter = list.iterator() ;
    counter = new Counter[many] ;
    for( int j = 0; j < many; j++ )
    counter[j] = (Counter)iter.next() ;
    sortRecord() ; writeRecord() ;
    }

    public void sortRecord() {

    for( int i =0; i < many; i++ )
    for( int k = 0; k < many - 1; k++ )
    if ( counter[k].getDate().compareTo( counter[i].getDate() ) > 0 ) {
    Counter temp = counter[k] ;
    counter[k] = counter[k + 1] ;
    counter[k + 1] = temp ;
    }

    for( int i = 1; i < many; i++ )
    for( int k = 0; k < many - 1; k++ )
    if(( counter[k].getDate() == counter[i].getDate() )
    && (counter[k].getShiftNumber() > counter[i].getShiftNumber() )){

    Counter temp = counter[k] ;
    counter[k] = counter[k + 1] ;
    counter[k + 1] = temp ;
    }
    }

    private void writeRecord() {

    try{
    FileOutputStream fos = new FileOutputStream( "Transaction", false ) ;
    ObjectOutputStream oos = new ObjectOutputStream( fos ) ;
    for( Counter c : counter )
    oos.writeObject( c ) ;
    oos.close() ;

    }catch( FileNotFoundException fe ) {
    fe.printStackTrace() ;
    }catch( IOException ioe ) {
    ioe.printStackTrace() ;
    }
    }

    public String displayRecord() {

    String put = " " ;
    try{
    FileInputStream fis = new FileInputStream( "Transaction" ) ;
    ObjectInputStream ois = new ObjectInputStream( fis ) ;
    do{
    count = ( Counter )ois.readObject() ;
    put += count + "\n" ;

    }while( count != null ) ;
    ois.close() ;
    }catch( EOFException eofe ) {
    }catch( ClassNotFoundException ce ) {
    ce.printStackTrace() ;
    }catch( FileNotFoundException fe ) {
    fe.printStackTrace() ;
    }catch( IOException ioe ) {
    ioe.printStackTrace() ;
    }
    return put ;
    }

    public int searchBinary( Counter count[], Date d ) {

    int left = 0, right = count.length ;
    int mid = ( left + right ) / 2 ;
    while( left <= right ) {
    if( d == count[mid].getDate() ) return mid ;
    else if ( d.compareTo( count[mid].getDate() ) > 0 ) return left = mid + 1 ;
    else right = mid - 1 ;
    }
    return -1 ;
    }

    public void searchDate() {
    int choice = Integer.parseInt(JOptionPane.showInputDialog("Search record-1 Exit-0"));
    if( choice == 1 ){
    int day = Integer.parseInt( showInputDialog( "Date" ) ) ;
    int month = Integer.parseInt( showInputDialog( "Month" ) ) ;
    int year = Integer.parseInt( showInputDialog( "Year" ) ) ;

    Date d = new Date( month, year, day ) ;

    Object p = searchBinary( counter , d ) ;
    JOptionPane.showMessageDialog( null , p ) ;
    }
    }
    }

    Pls check on the searching machanism in the Transaction Class n let me know where is the error...N also guide me on how 2 improve it....

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Searching got problem....Help needed

    The Date constructor you're using is deprecated (use java.util.GregorianCalendar) and you're passing the arguments in the wrong order anyway.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    New Member raggy_man's Avatar
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    12

    Re: Searching got problem....Help needed

    Ya, my fren also told me that the DATe class is the problem. Anywayz thanx for helping me out bro!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width