Question: when i run the program, it runs through order1 before catching an invalid letter input in the variiables id1 and value 1.It acts the same

when i run the program, it runs through order1 before catching an invalid letter input in the variiables id1 and value 1.It acts the same way for creditOrder1 and creditOrder2. Also if an invalid entry further on in the program, it restarts and asks the user to restart from the beginning. I would like for the program to catch the invalid response right away instead of at the end of the program. The portion of the program that i need help with is in TestOrder.java PLEASE HELP

TestOrder.java

import javax.swing.JOptionPane;

class TestOrder {

public static void main(String[] args) throws Exception { boolean keepAsking = true; while (keepAsking){

try{ String name1 = JOptionPane.showInputDialog("Order #1 Input Client Name:"); String sID1 = JOptionPane.showInputDialog("Order#1 Input Order ID (intigers only):"); String sValue1 = JOptionPane.showInputDialog("Order#1 Input Order Value (intigers only):"); //convert string id1, and value1 to an integer variable

int id1 = Integer.parseInt(sID1); int value1 = Integer.parseInt(sValue1); //initiates TotalOrder in order to calculate the total amount of money generated by the three orders int TotalOrder;

//initiate object order1 with variables name1, id1, and value1 Order order1 = new Order(name1, id1, value1);

//output toString method from Order System.out.println(order1.toString());

// orderAmt1 equal to the value of the first order from Order.java int orderAmt1 = order1.getOrderValue(value1);

//initiate CreditOrder input values String name2 = JOptionPane.showInputDialog("Credit Order#1 Input Client Name (intigers only):"); String sID2 = JOptionPane.showInputDialog("Credit Order#1 Input Order ID (intigers only):"); String sValue2 = JOptionPane.showInputDialog("Credit Order#1 Input Order Value (intigers only):"); String sinterest11 = JOptionPane.showInputDialog("Credit Order#1 Input Credit Order interest1 Value (intigers only):"); //convert string id2, and value2 to an integer variable int id2 = Integer.parseInt(sID2); int value2 = Integer.parseInt(sValue2); // convert string interest1 to a double variable double interest1 = Double.parseDouble(sinterest11); // check to see if interest is in between 8 and 25. If not reenter interest until it is while (interest1 < 8 || interest1 > 25) { String intresterror = JOptionPane.showInputDialog("Input Credit Order interest1 Value must be between 8 and 25 percent:"); double intreste1 = Double.parseDouble(intresterror); interest1 = intreste1; } //initiate new credit order object from new values CreditOrder creditOrder1 = new CreditOrder(name2, id2, value2, interest1); //output toString values of new credit order object System.out.println(creditOrder1.toString()); //get order value of creditOrder1 double creditOrderAmt1 = creditOrder1.getCreditOrderTotalValue(value2, interest1); //print order value of creditOrder1 String name3 = JOptionPane.showInputDialog("Credit Order#1 Input Client Name (intigers only):"); String sID3 = JOptionPane.showInputDialog("Credit Order#1 Input Order ID (intigers only):"); String sValue3 = JOptionPane.showInputDialog("Credit Order#1 Input Order Value (intigers only):"); String sinterest2 = JOptionPane.showInputDialog("Credit Order#2 Input Credit Order interest Value (intigers only):"); //convert input strings to int and double variables int id3 = Integer.parseInt(sID3); int value3 = Integer.parseInt(sValue3); double interest2 = Double.parseDouble(sinterest2); //check to see if interest for the second order is in between 8 and 25. If not reenter interest until it is while (interest2 < 8 || interest2 > 25) { String intresterror = JOptionPane.showInputDialog("Input Credit Order interest Value must be between 8 and 25 percent:"); double intreste2 = Double.parseDouble(intresterror); interest2 = intreste2; } //initiate new CreditOrder object for the second credit order. CreditOrder creditOrder2 = new CreditOrder(name3, id3, value3, interest1); //output toString values of new credit order object System.out.println(creditOrder2.toString()); //get order value of creditOrder2 double creditOrderAmt2 = creditOrder2.getCreditOrderTotalValue(value3, interest1); //print order value of creditOrder2 // calculate TotalOrder TotalOrder = (int) (creditOrderAmt1 + creditOrderAmt2 + orderAmt1); // print out TotalOrder System.out.println("Amount earned from all Orders: " + TotalOrder); /*Checks to see if ClientName is the same for creditOrder1 and two. If a so display message * that indicates a returning customer. If not then indicate that both customers are new. */ if (creditOrder1.getClientName().equals(creditOrder2.getClientName())) { System.out.println(creditOrder1.getClientName()+ " is a returning credit customer"); } else { System.out.println(creditOrder1.getClientName() + " and " + creditOrder2.getClientName() + " are new credit customers "); } //determine if order1 or creditOrder2 had the most profit. If both orders are equal, then display a message indicating this. if (order1.getOrderValue(value1) > creditOrder2.getCreditOrderTotalValue(value3, interest1)) { System.out.println("Greatest income from client " + name1 + orderAmt1); } else if (order1.getOrderValue(value1) < creditOrder2.getCreditOrderTotalValue(value3, interest1)){ System.out.println("Greatest income is from client " + name3 + " with a value of $" + creditOrderAmt2); } else { System.out.println("both " + name1 + " and " + name3 + " had the same value of $" +orderAmt1); } if ( id2 != Integer.parseInt(sID2)){ sID2 = JOptionPane.showInputDialog("Order#1 Input Order ID:"); } }

catch (NumberFormatException e){ JOptionPane.showMessageDialog(null,"invalid entry",null, JOptionPane.ERROR_MESSAGE);

} } } }

Order.java

public class Order { //initiates private int orderID = 0; private String clientName = " "; private int orderValue = 0;

public Order(String clientName, int orderID, int orderValue) { this.orderID = orderID; this.clientName = clientName; this.orderValue = orderValue; } // get and set methods for Order ID. public int getOrderID(int Id) throws Exception{ orderID = Id; return orderID; } public void setOrderID(int orderID) throws Exception{ if(orderID < 0) { throw new Exception("Exception: Order ID cannot be less than 0!"); } this.orderID = orderID; }

public String getClientName() { return clientName; } public void setClientName(String clientName) { this.clientName = clientName; } // get and set methods for Order Value public int getOrderValue(int Value){

orderValue = Value; return orderValue; } public void setOrderValue(int orderValue) throws Exception{ if(orderValue < 0) { throw new Exception("Exception: Order Value cannot be less than 0!"); } this.orderValue = orderValue; }

public String toString() { return "Client: " + clientName + " Order ID#: " + orderID + " Order Value: " + orderValue; } }

CreditOrder.java

public class CreditOrder extends Order {

private double interest = 0; private double creditOrderTotal; /** * @param args * @throws Exception */ public CreditOrder(String clientName, int orderID, int orderValue, double interest) { super(clientName,orderID,orderValue); this.interest = interest; }

public double getInterest(double interestRate) { interest = interestRate; return interest; } public void setInterest(double interest) throws Exception{

this.interest = interest; } public String toString() { return super.toString() + " Interest Percentage: " + interest; } /** * @param int order value, double interest value * * @return credit order plus 1 year worth of interest * */ public double getCreditOrderTotalValue(int value, double interest){ double newValue = value * (interest/100) + value; creditOrderTotal = newValue; return creditOrderTotal; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!