I have the following code which is in a class called customerModel. How do I call this method to another method inside another class called customerController? I am abit new to this and have been trying for ages but have had no luck. Please help thank you.

public class customerModel{
    .......
    .......

    public ArrayList<Customer> search(Customer cust) {
        ArrayList<Customer> custs = new ArrayList<Customer>();
        Connection connection = GaritsConnectivity.connect();
        Tuple<ResultSet, Statement> trs = GaritsConnectivity.read("SELECT * FROM Customer WHERE Name=1", connection);
        ResultSet rs = trs.getFirst();
        try {
            //takes a customer, returns it with the correct ID set
            while (rs.next()) {
                custs.add(createFromRs(rs));
            }
        } catch (SQLException ex) {
            GaritsConnectivity.manageException("Customer search failure", ex);
        }
        GaritsConnectivity.doneReading(connection, trs);
        return custs;
    }
}

The above code is inside customerModel and below is the code I have inside customerController

package Customer;

import GUI.TestFrame;
import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Stack;
import javax.swing.JPanel;

public class CustomerController {

    private CustomerModel cMod;
    private CustomerView cView;
    private TestFrame context;

    //creates the view and model objects
    public CustomerController(GaritsFrame context) {
        this.context = context;
        cView = new CustomerView(this);
        context.stackPush(cView);
        context.setContentPane(cView);
        context.pack();
        cView.disableFields();
        cMod = new CustomerModel();
        populateCustomerList();

    public boolean deleteCusts(List<Customer> custs) {
        boolean retB = true;
        for(Customer cust : custs){
            retB = retB && cMod.deleteSingle(cust);
        }
        return retB;
    }

    public boolean search(){
        //what goes in here im lost? how can i connect the above search from customerModel to customerController? 

    }

    public TestFrame getContext(){
        return context;
    }
}

Recommended Answers

All 5 Replies

To call an instance method of the CustomerModel class you need an instance of that class.
Your CustomerController creates such an instance on line 25 (cMod), so you can use that to call the method(s), as in
cMod.search(etc...

The issue is i know how to do that but dont know the exact fields or parameters which should go inside cMod.search(....);

The signature of that method is
public ArrayList<Customer> search(Customer cust) {
so you must pass a single instance of Customer as the parameter, and it will return you a list of Customers

(although having looked at the code of that method it makes little sense to me)

yes james is correct.
You have to pass instance of Customer class as a paramter.Most likely Customer class is a DTO(data transfer objects) class that just contains mainly setter and getter methods.So before calling,first do the required validation for instance of Customer class,if required to set any value,then set it using setter method and pass it to search() method.

What is more worrying is that the search method never uses the Customer value that is passed into it - obviously some mistake there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.