PP 11.3
Suppose in a particular business all documents are given a twocharacter designation starting with either U, C, or P, standing for unclassified, confidential, or proprietary. Create an exception class called InvalidDocumentCodeException, designed to be thrown when an improper designation for a document is encountered during processing. If a document designation is encountered that doesn’t fit that description, the exception is thrown. Create a driver program to test the exception, allowing it to terminate the program.
PP11_3.java
import java.util.Scanner;
public class PP11_3 {
public static void main(String[] args) {
System.out.println("Starting with either U, C, or P, standing for unclassified, confidential, or proprietary. : ");
Scanner scanner = new Scanner(System.in);
String UCP = scanner.next();
DocumentCode doc = new DocumentCode(UCP);
System.out.println(doc);
}
}
DocumentCode.java
public class DocumentCode {
private String designation = " ";
public DocumentCode(String ucp) {
try {
if(ucp.compareToIgnoreCase("U")!=0 &&
ucp.compareToIgnoreCase("C")!=0 &&
ucp.compareToIgnoreCase("P")!=0)
throw new InvalidDocumentCodeException();
designation = ucp;
} catch(InvalidDocumentCodeException e) {
System.out.println("Invalid Character"+e);
System.out.println(e.getMessage());
}
}
public String toString() {
if(designation.compareToIgnoreCase("U")==0 ||
designation.compareToIgnoreCase("C")==0 ||
designation.compareToIgnoreCase("P")==0 )
return designation.toUpperCase();
else return " ";
}
}
InvalidDocumentCodeException.java
public class InvalidDocumentCodeException extends Exception{
public InvalidDocumentCodeException() {}
public String getMessage() { return "starting with either U, C, or P, standing for unclassified, confidential, or proprietary." ; }
}
< Console >
Starting with either U, C, or P, standing for unclassified, confidential, or proprietary. :
u
U
'[JAVA] 개발' 카테고리의 다른 글
Java Software Solution Ch7_PP2 (0) | 2024.05.01 |
---|---|
Java Software Soution Ch7_PP8 (0) | 2024.05.01 |
Java Software Solution Ch11_ PP1 ; Exception Class (try-catch) (0) | 2024.04.27 |
Java Software Solution Ch10_PP3 ; Interface (0) | 2024.04.26 |
Java Software Solution Ch7_PP8 ; Interface, Class, toString() (0) | 2024.03.17 |