Textdoc
Zipdoc
Writeurl
Loading…
import java.util.Scanner; import java.text.DecimalFormat; import static java.lang.Math.*; public class Num { private String stringValue; private char type; private double doubleValue; private static String stringOperation; private static boolean dontKnow; private static double result; private static boolean isOn = true; private static int timesRun; private static final DecimalFormat df = new DecimalFormat("#.##"); public Num() { } public static void main(String[] args) { Num firstNum = new Num(); Num secondNum = new Num(); System.out.println(getGreeting() + " I'm Alec the Calculator! I can perform the following operations: " + "addition (+), subtraction (-), multiplication (*), and division (/). But I'm only a beginner! " + "That's why I can only operate with integers of up to ten. Keep in mind, if you input more than two " + "operands, I will execute only the first operation and ignore the rest. The good news is you can " + "use both Arab (1, 2, 3...) and Roman numbers (I, II, III...)!"); while (isOn) { if(timesRun == 0){ System.out.println(" You can type in your math problem below:"); } else if(timesRun < 15) { System.out.println(" You can type in your another math problem below:"); } else if(timesRun == 15) { System.out.println(" Wow! I've solved quite a lot of math problems! I'm getting the hang of it, aren't I? " + "If you have another one you can type it in below:"); } else if(timesRun < 50) { System.out.println(" Another one?"); } else if(timesRun == 50) { System.out.println(" Oh my goodness! It's been a 50th math problem solved by me during this run! I'm " + "getting drowsy..."); } else if(timesRun < 100) { System.out.println(" It was the last one, wasn't it?.."); } else { System.out.println(" I'm sorry, but I keep nodding off... I think I'll turn myself off. See you next time!"); isOn = false; } Scanner scanner = new Scanner(System.in); firstNum.stringValue = scanner.next(); stringOperation = scanner.next(); secondNum.stringValue = scanner.next(); firstNum.checkType(); secondNum.checkType(); if (firstNum.type == 'd' || secondNum.type == 'd') { firstNum.decimalRejection(firstNum, secondNum); } if (firstNum.type == 'i' || secondNum.type == 'i') { integerHandling(firstNum, secondNum); } if (firstNum.type == 'r' || secondNum.type == 'r') { romanHandling(firstNum, secondNum); } calculatingResult(firstNum, secondNum); if(printingResult(firstNum, secondNum) != null){ if(printingResult(firstNum, secondNum).equals("off")){ isOn = false; } else { System.out.println(printingResult(firstNum, secondNum)); timesRun++; } } // System.out.println("Result is: " + result); // // System.out.println("String.valueOf(result) is: " + String.valueOf(result)); // System.out.println("The datatype of String.valueOf(result) is: " + String.valueOf(result).getClass().getName()); // // System.out.println("df.format(result) is: " + df.format(result)); // System.out.println("The datatype of df.format(result) is: " + df.format(result).getClass().getName()); // // System.out.println("Is String.valueOf(result) equal to df.format(result): " + String.valueOf(result).equals(df.format(result))); } } public static String getGreeting() { int ri = (int) (Math.random() * 9 + 1); return switch (ri) { case 1, 2 -> "Hello!"; case 3, 4 -> "Hi there!"; case 5, 6 -> "Nice to see you!"; case 7, 8 -> "Greetings!"; default -> "Aloha!"; }; } public void checkType() { if (stringValue.compareTo("$") >= 12 && stringValue.compareTo("$") <= 21) { if ((int) (double) Double.valueOf(stringValue) == (double) Double.valueOf(stringValue)) { type = 'i'; } else { type = 'd'; } } else { type = 'r'; } } public void decimalRejection(Num firstNum, Num secondNum) { if (firstNum.type == 'd' && secondNum.type != 'd') { System.out.print("It looks like your first number is decimal! "); } else if (firstNum.type != 'd') { System.out.print("It looks like your second number is decimal! "); } else { System.out.print("It looks like both of your numbers are decimal! "); } System.out.println("Can't work with decimals, sorry! I wish I could, though"); dontKnow = true; } public static void romanHandling(Num firstNum, Num secondNum) { if (firstNum.type == 'r') { firstNum.romanToArab(); } if (secondNum.type == 'r') { secondNum.romanToArab(); } if (firstNum.doubleValue == 0 || secondNum.doubleValue == 0) { if (firstNum.doubleValue == 0 && secondNum.doubleValue != 0) { System.out.println("I don't know the first number! It's supposed to be a Roman, isn't it?"); } else if (firstNum.doubleValue != 0) { System.out.println("I don't know the second number! It's supposed to be a Roman, isn't it?"); } else { System.out.println("I know neither of the numbers! They are supposed to be Romans, aren't they?"); } dontKnow = true; } else if (firstNum.type != 'r' || secondNum.type != 'r') { System.out.println("Poof! We got an error! I'm not allowed to calculate the result if only " + "one of the numbers is Roman! To be honest, I think it's a silly feature of my design!"); dontKnow = true; } } public void romanToArab() { switch (stringValue) { case "I" -> doubleValue = 1; case "II" -> doubleValue = 2; case "III" -> doubleValue = 3; case "IV" -> doubleValue = 4; case "V" -> doubleValue = 5; case "VI" -> doubleValue = 6; case "VII" -> doubleValue = 7; case "VIII" -> doubleValue = 8; case "IX" -> doubleValue = 9; case "X" -> doubleValue = 10; default -> dontKnow = true; } } public static void integerHandling(Num firstNum, Num secondNum) { if (firstNum.type == 'i') { firstNum.doubleValue = Integer.decode(firstNum.stringValue); } if (secondNum.type == 'i') { secondNum.doubleValue = Integer.decode(secondNum.stringValue); } if (firstNum.doubleValue > 10 || secondNum.doubleValue > 10) { if (firstNum.doubleValue > 10 && secondNum.doubleValue != 10) { System.out.println("It appears the first number is too big for me! I can only handle numbers of up to ten, remember!"); } else if (firstNum.doubleValue != 10) { System.out.println("It appears the second number is too big for me! I can only handle numbers of up to ten, remember!"); } else { System.out.println("It appears both numbers are too big for me! I can only handle numbers of up to ten, remember!"); } dontKnow = true; } } public static void calculatingResult(Num firstNum, Num secondNum) { if (!dontKnow) { switch (stringOperation) { case "+" -> result = firstNum.doubleValue + secondNum.doubleValue; case "-" -> result = firstNum.doubleValue - secondNum.doubleValue; case "*" -> result = firstNum.doubleValue * secondNum.doubleValue; case "/" -> result = firstNum.doubleValue / secondNum.doubleValue; default -> { System.out.println("I don't know such an operation!"); dontKnow = true; } } } } public static String arabToRoman() { String sRTens = "null"; String sROnes = "null"; String sRValue; int ls = (int) (result / 50); int xs = (int) (result / 10); int ones = (int) (result % 10); String andSome = " and some more. Roman numerals had no concept of decimal fractions"; switch (ls) { case 2 -> { sRValue = "C"; return sRValue; } default -> { } } switch (xs) { case 1 -> sRTens = "X"; case 2 -> sRTens = "XX"; case 3 -> sRTens = "XXX"; case 4 -> sRTens = "XL"; case 5 -> sRTens = "L"; case 6 -> sRTens = "LX"; case 7 -> sRTens = "LXX"; case 8 -> sRTens = "LXXX"; case 9 -> sRTens = "XC"; default -> { } } switch (ones) { case 1 -> sROnes = "I"; case 2 -> sROnes = "II"; case 3 -> sROnes = "III"; case 4 -> sROnes = "IV"; case 5 -> sROnes = "V"; case 6 -> sROnes = "VI"; case 7 -> sROnes = "VII"; case 8 -> sROnes = "VIII"; case 9 -> sROnes = "IX"; default -> { } } if (sRTens.equals("null")) { if (result == (int) result) { sRValue = sROnes; } else { sRValue = sROnes.concat(andSome); } } else if (sROnes.equals("null")) { if (result == (int) result) { sRValue = sRTens; } else { sRValue = sRTens.concat(andSome); } } else { if (result == (int) result) { sRValue = sRTens.concat(sROnes); } else { sRValue = sRTens.concat(sROnes).concat(andSome); } } return sRValue; } public static String printingResult(Num firstNum, Num secondNum) { if (!(stringOperation.equals("/") && secondNum.doubleValue == 0)) { if (!dontKnow) { if (firstNum.type == 'r' && secondNum.type == 'r') { if (result == 0) { return "Oops! It seems we got a zero – or "nulla", as Romans said. " + "The Roman numeral system didn't have a zero, you see!"; } else if (result < 0) { return "Uh-oh... It looks like you subtracted more than you had in the first place! " + " It's generally okay, but the problem is the Roman numeral system didn't have negative numbers"; } else { return "It's " + arabToRoman() + "!"; } } else { if (result != (int) result) { if (String.valueOf(result).equals(df.format(result))) { return "It's " + result + "! If you don't like decimals, here's your integer " + "answer: " + (int) result + ". I like decimals, though"; } else { return "It's approximately " + df.format(result) + "! " + "Or, if we choose to leave out the decimal part, it's " + (int) result; } } else { return "It's " + (int) result + "!"; } } } else { return null; } } else { if (!dontKnow) { System.out.println("Gosh! I tried to divide it by zero, as you requested, but my virtual head nearly " + "exploded! I need to recover..."); return "off"; } else { return "Besides, you can't even divide by zero, I'm so told!"; } } } }