无法获得以gui显示的小计,总税和销售税

时间:2015-05-08 04:00:16

标签: java swing

首先,由于某些原因,数学被搞砸了我的gui所以20.61的书总是显示为16.43或类似的东西。除此之外,我正在尝试为销售税创建字段,要在应用程序中显示总数和小计...但是唯一显示的是总计。 如何创建与总计相同的销售税和小计费用?

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 to Cart" );
        addToCartButton.addActionListener( new AddButtonListener() );

        removeFromCartButton = new JButton( "Remove from 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 a book before adding to 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 a book from the 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();
    }
}

1 个答案:

答案 0 :(得分:0)

为小计和销售税增加2个JTextField:

private JTextField subTotal;
private JTextField salesTax;
private JTextField totalText;

修改buildCheckOutPanel()方法:

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 );
}

修改updateTotal()方法:

private void updateTotal()
{
    DecimalFormat fmt = new DecimalFormat( "$##,##0.00" );
    subTotal.setText( fmt.format( total ) );
    double tax =total*0.1; //if sales-tax is 10% of total
    salesTax.setText( fmt.format(tax) );
    totalText.setText(fmt.format(total+tax));

}