-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
93 lines (91 loc) · 3.79 KB
/
Copy pathMain.java
File metadata and controls
93 lines (91 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import java.util.ArrayList;
import java.util.Scanner;
import shapes.*;
public class Main {
public static void main(String[] args) {
ArrayList<Shape> shapes = new ArrayList<Shape>();
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 4; i++) {
shapes.add(addShape(scanner));
}
for (Shape shape : shapes) {
shape.displayInfo();
}
}
public static Shape addShape(Scanner scanner) {
String type;
String name;
while (true) {
System.out.print("Desired Shape:");
type = scanner.nextLine();
switch(type) {
case "rectangle":
try {
System.out.print("Name: ");
name = scanner.nextLine();
System.out.print("Length: ");
Double length = Double.parseDouble(scanner.nextLine());
System.out.print("Width: ");
Double width = Double.parseDouble(scanner.nextLine());
System.out.println();
return new Rectangle(name, length, width);
}
catch (Exception e) {
System.out.println("Please enter valid value." + e.getMessage());
}
break;
case "triangle":
try {
System.out.print("Name: ");
name = scanner.nextLine();
System.out.print("Side A: ");
Double SideA = Double.parseDouble(scanner.nextLine());
System.out.print("SideB: ");
Double SideB = Double.parseDouble(scanner.nextLine());
System.out.print("SideC: ");
Double SideC = Double.parseDouble(scanner.nextLine());
System.out.println();
return new Triangle(name, SideA, SideB, SideC);
}
catch (Exception e) {
System.out.println("Please enter valid value." + e.getMessage());
}
break;
case "circle":
try {
System.out.print("Name: ");
name = scanner.nextLine();
System.out.print("Radius: ");
Double radius = Double.parseDouble(scanner.nextLine());
System.out.println();
return new Circle(name, radius);
}
catch (Exception e) {
System.out.println("Please enter valid value." + e.getMessage());
}
break;
case "polygon":
try {
System.out.print("Name: ");
name = scanner.nextLine();
System.out.print("Number of Sides: ");
int numSides = Integer.parseInt(scanner.nextLine());
System.out.print("Length of Sides: ");
double lengthSides = Double.parseDouble(scanner.nextLine());
System.out.println();
return new Polygon(name, numSides, lengthSides);
}
catch (Exception e) {
System.out.println("Please enter valid value." + e.getMessage());
}
break;
case "":
break;
default:
System.out.println("Please enter a valid shape.");
break;
}
continue;
}
}
}