Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- boolean runAgain = false;
- Double result = .0;
- List<Double> inputs = new ArrayList<Double>();
- Scanner scanner = new Scanner(System.in);
- do {
- System.out.print("Enter the first number : ");
- inputs.add(scanner.nextDouble());
- System.out.print("Enter the second number : ");
- inputs.add(scanner.nextDouble());
- System.out.print("Enter the operator : ");
- String op = scanner.next();
- result = calculateAnswer(inputs, op);
- System.out.println("Answer : "+result);
- System.out.print("Do you want to try again?[Y/N] : ");
- runAgain = scanner.next().equals("Y")?true : false;
- } while (runAgain);
- System.out.println("Program finished!");
- scanner.close();
- }
- static Double calculateAnswer(List<Double> inputs, String operator){
- Operations[] ops = Operations.values();
- Operations correctOp = Arrays.asList(ops).stream()
- .filter(op -> op.getSymbol().equals(operator))
- .findFirst().get();
- return correctOp.getResult(inputs.get(0), inputs.get(1));
- }
- enum Operations{
- ADD(){
- @Override
- String getSymbol() {
- return "+";
- }
- @Override
- Double getResult(Double d1, Double d2) {
- return d1 + d2;
- }
- },SUB{
- @Override
- String getSymbol() {
- return "-";
- }
- @Override
- Double getResult(Double d1, Double d2) {
- return d1 - d2;
- }
- },MUL{
- @Override
- String getSymbol() {
- return "*";
- }
- @Override
- Double getResult(Double d1, Double d2) {
- return d1 * d2;
- }
- },DIV{
- @Override
- String getSymbol() {
- return "/";
- }
- @Override
- Double getResult(Double d1, Double d2) {
- return d1 / d2;
- }
- };
- abstract Double getResult(Double d1, Double d2);
- abstract String getSymbol();
- }
- enum Operation {
- ADD("+", (d1, d2) -> d1 + d2), // or Double::sum
- SUB("-", (d1, d2) -> d1 - d2),
- MUL("*", (d1, d2) -> d1 * d2),
- DIV("/", (d1, d2) -> d1 / d2);
- private final String symbol;
- private final DoubleBinaryOperator operator;
- private Operation(String symbol, DoubleBinaryOperator operator) {
- this.symbol = symbol;
- this.operator = operator;
- }
- public double getResult(double d1, double d2) {
- return operator.applyAsDouble(d1, d2);
- }
- public String getSymbol() {
- return symbol;
- }
- }
- Operation correctOp =
- Arrays.stream(Operation.values())
- .filter(op -> op.getSymbol().equals(operator))
- .findFirst()
- .orElseThrow(() -> new IllegalArgumentException("No operation found for operator " + operator));
- double calculateAnswer(double firstInput, double secondInput, String operator)
- runAgain = scanner.next().equals("Y")?true : false;
- runAgain = scanner.next().equals("Y");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement