I'm the newbie in Java (and programming) and I've tried to make a simple calculator. Code is barbaric, I know, so I would like to make it more beatiful.
Main.java:
import java.util.Scanner;
import java.util.Vector;
public class Main {
static Main mainobject = new Main();
ActionsToDo act = new ActionsToDo();
Solution solve = new Solution();
public static Vector<Double> v1 = new Vector<Double>();
public void setValues(double fn, double sn) {
solve.solveExpression(chooseOperator(), fn, sn);
act.chooseAction(fn, sn);
}
public static double getFirstNumber() {
System.out.println("Input the 1st number");
Scanner sc = new Scanner(System.in);
return sc.nextDouble();
}
public static double getSecondNumber() {
System.out.println("Input the 2nd number");
Scanner sc = new Scanner(System.in);
return sc.nextDouble();
}
public char chooseOperator() {
Scanner sc = new Scanner(System.in);
System.out.println("What to do?" +
"\n + for add" +
"\n - for minus" +
"\n * for multiply" +
"\n / for divide" +
"\n % for mod" +
"\n ^ for first number into the power of second number");
String operator = sc.next();
return operator.charAt(0);
}
public static void main(String[] args) {
mainobject.setValues(getFirstNumber(), getSecondNumber());
}
}
Solution.java
public class Solution {
static Main objectMain2 = new Main();
public void solveExpression (char checker, double fn, double sn) {
switch (checker) {
case '+':
System.out.println((fn + sn));
objectMain2.v1.addElement((fn + sn));
break;
case '-':
System.out.println((fn - sn));
objectMain2.v1.addElement((fn - sn));
break;
case '*':
System.out.println((fn * sn));
objectMain2.v1.addElement((fn * sn));
break;
case '/':
System.out.println((fn / sn));
objectMain2.v1.addElement((fn / sn));
break;
case '%':
System.out.println((fn % sn));
objectMain2.v1.addElement((fn % sn));
break;
case '^':
System.out.println(Math.pow(fn, sn));
objectMain2.v1.addElement(Math.pow(fn,sn));
break;
}
}
}
ActionsToDo.java:
import java.util.Scanner;
public class ActionsToDo {
static Main objectMain = new Main();
public void chooseAction(double fn, double sn) {
Scanner sc = new Scanner(System.in);
System.out.println("Dormammu, I came to bargain! " +
"Wanna do some extra calculations?" +
"\n 1 - for 'Yes'" +
"\n 2 - for 'No'" +
"\n 3 - to change the numbers");
int dormammu = sc.nextInt();
switch (dormammu) {
case 1:
objectMain.setValues(fn, sn);
break;
case 2:
System.out.println("Okay! Nobody misses you" +
"\n But here's the calculations you've done so far");
for (int i = 0; i < objectMain.v1.size();i++) {
System.out.print(objectMain.v1.get(i) + " ");
}
break;
case 3:
objectMain.setValues(objectMain.getFirstNumber(), objectMain.getSecondNumber());
break;
default:
System.out.println("Shit, wrong answer, you'll have to calculate " +
"with these numbers again and then you can do whatever you want");
objectMain.setValues(fn, sn);
}
}
}
Note that the different classes are used just for training of using them