Yes, I am new to Java as you will see from my post. I have done most of the work therefore; I am not interested in someone to doing my work for me rather I am looking for a new set of eyes. My program is to keep track of product information; I constructed this program from scratch. The output I am currently getting producing is as follows:

HP ENVY x360 TouchSmart, [price = 0.00]
Enter discount percentage [0-10]:
HP ENVY x360 TouchSmart, [price = 0.00]

Now if you are asking, No, that is not the correct output. Obviously, there should be amounts in place of the zeros. Would someone be so kind and look at this program to see what I am doing wrong? Maybe it is my logic?

This is my tester--

import java.util.Scanner;

public class ProductTester {

    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);     
        Product product = new Product("200.00", 0);

        String formatString = "HP ENVY x360 TouchSmart,  [price = %.2f]";

        System.out.printf(formatString, product.getPrice());

        //user input the discount amount
        System.out.println("\nEnter discount percentage [0 - 100]: ");
        double discount = input.nextDouble();

        System.out.printf(formatString, product.getTotal());
    }

}

Class--

public class Product {

    private String product;
    private double price;
    private double discount;
    private double total;

    public Product(final String product, double price)
    {
        this.product = product; // assign product name
    }

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

    public double getPrice()
    {
        return price;
    }

    public double total(double amount)
    {
        total = (discount * price) - price;
        return total;
    }

    public double getTotal(){
        return total;
    }
}

We all have been here where I am at, it is my hope that one day I will be able to help someone that needs help. After all, we all are here to learn from each other.

Recommended Answers

All 11 Replies

The constructor takes Product(string product, double price), but you are creating it (on line 8) with Product("200.00", 0). You set the price to 0 and the product to 200.00. Try this:

Product product = new Product("HP Computer", 200);

looks to me like this line is the problem:

Product product = new Product("200.00", 0);

In your product class the first parameter of your constructor is the name as a string, the second is the price. So you want the price to be the second parameter when creating the class and, in your constructor, include
this.price = price;
By passing in zero, you're getting zero back.

public Product(final String product, double price)
{
    this.product = product; // assign product name
}

So this constructor takes in a string for the product, and a double for the price. Notice that the price is not saved.

Product product = new Product("200.00", 0);

The first argument here is the name of the product. 200.00 doesn't seem like the name of the product though. And 0 doesn't look like the correct price either.

Well for one when you initialize Product product = new Product("200.00", 0); there the second argument should be the price. In this case it is 0 the first argument should be the product name.

Then instead of hard coding the string "HP ENVY x360 TouchSmart" you can use the objects property.

Edit. Whoops got in too late and didn't refresh. Sorry.

Chris, Yes, thank you for pointing that out. I did the update and I am still getting the same thing. I looked at my code a little closer and on line 8, I have 'Product product = new Product("HP Computer", 200); but in the next line I have my String formatString = "HP ENVY x360 Touch Smart, [price = %.2f]. I did some rearranging and if I change the Product in line 8 it is giving me another error. What are your thoughts on that?

What is the error? return this.price instead of return price?

Also, your second print.. you never set the discount for that product. So I don't think it will have any effect.

Hey, team, I did some reconfiguring and here is what I have now.

ProductTester

import java.util.Scanner;

public class ProductTester //the class ProductTester
{
    public static void main( String[] args) //the main statement
    {
          Scanner user_input = new Scanner( System.in );

          Product product =  new Product("HP ENVY x360 TouchSmart");

          String formatString = "Product[name = %s, price = %.2f]\n";

          System.out.printf(formatString, product.getName(), product.getPrice());

          //user input the discount amount
          System.out.println("\nEnter discount percentage [0 - 100]: %.2f ");
          double discount = user_input.nextDouble();

          System.out.printf(formatString, product.getName(), product.applyDiscount());
    }
}

Product Class ~

public class Product //the class named Product
{
    private String product; //the product name
    private double price = 0; //the product's value

    public Product (String name)
{
    product=name; //establishing the name as HP ENVY x360 TouchSmart

}
    public String getName()
{
        return product; //returning the value of the String

}
    public void setPrice(double p)
{
    price = 200; //establishing the value as 
}
    public double getPrice()
{
    return price; //returning the value of the price
}
    public void applyDiscount(double percent)
{
    percent = price - (price * percent); //establishing the discount
}

}

Now, I am getting an error on the 'printf' in the tester last line.

System.out.printf(formatString, product.getName(), product.applyDiscount());

The error says:

[The method printf(String, Object...) in the type PrintStream is not applicable for the arguments (String, String, void)
at ProductTester.main(ProductTester.java:19)]

Any thoughts?

applyDiscount() doesn't return anything. You shouldn't be using it like that. If you want it to return the discounted price then refactor it to do so.

Alright, this is what I have come up with if anyone is watching this thread.

public double applyDiscount(double percent){
        return (100 - percent) * price / 100; 
    }

    public double getPercent()
    {
        return percent;
    }

    public void setPercent(double percent)
    {
        this.percent = percent;
    }

Taking out the "void" of the equation. Now there are no error (per say) but the system is not allowing me to run the program.

@codewar, this is not the complete code. Please post the code in question and any errors that are printed. "the system is not allowing me to run the program" does not give us enough information to help you.

Also, your formatString is a format that expects 2 arguments (%s and %.2f). It will work fine for printf(formatString, getName(), getPrice()), but will fail for printf(formatString, getName(), getPrice(), applyDiscount()). You need a new format string if you are going to be passing 3 arguments.

Thank you I figured it out.

String formatString = "Product[name = %s, price = %.2f]\n";

          System.out.printf(formatString, product.getName(), product.getPrice());

I am getting this and it all works together beautifully.

System.out.printf(formatString, product.getName(), product.applyDiscount(discount));

Defining the methods like this.

public double getPrice()
{
    return price; //returning the value of the price
}
    public double applyDiscount(double percent){
        return (100 - percent) * price / 100; 
    }

    public double getPercent()
    {
        return percent;
    }

    public void setPercent(double percent)
    {
        this.percent = percent;
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.