Question: Java Assignment When using the menu selection save, I need it to open a JFileChooser dialog that will allow themto select where to save the

Java Assignment

When using the menu selection save, I need it to open a JFileChooser dialog that will allow themto select where to save the data and then writes all the strings from the ArrayList to that text file. The first line in the File Output should be Intital value is 0.

Here is my code. There are two classes. Memory and Calculator.

Memory Code

import java.io.FileWriter;

import java.util.ArrayList;

import java.util.Scanner;

public class Memory {

public static void main(String[] args) // Main function

{

Calculator c = new Calculator(0);

Scanner choose = new Scanner(System.in);

int choice = 0;

String prompt;

boolean right = false;

double operand2;

do // Loop until user wants to quit

{

if(c.getCurrentValue() > 0)

System.out.println("The current value is " + c.getCurrentValue()); //Displaying current value

choice = c.displayMenu(choose); //Displaying the user's menu

if ((choice <= 7) && (choice > 0)) //Checking for a valid option to be selected

{

switch(choice) //Calling the appropriate function

{

case 1: operand2 = c.getOperand("What is the second number? ");

c.add(operand2);

break;

case 2: operand2 = c.getOperand("What is the second number? ");

c.subtract(operand2);

break;

case 3: operand2 = c.getOperand("What is the second number? ");

c.multiply(operand2);

break;

case 4: operand2 = c.getOperand("What is the second number? ");

c.divide(operand2);

break;

case 5:

c.currentValue = 0;

System.out.println("The current value is 0.0");

break;

case 6: c.currentValue = 0;

System.out.println("Goodbye!");

break;

case 7: c.currentValue = 0;

c.dispaly();

break;

default:

System.out.println("Enter correct choice");

break;

}

}

else

{

right = true;

System.out.println("I'm sorry, " + choice + " wasn't one of the options. Please try again.");

}

}while (choice != 6);

}

}

Calculator Code

import java.io.FileWriter;

import java.util.ArrayList;

import java.util.Scanner;

class Calculator {

ArrayList list =new ArrayList();

public static double currentValue=0; //Variable to hold result

public Calculator(double currentValue)

{

this.currentValue = currentValue;

}

public void dispaly() {

for(String i:list)

System.out.println(i);

try{

FileWriter fw=new FileWriter("testout.txt");

for(String i:list)

fw.write(i+" ");

fw.close();

}catch(Exception e){System.out.println(e);}

System.out.println("Success... this content has return to file");

}

public static int displayMenu(Scanner choose) // Displaying the user's menu

{

int choice;

System.out.println(" ");

System.out.println("Menu ");

System.out.println("1. Add");

System.out.println("2. Subtract");

System.out.println("3. Multiply");

System.out.println("4. Divide");

System.out.println("5. Clear");

System.out.println("6. Quit");

System.out.println("7. Save");

System.out.println(" ");

System.out.print("What would you like to do? ");

choice = choose.nextInt();

return choice;

}

public static double getOperand(String prompt)

{

Scanner choose = new Scanner(System.in);

System.out.print(prompt);

double option = choose.nextDouble();

return option;

}

public double getCurrentValue()

{

return this.currentValue;

}

public void add(double operand2)

{

list.add((int)this.currentValue+" + "+(int)operand2+" = "+(int)(this.currentValue + operand2));

this.currentValue += operand2;

}

public void subtract(double operand2)

{

list.add((int)this.currentValue+" - "+(int)operand2+" = "+(int)(this.currentValue - operand2));

this.currentValue -= operand2;

}

public void multiply(double operand2)

{

list.add((int)this.currentValue+" * "+(int)operand2+" = "+(int)(this.currentValue * operand2));

this.currentValue *= operand2;

}

public void divide(double operand2)

{

if (operand2 == 0)

{

System.out.println("The current value is NaN. ");

clear();

}

else

{

list.add((int)this.currentValue+" / "+(int)operand2+" = "+(int)(this.currentValue / operand2));

this.currentValue /= operand2;

}

}

public void clear()

{

this.currentValue = 0;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!