123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. import java.util.*;
  2. import java.text.DecimalFormat;
  3. public class MainApplication {
  4. public static void main(String[] args) {
  5. Scanner input = new Scanner(System.in);
  6. CoreFeatures cor = new CoreFeatures();
  7. CustomFeatureDistance cust = new CustomFeatureDistance();
  8. CoreAl cal =new CoreAl();
  9. ScientificAl sci=new ScientificAl();
  10. CustomFeatureTemperature custom= new CustomFeatureTemperature();
  11. double calculationResult = 0;
  12. double conversionResult = 0;
  13. double mem = 0;
  14. //double defaultCalcResult = 0;
  15. //System.out.println("Enter a command: ");
  16. //consider using boolean to store the first calculation (first numb)
  17. while (true) {
  18. try
  19. {
  20. double defaultCalcResult = 0;
  21. System.out.println("Enter a command: ");
  22. if (calculationResult != 0) {
  23. System.out.println(calculationResult);
  24. }else {
  25. System.out.println(defaultCalcResult);
  26. }
  27. //User input
  28. String command = input.next();
  29. //Makes the current display 0
  30. if (command.equalsIgnoreCase("C")){
  31. calculationResult = defaultCalcResult;
  32. System.out.println("Clear!");
  33. //System.out.println("Enter a command: ");
  34. }
  35. //Turn off calculator
  36. if (command.equalsIgnoreCase("exit")){
  37. System.out.print("Thanks for calculating with us!");
  38. break;
  39. }
  40. //Display Mode
  41. if (command.equalsIgnoreCase("DisplayMode")){
  42. String userInput = Console.getStringInput(
  43. "Please enter in the desired mode: \n \"Bin\" - for binary \n \"Oct\" - for octal \n \"Dec\" - for deciaml \n \"Hex\" - for hexadeciaml");
  44. if (userInput.equalsIgnoreCase("Bin")){
  45. int userInputBin = Console.getIntegerInput("Please enter the integer value you wish to convert: ");
  46. System.out.println("Current value in binary is: " + Integer.toBinaryString(userInputBin));
  47. }
  48. if (userInput.equalsIgnoreCase("Oct")){
  49. int userInputBin = Console.getIntegerInput("Please enter the integer value you wish to convert: ");
  50. System.out.println("Current value in octal is: " + Integer.toOctalString(userInputBin));
  51. }
  52. if (userInput.equalsIgnoreCase("Dec")){
  53. int userInputBin = Console.getIntegerInput("Please enter the integer value you wish to convert: ");
  54. DecimalFormat decimalValue = new DecimalFormat("#");
  55. String value = decimalValue.format(userInputBin);
  56. System.out.println("Current value in deciaml is: " + value);
  57. }
  58. if (userInput.equalsIgnoreCase("Hex")){
  59. int userInputBin = Console.getIntegerInput("Please enter the integer value you wish to convert: ");
  60. System.out.println("Current value in hexadecimal is: " + Integer.toHexString(userInputBin));
  61. }
  62. }
  63. //aleeena
  64. //double calculationResult=0.0;
  65. //while
  66. //Scanner input = new Scanner(System.in);
  67. /* double defaultCalcResult = 0.0;
  68. if (calculationResult != 0.0) {
  69. System.out.printf("\nCurrent Value :%.2f",calculationResult);
  70. }else {
  71. System.out.printf("\nCurrent Value :%.2f",defaultCalcResult);
  72. }
  73. System.out.print("\nEnter a command: ");
  74. String command = input.next();
  75. if (command.equalsIgnoreCase("C")){
  76. calculationResult = defaultCalcResult;
  77. System.out.println("Clear!");
  78. }
  79. //String command = input.next();
  80. if (command.equalsIgnoreCase("exit")){
  81. System.out.print("Thanks for calculating with us!\n");
  82. break;
  83. }*/
  84. if (command.equalsIgnoreCase("square")){
  85. if(calculationResult!=0.00)
  86. {
  87. double num=calculationResult;
  88. calculationResult=cal.square(num);
  89. Console.printDouble(calculationResult);
  90. }
  91. else
  92. {
  93. double num=Console.getDoubleInput("Enter the number :");
  94. calculationResult=cal.square(num);
  95. Console.printDouble(calculationResult);
  96. }
  97. }
  98. if(command.equalsIgnoreCase("squareroot"))
  99. {
  100. if(calculationResult!=0.00)
  101. {
  102. double num=calculationResult;
  103. calculationResult=cal.squareroot(num);
  104. Console.printDouble(calculationResult);
  105. }
  106. else
  107. {
  108. double num=Console.getDoubleInput("Enter the number :");
  109. calculationResult=cal.squareroot(num);
  110. Console.printDouble(calculationResult);
  111. }
  112. }
  113. if(command.equalsIgnoreCase("exponent"))
  114. {
  115. if(calculationResult!=0.00)
  116. {
  117. double power=Console.getDoubleInput("Enter the power");
  118. calculationResult=cal.exponent(calculationResult,power);
  119. Console.printDouble(calculationResult);
  120. }
  121. else
  122. {
  123. double base=Console.getDoubleInput("Enter the base value");
  124. double power=Console.getDoubleInput("Enter the power");
  125. calculationResult=cal.exponent(base,power);
  126. Console.printDouble(calculationResult);
  127. }
  128. }
  129. if(command.equalsIgnoreCase("inverse"))
  130. {
  131. if(calculationResult!=0.00)
  132. {
  133. calculationResult=cal.inverse(calculationResult);
  134. Console.printDouble(calculationResult);
  135. }
  136. else
  137. {
  138. double num=Console.getDoubleInput("Enter the number");
  139. calculationResult=cal.inverse(num);
  140. Console.printDouble(calculationResult);
  141. }
  142. }
  143. if(command.equalsIgnoreCase("invert"))
  144. {
  145. if(calculationResult!=0.00)
  146. {
  147. calculationResult=cal.invertsign(calculationResult);
  148. Console.printDouble(calculationResult);
  149. }
  150. else
  151. {
  152. double num=Console.getDoubleInput("Enter the number");
  153. calculationResult=cal.invertsign(num);
  154. Console.printDouble(calculationResult);
  155. }
  156. }
  157. if(command.equalsIgnoreCase("sin"))
  158. {
  159. double number=Console.getDoubleInput("Enter the degree");
  160. double result=sci.sine(number);
  161. Console.printDouble(result);
  162. calculationResult=0;
  163. }
  164. if(command.equalsIgnoreCase("cos"))
  165. {
  166. double number=Console.getDoubleInput("Enter the degree");
  167. double result=sci.cosine(number);
  168. Console.printDouble(result);
  169. calculationResult=0;
  170. }
  171. if(command.equalsIgnoreCase("tan"))
  172. {
  173. double number=Console.getDoubleInput("Enter the degree");
  174. double result=sci.tangent(number);
  175. Console.printDouble(result);
  176. calculationResult=0;
  177. }
  178. if(command.equalsIgnoreCase("inversesin"))
  179. {
  180. String mode=Console.getStringInput
  181. ("Type in \"D\" for Degree output\nType in \"R\" to Radians output");
  182. if(mode.equalsIgnoreCase("D"))
  183. {
  184. double number=Console.getDoubleInput("\nEnter the value :");
  185. calculationResult=sci.inversesinDegree(number);
  186. Console.printDouble(calculationResult);
  187. calculationResult=0;
  188. }
  189. else if(mode.equalsIgnoreCase("R"))
  190. {
  191. double number=Console.getDoubleInput("\nEnter the value :");
  192. calculationResult=sci.inversesinRadian(number);
  193. Console.printDouble(calculationResult);
  194. calculationResult=0;
  195. }
  196. }
  197. if(command.equalsIgnoreCase("inversecos"))
  198. {
  199. String mode=Console.getStringInput
  200. ("Type in \"D\" for Degree output\nType in \"R\" to Radians output");
  201. if(mode.equalsIgnoreCase("D"))
  202. {
  203. double number=Console.getDoubleInput("\nEnter the value :");
  204. calculationResult=sci.inversecosDegree(number);
  205. Console.printDouble(calculationResult);
  206. calculationResult=0;
  207. }
  208. else if(mode.equalsIgnoreCase("R"))
  209. {
  210. double number=Console.getDoubleInput("\nEnter the value :");
  211. calculationResult=sci.inversecosRadians(number);
  212. Console.printDouble(calculationResult);
  213. calculationResult=0;
  214. }
  215. }
  216. if(command.equalsIgnoreCase("inversetan"))
  217. {
  218. String mode=Console.getStringInput
  219. ("Type in \"D\" for Degree output\nType in \"R\" to Radians output");
  220. if(mode.equalsIgnoreCase("D"))
  221. {
  222. double number=Console.getDoubleInput("\nEnter the value :");
  223. calculationResult=sci.inversetanDegree(number);
  224. Console.printDouble(calculationResult);
  225. calculationResult=0;
  226. }
  227. else if(mode.equalsIgnoreCase("R"))
  228. {
  229. double number=Console.getDoubleInput("\nEnter the value :");
  230. calculationResult=sci.inversetanRadians(number);
  231. Console.printDouble(calculationResult);
  232. calculationResult=0;
  233. }
  234. }
  235. if(command.equalsIgnoreCase("temperature"))
  236. {
  237. String userInput = Console.getStringInput
  238. ("Type in \"c\" to convert Celcius to Farhenheit\nType in \"f\" to convert Farhenheit to Celcius");
  239. if(userInput.equalsIgnoreCase("c"))
  240. {
  241. double temp=Console.getDoubleInput("\nEnter the temperature in Celcius :");
  242. double result=custom.celciustoFarhenheat(temp);
  243. //System.out.println(temp+"Celcius to f
  244. calculationResult = result;
  245. Console.printDouble(result);
  246. }
  247. else if(userInput.equalsIgnoreCase("f"))
  248. {
  249. double temp=Console.getDoubleInput("\nEnter the temperature in Farhenheit :");
  250. double result=custom.farhenheatTocelcius(temp);
  251. calculationResult = result;
  252. Console.printDouble(result);
  253. }
  254. }
  255. if(command.equalsIgnoreCase("unit"))
  256. {
  257. String userInput=Console.getStringInput
  258. ("Type in \"D\" to convert Degree to Radians\nType in \"R\" to convert Radians to Degree");
  259. if(userInput.equalsIgnoreCase("D"))
  260. {
  261. double deg=Console.getDoubleInput("\nType in Degree to display the Radian value:");
  262. double result=sci.degToradian(deg);
  263. //System.out.println(temp+"Celcius to f
  264. Console.printDouble(result);
  265. }
  266. if(userInput.equalsIgnoreCase("R"))
  267. {
  268. double rad=Console.getDoubleInput("\nType in Radian to display the Degree value:");
  269. double result=sci.radToDegree(rad);
  270. Console.printDouble(result);
  271. }
  272. }
  273. //if (userInput.equalsIgnoreCase("Oct"))
  274. //Memory
  275. if (command.equalsIgnoreCase("Memory")){
  276. double memInput = Console.getDoubleInput("Please enter in a numeric value to save: ");
  277. mem = memInput;
  278. System.out.println("The value (" + mem + ") has been saved to the memory.");
  279. }
  280. //Memory +
  281. if (command.equalsIgnoreCase("M+")){
  282. double memAdd = mem + calculationResult;
  283. //calculationResult = memAdd;
  284. System.out.println("Saved value (" + mem + ") has been added to the current value (" + calculationResult + ").");
  285. calculationResult = memAdd;
  286. }
  287. //Memory reset
  288. if (command.equalsIgnoreCase("MC")){
  289. if (mem != 0){
  290. mem = 0;
  291. System.out.println("Reset memory value.");
  292. }
  293. else{
  294. System.out.println("Reset memory value.");
  295. }
  296. }
  297. //Display memory
  298. if (command.equalsIgnoreCase("MRC")){
  299. calculationResult = mem;
  300. System.out.println("Recall memory value.");
  301. }
  302. //Addition
  303. if (command.equalsIgnoreCase("add")){
  304. if(calculationResult != 0){
  305. double firstNumb = calculationResult;
  306. double secondNumb = Console.getDoubleInput("Type in the second number you wish to add: ");
  307. calculationResult = firstNumb + secondNumb;
  308. //double x = calculationResult;
  309. //calculationResult = cor.add(x);
  310. defaultCalcResult = calculationResult;
  311. }
  312. else{
  313. double firstNumb = Console.getDoubleInput("Type in the first number you wish to add: ");
  314. double secondNumb = Console.getDoubleInput("Type in the second number you wish to add: ");
  315. calculationResult = firstNumb + secondNumb;
  316. defaultCalcResult = calculationResult;
  317. }
  318. //double firstNumb = Console.getDoubleInput("Type in the first number you wish to add: ");
  319. //double secondNumb = Console.getDoubleInput("Type in the second number you wish to add: ");
  320. //calculationResult = firstNumb + secondNumb;
  321. //defaultCalcResult = calculationResult;
  322. System.out.println("The sum of two number is: " + calculationResult);
  323. //Console.println(firstNumb);
  324. }
  325. //Subtraction
  326. if (command.equalsIgnoreCase("subtract")){
  327. if(calculationResult != 0){
  328. double firstNumb = calculationResult;
  329. double secondNumb = Console.getDoubleInput("Type in the second number you wish to subtract: ");
  330. calculationResult = cor.subtract(firstNumb, secondNumb);
  331. defaultCalcResult = calculationResult;
  332. }
  333. else{
  334. double firstNumb = Console.getDoubleInput("Type in the first number you wish to subtract: ");
  335. double secondNumb = Console.getDoubleInput("Type in the second number you wish to subtract: ");
  336. calculationResult = cor.subtract(firstNumb, secondNumb);
  337. defaultCalcResult = calculationResult;
  338. }
  339. System.out.println("The difference of the two number is: " + calculationResult);
  340. }
  341. //Multiplication
  342. if (command.equalsIgnoreCase("multiply")){
  343. if(calculationResult != 0){
  344. double firstNumb = calculationResult;
  345. double secondNumb = Console.getDoubleInput("Type in the second number you wish to multiply: ");
  346. calculationResult = cor.multiply(firstNumb, secondNumb);
  347. System.out.println("The multiplication of the two number is: " + calculationResult);
  348. }
  349. else{
  350. double firstNumb = Console.getDoubleInput("Type in the first number you wish to multiply: ");
  351. double secondNumb = Console.getDoubleInput("Type in the second number you wish to multiply: ");
  352. calculationResult = cor.multiply(firstNumb, secondNumb);
  353. System.out.println("The multiplication of the two number is: " + calculationResult);
  354. }
  355. }
  356. //Division
  357. if (command.equalsIgnoreCase("divide")){
  358. if (calculationResult != 0){
  359. double firstNumb = calculationResult;
  360. double secondNumb = Console.getDoubleInput("Type in the divisor: ");
  361. calculationResult = cor.divide(firstNumb, secondNumb);
  362. System.out.println("The division of the two number is: " + calculationResult);
  363. }
  364. else{
  365. double firstNumb = Console.getDoubleInput("Type in the dividend: ");
  366. double secondNumb = Console.getDoubleInput("Type in the divisor: ");
  367. calculationResult = firstNumb / secondNumb;
  368. System.out.println("The division of the two number is: " + calculationResult);
  369. }
  370. }
  371. //Distance conversion *Custom Feature*
  372. if (command.equalsIgnoreCase("distance")){
  373. //System.out.println("[Current distance is set to Km]");
  374. String userInput = Console.getStringInput(
  375. "Please select from the following options: \nType in \"km \"to convert Kilometers to Miles\nType in \"Mi \"to convert Miles to Kilometers");
  376. //Kilimeter conversion
  377. if (userInput.equalsIgnoreCase("km")){
  378. double firstNumb = Console.getDoubleInput(
  379. "Type in the numeric distance you wish to convert to miles: ");
  380. conversionResult = cust.kmToMiles(firstNumb);
  381. calculationResult = conversionResult;
  382. System.out.println(firstNumb + "Km(s) in mile(s) is: " + calculationResult);
  383. }
  384. //Mile conversion
  385. if(userInput.equalsIgnoreCase("Mi")){
  386. double firstNumb = Console.getDoubleInput(
  387. "Type in the numeric distance you wish to convert to kilometers: ");
  388. conversionResult = cust.milesToKm(firstNumb);
  389. calculationResult = conversionResult;
  390. System.out.println(firstNumb + "mile(s) in Km(s) is: " + calculationResult);
  391. }
  392. }
  393. // else{
  394. // System.out.println("Invalid command! Please enter a valid command");
  395. // }
  396. // Console.println("Welcome to my calculator!");
  397. // String s = Console.getStringInput("Enter a string");
  398. // Integer i = Console.getIntegerInput("Enter an integer");
  399. // Double d = Console.getDoubleInput("Enter a double.");
  400. // Console.println("The user input %s as a string", s);
  401. // Console.println("The user input %s as a integer", i);
  402. // Console.println("The user input %s as a d", d);
  403. //String output = Console.getStringInput("type something");
  404. //Console.println(output);
  405. //public void runCommandLoop()
  406. // Scanner input = new Scanner(System.in);
  407. // Scanner input2 = new Scanner(System.in);
  408. // while (true) {
  409. // System.out.print("Enter a command: ");
  410. // String command = input.next();
  411. // if (command.equalsIgnoreCase("exit")){
  412. // System.out.print("Thanks for calculating with us!");
  413. // break;
  414. // }
  415. // if (command.equalsIgnoreCase("add")){
  416. // double firstNumb = Console.getDoubleInput("Type in the first number you wish to add: ");
  417. // double secondNumb = Console.getDoubleInput("Type in the second number you wish to add: ");
  418. // //Console.println(firstNumb);
  419. // }
  420. // }
  421. }
  422. catch(Exception e)
  423. {
  424. System.out.println("An error has occured");
  425. continue;
  426. }
  427. }
  428. }
  429. }