Java Calculator History Feature -


i hoping me feature store calculator's history string , have show history when user hits h key. i'm looking record each button pressed, including operators. appreciated. thanks.

public class gui extends jpanel implements keylistener {   private jbutton b1; private jbutton b2; private jbutton b3; private jbutton b4; private jbutton b5; private jbutton b6; private jbutton b7; private jbutton b8; private jbutton b9; private jbutton b0; private jbutton add; private jbutton subtract; private jbutton multiply; private jbutton divide; private jbutton solve; private jbutton clear; private jbutton decimal; private jbutton sqrt; private jbutton recip; private jbutton backspace; private jbutton ms; private jbutton mr; private jbutton mc; private jbutton percent; private jbutton love; private jtextfield jtf; string display = ""; string mstore; string history; boolean multiplybool = false; boolean dividebool = false; boolean addbool = false; boolean subtractbool = false; boolean percentbool = false; imageicon heart = new imageicon("heartt.png");  private calclogic cl = new calclogic();   public gui(){ setlayout(new borderlayout());  jpanel p3 = new jpanel(new borderlayout()); p3.setfont(new font("arial", font.bold, 30)); p3.add(jtf = new jtextfield("0",12)); jtf.seteditable(false); jtf.sethorizontalalignment(jtextfield.right); jtf.setfont(new font("arial", font.bold, 40));     jpanel p2 = new jpanel(); p2.setbackground(color.cyan);  p2.setlayout(new gridlayout(5,5,2,2));   //row 1 buttons   p2.add(mc = new jbutton("mc")); mc.setfont(new font("arial", font.bold, 12));  p2.add(mr = new jbutton("mr")); mr.setfont(new font("arial", font.bold, 12));  p2.add(ms = new jbutton("ms")); ms.setfont(new font("arial", font.bold, 12));  p2.add(divide = new jbutton("\u00f7")); divide.setfont(new font("arial", font.plain, 20));  p2.add(backspace = new jbutton("\u2190")); backspace.setfont(new font("arial bold", font.bold, 18));   //row 2 buttons   p2.add(b7 = new jbutton("7")); b7.setfont(new font("arial", font.plain, 20));  p2.add(b8 = new jbutton("8")); b8.setfont(new font("arial", font.plain, 20));  p2.add(b9 = new jbutton("9")); b9.setfont(new font("arial", font.plain, 20));  p2.add(multiply = new jbutton("\u00d7")); multiply.setfont(new font("arial", font.plain, 20));  p2.add(sqrt = new jbutton("\u221a")); sqrt.setfont(new font("arial", font.plain, 20));   //row 3 buttons   p2.add(b4 = new jbutton("4")); b4.setfont(new font("arial", font.plain, 20));  p2.add(b5 = new jbutton("5")); b5.setfont(new font("arial", font.plain, 20));  p2.add(b6 = new jbutton("6")); b6.setfont(new font("arial", font.plain, 20));  p2.add(subtract = new jbutton("\u2212")); subtract.setfont(new font("arial", font.plain, 20));  p2.add(recip = new jbutton("1/x")); recip.setfont(new font("arial", font.plain, 14));   //row 4 buttons   p2.add(b1 = new jbutton("1")); b1.setfont(new font("arial", font.plain, 20));  p2.add(b2 = new jbutton("2")); b2.setfont(new font("arial", font.plain, 20));  p2.add(b3 = new jbutton("3")); b3.setfont(new font("arial", font.plain, 20));  p2.add(add = new jbutton("+")); add.setfont(new font("arial", font.plain, 20));  p2.add(percent = new jbutton("%")); percent.setfont(new font("arial", font.plain, 20));   //row 5 buttons   p2.add(b0 = new jbutton("0")); b0.setfont(new font("arial", font.plain, 20));  p2.add(decimal = new jbutton(".")); decimal.setfont(new font("arial", font.plain, 20));  p2.add(clear = new jbutton("c")); clear.setfont(new font("arial", font.plain, 20));  p2.add(solve = new jbutton("=")); solve.setfont(new font("arial", font.plain, 20));  p2.add(love = new jbutton(heart)); love.addactionlistener(new lovelistener());  add(p3, borderlayout.north); add(p2, borderlayout.center); this.setfocusable(true); addkeylistener(this);  actionlistener buttonlistener = new buttonlistener();  actionlistener clearlistener = new clearlistener();    b7.addactionlistener(buttonlistener); b8.addactionlistener(buttonlistener); b9.addactionlistener(buttonlistener); add.addactionlistener(new addlistener()); b4.addactionlistener(buttonlistener); b5.addactionlistener(buttonlistener); b6.addactionlistener(buttonlistener); subtract.addactionlistener(new subtractlistener()); b1.addactionlistener(buttonlistener); b2.addactionlistener(buttonlistener); b3.addactionlistener(buttonlistener); multiply.addactionlistener(new multiplylistener()); b0.addactionlistener(buttonlistener); decimal.addactionlistener(buttonlistener); clear.addactionlistener(clearlistener); solve.addactionlistener(new solvelistener()); divide.addactionlistener(new dividelistener()); sqrt.addactionlistener(new sqrtlistener()); recip.addactionlistener(new reciplistener()); backspace.addactionlistener(new backspacelistener()); ms.addactionlistener(new mslistener()); mr.addactionlistener(new mrlistener()); mc.addactionlistener(new mclistener()); percent.addactionlistener(new percentlistener());  } //gui()      private void action_clear(){  jtf.settext(""); display = jtf.gettext(); //op = ""; cl.settotal("0"); multiplybool = false; addbool = false; subtractbool = false; dividebool = false;      }         public class buttonlistener implements actionlistener     {    public void actionperformed(actionevent e) {            string digit = e.getactioncommand();       jtf.settext(display + digit );     display = jtf.gettext();     playsound("click.wav"); }     }//buttonlistener      public class multiplylistener implements actionlistener     { private calclogic cl = new calclogic();  public void actionperformed(actionevent e) {        if(multiplybool == true)     {            if (percentbool==true)         {             cl.percentage(display);             display = cl.getpercent();         }         cl.multiply(display);         jtf.settext(cl.getstring());         display = "";     }else if(dividebool == true)     {         if (percentbool==true)         {             cl.percentage(display);             display = cl.getpercent();         }         cl.divide(display);         jtf.settext(cl.getstring());         display = "";     }else if(addbool == true)     {         if (percentbool==true)         {             cl.percentage(display);             display = cl.getpercent();             cl.percmult(display);             display = cl.getpercent();          }          cl.add(display);          jtf.settext(cl.getstring());         display = "";     }else if (subtractbool == true)     {         if (percentbool==true)         {             cl.percentage(display);             display = cl.getpercent();             cl.percmult(display);             display = cl.getpercent();         }         cl.subtract(display);         jtf.settext(cl.getstring());         display = "";     }else      {     cl.settotal(display);     jtf.settext("");     multiplybool = true;     dividebool = false;     addbool = false;     subtractbool = false;     percentbool = false;     display = "";     }      multiplybool = true;     dividebool = false;     addbool = false;     subtractbool = false;     percentbool = false; }      }      public class dividelistener implements actionlistener    { public void actionperformed(actionevent e) {     if(multiplybool == true)     {            if (percentbool==true)         {             cl.percentage(display);             display = cl.getpercent();         }         cl.multiply(display);         jtf.settext(cl.getstring());         display ="";     }else if(dividebool == true)     {         if (percentbool==true)         {             cl.percentage(display);             display = cl.getpercent();         }         cl.divide(display);         jtf.settext(cl.getstring());         display = "";     }else if(addbool == true)     {         if (percentbool==true)         {             cl.percentage(display);             display = cl.getpercent();             cl.percmult(display);             display = cl.getpercent();          }          cl.add(display);          jtf.settext(cl.getstring());         display = "";     }else if (subtractbool == true)     {         if (percentbool==true)         {             cl.percentage(display);             display = cl.getpercent();             cl.percmult(display);             display = cl.getpercent();         }         cl.subtract(display);         jtf.settext(cl.getstring());         display = "";     }else     {     cl.settotal(display);     jtf.settext("");     dividebool = true;     multiplybool = false;     addbool = false;     subtractbool = false;     percentbool = false;     display = "";     }      multiplybool = false;     dividebool = true;     addbool = false;     subtractbool = false;     percentbool = false; }     }     public class addlistener implements actionlistener     { public void actionperformed(actionevent e) {     if(multiplybool == true)     {            if (percentbool==true)         {             cl.percentage(display);             display = cl.getpercent();         }         cl.multiply(display);         jtf.settext(cl.getstring());         display ="";     }else if(dividebool == true)     {         if (percentbool==true)         {             cl.percentage(display);             display = cl.getpercent();         }         cl.divide(display);         jtf.settext(cl.getstring());         display = "";     }else if(addbool == true)     {         if (percentbool==true)         {             cl.percentage(display);             display = cl.getpercent();             cl.percmult(display);             display = cl.getpercent();          }          cl.add(display);          jtf.settext(cl.getstring());         display = "";     }else if (subtractbool == true)     {         if (percentbool==true)         {             cl.percentage(display);             display = cl.getpercent();             cl.percmult(display);             display = cl.getpercent();         }         cl.subtract(display);         jtf.settext(cl.getstring());         display = "";     }else     {     cl.settotal(display);     jtf.settext("");     dividebool = false;     multiplybool = false;     addbool = true;     subtractbool = false;     percentbool = false;     display = "";     }      dividebool = false;     multiplybool = false;     subtractbool = false;     addbool = true;     percentbool = false;  }     }//addlistener      public class subtractlistener implements actionlistener     { public void actionperformed(actionevent e) {     if(multiplybool == true)     {            if (percentbool==true)         {             cl.percentage(display);             display = cl.getpercent();         }         cl.multiply(display);         jtf.settext(cl.getstring());         display ="";     }else if(dividebool == true)     {         if (percentbool==true)         {             cl.percentage(display);             display = cl.getpercent();         }         cl.divide(display);         jtf.settext(cl.getstring());         display = "";     }else if(addbool == true)     {         if (percentbool==true)         {             cl.percentage(display);             display = cl.getpercent();             cl.percmult(display);             display = cl.getpercent();          }          cl.add(display);          jtf.settext(cl.getstring());         display = "";     }else if (subtractbool == true)     {         if (percentbool==true)         {             cl.percentage(display);             display = cl.getpercent();             cl.percmult(display);             display = cl.getpercent();         }         cl.subtract(display);         jtf.settext(cl.getstring());         display = "";     }else     {     cl.settotal(display);     jtf.settext("");     dividebool = false;     multiplybool = false;     addbool = false;     subtractbool = true;     percentbool = false;     display = "";     }     dividebool = false;     multiplybool = false;     addbool = false;     subtractbool = true;     percentbool = false;  }     }//subtractlistener      public class sqrtlistener implements actionlistener     { public void actionperformed(actionevent e) {     cl.settotal(display);     cl.squareroot(display);     jtf.settext(cl.getstring());     playsound("click.wav"); }     }      public class reciplistener implements actionlistener     { public void actionperformed(actionevent e) {     cl.settotal(display);     cl.reciprical(display);     jtf.settext(cl.getstring());     playsound("click.wav"); }     }      public class backspacelistener implements actionlistener     { public void actionperformed(actionevent e) {     jtf.settext("");     display = ""; }       }      public class mslistener implements actionlistener     { public void actionperformed(actionevent e) {     mstore = jtf.gettext(); }     }      public class mrlistener implements actionlistener     { public void actionperformed(actionevent e) {     jtf.settext(mstore);     display = mstore;  }     }      public class mclistener implements actionlistener     { public void actionperformed(actionevent e) {     mstore = ""; }     }      public class percentlistener implements actionlistener     { public void actionperformed(actionevent e) {      percentbool = true; }     }       public class solvelistener implements actionlistener     { public void actionperformed(actionevent e) {     playsound("that_was_easy.wav");      if(multiplybool == true)     {            if (percentbool==true)         {             cl.percentage(display);             display = cl.getpercent();         }         cl.multiply(display);         jtf.settext(cl.getstring());     }else if(dividebool == true)     {         if (percentbool==true)         {             cl.percentage(display);             display = cl.getpercent();         }         cl.divide(display);         jtf.settext(cl.getstring());     }else if(addbool == true)     {         if (percentbool==true)         {             cl.percentage(display);             display = cl.getpercent();             cl.percmult(display);             display = cl.getpercent();          }          cl.add(display);          jtf.settext(cl.getstring());     }else if (subtractbool == true)     {         if (percentbool==true)         {             cl.percentage(display);             display = cl.getpercent();             cl.percmult(display);             display = cl.getpercent();         }         cl.subtract(display);         jtf.settext(cl.getstring());     }     multiplybool = false;     addbool = false;     subtractbool = false;     dividebool = false;     percentbool = false;      //display = jtf.gettext();  }      }   public class clearlistener implements actionlistener     { public void actionperformed(actionevent e) {     action_clear();     playsound("click.wav"); }//clearlistener       } public void keytyped(keyevent e){  if(e.getkeycode() == keyevent.vk_h){     system.out.println(history); ) public void keytyped(keyevent e){  } public void keyreleased(keyevent e){     } } 

just store each single line calculator command string array before erasing it. , show when press h.

for ex:

array array = new array(5) (you use collection classes make dynamic , more efficient)

123 (line#1 in calculator) >> array[0] + (line#2 in calculator) >> array[1] 34567 (line#3 in calculator) >> array[2] % (line#4 in calculator) >> array [3] 345 (line#5 in calculator) >> array [4] 

so on , forth.

regards,


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -