I need help!!! im trying to get my sentinal to stop counting as my last value passed. Im imputting a value for a house,and its calculating a total price based on interest and stuff ive set up according to the house value. then pressing -1 to go onto the next house.the problem is, when i press the -1 it just keeps taking my house value as -1(my sentinal) i cant continue anything else without fixing this!!


heres my test:

public class AuctionTest
{

	public static void main(String args[])
	{

		Auction pointer1=new Auction("Peter Dunphy\n15 Collins Drive\nKillester\nDublin 5");
		Auction pointer2=new Auction("Katie Molloy\n10 New Road\nGlasnevin\nDublin 11");

		pointer1.displayMessage();
		pointer1.determineAfford();

		pointer2.displayMessage();
		pointer2.determineAfford();




	}


}

and heres my main code:

import java.util.Scanner;

public class Auction
{

	private String personName;

	public Auction(String name)

	{

		personName=name;

	}

	public void setPersonName(String name)
	{

		personName=name;

	}

	public String getPersonName()
	{
	
		return personName;

	}

	public void displayMessage()
	{
		System.out.printf("These are the details on file for \n%s\n",getPersonName());
	}


	public void determineAfford()

	{


		Scanner input=new Scanner(System.in);

		int houseCounter;
		double houseCost;
		double totalCost;
		double interest;
		double peter;
		double katie;


		houseCounter=1;
		peter=550000;
		katie=1500000;
		

		System.out.println("Enter house cost or -1 to quit");
		houseCost=input.nextDouble();

		
	

		while (houseCost !=-1)
		{


		
		houseCounter=houseCounter+1;
		

		
		System.out.println("Enter house cost or -1 to quit");
		houseCost=input.nextDouble();

		houseCost=houseCost;

		}


	if (houseCost<=100000)


	{
		
		interest=0;
		totalCost=houseCost+interest;

		System.out.printf("Total cost is %.2f",totalCost);		
		


	}


	if (houseCost<=200000)
	
	{

		interest=(houseCost-100000/100)*5;
		totalCost=houseCost+interest;

		System.out.printf("Total cost is %.2f",totalCost);		
		
	}	
	

	if (houseCost<=300000)
	
	{

		interest=(houseCost-200000/100)*6+5000;
		totalCost=houseCost+interest;

		System.out.printf("Total cost is %.2f",totalCost);		
		
	}	
	
	
	if (houseCost<=400000)
	
	{

		interest=(houseCost-300000/100)*7+5000+6000;
		totalCost=houseCost+interest;

		System.out.printf("Total cost is %.2f",totalCost);		
		
	}	



	if (houseCost>500000)
	
	{

		interest=(houseCost-400000/100)*8+5000+6000+7000;
		totalCost=houseCost+interest;

		System.out.printf("Total cost is %.2f",totalCost);		
		
	}	
















	}


}

Recommended Answers

All 9 Replies

Comparing an double to a specific int value might be a problem. Why not just test if the value is less than 0.

Try debugging the code by printing out the value that is read in to make sure the computer is getting what you expect for input.

Can you copy and paste the console for when you test the program?
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

the problem is that in line 74 you directly get the value and directly assign it to the variable

houseCost

which is why you get the cost as -1.

Suggest that you change the code as a different variable to take the input
say

double enteredVal;

And if the input is not -1 assign it to the

houseCost

else do not. From this the problem that you are facing could be overcome.
Also try initializing the variable

houseCost

to something like 1- and if the

houseCost

is -1 you do not have to make the calculations and just simply say like the houseCost is not specified or some message.

Try that out, I dont wanna post the code for it as it would take your creative thoughts away, so try it out and if any issues would be glad to help out. :)

Ok im getting some where. But i need to do one more thing to complete the program perfectly,i need my second object.Katie, to have a budget of 1500000, and Peter, my first object , have 550000, the way ive done it is just to call peter, do my method for him, calculate based on the house price the interest, but im just saying, the house is within budget for peter, and for katie, if you know what i mean, then when i press -1, it ends peter, and starts katie. i want katie to have 1500000 associated with her for when she starts, and peter 550000, i know i need to use a constructor but i can figure it out.

heres my new code

import java.util.Scanner; 

public class Auction
{
	private String personDetails; 

	public Auction( String details )
	{
		personDetails = details; 
	} 

	public void setPersonDetails( String details )
	{
		personDetails = details; 
	} 

	
	public String getPersonDetails()
	{
		return personDetails;
	} 
	
	public void displayMessage()
	{
		System.out.printf( "These are the details for\n%s!\n\n", getPersonDetails());
	} 

	
	
	public void determineAfford()
	{

		

		Scanner input = new Scanner( System.in );

		 
		double budgetPeter;
		double budgetKatie;
		double houseCost;
		double totalCost;
		double interest;
		


		totalCost=0;
		houseCost=0;
		interest=0;
		budgetPeter=550000;
		budgetKatie=1500000;
		
		

		
		System.out.print( "Enter House Price or -1 to quit: ");
		houseCost = input.nextDouble();
		
		
		
		while ( houseCost != -1 )  
		{
			
			if (houseCost<=100000)
		
			{
		
				interest=0;
				totalCost=(houseCost/100)*1.5+houseCost;	

			System.out.printf("Total dcost is %.2f\n",totalCost);		
		
			}
	
			

			if (houseCost > 100000 && houseCost <= 200000 ) 
			{
				interest =((houseCost-100000)/100)*5;
				totalCost=((houseCost+interest)/100)*1.5+houseCost+interest;

				System.out.printf("\nTotal cost is %.2f\n",totalCost);
		
			}
			if (houseCost > 200000 && houseCost <= 300000)
			{
				interest =((houseCost-200000)/100)*6+5000;
				totalCost=((houseCost+interest)/100)*1.5+houseCost+interest;

				System.out.printf("\nTotal cost is %.2f\n",totalCost);

		 	}

			if (houseCost > 300000 && houseCost <= 400000) 
			{
				interest =((houseCost-300000)/100)*7+11000;
				totalCost=((houseCost+interest)/100)*1.5+houseCost+interest;

				System.out.printf("\nTotal cost is %.2f\n",totalCost);

		 	}
			
			if (houseCost > 400000)
			{
				interest =((houseCost-400000)/100)*8+18000;
				totalCost=((houseCost+interest)/100)*1.5+houseCost+interest;

				System.out.printf("\nTotal cost is %.2f\n",totalCost);

		 	}

			
			
			if(totalCost <= 550000 && totalCost <= 1500000)
			{

			
			System.out.printf("\nHouse is in budget for Peter by %.2f\nHouse is in budget for Katie by %.2f\n",550000-totalCost,1500000-totalCost); 
			

			}


			if(totalCost > 550000 && totalCost <= 1500000)
			{

			System.out.printf("\nHouse is in budget for Katie by %.2f\n",1500000-totalCost);

			}

			
			if(totalCost > 1500000)
			{

			System.out.println("House is not within either clients budget");

			}
			    








			
			System.out.print( "Enter House Price or -1 to quit: ");
			houseCost = input.nextDouble();
		
		}

Please edit your post and wrap your code in code tags. Use the [code] icon above the input box. Also you could remove most of the extra blank lines to make your code more readable.

Ok im getting some where. But i need to do one more thing to complete the program perfectly,i need my second object.Katie, to have a budget of 1500000, and Peter, my first object , have 550000, the way ive done it is just to call peter, do my method for him, calculate based on the house price the interest, but im just saying, the house is within budget for peter, and for katie, if you know what i mean, then when i press -1, it ends peter, and starts katie. i want katie to have 1500000 associated with her for when she starts, and peter 550000, i know i need to use a constructor but i can figure it out.

heres my new code


import java.util.Scanner;

public class Auction
{
private String personDetails;

public Auction( String details )
{
personDetails = details;
}

public void setPersonDetails( String details )
{
personDetails = details;
}


public String getPersonDetails()
{
return personDetails;
}

public void displayMessage()
{
System.out.printf( "These are the details for\n%s!\n\n", getPersonDetails());
}

public void determineAfford()
{

Scanner input = new Scanner( System.in );


double budgetPeter;
double budgetKatie;
double houseCost;
double totalCost;
double interest;

totalCost=0;
houseCost=0;
interest=0;
budgetPeter=550000;
budgetKatie=1500000;


System.out.print( "Enter House Price or -1 to quit: ");
houseCost = input.nextDouble();

while ( houseCost != -1 )
{

if (houseCost<=100000)

{

interest=0;
totalCost=(houseCost/100)*1.5+houseCost;

System.out.printf("Total dcost is %.2f\n",totalCost);

}

if (houseCost > 100000 && houseCost <= 200000 )
{
interest =((houseCost-100000)/100)*5;
totalCost=((houseCost+interest)/100)*1.5+houseCost+interest;

System.out.printf("\nTotal cost is %.2f\n",totalCost);

}
if (houseCost > 200000 && houseCost <= 300000)
{
interest =((houseCost-200000)/100)*6+5000;
totalCost=((houseCost+interest)/100)*1.5+houseCost+interest;

System.out.printf("\nTotal cost is %.2f\n",totalCost);

}

if (houseCost > 300000 && houseCost <= 400000)
{
interest =((houseCost-300000)/100)*7+11000;
totalCost=((houseCost+interest)/100)*1.5+houseCost+interest;

System.out.printf("\nTotal cost is %.2f\n",totalCost);

}

if (houseCost > 400000)
{
interest =((houseCost-400000)/100)*8+18000;
totalCost=((houseCost+interest)/100)*1.5+houseCost+interest;

System.out.printf("\nTotal cost is %.2f\n",totalCost);

}

if(totalCost <= 550000 && totalCost <= 1500000)
{


System.out.printf("\nHouse is in budget for Peter by %.2f\nHouse is in budget for Katie by %.2f\n",550000-totalCost,1500000-totalCost);


}


if(totalCost > 550000 && totalCost <= 1500000)
{

System.out.printf("\nHouse is in budget for Katie by %.2f\n",1500000-totalCost);

}


if(totalCost > 1500000)
{

System.out.println("House is not within either clients budget");

}


System.out.print( "Enter House Price or -1 to quit: ");
houseCost = input.nextDouble();

}

wouldnt you maybe do something like this:

import java.util.Scanner; 

public class Auction
{
private String personDetails; 

private double budgetPeter;
private double budgetKatie;
public Auction(String details )
{
personDetails = details; 
budgetPeter=550000;
budgetKatie=150000;
} 
}

or maybe:

public class Auction
{
private String personDetails; 

private double budgetPeter;
private double budgetKatie;
public Auction(String details )
{
personDetails = details; 
if(deatils.equals("katie")) {
budgetKatie=150000;
} else {
budgetPeter=550000;
}
} 
}

Ok im with you cOrRuPtG3n3t!x,

but my test code would have to reflect this, this is what i have so far,

public class AuctionTest
{

	public static void main(String args[])
	{

		Auction pointer1=new Auction("Peter Dunphy\n15 Collins Drive\nKillester\nDublin 5\nBudget:550000");
		Auction pointer2=new Auction("Katie Molloy\n10 New Road\nGlasnevin\nDublin 11\nBudget:1500000");

		pointer1.displayMessage();
		pointer1.determineAfford();

		pointer2.displayMessage();
		pointer2.determineAfford();




	}

Ok im with you cOrRuPtG3n3t!x,

but my test code would have to reflect this, this is what i have so far,

public class AuctionTest
{

	public static void main(String args[])
	{

		Auction pointer1=new Auction("Peter Dunphy\n15 Collins Drive\nKillester\nDublin 5\nBudget:550000");
		Auction pointer2=new Auction("Katie Molloy\n10 New Road\nGlasnevin\nDublin 11\nBudget:1500000");

		pointer1.displayMessage();
		pointer1.determineAfford();

		pointer2.displayMessage();
		pointer2.determineAfford();




	}

i'd suggest this, because you create instances for both people why not have a constructor that has a variable to set the budget amount of the instance of the person i.e

import java.util.Scanner; 
 
public class Auction
{
private String personDetails; 
 
private double budget;
public Auction(String details,double budgetStartAmount)
{
personDetails = details; 
budget=budgetStartAmount;
} 
}

you would then call the instance like so:

public static void main(String args[])
	{

		Auction pointer1=new Auction("Peter Dunphy\n15 Collins Drive\nKillester\nDublin 5",550000);
		Auction pointer2=new Auction("Katie Molloy\n10 New Road\nGlasnevin\nDublin 11",1500000);
...



	}

Therfore setting the budget starting amount to whatever you aplied as an argument in an instance to the object.

I would also suggest parsing their address as a variable to the object so that you got more reusable code i.e

public class Auction
{
private String personDetails; 
 
private double budget;
private String address;
public Auction(String details,String add,double budgetStartAmount)
{
personDetails = details; 
budget=budgetStartAmount;
address=add;
} 
public double getBudget()
{
return budget;
}

public String getName() {
return personDetails;
}
}

therefore each instance will be created with its own name, address and budget:

public static void main(String args[])
	{

		Auction pointer1=new Auction("Peter Dunphy","15 Collins Drive Killester Dublin 5",550000);
		Auction pointer2=new Auction("Katie Molloy","10 New Road Glasnevin Dublin 11",1500000);
                System.out.println("Budget for "+pointer1.getName()+"is "pointer1.getBudget());//this would show Budget for Peter Dunphy is 550000
                System.out.println("Budget for "+pointer2.getName()+"is "pointer2.getBudget());//this would show Budget for Katie Molloy is 1500000

		pointer1.displayMessage();
		pointer1.determineAfford();

		pointer2.displayMessage();
		pointer2.determineAfford();




	}

@ cOrRuPtG3n3t!x

Thank you very much for taking the time to help, couldn't get my head around constructors at all but it's working 100% now thanks to you! Much appreciated!

@ cOrRuPtG3n3t!x

Thank you very much for taking the time to help, couldn't get my head around constructors at all but it's working 100% now thanks to you! Much appreciated!

Glad to be of help :) and at least its working now!!

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.