-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
138 lines (129 loc) · 5.75 KB
/
Copy pathMain.java
File metadata and controls
138 lines (129 loc) · 5.75 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;
//create an enumerator for each of the different menus
enum mode
{
MAINMENU,
CALCULATOR,
QUADRATICCALC,
RANDOMQUESTION,
};
public class Main {
public static void main(String[] args)
{
//print out a little title
System.out.println("Welcome to Group 6 Calculator");
//create instances of objects we need to use
Scanner input = new Scanner(System.in);
Calculator calc = new Calculator();
QuadraticCalc qcalc = new QuadraticCalc();
//create the variables for managing our menu
QuadraticRandom randomq = new QuadraticRandom();
boolean using_program = true;
mode Current_menu = mode.MAINMENU;
String user_input;
while (using_program)
{
switch (Current_menu) {
case MAINMENU:
//in the main menu we present options of submenus the user can enter by inputting a number
try {
System.out.println("Press the numbers to acess the respective tools of this program\n" +
"1 - Calculator 2 - Quadratic calculator 3 - Problem Generator 4 - Exit Program");
switch (input.nextInt()) {
case 1:
Current_menu = mode.CALCULATOR;
break;
case 2:
Current_menu = mode.QUADRATICCALC;
break;
case 3:
Current_menu = mode.RANDOMQUESTION;
break;
case 4:
System.out.println("Thank you for using Team 6 Calculator");
using_program = false;
break;
default:
//if a user types something invalid, we throw an error to catch
throw new InputMismatchException();
}
} catch (InputMismatchException e) {
//if the user typed something invalid. We tell them what happened
//and let them input again
input.nextLine();
System.out.println("Invalid input, please only enter the designated numbers");
}
break;
case CALCULATOR:
//in this menu, we use the calculator class to preform calculations
System.out.println("Enter your calculation: (type 'esc' to go back to main menu)");
user_input = input.next();
if(user_input.equals("esc"))
{
Current_menu = mode.MAINMENU;
}
else {
try {
String calculation = user_input;
SyntaxHandler tester = new SyntaxHandler();
//allow the user to input, if what they enter is valid according to the verify string
//method, we calcualte using that input
if (tester.verifiyString(calculation)) {
calc.UseCalculator(calculation);
} else {
System.out.println();
input.nextLine();
}
}
catch (NumberFormatException e)
{
}
}
break;
case QUADRATICCALC:
System.out.println("Enter your quadratic in the form of a,b,c where ax^2 + bx + c=0: (type 'esc' to go back to main menu)");
user_input = input.next();
try {
if (user_input.equals("esc")) {
Current_menu = mode.MAINMENU;
} else {
String calculation = user_input;
SyntaxHandler tester = new SyntaxHandler();
String[] num_array = calculation.split(",");
//allow the user to input, if what they enter is valid according to the verify string
//method, we calcualte using that input
if (tester.veriftyQuadratic(calculation)) {
qcalc.solveEquation(Double.valueOf(num_array[0]), Double.valueOf(num_array[1]), Double.valueOf(num_array[2]));
} else {
System.out.println("Invalid formatting");
throw new NumberFormatException();
}
}
}
catch (NumberFormatException e)
{
System.out.println("Error with input");
input.nextLine();
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("Please enter three terms");
input.nextLine();
}
break;
case RANDOMQUESTION:
user_input = input.nextLine();
if(user_input.equals("esc"))
{
Current_menu=mode.MAINMENU;
}
else {
randomq.questionmethod(user_input);
}
break;
}
}
}
}