In: Computer Science
A company accepts user orders by part numbers interactively.
Users might make the following errors
as they enter data:
The part number is not numeric. u The quantity is not
numeric.
The part number is too low (less than 0).
The part number is too high (more than 999).
The quantity ordered is too low (less than 1).
The quantity ordered is too high (more than 5,000).
Create a class that stores an array of usable error messages; save
the file as DataMessages.java. Create a
DataException class; each object of this class will store one of
the messages. Save the file as
DataException.java. Create an application that prompts the user for
a part number and quantity. Allow
for the possibility of nonnumeric entries as well as out-of-range
entries, and display the appropriate
message when an error occurs. If no error occurs, display the
message “Valid entry”. Save the program
as PartAndQuantityEntry.java.
PartAndQuantityEntry.java
import java.util.*;
import java.util.Scanner;
import java.lang.*;
public class Main{
public static void PartAndQuantityEntry(String[] args)
{
DataMessages datamsg=new DataMessages();
DataException obj1=new DataException(datamsg.msg[0]);
DataException obj2=new DataException(datamsg.msg[1]);
DataException obj3=new DataException(datamsg.msg[2]);
DataException obj4=new DataException(datamsg.msg[3]);
DataException obj5=new DataException(datamsg.msg[4]);
DataException obj6=new DataException(datamsg.msg[5]);
Scanner sc=new Scanner(System.in);
System.out.println("Enter part number and quantity : ");
String pn=sc.next();
String qn=sc.next();
int flag1=1,flag2=1,flag3=1,flag4=1,flag5=1,flag6=1;
for(int i=0;i<pn.length();i++)
{
if(i==0&&pn.charAt(0)=='-')
continue;
if(pn.charAt(i)!='0'&&pn.charAt(i)!='1'&&pn.charAt(i)!='2'&&pn.charAt(i)!='3'&&pn.charAt(i)!='4'&&pn.charAt(i)!='5'&&pn.charAt(i)!='6'&&pn.charAt(i)!='7'&&pn.charAt(i)!='8'&&pn.charAt(i)!='9')
flag1=-1;
}
for(int i=0;i<qn.length();i++)
{
if(i==0&&qn.charAt(0)=='-')
continue;
if(qn.charAt(i)!='0'&&qn.charAt(i)!='1'&&qn.charAt(i)!='2'&&qn.charAt(i)!='3'&&qn.charAt(i)!='4'&&qn.charAt(i)!='5'&&qn.charAt(i)!='6'&&qn.charAt(i)!='7'&&qn.charAt(i)!='8'&&qn.charAt(i)!='9')
flag2=-1;
}
if(flag1!=-1&&Integer.parseInt(pn)<0)
flag3=-1;
if(flag1!=-1&&Integer.parseInt(pn)>999)
flag4=-1;
if(flag2!=-1&&Integer.parseInt(qn)<0)
flag5=-1;
if(flag2!=-1&&Integer.parseInt(qn)>5000)
flag6=-1;
String result="";
if(flag1!=-1&&flag2!=-1&&flag3!=-1&&flag4!=-1&&flag5!=-1&&flag6!=-1)
result="Valid Entry";
else
{
result+=(flag1==-1)?obj1.err+"\n":"";
result+=(flag2==-1)?obj2.err+"\n":"";
result+=(flag3==-1)?obj3.err+"\n":"";
result+=(flag4==-1)?obj4.err+"\n":"";
result+=(flag5==-1)?obj5.err+"\n":"";
result+=(flag6==-1)?obj6.err+"\n":"";
}
System.out.println(result);
}
}
Explanation:
DataMessages.java
import java.util.*;
public class DataMessages
{
String[]msg =
{
"The part number is not numeric.","The Qunatity is not numeric.", "The part number is too low.",
"The part number is too high.", "The quantity ordered is too low.",
"The quantity ordered is too high."};
}
DataException.java
import java.util.*;
public class DataException{
String err="";
DataException(String str)
{
this.err=str;
}
}