Home > Software design >  Construtor Drink in class Drink cannot be applied to given type
Construtor Drink in class Drink cannot be applied to given type

Time:07-31

I am facing an error. The code contains two classes, one is Store and the other is Drink. The purpose is to calculate the total cost. I have written the code according to the UML class diagram which is given. I declared classes and variable according to it.

The error is:

constructor Drink in class Drink cannot be applied to given types;

Drink firstOrder=new Drink();
                         ^
  required: String,double
  found: no arguments
  reason: actual and formal argument lists differ in length

Here is my code. This is my assignment work, can anyone help me out on this?

public class Store
{

    private Drink orderOne;
    private Drink orderTwo;
    public Store(String name,double cost,String nameone,double costone)
    {
        Drink firstOrder=new Drink();
        firstOrder.orderOne=name;
        firstOrder.orderOne=cost;
        Drink secondOrder=new Drink();
        secondOrder.orderTwo=nameone;
        secondOrder.orderTwo=costone; 
    }
    
    public static void main(String args[]) {
        Store s1=new Store("Lemon Tea",5.25,Coffe,3.99);
        Drink d2=new Drink("Lemon Tea",5.25);
        Drink d2=new Drink("Coffe",3.99);
        s1.printReport();
    }

    void printReport()
    {
        Drink dr=new Drink();
        dr.printDrink();
        dr.getCost();
    }
}
class Drink{
    private String name;
    private double cost;

    public Drink(String name2,double cost2)
    {
        name=name2;
        cost=cost2;
    }

    void printDrink()
    {
        store obj;
        System.out.println("Drink order one is ");
        System.out.print(obj.name);
        System.out.print("( " );
        System.out.print(cost);
        System.out.print(")");
        System.out.println("Drink order two is ");
        System.out.print(obj.nameOne);
        System.out.print("( " );
        System.out.print(costOne);
        System.out.print(")");
    }

    double getCost()
    {
        store obj1;
        double total;
        total=obj1.cost obj1.cost1;
        System.out.println("Total Cost : $ "  total);
    }
}

CodePudding user response:

Add a public Drink() {} constructor to your Drink class, that needs a second constructor, without argument.

class Drink{
    private String name;
    private double cost;

   public Drink() {
   }

   public Drink(String name2,double cost2) {
       name=name2;
       cost=cost2;
    }

    void printDrink() {
        store obj;
        System.out.println("Drink order one is ");
        System.out.print(obj.name);
        System.out.print("( " );
         System.out.print(cost);
         System.out.print(")");
       System.out.println("Drink order two is ");
        System.out.print(obj.nameOne);
        System.out.print("( " );
         System.out.print(costOne);
         System.out.print(")");
    }

    double getCost() {
        store obj1;
        double total;
        total=obj1.cost obj1.cost1;
        System.out.println("Total Cost : $ "  total);
    }
}
  • Related