Last active
September 24, 2018 11:27
-
-
Save swapnil1104/450c402961bef1137d47b5638f8a5fc2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.util.Scanner; | |
/** | |
* | |
* main()-| | |
* | parseNumber()--| | |
* | / | | |
* |-> performAction() -> calculateResult() <-------------| | |
* \ | | |
* parseNumber()--| | |
* | |
*/ | |
public class Home { | |
public static int NaN = -989898; | |
public static void main(String[] args) { | |
performAction(); | |
} | |
//runner method | |
private static void performAction() { | |
try { | |
String firstNum, secondNum; | |
System.out.println("This calculator takes input in form of numbers(one hundred) and performs divison"); | |
System.out.println("Please enter the first number:"); | |
Scanner input = new Scanner(System.in); | |
firstNum = input.nextLine(); | |
System.out.println("Please enter the second number:"); | |
secondNum = input.nextLine(); | |
int result = calculateResult(firstNum.trim(), secondNum.trim()); | |
if (result != NaN) | |
System.out.println(result); | |
} catch (IOException e) { | |
System.out.println(e.getMessage()); | |
} finally { | |
input.close(); | |
} | |
} | |
/** | |
* Performs divison operation on the provided input in the form of (firstNum / secondNum) | |
* | |
* @param firstNum | |
* @param secondNum | |
* @return the computed result | |
*/ | |
private static int calculateResult(String firstNum, String secondNum) { | |
try { | |
int first = parseNumber(firstNum); | |
int second = parseNumber(secondNum); | |
return (first / second); | |
} catch (InvalidStringSequenceException e) { | |
System.out.println("\nEXCEPTION:---------Please enter valid numbers and try again.---------\n"); | |
performAction(); | |
} catch (ArithmeticException e) { | |
System.out.println("\nEXCEPTION:---------This is not possible mathematically.---------\n"); | |
performAction(); | |
} | |
return NaN; | |
} | |
/** | |
* Method to return the integer value of string sequence entered, else throw InvalidStringSequenceException | |
* | |
* @param num :: A string containing numbers in verbal format | |
* @return Integer value of the input string | |
* @throws InvalidStringSequenceException | |
*/ | |
private static int parseNumber(String num) throws InvalidStringSequenceException { | |
if (num.equals("one hundred")) | |
return 100; | |
else if (num.equals("fifty")) | |
return 50; | |
else if (num.equals("zero")) | |
return 0; | |
else | |
throw new InvalidStringSequenceException("invalid string sequence"); | |
} | |
} | |
// custom exception class | |
class InvalidStringSequenceException extends Exception { | |
public static final long serialVersionUID = 123123; | |
public InvalidStringSequenceException(String s) { | |
super(s); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment