0
public class Chair
{
   private String typeChair;
   private String materials;
   private String color;
   private double legs;
   private double pricePerChair;

   public void setTypeChair(String t) {
      typeChair = t;
   }
   public String getTypeChair() {
      return typeChair;
   }
   public void setMaterial(String m) {
      materials = m;
   }
   public String getMaterials() {
      return materials;
   }
   public void setColor(String c) {
      color = c;
   }
   public String getColor() {
      return color;
   }
   public void setLegs(double l) {
      if (l >= 0 && l < 10) {
         legs = l;
      } else {
         System.out.println("Legs must be between 0 and 9");
      }
   }
   public double getLegs() {
      return legs;
   }
   public void setPricePerChair(double p) {
      if (p > 0) {
         pricePerChair = p;
      } else {
         System.out.println("Price must be greater than 0");
      }
   }
   public double getPricePerChair() {
      return pricePerChair;
   }
   public double getCost() {
      double cost = 0;
      if (materials == "vinyl") {
         cost += (pricePerChair * 0) + pricePerChair;
      } else if (materials == "leather") {
         cost += (pricePerChair * .4) + pricePerChair;
      } else if (materials == "cloth") {
         cost += (pricePerChair * .1) + pricePerChair;
      } 
      return cost;
   }
   }

hello, i'm having trouble getting the cost of a chair from the material. the material is input from the user. if the material is vinyl, the additonal cost is 0%, leather is .4%, and cloth is .1%. everytime i run my main method, the cost prints 0. whats the problem in the code?

2 Answers2

1

Very common mistake here - Java Strings should always be compared with the equals() method, not with ==. Example:

 } else if (materials.equals("cloth")) {
Simon DeWolf
  • 400
  • 3
  • 10
0

try .equals() and not == to compare

TK8
  • 125
  • 6