How to save and open file order Java GUI -
so problem is last buttonpanel. don't know how save , open file gui, can total yea appreciated. "the user should able click button labeled total displays total purchase price in label (the total should include michigan 6% sales tax). button allows user save order file. yet button allows user open order file."
for saving "additionally, before save committed, program should ask user if sure want save file. should able indicate if want save or not pop dialog. if user indicates not want save file, order not saved. if indicate want save it, file created (or overwritten) new order. name of order file order.txt."
for opening "your program should able open file saving it. when program opens order, gui components should set appropriate values reflect order in file. if order.txt file not yet exist (i.e., order has never been saved), dialog box displayed telling user "no saved order available", , main gui remains unchanged."
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.text.decimalformat; public class icecreamordersgui extends jframe { private icecreamflavorpanel icecreamflavors; // icecreamflavor panel private nutspanel nuts; //nuts panel private syruppanel syrup; //syrup panel private scoopspanel scoops; //scoops panel private jpanel buttonpanel; //to hold buttons private jbutton savebutton; // save order private jbutton openbutton; // open order private jbutton totalbutton; //to figure out total private final double tax_rate = 0.06; // sales tax rate /** constructor */ public icecreamordersgui() { //display title. settitle("icecream orders"); //specify action close button. setdefaultcloseoperation(jframe.exit_on_close); //create borderlayout manager. setlayout(new borderlayout()); // create custom panels. icecreamflavors = new icecreamflavorpanel(); scoops = new scoopspanel(); nuts = new nutspanel(); syrup = new syruppanel(); // create button panel. buildbuttonpanel(); //add components content pane. add(icecreamflavors, borderlayout.west); add(scoops, borderlayout.center); add(nuts, borderlayout.north); add(syrup, borderlayout.east); add(buttonpanel, borderlayout.south); //pack contents of window , display it. pack(); setvisible(true); } private void buildbuttonpanel() { //create panel buttons. buttonpanel = new jpanel(); //create buttons. savebutton = new jbutton("save order"); openbutton = new jbutton("open order"); totalbutton = new jbutton("total"); //register action listeners. savebutton.addactionlistener(new savebuttonlistener()); openbutton.addactionlistener(new openbuttonlistener()); totalbutton.addactionlistener(new totalbuttonlistener()); //add buttons button panel. buttonpanel.add(savebutton); buttonpanel.add(openbutton); buttonpanel.add(totalbutton); } private class totalbuttonlistener implements actionlistener { public void actionperformed(actionevent e) { //variables hold subtotal, tax, , total double subtotal, tax, total; //calculate subtotal. subtotal = icecreamflavor.geticecreamflavorcost() + nuts.getnutscost() + syrup.getsyrupcost() + scoops.getscoopscost(); // calculate sales tax tax = subtotal * tax_rate; // calcuate total total = subtotal + tax; //create decimalformat object format output. decimalformat dollar = new decimalformat("0.00"); //display charges. joptionpane.showmessagedialog(null, "subtotal: $" + dollar.format(subtotal) + "\n" + "tax: $" + dollar.format(tax) + "\n" + "total: $" + dollar.format(total)); } } /** private inner class handles event when user clicks exit button. */ private class exitbuttonlistener implements actionlistener { public void actionperformed(actionevent e) { system.exit(0); } } /** main method */ public static void main(string[] args) { new ordercalculatorgui(); } }
use jfilechooser open/save file. see usage
Comments
Post a Comment