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());

New Topic/Question
Reply


MultiQuote



|