Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass values to parameters

Tags:

java

when i trying to do this i got the problem said

Constructor Product in class Product cannot be applied to given types;
required: java.lang.String,int,double; found: java.lang.String;
reason: actual and formal arguments lists differ in length

And i have 2 classes:

import java.text.*
public class Product {
    private String name;
    private int stock;
    private double price;

    public Product(String name, int stock, double price) {
        this.name = name;
        this.stock = stock;
        this.price = price;
    }
public double sell(int n) {
        stock = n;
        return stock; 
    }
public void restock(int n) {
    }
@Override
    public String toString() {
        return stock + name + "at $"+price;
    }
}

public class Store {
   public static void main(String[] args) {
       new Store().use();
    }
    private Product product;
    private Product cashRegister;

    public Store() {
        product = new Product("Sticky tape");
        cashRegister = new Product("Cash register");
    }


    public void use() {
    }

    private void sell() {
    }

    private void restock() {
    }

    private void viewStock() {
    }

    private void viewCash() {
    }

    private void help() {
        System.out.println("Menu options");
        System.out.println("s = sell");
        System.out.println("r = restock");
        System.out.println("v = view stock");
        System.out.println("c = view cash");
        System.out.println("x = exit");
    }
}

I understand that i have to declare for Product constructor. But i think i have done it. If anyone know where i got wrong please explain. Thank you!

like image 487
COI Avatar asked Aug 14 '26 23:08

COI


1 Answers

you do not have constructor with one param, so you can not using this form

product = new Product("Sticky tape");

decare one more constructor with one param or fill all param

product = new Product("Sticky tape", 10, 20.0);
like image 97
Tiep Phan Avatar answered Aug 16 '26 14:08

Tiep Phan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!