I am having issues with a problem that I coded for a Java course I am taking, and I cannot figure out why it is behaving a certain way. Here is the problem from the book, both part A and part B:
A) Create a class named Purchase. Each Purchase contains an invoice number, amount of sale, and amount of sales tax. Include set methods for the invoice number and sale amount. Within the set() method for the sale amount, calculate the sales tax as 5% of the sale amount. Also include a display method that displays a purchase's details. Save the file as Purchase.java
B) Create an application that declares a Purchase object and prompts the user for purchase details. When you prompt for an invoice number, do not let the user proceed until a number between 1000 and 8000 has been entered. When you prompt for a sale amount, do not proceed until the user has entered a non-negative number, sale amount, and sales tax. Save the file as CreatePurchase.java.
Here is the code for the first part of the problem:
public class Purchase
{
int invoiceNumber = 1234;
double salePrice = 10.00;
double SalesTax;
public void setInvoiceNumber(int invoice)
{
invoiceNumber = invoice;
}
public void setSalePrice(double saleAmount)
{
salePrice = saleAmount;
SalesTax = (saleAmount * .05);
}
public void displaySalePrice()
{
System.out.println("Your invoice number is:" + invoiceNumber + ".");
System.out.println("Your sale amount is: " + salePrice + ".");
System.out.println("Your sales tax is: " + SalesTax + ".");
}
}
Here is the code for the second part of the problem:
import java.util.Scanner;
public class CreatePurchase
{
public static void main(String[] args)
{
int invoice;
double saleAmount;
invoice = 0;
saleAmount = 0.0;
Purchase completedPurchase = new Purchase();
Scanner input = new Scanner(System.in);
System.out.println("Please enter the invoice number: ");
invoice = input.nextInt();
System.out.println("Please enter the sale amount: ");
saleAmount = input.nextDouble();
do
{
System.out.println("You entered an invalid number.");
System.out.println("Please enter a number between 1000 and 8000.");
invoice = input.nextInt();
}
while (invoice < 1000 || invoice > 8000);
do
{
System.out.println("You entered an invalid number.");
System.out.println("Please enter a number greater than 0.");
saleAmount = input.nextDouble();
}
while (saleAmount < 0);
completedPurchase.setInvoiceNumber(invoice);
completedPurchase.setSalePrice(saleAmount);
completedPurchase.displaySalePrice();
}
}
When I compile CreatePurchase.java and run it, it works, but has to cycle through the loops first before it works. For instance, I will type in 7000 for the invoice value and 100 for the sale amount. Those two values should automatically call the completePurchase.displaySalePrice(); method because the invoice number is greater than 1000 and less than 8000, and the sale amount is greater than 0. That being the case, it still cycles through the do while loops once before calling that method.
I cannot for the life of me figure this out. It's probably something pretty simple I am missing. Any help would be greatly appreciated.
After the great guidance of everyone below, I changed the code for the loops to the following:
while (invoice < 1000 || invoice > 8000)
{
System.out.println("You entered an invalid number.");
System.out.println("Please enter a number between 1000 and 8000.");
invoice = input.nextInt();
}
while (saleAmount < 0)
{
System.out.println("You entered an invalid number.");
System.out.println("Please enter a number greater than 0.");
saleAmount = input.nextDouble();
}
It still isn't working correctly. Changing the loops to while loops certainly worked, but now when I enter a number for the invoice number that's in the correct range and an incorrect number for the saleAmount, the program finished and does not execute the while loop for saleAmount? I seem to be missing a concept here.
Also, can anyone recommend a better IDE than JGrasp. That is what we were told to use, but it's cumbersome. I have VisualStudio and Eclipse, but I feel that doing java homework in those two IDE's might be overkill. I will be taking more java courses and c++ courses, so maybe it's worth learning the basics in VS or Eclipse. Any suggestions would be appreciated.