Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sample;
- import javafx.fxml.FXML;
- import javafx.fxml.Initializable;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.control.TextField;
- import jdk.internal.util.xml.impl.Input;
- import java.io.*;
- import java.net.URL ;
- import java.util.ResourceBundle;
- import java.util.Stack;
- import java.util.Vector;
- public class Controller implements Initializable {
- @FXML
- public Button one ;
- public Button two ;
- public Button three ;
- public Button four ;
- public Button five ;
- public Button six ;
- public Button seven ;
- public Button eight ;
- public Button nine ;
- public Button zero ;
- public Button point ;
- public Button clear ;
- public Button delete ;
- public Button next ;
- public Button prev ;
- public Button save ;
- public Button load ;
- public Button add ;
- public Button minus ;
- public Button divided ;
- public Button multi ;
- public Button equal ;
- public Button negative ;
- public TextField input ;
- public Label prom ;
- private double fNumber ;
- private double sNumber ;
- private String result ;
- private String operation , n , m,temp ;
- private Vector<String> equation ;
- private Stack previous = new Stack();
- private Stack nextt = new Stack();
- private Stack<String> opP = new Stack();
- private Stack<String> opN = new Stack();
- private boolean flag = false ;
- private boolean flag1 = false ;
- @Override
- public void initialize (URL arg0 , ResourceBundle arg1){
- }
- public void input(String s ){
- temp = s ;
- equation.add(s);
- prom.setText(s);
- }
- public String getResult() {
- for (int i = 1; i < temp.length(); i++) {
- if (temp.charAt(i) == '+' || temp.charAt(i) == '-' || temp.charAt(i) == 'x' || temp.charAt(i) == '/') {
- operation = String.valueOf(temp.charAt(i));
- }
- }
- for (int i = 0; i < temp.length(); i++) {
- if (temp.charAt(i) == operation.charAt(0)) {
- flag = true;
- }
- if (!flag && temp.charAt(i) != operation.charAt(0)) {
- m = m + temp.charAt(i);
- this.fNumber = Double.valueOf(m);
- } else if (flag && temp.charAt(i) != operation.charAt(0)) {
- n = n + temp.charAt(i);
- this.sNumber = Double.valueOf(n);
- }
- }
- switch (operation) {
- case "+":
- Double system = this.fNumber + this.sNumber;
- previous.push(system);
- input.setText(String.valueOf(system));
- result = String.valueOf(system);
- String oldProm = prom.getText();
- prom.setText(String.valueOf(this.fNumber) + operation + String.valueOf(this.sNumber));
- opP.push(String.valueOf(this.fNumber) + operation + String.valueOf(this.sNumber));
- operation = "=";
- break;
- case "-":
- Double systemMinus = this.fNumber - this.sNumber;
- previous.push(systemMinus);
- input.setText(String.valueOf(systemMinus));
- result = String.valueOf(systemMinus);
- String oldPromMinus = prom.getText();
- prom.setText(String.valueOf(this.fNumber) + operation + String.valueOf(this.sNumber));
- opP.push(String.valueOf(this.fNumber) + operation + String.valueOf(this.sNumber));
- operation = "=";
- break;
- case "x":
- Double systemMulti = this.fNumber * this.sNumber;
- previous.push(systemMulti);
- input.setText(String.valueOf(systemMulti));
- result = String.valueOf(systemMulti) ;
- String oldPromMulti = prom.getText();
- prom.setText(String.valueOf(this.fNumber) + operation + String.valueOf(this.sNumber));
- opP.push(String.valueOf(this.fNumber) + operation + String.valueOf(this.sNumber));
- operation = "=";
- break;
- case "/":
- if (this.sNumber == 0.0) {
- input.setText("error");
- } else {
- Double systemDevided = this.fNumber / this.sNumber;
- previous.push(systemDevided);
- input.setText(String.valueOf(systemDevided));
- result = String.valueOf(systemDevided);
- String oldPromDevided = prom.getText();
- prom.setText(String.valueOf(this.fNumber) + operation + String.valueOf(this.sNumber));
- opP.push(String.valueOf(this.fNumber) + operation + String.valueOf(this.sNumber));
- operation = "=";
- }
- break;
- }
- return result;
- }
- public String Current(){
- if(!opP.isEmpty()){
- return opP.peek();
- }else{
- return null ;
- }
- }
- public String prev(){
- if(!opP.isEmpty()){
- opN.push(opP.peek());
- return opP.pop();
- }else{
- return null ;
- }
- }
- public String next(){
- if(!opN.isEmpty()){
- opP.push(opP.peek());
- return opN.pop();
- }else{
- return null ;
- }
- }
- public void save(){
- try{
- ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("history.file"));
- out.writeObject(equation);
- out.writeObject(previous);
- out.flush();
- out.close();
- }catch (Exception a){
- throw new RuntimeException("save failed");
- }
- }
- public void load(){
- try{
- ObjectInputStream in=new ObjectInputStream(new FileInputStream("history.file"));
- equation=(Vector<String>) in.readObject();
- previous = (Stack<String>) in.readObject();
- prom.setText(equation.lastElement());
- input.setText(String.valueOf(previous.peek()));
- }catch (Exception a){
- throw new RuntimeException("load failed");
- }
- }
- public void point_Click(){
- if(operation=="="&&!flag1){
- clear_Click();
- }
- flag1 = false ;
- String oldValue = input.getText();
- String set = String.valueOf('.');
- input.setText(oldValue+set);
- }
- public void zero_Click(){
- if(operation=="="){
- clear_Click();
- }
- String oldValue = input.getText();
- String set = String.valueOf("0");
- input.setText(oldValue+set);
- }
- public void one_Click(){
- if(operation=="="){
- clear_Click();
- }
- String oldValue = input.getText();
- String set = String.valueOf("1");
- input.setText(oldValue+set);
- }
- public void two_Click(){
- if(operation=="="){
- clear_Click();
- }
- String oldValue = input.getText();
- String set = String.valueOf("2");
- input.setText(oldValue+set);
- }
- public void three_Click(){
- if(operation=="="){
- clear_Click();
- }
- String oldValue = input.getText();
- String set = String.valueOf("3");
- input.setText(oldValue+set);
- }
- public void four_Click(){
- if(operation=="="){
- clear_Click();
- }
- String oldValue = input.getText();
- String set = String.valueOf("4");
- input.setText(oldValue+set);
- }
- public void five_Click(){
- if(operation=="="){
- clear_Click();
- }
- String oldValue = input.getText();
- String set = String.valueOf("5");
- input.setText(oldValue+set);
- }
- public void six_Click(){
- if(operation=="="){
- clear_Click();
- }
- String oldValue = input.getText();
- String set = String.valueOf("6");
- input.setText(oldValue+set);
- }
- public void seven_Click() {
- if (operation == "=") {
- clear_Click();
- }
- String oldValue = input.getText();
- String set = String.valueOf("7");
- input.setText(oldValue + set);
- }
- public void eight_Click(){
- if(operation=="="){
- clear_Click();
- }
- String oldValue = input.getText();
- String set = String.valueOf("8");
- input.setText(oldValue+set);
- }
- public void nine_Click(){
- if(operation=="="){
- clear_Click();
- }
- String oldValue = input.getText();
- String set = String.valueOf("9");
- input.setText(oldValue+set);
- }
- public void clear_Click(){
- input.setText("");
- prom.setText("");
- this.fNumber = 0 ;
- this.sNumber = 0 ;
- }
- public void multi_Click(){
- String value = input.getText();
- Double valueNumber = Double.valueOf(value);
- this.fNumber = valueNumber ;
- input.setText("");
- prom.setText(value + "x");
- operation = "x" ;
- }
- public void add_Click(){
- String value = input.getText();
- Double valueNumber = Double.valueOf(value);
- this.fNumber = valueNumber ;
- input.setText("");
- prom.setText(value + "+");
- operation = "+" ;
- }
- public void minus_Click(){
- String value = input.getText();
- Double valueNumber = Double.valueOf(value);
- this.fNumber = valueNumber ;
- input.setText("");
- prom.setText(value + "-");
- operation = "-" ;
- }
- public void divided_Click(){
- String value = input.getText();
- Double valueNumber = Double.valueOf(value);
- this.fNumber = valueNumber ;
- input.setText("");
- prom.setText(value + "/");
- operation = "/" ;
- }
- public void equal_Click(){
- switch (operation){
- case "+" :
- String value = input.getText();
- this.sNumber = Double.valueOf(value);
- Double system = this.fNumber + this.sNumber ;
- previous.push(system);
- input.setText(String.valueOf(system));
- String oldProm = prom.getText();
- prom.setText(oldProm + value);
- opP.push(oldProm + value);
- operation = "=";
- flag1 = true ;
- break;
- case "-" :
- String valueMinus = input.getText();
- this.sNumber = Double.valueOf(valueMinus);
- Double systemMinus = this.fNumber - this.sNumber ;
- previous.push(systemMinus);
- input.setText(String.valueOf(systemMinus));
- String oldPromMinus = prom.getText();
- prom.setText(oldPromMinus + valueMinus);
- opP.push(oldPromMinus + valueMinus);
- operation = "=";
- flag1 = true ;
- break;
- case "x" :
- String valueMulti = input.getText();
- this.sNumber = Double.valueOf(valueMulti);
- Double systemMulti = this.fNumber * this.sNumber ;
- previous.push(systemMulti);
- input.setText(String.valueOf(systemMulti));
- String oldPromMulti = prom.getText();
- prom.setText(oldPromMulti + valueMulti);
- opP.push(oldPromMulti + valueMulti);
- operation = "=";
- flag1 = true ;
- break;
- case "/" :
- String valueDevided = input.getText();
- this.sNumber = Double.valueOf(valueDevided);
- if (this.sNumber == 0.0){
- input.setText("error");
- }else {
- Double systemDevided = this.fNumber / this.sNumber;
- previous.push(systemDevided);
- input.setText(String.valueOf(systemDevided));
- String oldPromDevided = prom.getText();
- prom.setText(oldPromDevided + valueDevided);
- opP.push(oldPromDevided + valueDevided);
- operation = "=";
- flag1 = true ;
- }
- break;
- }
- }
- public void save_Click() {
- try{
- ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("history.file"));
- out.writeObject(opP);
- out.writeObject(previous);
- out.flush();
- out.close();
- System.out.println("saved");
- }catch (Exception a){
- throw new RuntimeException("save failed");
- }
- }
- public void load_Click(){
- try{
- ObjectInputStream in=new ObjectInputStream(new FileInputStream("history.file"));
- opP=(Stack<String>) in.readObject();
- previous = (Stack<String>) in.readObject();
- prom.setText(opP.peek());
- input.setText(String.valueOf(previous.peek()));
- }catch (Exception a){
- throw new RuntimeException("load failed");
- }
- }
- public void next_Click(){
- if(!nextt.isEmpty()) {
- opP.push(opN.peek());
- prom.setText(String.valueOf(opN.pop()));
- previous.push(nextt.peek());
- input.setText(String.valueOf(nextt.pop()));
- }
- }
- public void prev_Click(){
- if(!previous.isEmpty()) {
- opN.push(opP.peek());
- prom.setText(String.valueOf(opP.pop()));
- nextt.push(previous.peek());
- input.setText(String.valueOf(previous.pop()));
- }
- }
- public void negative_Click(){
- String value = input.getText();
- Double valueNumber = Double.valueOf(value);
- valueNumber = -1*valueNumber ;
- value = String.valueOf(valueNumber);
- input.setText( value);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement