7 Replies - 439 Views - Last Post: 09 October 2015 - 02:53 PM Rate Topic: -----

#1 Cnelson720   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 15-September 15

if/while loop logic question

Posted 09 October 2015 - 11:53 AM

first, here is my code.

Purchase.java
public class Purchase {
	int invNum;
	double sale;
	double tax = 0.05;
	
	public Purchase(int invNum, double sale){
		this.invNum = invNum;
		this.sale = sale;
		
	}

	public int getInvNum() {
		return invNum;
	}

	public void setInvNum(int invNum) {
		this.invNum = invNum;
	}

	public double getSale() {
		return sale;
	}

	public void setSale(double sale) {
		this.sale = sale;
	}

	public double getTax() {
		return tax;
	}

	public void setTax(double tax) {
		this.tax = tax;
	}

}



CreatePurchase.java
import javax.swing.*;
import java.awt.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class CreatePurchase {

	public static void main(String[] args) {
		double tax = 0.05;
		NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance();
		
		
		JTextField invoice = new JTextField();
		JTextField saleAm = new JTextField();
		Object[] message = 
			{"Enter invoice number: ", invoice,
			 "Enter sale amount: ", saleAm 		
			};
		
		
		JOptionPane.showConfirmDialog(null, message, "Input", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
		int invoiceINP = Integer.parseInt(invoice.getText());
		double salesINP = Integer.parseInt(saleAm.getText());
		
		if(invoiceINP >= 1000 && invoiceINP <= 8000){
			Purchase purchase = new Purchase(invoiceINP,salesINP);
			JOptionPane.showMessageDialog(null, "Invoice #" + purchase.getInvNum() + " Amount of sale: " + currencyFormatter.format(purchase.getSale()) + " Tax: " + currencyFormatter.format(tax * purchase.getSale()), "Input", JOptionPane.OK_CANCEL_OPTION);
		
		}
		
		while(invoiceINP < 1000 || invoiceINP > 8000){
				JOptionPane.showMessageDialog(null, "Please enter a number between 1000 and 8000");
				
				JOptionPane.showConfirmDialog(null, message, "Input", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
				
				if(invoiceINP >= 1000 && invoiceINP <= 8000){
					
					Purchase purchase = new Purchase(invoiceINP,salesINP);
					JOptionPane.showMessageDialog(null, "Invoice #" + purchase.getInvNum() + " Amount of sale: " + currencyFormatter.format(purchase.getSale()) + " Tax: " + currencyFormatter.format(tax * purchase.getSale()), "Input", JOptionPane.OK_CANCEL_OPTION);
				
				}
				
			}
			
			
			
	}
		
}



So my goal here, is to prompt the user with a JOptionPane to have them input a sale which includes an invoice number, and the sale amount. Then it spits out the invoice, sale amount, and the tax amount. We're learning about if and while loops so i'm suppose to use them but the problem is i just am having a really hard time with the logic. I don't think i can use try/catch.

The error is suppose to be that the user is only allowed to input an invoice number between 1000 and 8000. So if any numbers are entered less than 1000 or greater than 8000 it prompts the user with a message saying to fix it.

Here is a gif of what happens. (i couldn't make the gif any longer, but if it were to continue, it would just keep going back and forth without going to the final Joptionpane after i corrected my input)

Hope this makes sense, i would appreciate the help!

made a quick correction in my code. should be double salesINP = Double.parseDouble(saleAm.getText());

Is This A Good Question/Topic? 0
  • +

Replies To: if/while loop logic question

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: if/while loop logic question

Posted 09 October 2015 - 12:18 PM

This should give you the general idea

import java.text.DecimalFormat;
import java.text.NumberFormat;

import javax.swing.*;


public class CreatePurchase {
    public static void main(String[] args) {
        double tax = 0.05;
        NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance();
        int invoiceINP = 0;
        double salesINP = 0.0;
        boolean purchaseValid = false;

        JTextField invoice = new JTextField();
        JTextField saleAm = new JTextField();
        Object[] message = {
                "Enter invoice number (1000-8000 inc): ", invoice, "Enter sale amount: ", saleAm
            };

        while (!purchaseValid) {
            JOptionPane.showConfirmDialog(null, message, "Input",
                JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);

            invoiceINP = Integer.parseInt(invoice.getText());
            salesINP = Double.parseDouble(saleAm.getText());
            purchaseValid = ((invoiceINP >= 1000) && (invoiceINP <= 8000));
        } // end while

        Purchase purchase = new Purchase(invoiceINP, salesINP);
        System.out.println(purchase);
    }
}

Was This Post Helpful? 0
  • +
  • -

#3 Cnelson720   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 15-September 15

Re: if/while loop logic question

Posted 09 October 2015 - 12:28 PM

Ah i wasn't thinking of using a boolean. Thank you so much! This is a much better start and much simpler
Was This Post Helpful? 0
  • +
  • -

#4 Cnelson720   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 15-September 15

Re: if/while loop logic question

Posted 09 October 2015 - 01:03 PM

Here's what i've got. I still have some problems.

import javax.swing.*;
import java.awt.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class CreatePurchase {

	public static void main(String[] args) {
		double tax = 0.05;
		NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance();
		int invoiceINP = 0;
		double salesINP = 0.0;
		boolean purchaseValid = false;
		
		JTextField invoice = new JTextField();
		JTextField saleAm = new JTextField();
		Object[] message = 
			{"Enter invoice number 1000 - 8000: ", invoice,
			 "Enter sale amount: ", saleAm 		
			};
		
		JTextField invoice2 = new JTextField();
		JTextField saleAm2 = new JTextField();
		Object[] message2 = 
			{"Enter invoice number 1000 - 8000: ", invoice2,
			 "Enter sale amount: ", saleAm2 		
			};
		
		JOptionPane.showConfirmDialog(null, message, "Input", JOptionPane.OK_CANCEL_OPTION);
		
		while(!purchaseValid){
			
			invoiceINP = Integer.parseInt(invoice.getText());
			salesINP = Double.parseDouble(saleAm.getText());
			purchaseValid = ((invoiceINP >= 1000) && (invoiceINP <= 8000));
			
			if(purchaseValid = ((invoiceINP < 1000 || invoiceINP > 8000))){
				JOptionPane.showMessageDialog(null, "Please enter an invoice number between 1000 and 8000");
				
				JOptionPane.showConfirmDialog(null, message2, "Input", JOptionPane.OK_CANCEL_OPTION);
				
				while(purchaseValid = ((invoiceINP >= 1000) && (invoiceINP <= 8000))){
					
					int invoiceINP2 = Integer.parseInt(invoice2.getText());
					double salesINP2 = Double.parseDouble(saleAm2.getText());
					
					Purchase purchase2 = new Purchase(invoiceINP2, salesINP2);
					
					JOptionPane.showMessageDialog(null, "Invoice #" + purchase2.getInvNum() + " Amount of sale: " + currencyFormatter.format(purchase2.getSale()) + " Tax: " + currencyFormatter.format(tax * purchase2.getSale()), "Input", JOptionPane.OK_CANCEL_OPTION);
					System.exit(0);
				}
			}
			
			Purchase purchase = new Purchase(invoiceINP, salesINP);
			JOptionPane.showMessageDialog(null, "Invoice #" + purchase.getInvNum() + " Amount of sale: " + currencyFormatter.format(purchase.getSale()) + " Tax: " + currencyFormatter.format(tax * purchase.getSale()), "Input", JOptionPane.OK_CANCEL_OPTION);
			System.exit(0);
		}
		
		
			
	}
		
}



My problem was that after i input an incorrect invoice number, then it would go back to the first Joptionpane which is what i want, then i could fix my mistake. After i fix it, it goes onto the output joptionpane but it still uses the old incorrect invoice number rather than the one i entered the second time.

I tried to fix this by mirroring all the code as you can probably see, and using the mirrored version to try and get it to get the correct invoice number but it still spits out the incorrect one. I feel like there is a much more efficient way of doing this but i am just now seeing it lol
Was This Post Helpful? 0
  • +
  • -

#5 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: if/while loop logic question

Posted 09 October 2015 - 01:36 PM

I'm puzzled. I've given you code that you could have used twice for what i now see are two sets of inputs and yet you seem to have reverted to something pretty similar to what you had in the first place. Why??
Was This Post Helpful? 0
  • +
  • -

#6 Cnelson720   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 15-September 15

Re: if/while loop logic question

Posted 09 October 2015 - 01:56 PM

The only problem was I needed it to display "Please enter the correct invoice number" etc either in a message dialogue or in the first joptionpane like this

Posted Image

I have the code you helped me with, the one i posted second was just me trying to get it to pop up with another joptionpane with the error message and go back.
Was This Post Helpful? 0
  • +
  • -

#7 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: if/while loop logic question

Posted 09 October 2015 - 02:24 PM

If you need yet another dialog (though it would really just repeat the instructions of the first) then you could check the boolean at line 27 in my code and show (or not show) the dialog
Was This Post Helpful? 0
  • +
  • -

#8 Cnelson720   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 15-September 15

Re: if/while loop logic question

Posted 09 October 2015 - 02:53 PM

Thanks i got it..

import javax.swing.*;
import java.awt.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class CreatePurchase {

	public static void main(String[] args) {
		double tax = 0.05;
		NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance();
		int invoiceINP = 0;
		double salesINP = 0.0;
		boolean purchaseValid = false;
		
		JTextField invoice = new JTextField();
		JTextField saleAm = new JTextField();
		Object[] message = 
			{"Enter invoice number 1000 - 8000: ", invoice,
			 "Enter sale amount: ", saleAm 		
			};
		
		
		while(!purchaseValid){
			
			JOptionPane.showConfirmDialog(null, message, "Input", JOptionPane.OK_CANCEL_OPTION);
			
			invoiceINP = Integer.parseInt(invoice.getText());
			salesINP = Double.parseDouble(saleAm.getText());
			purchaseValid = ((invoiceINP >= 1000) && (invoiceINP <= 8000));
			if(!purchaseValid){
				JOptionPane.showMessageDialog(null, "Error");
			}
			} //end while
		
			
			Purchase purchase = new Purchase(invoiceINP, salesINP);
			JOptionPane.showMessageDialog(null, "Invoice #" + purchase.getInvNum() + " Amount of sale: " + currencyFormatter.format(purchase.getSale()) + " Tax: " + currencyFormatter.format(tax * purchase.getSale()), "Input", JOptionPane.OK_CANCEL_OPTION);
			
		}
		
		
			
	}




Sorry i am still a beginner and this is my first language. I appreciate your help, this made it work.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1