java - Program does not save File -


i not putting classes here here is:

checkoptionpanel

package main;  import java.io.*; import javax.swing.*; import java.awt.event.*; import java.awt.*; import javax.swing.joptionpane; import java.text.decimalformat;  public class checkoptionspanel extends jframel implements serializable {      public static checkingaccount account; //    static void choosefile(int i) { //        throw new unsupportedoperationexception("not supported yet."); //to change body of generated methods, choose tools | templates. //    }     private jpanel panel;     private jlabel title;     private jradiobutton entertrans;     private jradiobutton listtrans;     private jradiobutton listchecks;     private jradiobutton listdep;     private jradiobutton readfile;     private jradiobutton writefile;     private buttongroup radiobuttongroup;  // group radio buttons     //     private static string filepath;     public static boolean changeinacc;     //     private int transcode, checknumber;     private double transamount, chargeamount;     private string transcodestr, transamountstr, message, checknumberstr, servicecharge = "svc.chrg.";     private string transtype;//check or deposit     private int chargednum = 0; //number of times charged having balance below $500     private double finalbalance;     /**      * ********************************************************      */     private final int window_width = 400;     private final int window_height = 150;      //constructor     public checkoptionspanel(checkingaccount acc) {          account = acc;         settitle("checking account actions");         setsize(window_width, window_height);         //setdefaultcloseoperation(jframe.exit_on_close);         buildpanel();         //adding panel frame's content pane         add(panel);         //display         //setbackground(color.green);         setvisible(true);      }      private void buildpanel() {         entertrans = new jradiobutton("enter transaction");         entertrans.setbackground(color.green);         listtrans = new jradiobutton("list transactions");         listtrans.setbackground(color.green);         listchecks = new jradiobutton("list checks");         listchecks.setbackground(color.green);         listdep = new jradiobutton("list deposits");         listdep.setbackground(color.green);         readfile = new jradiobutton("read file");         readfile.setbackground(color.green);         writefile = new jradiobutton("write file");         writefile.setbackground(color.green);          title = new jlabel("choose action:\n");         title.setfont(new font("helvetica", font.bold, 24));         setbackground(color.green);          //group radiobuttons         radiobuttongroup = new buttongroup();         radiobuttongroup.add(entertrans);         radiobuttongroup.add(listtrans);         radiobuttongroup.add(listchecks);         radiobuttongroup.add(listdep);         radiobuttongroup.add(readfile);         radiobuttongroup.add(writefile);           //adding listeners radio buttons         entertrans.addactionlistener(new radiobuttonlistener());         listtrans.addactionlistener(new radiobuttonlistener());         listchecks.addactionlistener(new radiobuttonlistener());         listdep.addactionlistener(new radiobuttonlistener());         readfile.addactionlistener(new radiobuttonlistener());         writefile.addactionlistener(new radiobuttonlistener());           //create panel         panel = new jpanel();         panel.add(title);         panel.add(entertrans);         panel.add(listtrans);         panel.add(listchecks);         panel.add(listdep);         panel.add(readfile);         panel.add(writefile);  //setbackground(color.green);      }      //raidibuttonlistener                   private class radiobuttonlistener implements actionlistener {          decimalformat formatter = new decimalformat("0.00");          public void actionperformed(actionevent e) {              if (e.getsource() == entertrans) {                 boolean transexception;                 {                     transexception = false;                     try {                         {                              transcodestr = joptionpane.showinputdialog("enter trans code:");                             transcode = integer.parseint(transcodestr);                             if (transcode != 0 && transcode != 1 && transcode != 2) {                                 string message = "invalid input!\nplease enter 1 check, 2 deposit or 0 exit "                                         + "please try again:";                                 joptionpane.showmessagedialog(null, message);                             }                         } while (transcode != 0 && transcode != 1 && transcode != 2);                     } catch (numberformatexception ep) {                          joptionpane.showmessagedialog(null, "wrong input format!\ntry again:");                         transexception = true;                     }                  } while (transexception == true);                 if (transcode == 1) {                     boolean checkexception;                     {                         checkexception = false;                         try {                             checknumberstr = joptionpane.showinputdialog("enter check number:");                             checknumber = integer.parseint(checknumberstr);                         } catch (numberformatexception nfe) {                             joptionpane.showmessagedialog(null, "wrong input format!\ntry again:");                             checkexception = true;                         }                      } while (checkexception == true);                 }                  if (transcode != 0) {                     boolean amtexception;                     {                         amtexception = false;                         try {                             {                                 transamountstr = joptionpane.showinputdialog("enter trans amt:");                                 transcode = integer.parseint(transcodestr);                                 transamount = double.parsedouble(transamountstr);                                 if (transamount < 0) {                                     string message = "invalid input!\ntransaction amount must positive number.\n"                                             + "please try again:";                                     joptionpane.showmessagedialog(null, message);                                 }                             } while (transamount < 0);                         } catch (numberformatexception ae) {                             joptionpane.showmessagedialog(null, "wrong input format!\ntry again:");                             amtexception = true;                         }                      } while (amtexception == true);                     account.setbalance(transamount, transcode);                     account.setservicecharge(transamount, transcode);                      if (transcode == 1) {                         transtype = "check";                         chargeamount = 0.15;                         //create transaction abject check                         transaction checktrans = new check(checknumber, account.gettranscount(), transamount, transtype);                         account.addtrans(checktrans);                         //increment number of transactions                         account.settranscount();                         // create transaction object service charge                         account.addtrans(account.gettranscount(), chargeamount, servicecharge);                         account.settranscount();                     } else {                         transtype = "deposit";                         chargeamount = 0.10;                         account.addtrans(account.gettranscount(), transamount, transtype);                         account.settranscount();                         account.addtrans(account.gettranscount(), chargeamount, servicecharge);                         account.settranscount();                     }                       message = account.getname() + "'s account\n" + "transaction: ";                     if ("check".equals(transtype)) {                         message += transtype + " #" + checknumber + " in amount of $";                     } else {                         message += transtype + " in amount of $";                     }                     message += formatter.format(transamount) + " \ncurrnet balance: $"                             + formatter.format(account.getbalance())                             + " \nservice charge: " + transtype + " --- charge $"                             + formatter.format(chargeamount);                      if (account.warningmessage()) {                         message += " \nwarning: balance below $50";                     }                     if (account.negativebalance()) {                         message += " \nservice charge: below $0 --- charge: $10.00";                         account.addtrans(account.gettranscount(), 10, servicecharge);                         account.settranscount();                     }                     if (account.belowbalancecheck() && account.getbelow500charge() && chargednum == 0) {                         message += " \nservice charge: below $500 --- charge: $5.00";                         chargednum++;                         account.addtrans(account.gettranscount(), 5, servicecharge);                         account.settranscount();                     }                     message += " \ntotal service charge: $" + formatter.format(account.getservicecharge());                     joptionpane.showmessagedialog(null, message);                 } else {                      finalbalance = account.getbalance() - account.getservicecharge();                     message = account.getname() + "'s account\n" + "transaction : end" + " \ncurrnet balance:  ($"                             + formatter.format(account.getbalance()) + ")" + " \ntotal service charge:  $"                             + formatter.format(account.getservicecharge())                             + "\nfinal balance:  ($" + finalbalance + ")";                     joptionpane.showmessagedialog(null, message);                 }                 changeinacc = true;                 /*endof entertrans*/            } else if (e.getsource() == listtrans) {                  account.getalltransreport();              } else if (e.getsource() == listchecks) {                 account.checksort();             } else if (e.getsource() == listdep) {                 account.depsort();             } else if (e.getsource() == writefile) {                 choosefile(2);                 try {                      objectoutputstream out = new objectoutputstream(new fileoutputstream(filepath));                     out.writeobject(account);                     out.close();                 } catch (ioexception exc) {                     exc.getmessage();                 }             } else {                 choosefile(1);                 try {                     objectinputstream in = new objectinputstream(new fileinputstream(filepath));                     account = (checkingaccount) in.readobject();                      in.close();                  } catch (classnotfoundexception ex) {                     joptionpane.showmessagedialog(null, "class not found in file", "warning", joptionpane.warning_message);                 } catch (classcastexception ex) {                     joptionpane.showmessagedialog(null, "unable cast file", "warning", joptionpane.warning_message);                 } catch (invalidclassexception ex) {                     joptionpane.showmessagedialog(null, "invalid class", "warning", joptionpane.warning_message);                 } catch (eofexception ex) {                     joptionpane.showmessagedialog(null, "file empty", "warning", joptionpane.warning_message);                 } catch (streamcorruptedexception ex) {                     joptionpane.showmessagedialog(null, "file corrupted", "warning", joptionpane.warning_message);                 } catch (filenotfoundexception ex) {                     joptionpane.showmessagedialog(null, "file not found", "warning", joptionpane.warning_message);                 } catch (ioexception ex) {                     joptionpane.showmessagedialog(null, e);                 }             }          }     }      public static void choosefile(int iooption) {         int status;         jfilechooser chooser = new jfilechooser();         if (iooption == 1) {             status = chooser.showopendialog(null);         } else {             status = chooser.showsavedialog(null);         }          if (status == jfilechooser.approve_option) {             file file = chooser.getselectedfile();             filepath = file.getpath();         }         changeinacc = false;     } } 

jframel

which controls exit button.

package main; import javax.swing.*; import java.awt.event.*; public class jframel extends jframe {     /** creates new instance of jframel */     public jframel() {         //super(title);         jframel.framelistener listener = new jframel.framelistener();         addwindowlistener(listener);     }    private class framelistener extends windowadapter    {     public void windowclosing(windowevent e) {             //this seen on standard output.        //system.out.println("windowlistener method called:");        if(checkoptionspanel.changeinacc ==true){        checkoptionspanel.choosefile(2);        }           setvisible(false);           system.exit(0);     }    }    } 

when hit exit opens saving window, type in name of file being saved , seems work okay file not saved in computer.

the thing that, choosefile() method works fine when i'm manually saving file, meaning hit 'write file' button... when hitting exit button wont save file.

any ideas why not saving?

your choosefile no i/o. allows user select , file , nothing...

public static void choosefile(int iooption) {     int status;     jfilechooser chooser = new jfilechooser();     if (iooption == 1) {         status = chooser.showopendialog(null);     } else {         status = chooser.showsavedialog(null);     }      if (status == jfilechooser.approve_option) {         file file = chooser.getselectedfile();         filepath = file.getpath();     }     changeinacc = false; } 

as compared "write" option...

choosefile(2); try {     objectoutputstream out = new objectoutputstream(new fileoutputstream(filepath));     out.writeobject(account);     out.close(); } catch (ioexception exc) {     exc.getmessage(); } 

a word of warning. relying static variables in way going blow face. choosefile method should returning file selected.

you should create read , write method performs these actions , allows gain access them (from instance of class) needed...

i fail see how magically imagined calling choosefile method going save anything...

addition

thank andrew...

your exception handling leaves little desired. points @ least making effort. if want display error message, should using exception#printstacktrace automatically dump stack trace standard out, making life lot simpler...

choosefile(2); try {     objectoutputstream out = new objectoutputstream(new fileoutputstream(filepath));     out.writeobject(account);     out.close(); } catch (ioexception exc) {     //exc.getmessage(); <-- useless     exc.printstacktrace(); } 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -