public class Checkvalidate {
private Scanner scanner;
public Checkvalidate() {
scanner = new Scanner(System.in);
}
public double getDoubleInput() {
double Input;
while (true) { // Start an infinite loop to keep asking for input until a valid value is provided.
try {
Input = Double.parseDouble(scanner.nextLine()); // Read the user's input and parse it as an integer.
if (Input > 0) {
break;
// if size > 0 , valid value and loop break
} else {
System.err.println("You have to input a positive number , Reinpiut : ");
// iff size <= 0 , Display notice
}
} catch (NumberFormatException e) {
System.err.println("Invalid value , Reinput : ");
// if users input a special character , program will notice to users
}
}
return Input;
}
public boolean isRectangleValid(double width, double length) {
if (width > length) {
System.err.println("Invalid Rectangle.");
return false;
}
return true;
}
public boolean isTriangleValid(double sideA, double sideB, double sideC) {
if (sideA + sideB <= sideC || sideA + sideC <= sideB || sideB + sideC <= sideA) {
System.err.println("Invalid Triangle.");
return false;
}
return true;
}
}
public class Main {
public static void main(String[] args) {
System.out.println("Please input side Width of Rectangle : ");
Checkvalidate width = new Checkvalidate();
double Width = width.getDoubleInput();
System.out.println("Please input side Length of Rectangle : ");
Checkvalidate length = new Checkvalidate();
double Length = length.getDoubleInput();
while (!width.isRectangleValid(Width, Length)) {
System.out.println("Please input side Width of Rectangle: ");
Width = width.getDoubleInput();
System.out.println("Please input side Length of Rectangle: ");
Length = length.getDoubleInput();
}
System.out.println("Please input side radius of Circle :");
Checkvalidate radius = new Checkvalidate();
double Radius = radius.getDoubleInput();
System.out.println("Please input side sideA of Triangle :");
Checkvalidate sideA = new Checkvalidate();
double SideA = sideA.getDoubleInput();
System.out.println("Please input side sideB of Triangle :");
Checkvalidate sideB = new Checkvalidate();
double SideB = sideB.getDoubleInput();
System.out.println("Please input side sideA of Triangle :");
Checkvalidate sideC = new Checkvalidate();
double SideC = sideC.getDoubleInput();
while (!sideA.isTriangleValid(SideA, SideB, SideC)) {
System.out.println("Please input side sideA of Triangle: ");
SideA = sideA.getDoubleInput();
System.out.println("Please input side sideB of Triangle: ");
SideB = sideB.getDoubleInput();
System.out.println("Please input side sideC of Triangle: ");
SideC = sideC.getDoubleInput();
}
System.out.println("-----Rectangle-----");
Rectangle rectangle = new Rectangle(Width, Length);
rectangle.printResult();
System.out.println("-----Circle-----");
Circle circle = new Circle(Radius);
circle.printResult();
System.out.println("-----Triangle-----");
Triangle triangle = new Triangle(SideA, SideB, SideC);
triangle.printResult();
}
}