44.3k views
3 votes
Create a class that stores an array of usable error messages. Create an OrderException class that stores one of the messages. Create an application that contains prompts for an item number and quantity. Allow for the possibility of nonnumeric entries as well as out-of-range entries and entries that do not match any of the currently available item numbers. The program should display an appropriate message if an error has occurred. If no errors exist in the entered data, compute the user’s total amount due (quantity times price each) and display it.

User Saar
by
4.3k points

1 Answer

3 votes

Answer:

Check the explanation

Step-by-step explanation:

Given below is the code for the question.

import java.util.*;

public class PlaceAnOrder

{

public static void main(String[] args)

{

final int HIGHITEM = 9999;

final int HIGHQUAN = 12;

int code;

int x;

boolean found;

final int[] ITEM = {111, 222, 333, 444};

final double[] PRICE = {0.89, 1.47, 2.43, 5.99};

int item;

int quantity;

double price = 0;

double totalPrice = 0;

Scanner input = new Scanner(System.in);

try{

System.out.print("Enter Item number: ");

if(input.hasNextInt()){

item = input.nextInt();

if(item < 0)

throw new OrderException(OrderMessages.message[2]);

if(item > HIGHITEM)

throw new OrderException(OrderMessages.message[3]);

System.out.print("Enter Item quantity: ");

if(input.hasNextInt()){

quantity = input.nextInt();

if(quantity < 1)

throw new OrderException(OrderMessages.message[4]);

if(quantity > HIGHQUAN)

throw new OrderException(OrderMessages.message[5]);

found = false;

for(int i = 0; i < ITEM.length; i++){

if(ITEM[i] == item){

found = true;

totalPrice = PRICE[i] * quantity;

break;

}

}

if(!found)

throw new OrderException(OrderMessages.message[6]);

System.out.println("Total Amount due: $" + totalPrice);

}

else

throw new OrderException(OrderMessages.message[1]);

}

else

throw new OrderException(OrderMessages.message[0]);

}catch(OrderException e){

System.out.println(e.getMessage());

}

}

}

User Godzilla
by
3.8k points