0

classyclass.java :

package work1;

public class classyclass {

    public static void main(String[] args) {

        int num;
        num = 0;
        num++;
        System.out.println(" blah blah blah " + num);

        Coffee latte = new Coffee();
        Coffee capuccino = new Coffee();

        latte.price = 5;
        capuccino.price = 11;

        latte.beverage();
        capuccino.beverage();                       
    }    
}

Coffee.java :

package work1;

public class Coffee {

    int price;
    String coffeeType;      

    void beverage() {    
        if (coffeeType == "latte" )
        {
            System.out.println("The price of latte is  " + latte.price );
        }       
        else if(coffeeType == "capuccino")
        {
            System.out.println("The price of a cappuccino is  " + capuccino.price);
        }       
    }
}

As an amateur C programmer venturing into Java ,I am getting really confused with using these classes and objects. What I want is to take the value from the classyclass class into the coffee class and then execute on that data back again in the main method. I am mixing stuff up , please help

mpromonet
  • 11,326
  • 43
  • 62
  • 91
No Holidays
  • 105
  • 10
  • 1
    Use `String.equals()` to compare strings. see http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java for more information – Aakash Jun 17 '15 at 07:37
  • `if (coffeeType == "latte" )` should be `if ("latte".equals(coffeeType) )` – moffeltje Jun 17 '15 at 07:41
  • Other than what others have mentioned, it would also be beneficial to look into access modifiers and getters and setters. Those should better help you *encapsulate* your code. – npinti Jun 17 '15 at 07:45
  • Thanks all you guys! This website is pretty cool – No Holidays Jun 17 '15 at 09:02

5 Answers5

1

change string comparison

 coffeeType == "latte" 

to

coffeeType.equals("latte")

that is how java compares string values. Make the changes in rest of your code with respect to these changes.

Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78
1

There are several things wrong or not logical in your code:

1) If you only use num to print the value 1, why not change it to:

int num = 1;

or

System.out.println(" blah blah blah " + 1);

or even

 System.out.println(" blah blah blah 1");

2) You make a new object of the type/class Coffee, but you never set the variable String coffeeType in the objects. The standard way to do this is to make a constructor like so:

in main():

Coffee latte = new Coffee("latte");

and in your Coffee class make the constructor:

public Coffee(String coffee){
    this.coffeeType = coffee;
}

3) You have to compare Strings with the .equal() method:

if("latte".equals(this.coffeeType)){  System.out.println("\nThis is latte coffee");  }

Some further notes as mentioned by npinti: use getter and setter methods to set variables en retrieve them:

public void setType(String type){
    this.coffeeType = type;
}

public void setPrice(int price){
    this.price = price;
}

public int getPrice(){
    return this.price;
}
Community
  • 1
  • 1
moffeltje
  • 4,521
  • 4
  • 33
  • 57
  • "If you only use num to print the value 1, why not change it to..." This was my first java program ever , so i was just getting a feel for it. This wasnt supposed to be practical , ykwim? I am very poor at programming to be honest , but i still want to make it,so i will try. the rest of the advice is spot on , only that I havent really got to constructors as of yet. thanks. – No Holidays Jun 17 '15 at 08:35
  • I don't accuse you of anything, I just posted this to help you understand the way Java works :) I have been in your position and so I think this will help you improve your Java skills. Good luck! – moffeltje Jun 17 '15 at 08:42
0

Try this code. I have tried to use java conventions for your understanding.

Your ClassyClass class:

import java.util.*;

public class ClassyClass
{
    ArrayList<Coffee> myClub;
    Scanner myInput;
    public ClassyClass()
    {
        myClub = new ArrayList<Coffee>();
        myInput = new Scanner(System.in);

        myClub.add(new Coffee("latte", 5));
        myClub.add(new Coffee("capuccinno", 11));

        System.out.println("Do you want to place an order? Enter 1 for yes and 2 for no.");
        int choice = myInput.nextInt();

        System.out.println(myClub.size());

        if (choice == 1)
        {
            myCoffeeClub();
        }
        else {  }
    }

    public void myCoffeeClub()
    {
        System.out.print("Enter latte or capuccinno: ");
        String item = myInput.next();
        int itemPrice = 0;

        for (Coffee s : myClub)
        {
            if (s.cName.equals(item))
            {
                itemPrice = s.cPrice;
            }
        }
        System.out.println ("The price of "+item+" is "+itemPrice+".");
    }
}

Your Coffee class:

public class Coffee
{
    String cName;
    int cPrice;

    public Coffee (String cName, int cPrice)
    {
        this.cName = cName;
        this.cPrice = cPrice;
    }
}
VD007
  • 267
  • 2
  • 15
0

You should have set coffeeType while setting price and you would have got correct results.

public class classyclass {

    public static void main(String[] args) {

        int num;
        num = 0;
        num++;
        System.out.println(" blah blah blah " + num);

        Coffee latte = new Coffee();
        latte.coffeeType = "latte";
        latte.price = 5;

        Coffee capuccino = new Coffee();
        capuccino.coffeeType = "capuccino";
        capuccino.price = 11;

        latte.beverage();
        capuccino.beverage();
    }
}

public class Coffee {

    int price;
    String coffeeType;

    void beverage() {

        if (this.coffeeType.equals("latte")) {
            System.out.println("The price of latte is  " + this.price);
        }
        else if (this.coffeeType.equals("capuccino")) {
            System.out.println("The price of a cappuccino is  " + this.price);
        }
    }
}
Rajesh
  • 2,135
  • 1
  • 12
  • 14
0

In java the == operator compares an object reference. To compare a string you should use the string.equals("..") method.

Madmenyo
  • 8,389
  • 7
  • 52
  • 99