to start, reason mathematics screwed in gui 20.61 book keeps showing total 16.43 or similar. aside i'm attempting create fields sales tax, total , subtotal shown in application...but 1 shows total. how create sales tax , subtotal sop show same total?
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.util.scanner; import java.util.vector; import java.text.decimalformat; public class javaprogram extends jframe { private final string book_data_filename = "bookdata1.txt"; private final int list_rows_visible = 5; private final string program_name = "books'r'us"; private jpanel bookpanel; private jpanel controlpanel; private jpanel cartpanel; private jpanel checkoutpanel; private jlist booklist; private jlist cartlist; private jbutton addtocartbutton; private jbutton removefromcartbutton; private jbutton clearcartbutton; private jtextfield totaltext; private double total = 0.00; private vector<book> books = new vector<book>(); private vector<book> cart = new vector<book>(); public javaprogram() throws ioexception { settitle( program_name ); setdefaultcloseoperation( jframe.exit_on_close ); setlayout( new borderlayout() ); buildbookpanel(); buildcontrolpanel(); buildcartpanel(); buildcheckoutpanel(); add( bookpanel, borderlayout.west ); add( controlpanel, borderlayout.center ); add( cartpanel, borderlayout.east ); add( checkoutpanel, borderlayout.south ); pack(); setlocationrelativeto( null ); setvisible( true ); } private void buildbookpanel() throws ioexception { bookpanel = new jpanel(); bookpanel.setborder( borderfactory.createtitledborder( "books" ) ); loadbooks(); booklist = new jlist( books ); booklist.setvisiblerowcount( list_rows_visible ); jscrollpane scrollpane = new jscrollpane( booklist ); bookpanel.add( scrollpane ); } private void loadbooks() throws ioexception { file bookfile = new file( book_data_filename ); scanner inputfile = new scanner( bookfile ); while( inputfile.hasnext() ) { string bookdataline = inputfile.nextline(); string[] token = bookdataline.split( "," ); book newbook = new book(); newbook.settitle( token[0] ); newbook.setauthor( token[1] ); newbook.setprice( double.parsedouble( token[2] ) ); books.add( newbook ); } inputfile.close(); } private void buildcontrolpanel() { controlpanel = new jpanel(); controlpanel.setborder( borderfactory.createemptyborder( 30, 10, 10, 10 ) ); controlpanel.setlayout( new gridlayout( 4, 1 ) ); addtocartbutton = new jbutton( "add cart" ); addtocartbutton.addactionlistener( new addbuttonlistener() ); removefromcartbutton = new jbutton( "remove cart" ); removefromcartbutton.addactionlistener( new removebuttonlistener() ); clearcartbutton = new jbutton( "clear cart" ); clearcartbutton.addactionlistener( new clearbuttonlistener() ); controlpanel.add( addtocartbutton ); controlpanel.add( removefromcartbutton ); controlpanel.add( clearcartbutton ); } private void buildcartpanel() { cartpanel = new jpanel(); cartpanel.setborder( borderfactory.createtitledborder( "shopping cart" ) ); cartlist = new jlist(); cartlist.setvisiblerowcount( list_rows_visible ); jscrollpane scrollpane = new jscrollpane( cartlist ); cartpanel.add( scrollpane ); } private void buildcheckoutpanel() { checkoutpanel = new jpanel(); checkoutpanel.add( new jlabel( "subtotal" ) ); totaltext = new jtextfield( 10 ); totaltext.seteditable( false ); checkoutpanel.add( new jlabel( "sales tax" ) ); totaltext = new jtextfield( 10 ); totaltext.seteditable( false ); checkoutpanel.add( new jlabel( "total: " ) ); totaltext = new jtextfield( 10 ); totaltext.seteditable( false ); checkoutpanel.add( totaltext ); } private void addtocart( book book ) { cart.add( book ); cartlist.setlistdata( cart ); total += book.getprice(); updatetotal(); } private void removefromcart( book book, int index ) { cart.remove( index ); cartlist.setlistdata( cart ); total -= book.getprice(); updatetotal(); } private void updatetotal() { decimalformat fmt = new decimalformat( "$##,##0.00" ); totaltext.settext( fmt.format( total ) ); } private class addbuttonlistener implements actionlistener { public void actionperformed( actionevent e ) { if( booklist.getselectedvalue()!=null ) { book selected = (book) booklist.getselectedvalue(); addtocart( selected ); booklist.clearselection(); } else joptionpane.showmessagedialog( javaprogram.this, "please select book before adding cart.", program_name, joptionpane.plain_message ); } } private class removebuttonlistener implements actionlistener { public void actionperformed( actionevent e ) { int selectedindex = cartlist.getselectedindex(); if( selectedindex != -1 ) { book selected = (book) cartlist.getselectedvalue(); removefromcart( selected, selectedindex ); } else joptionpane.showmessagedialog( javaprogram.this, "please select book cart.", program_name, joptionpane.plain_message ); } } private class clearbuttonlistener implements actionlistener { public void actionperformed( actionevent e ) { final int yes = 0; final int no = 1; int choice = joptionpane.showconfirmdialog( javaprogram.this, "clear shopping cart?", program_name, joptionpane.yes_no_option ); if( choice==yes ) { cart.clear(); cartlist.setlistdata( cart ); total = 0.00; updatetotal(); } } } public static void main( string[] args ) throws ioexception { new javaprogram(); } }
add 2 more jtextfields sub-total , sales-tax:
private jtextfield subtotal; private jtextfield salestax; private jtextfield totaltext;
modify buildcheckoutpanel() method:
private void buildcheckoutpanel() { checkoutpanel = new jpanel(); checkoutpanel.add( new jlabel( "subtotal" ) ); subtotal = new jtextfield( 10 ); subtotal.seteditable( false ); checkoutpanel.add( subtotal ); checkoutpanel.add( new jlabel( "sales tax" ) ); salestax = new jtextfield( 10 ); salestax.seteditable( false ); checkoutpanel.add( salestax ); checkoutpanel.add( new jlabel( "total: " ) ); totaltext = new jtextfield( 10 ); totaltext.seteditable( false ); checkoutpanel.add( totaltext ); }
modify updatetotal() method:
private void updatetotal() { decimalformat fmt = new decimalformat( "$##,##0.00" ); subtotal.settext( fmt.format( total ) ); double tax =total*0.1; //if sales-tax 10% of total salestax.settext( fmt.format(tax) ); totaltext.settext(fmt.format(total+tax)); }
Comments
Post a Comment