Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- import javax.swing.JFileChooser;
- public class MemorySavingCalc {
- static boolean onStart = true;
- static CalcOpps calc = new CalcOpps();
- public static void main(String[] args) {
- if(onStart){
- CalcOpps.start();
- onStart = false;
- }
- double a = 0;
- System.out.println("Please chose an option.");
- int opt = CalcOpps.getMenuOption();
- if(opt > 0 && opt < 5){
- a = CalcOpps.getOperand("Please give the second operand.");
- if (opt == 1) {
- calc.add(a);
- System.out.println("The current value is: " + calc.getCurrentValue());
- }if (opt == 2) {
- calc.subtract(a);
- System.out.println("The current value is: " + calc.getCurrentValue());
- }if (opt == 3) {
- calc.multiply(a);
- System.out.println("The current value is: " + calc.getCurrentValue());
- }if (opt == 4) {
- calc.divide(a);
- System.out.println("The current value is: " + calc.getCurrentValue());
- }
- }
- if (opt == 5) {
- calc.clear();
- System.out.println("The current value is: " + calc.getCurrentValue());
- }if(opt == 6){
- calc.save();
- }
- if(opt == -1){
- opt = 0;
- main(null);
- }
- if(opt != 7){
- main(null);
- }
- if(opt == 7){
- System.out.println("Have a nice day.");
- }
- }
- }
- class CalcOpps{
- static Scanner in = new Scanner(System.in);
- private static double currentValue;
- private static ArrayList<String> list = new ArrayList<String>();
- JFileChooser saver = new JFileChooser();
- public static void start(){
- list.add("~~~~~~~start~~~~~~~" + "\r\n");
- System.out.println("Initial value is 0.0. Would you like to change this?\n1) Yes\n2) No");
- int opt = in.nextInt();
- if(opt == 1){
- System.out.println("Please enter a number.");
- currentValue = in.nextDouble();
- list.add("Starting value was changed to " + currentValue);
- }
- if(opt == 2){
- list.add("Starting value set to " + currentValue);
- }
- if(opt != 2 && opt != 1){
- System.out.println("Please choose a valid option.");
- start();
- }
- }
- public CalcOpps(){
- currentValue = 0.0;
- }
- public static int getMenuOption() throws InputMismatchException{
- int r;
- System.out.println("1) Addition\n2) Subtraction\n3) Multiplication\n4) Division\n5) clear\n6) Save\n7) Exit");
- try{
- r = -1;
- while(r < 0 ||r > 7){
- r = in.nextInt();
- if((r < 0 ||r > 7)){
- System.out.println("Invalid Input.");
- }
- }
- return r;
- }catch(InputMismatchException e){
- System.out.println(e + ": Invalid Input");
- }
- return -1;
- }
- public static double getOperand(String prompt){
- System.out.println(prompt);
- double r = in.nextDouble();
- return r;
- }
- public void add(double a){
- list.add(currentValue + " + " + a + " = " + (currentValue + a) + "\r\n");
- currentValue += a;
- }
- public void subtract(double a){
- list.add(currentValue + " - " + a + " = " + (currentValue - a) + "\r\n");
- currentValue -= a;
- }
- public void multiply(double a){
- list.add(currentValue + " * " + a + " = " + (currentValue * a) + "\r\n");
- currentValue = currentValue * a;
- }
- public void divide(double a){
- if(a == 0){
- System.out.println("Cannot divide by 0. Please clear, and try again.");
- currentValue = Double.NaN;
- }
- list.add(currentValue + " / " + a + " = " + (currentValue / a) + "\r\n");
- currentValue = currentValue / a;
- }
- public double getCurrentValue(){
- return currentValue;
- }
- public void clear(){
- currentValue = 0.0;
- }
- public void save(){
- list.add("~~~~~~~~end~~~~~~~~");
- saver.setCurrentDirectory(new File("C:/Users/gdog2_000/Desktop"));
- int test = saver.showSaveDialog(null);
- if(test == JFileChooser.APPROVE_OPTION){
- try(FileWriter fw = new FileWriter(saver.getSelectedFile() + ".txt")){
- for(int i = 0; i < list.size(); i++){
- fw.write(list.get(i));
- }
- fw.close();
- }catch(IOException ex){
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement