简体   繁体   中英

import and use methods among different classes?

I created a method called calRoomCharges(int cusID) in the class public class transactions extends javax.swing.JFrame

Then I import this class to the class called public class AddReservation extends javax.swing.JFrame

I imported the class as follows.

import myhotel.FrontOffice.transactions;

these both classes are in the same project.

But when I'm going to call method calRoomCharges(int cusID) in AddReservation class it says that there is no such method.

I did this in netbeans.

So howshould I import the class and use the method that I created in another class?

here is the method I created

  public double calRoomCharges(int cusId)
   {
       double tot=0.0;

        try
        {


            //Get Rates
            String sql="SELECT nights FROM reservation where cus_id ='"+cusId+"'";
            pst=conn.prepareStatement(sql);
            rs=pst.executeQuery();
            int nights = 0;
                if(!rs.next())
                {
                    //handle no result
                } 
                else 
                {
                    nights = rs.getInt("nights") ;

                }




            //Get number of rooms  

            String sql2="SELECT no_of_rooms FROM reservation where cus_id ='"+cusId+"'";
            pst=conn.prepareStatement(sql2);
            rs=pst.executeQuery();
            int rooms =0;

                if(!rs.next())
                {
                    //handle no result
                } 
                else 
                {
                    rooms = rs.getInt("no_of_rooms") ;

                }


            //Get rates

           String sql3="SELECT rates from room_type a,rooms b ,reservation r where a.type=b.type AND b.room_no=r.room_no AND r.cus_id='"+cusId+"' group by r.cus_id";
            pst=conn.prepareStatement(sql3);
            rs=pst.executeQuery();
            double roomRates =0.0;

                if(!rs.next())
                {
                    //handle no result
                } 
                else 
                {
                    roomRates = rs.getDouble("rates") ;

                }



            //Calculate room charges

            tot =roomRates * rooms * nights;



        }
        catch(Exception e){}

       return tot;
   }

And this is the way I called it

private void EmpnamefieldMouseClicked(java.awt.event.MouseEvent evt) {                                          

        int cusNo =Integer.parseInt(cusID.getText());
        double roomCharges = calRoomCharges(cusNo);
    }           

You have 2 possibilities of calling the calRoomCharges method:

the method is a static method and you can call it via:

transactions.calRoomCharges(123);

the method is an object method, so you have to create an object first like:

transactions obj = new transactions();

obj.calRoomCharges(123);

You simply have to either make the method static or call it through an object

public static double calRoomCharges(int cusId)
{
     // ...
}

and call it like that:

double roomCharges = transactions.calRoomCharges(cusNo);

or call it through a created object:

transactions t = new transactions();
double roomCharges = t.calRoomCharges(cusNo);

And a good thing is to name classes with a first capital letter so you'd have class Transactions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM