Advertisement
gdog2u

MemorySaverCalc

Apr 8th, 2014
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.89 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. import javax.swing.JFileChooser;
  5.  
  6. public class MemorySavingCalc {
  7.  
  8.     static boolean onStart = true;
  9.     static CalcOpps calc = new CalcOpps();
  10.    
  11.     public static void main(String[] args) {
  12.        
  13.         if(onStart){
  14.             CalcOpps.start();
  15.             onStart = false;
  16.         }
  17.         double a = 0;
  18.         System.out.println("Please chose an option.");
  19.         int opt = CalcOpps.getMenuOption();
  20.         if(opt > 0 && opt < 5){
  21.             a = CalcOpps.getOperand("Please give the second operand.");
  22.            
  23.             if (opt == 1) {
  24.                 calc.add(a);
  25.                 System.out.println("The current value is: " + calc.getCurrentValue());
  26.             }if (opt == 2) {
  27.                 calc.subtract(a);
  28.                 System.out.println("The current value is: " + calc.getCurrentValue());
  29.             }if (opt == 3) {
  30.                 calc.multiply(a);
  31.                 System.out.println("The current value is: " + calc.getCurrentValue());
  32.             }if (opt == 4) {
  33.                 calc.divide(a);
  34.                 System.out.println("The current value is: " + calc.getCurrentValue());
  35.             }
  36.         }
  37.         if (opt == 5) {
  38.             calc.clear();
  39.             System.out.println("The current value is: " + calc.getCurrentValue());
  40.         }if(opt == 6){
  41.             calc.save();
  42.         }
  43.         if(opt == -1){
  44.             opt = 0;
  45.             main(null);
  46.         }
  47.         if(opt != 7){
  48.             main(null);
  49.         }
  50.         if(opt == 7){
  51.             System.out.println("Have a nice day.");
  52.         }
  53.  
  54.     }
  55.  
  56. }
  57. class CalcOpps{
  58.     static Scanner in = new Scanner(System.in);
  59.     private static double currentValue;
  60.     private static ArrayList<String> list = new ArrayList<String>();
  61.     JFileChooser saver = new JFileChooser();
  62.    
  63.     public static void start(){
  64.         list.add("~~~~~~~start~~~~~~~" + "\r\n");
  65.         System.out.println("Initial value is 0.0. Would you like to change this?\n1) Yes\n2) No");
  66.         int opt = in.nextInt();
  67.         if(opt == 1){
  68.             System.out.println("Please enter a number.");
  69.             currentValue = in.nextDouble();
  70.             list.add("Starting value was changed to " + currentValue);
  71.         }
  72.         if(opt == 2){
  73.             list.add("Starting value set to " + currentValue);
  74.         }
  75.         if(opt != 2 && opt != 1){
  76.             System.out.println("Please choose a valid option.");
  77.             start();
  78.         }
  79.     }
  80.    
  81.     public CalcOpps(){
  82.         currentValue = 0.0;
  83.     }
  84.    
  85.     public static int getMenuOption() throws InputMismatchException{
  86.         int r;
  87.         System.out.println("1) Addition\n2) Subtraction\n3) Multiplication\n4) Division\n5) clear\n6) Save\n7) Exit");
  88.         try{
  89.             r = -1;
  90.             while(r < 0 ||r > 7){
  91.                 r = in.nextInt();
  92.                 if((r < 0 ||r > 7)){
  93.                     System.out.println("Invalid Input.");
  94.                 }
  95.             }
  96.             return r;
  97.         }catch(InputMismatchException e){
  98.             System.out.println(e + ": Invalid Input");
  99.         }
  100.         return -1;
  101.     }
  102.    
  103.     public static double getOperand(String prompt){
  104.         System.out.println(prompt);
  105.         double r = in.nextDouble();
  106.         return r;
  107.     }
  108.    
  109.     public void add(double a){
  110.         list.add(currentValue + " + " + a + " = " + (currentValue + a) + "\r\n");
  111.         currentValue += a;
  112.     }
  113.    
  114.     public void subtract(double a){
  115.         list.add(currentValue + " - " + a + " = " + (currentValue - a) + "\r\n");
  116.         currentValue -= a;
  117.     }
  118.    
  119.     public void multiply(double a){
  120.         list.add(currentValue + " * " + a + " = " + (currentValue * a) + "\r\n");
  121.         currentValue = currentValue * a;
  122.     }
  123.    
  124.     public void divide(double a){
  125.         if(a == 0){
  126.             System.out.println("Cannot divide by 0. Please clear, and try again.");
  127.             currentValue = Double.NaN;
  128.         }
  129.         list.add(currentValue + " / " + a + " = " + (currentValue / a) + "\r\n");
  130.         currentValue = currentValue / a;
  131.     }
  132.    
  133.     public double getCurrentValue(){
  134.         return currentValue;
  135.     }
  136.    
  137.     public void clear(){
  138.         currentValue = 0.0;
  139.     }
  140.     public void save(){
  141.         list.add("~~~~~~~~end~~~~~~~~");
  142.         saver.setCurrentDirectory(new File("C:/Users/gdog2_000/Desktop"));
  143.         int test = saver.showSaveDialog(null);
  144.         if(test == JFileChooser.APPROVE_OPTION){
  145.             try(FileWriter fw = new FileWriter(saver.getSelectedFile() + ".txt")){
  146.                 for(int i = 0; i < list.size(); i++){
  147.                     fw.write(list.get(i));
  148.                 }
  149.             fw.close();
  150.             }catch(IOException ex){
  151.                
  152.             }
  153.         }
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement