Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
output is showing 0.0 instead of the actual number

my output is as follows

you need to pay 0.00 for the elec - tv

What I have tried:

Java
package myproject;

import java.util.*;
import java.lang.*;

class Met6{
	
	int catid;
	String catname;
	
	public int getCatId() {
		return catid;
	}
	public String getCatName() {
		return catname;
	}
	
	public void setCatId(int catid) {
		this.catid=catid;
	}
	public void setCatName(String catname) {
		this.catname=catname;
	}
}

class Meth6{
	
	int id;
	String name;
	double price;
	Met6 mm=new Met6();

	
	public int getId() {
		return id;
	}
	public String getName() {
		return name;
	}
	public double getPrice() {
		return price;
	}
	public Met6 getMm() {
		return mm;
	}

	public void setId(int id) {
		this.id=id;
	}
	public void setName(String name) {
		this.name=name;
	}
	public void setPrice(double Price) {
		this.price=price;
	}
	public void setMm(Met6 mm) {
		this.mm=mm;
	}
	
	public void applyCoupon() {
		
		if(mm.getCatName().equals("elec"))
			setPrice(price*0.90);
		if(mm.getCatName().equals("furn"))
			setPrice(price*0.95);
		if(mm.getCatName().equals("cosm"))
			setPrice(price*0.98);
		else
			setPrice(price*1);
		
	}
}

public class Methods6 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter the product id ");
		int id = sc.nextInt();
		System.out.println("Enter the product Name ");
		String name = sc.next();
		System.out.println("Enter the price");
		double price = sc.nextDouble();
		System.out.println("Enter the category id ");
		int catid = sc.nextInt();
		System.out.println("Enter the category name ");
		String catname = sc.next();
		
		Meth6 p = new Meth6();
		Met6 c = new Met6();
		p.setId(id);
		p.setName(name);
		p.setPrice(price);
		c.setCatId(catid);
		c.setCatName(catname);
		p.setMm(c);
		p.applyCoupon();
		
		System.out.println("you need to pay"+String.format("%.2f",p.getPrice())+ "for the" +c.getCatName()+ "-" +p.getName());

	}

}
Posted
Updated 16-Aug-20 5:00am
v2

java is case sensitive: "price" is not the same as "Price":
Java
public void setPrice(double Price) {
	this.price=price;
}
Try using the parameter value:
Java
public void setPrice(double Price) {
	this.price=Price;
}
Or change the parameter definition:
Java
public void setPrice(double price) {
	this.price=price;
}


TBH, the debugger would have shown you that in moments!
 
Share this answer
 
Quote:
output is showing 0.0 instead of the actual number

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900