1

So I have this

public class Client {

    private String name;
    private List<Product> purchase = new ArrayList<Product>();

    Client (){}

    Client (String name, List<Product> purchase ){
        this.name = name;
        this.purchase = purchase;
    }

}

and in Main I want to create a new client and put it in database.

public class Main {

    public static void main(String[] args) {

        InterDao daoProduct = new ProductDAOImpl();
        Product product1 = new Product(12,"waffle","2017-05-03",5);
        Product product2 = new Product (5,"fff","2017-05-08",7);

        List<Product> products = new ArrayList<Product>();

        products.add(product1);
        products.add(product2);
        daoProduct.create(product1);

        InterDao daoClient = new ClientDAOImpl();
        Client client1 = new Client("John", product1);

        daoClient.create(client1);

    }

}

Obviously,

Client client1 = new Client("John", product1);

does not work. I have tried so many ways of calling a specific product but none of them worked. Please give me some ideas.

This gives me this error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: The constructor Client(String, Product) is undefined

Abdullah Khan
  • 12,010
  • 6
  • 65
  • 78
wanna-be
  • 19
  • 3

3 Answers3

0

Your constructor takes a list of products. You have to change it like this:

Client (String name,Product purchase ){
    this.name = name;
    products.add(purchase);
}
Abdullah Khan
  • 12,010
  • 6
  • 65
  • 78
SF23
  • 174
  • 1
  • 12
  • In Client i dont have products declared. I made this instead `Client (String name, Product purchase ){ this.name = name; addProduct(0, purchase); }` – wanna-be May 07 '17 at 10:56
  • but in Main i get this `Invalid argument value: java.io.NotSerializableException` – wanna-be May 07 '17 at 10:58
  • You have to implement the Interface serializable in your class product. You can look here too [link](http://stackoverflow.com/questions/13895867/java-io-notserializableexception) – SF23 May 07 '17 at 11:02
  • I should make a new interface and then Product must implements it? – wanna-be May 07 '17 at 11:05
  • No the interface serializable exists already. You just have to implement it in your class. – SF23 May 07 '17 at 11:06
  • I did that Now it looks like this: `public class Client implements Serializable { private String name; private List purchase = new ArrayList(); Client (){} Client (String name,Product purchase){ this.name = name; this.purchase = (List) purchase; }` – wanna-be May 07 '17 at 11:21
  • `this.purchase.add(purchase); `will add the object directly to you List. – SF23 May 07 '17 at 11:24
0

Either pass products as second argument or you need to change signature of your constructor, to take second argument as instance of product.

Can you specify intention of your solution. Is it to save one product per client or list of products per client in single call?

Nipun
  • 65
  • 9
  • I want to create client and add to it specific product bought. In my db i have table product(id, item, quantity,date) and table client (name, foreign keys of all the columns in product) – wanna-be May 07 '17 at 11:01
  • Then you simply need to change your Client class. The instance variable should be Product instance and not a list. Secondly your constructor should be taking two arguments - name, product. – Nipun May 07 '17 at 11:07
0

you have to pass List<Products> to your constructor instead of Product object (instance) or you have to modify your constructor to be like:

Client (String name, Product purchase ){
    this.name = name;
    this.purchase.add(purchase);
}
Ali Alkhatib
  • 83
  • 12
  • I did that, but still gives me SQL errors. `Invalid argument value: java.io.NotSerializableException` and `Caused by: java.io.NotSerializableException: project.Product` – wanna-be May 07 '17 at 11:31
  • can you show me 'daoClient.create' function body ? ... and i think there is some thing you have to check with your SQL INSERT or stored procedure params – Ali Alkhatib May 07 '17 at 11:39
  • `public void create(Client obj) { try{ Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/project" , "root" , "pass"); PreparedStatement myStmt = myConn.prepareStatement("insert into client "+ "(name , cl_id, cl_item, cl_quantity,cl_date)" + " values (?,?,?,?,?)"); myStmt.setString(1, obj.getName()); myStmt.setObject(2,obj.getProduct(obj)); myStmt.executeUpdate(); System.out.println("insert complete"); } catch (Exception exc){ exc.printStackTrace(); } }` – wanna-be May 07 '17 at 11:50
  • there is a lot of code missing so i can't understand clearly where is the error occurs specifically but you have to check that and the related code to it: myStmt.setObject(2, obj.getProduct(obj)); ... hope you find where is your problem. – Ali Alkhatib May 07 '17 at 12:28