尝试在GUI中显示总库存方法

时间:2014-02-02 02:22:45

标签: java swing awt

import java.util.Arrays;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.*;

class Book implements Comparable<Book> {

    protected long isbn;
    protected String title;
    protected String autName;
    protected int year;
    protected String pubName;
    protected double price;
    protected int quantity;

    // Constructor
    public Book(long isbn, String title, String autName, int year, String pubName, double price) {
        setIsbn(isbn);
        setTitle(title);
        setAut(autName);
        setYear(year);
        setPub(pubName);
        setPrice(price);
    }

    // methods for class variables
    public long getIsbn() {
        return isbn;
    }

    public void setIsbn(long isbn) {
        this.isbn = isbn;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAut() {
        return autName;
    }

    public void setAut(String autName) {
        this.autName = autName;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public String getPub() {
        return pubName;
    }

    public void setPub(String pubName) {
        this.pubName = pubName;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    //toString for the book array
    public String toString() {
        return "ISBM: " + isbn + "\n" + "Title: " + title + "\n" + "Author's Name: " + autName + "\n" + "Year Published: " + year + "\n" + "Publisher's Name: " + pubName + "\n" + "Price: $" + price + "\n\n";
    }

    //comparable for array
    public int compareTo(Book otherBook) {
        return title.compareTo(otherBook.getTitle());
    }
}// end class Book

class EBook extends Book implements Comparable<Book> {

    private String website;

    //constructor and super
    public EBook(long isbn, String title, String autName, int year,
            String pubName, double price, String website) {
        super(isbn, title, autName, year, pubName, price);

        setWebsite(website);
    }

    // get and set 
    public String getWebsite() {
        return website;
    }

    public void setWebsite(String website) {
        this.website = website;
    }

    public double discount() {
        return price * 0.10;
    }

    DecimalFormat money = new DecimalFormat("$0.00");

    //toString for the Book array
    public String toString() {
        return "ISBM: " + isbn + "\n"
                + "Title: " + title + "\n"
                + "Author's Name: " + autName + "\n"
                + "Year Published: " + year + "\n"
                + "Publisher's Name: " + pubName + "\n"
                + "Price: " + money.format(price) + "\n"
                + "Website: " + website + "\n"
                + "Discount: " + money.format(discount()) + "\n\n";
    }

    //comparable for array
    public int compareTo(Book otherBook) {
        return title.compareTo(otherBook.getTitle());
    }
}//end class EBook

public class BookStore {

    //total price of inventory method
    public static double calculateInventoryTotal(Book[] books) {
        double total = 0;

        for (int i = 0; i < books.length; i++) {
            total += books[i].getPrice();
        }

        return total;
    }
    static int bookIndex = 0;

    public static JTextArea prepareDisplay(Book myBook, JTextArea myTextArea) {

        DecimalFormat money = new DecimalFormat("$0.00");
        myTextArea.setText("");
        myTextArea.append("Book Store Inventory: \n\n");
        myTextArea.append(myBook.toString());
        myTextArea.append("Total Inventory Value: ");
        return myTextArea;
    }
    //main method

    public static void main(String[] args) {

        // book array
        final Book[] inventory = new Book[5];

        Book a = new Book(7423540089L, "No David!", "David Shannon", 2009, "Shannon Rock", 12.99);
        EBook b = new EBook(75260012L, "David goes to School", "David Shannon", 2010, "Shannon Rock", 11.98, "http://www.tinyurl.qqwert67o9");
        Book c = new Book(743200616L, "Simple Abundance", "Sarah Breathnach", 2009, "Scribner", 14.99);
        EBook d = new EBook(78137521819L, "The very hungry caterpillar", "Eric Carle", 2005, "Philomel Books", 13.99, "http://www.tinyurl.fguopt8u90");
        Book e = new Book(9781416987116L, "We're going on a bear hunt", "Michael Rosen", 2009, "McElderry", 15.99);

        inventory[0] = a;
        inventory[1] = b;
        inventory[2] = c;
        inventory[3] = d;
        inventory[4] = e;
        Arrays.sort(inventory);

        //parameters of the JTextArea
        final JTextArea textArea = new JTextArea(15, 15);
        textArea.setText("");
        textArea.setEditable(false);

        DecimalFormat money = new DecimalFormat("$0.00");
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(1, 3));

        JButton firstButton = new JButton("First");
        buttonPanel.add(firstButton);
        firstButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                bookIndex = 0;
                prepareDisplay(inventory[bookIndex], textArea);
            }
        });

        JButton nextButton = new JButton("Next");
        buttonPanel.add(nextButton);
        nextButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                bookIndex++;
                if (bookIndex >= inventory.length) {
                    bookIndex = 0;
                }
                prepareDisplay(inventory[bookIndex], textArea);
            }
        });

        JButton previousButton = new JButton("Previous");
        buttonPanel.add(previousButton);
        previousButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                bookIndex--;
                if (bookIndex < 0) {
                    bookIndex = inventory.length - 1;
                }
                prepareDisplay(inventory[bookIndex], textArea);
            }
        });

        JButton lastButton = new JButton("Last");
        buttonPanel.add(lastButton);
        lastButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                bookIndex = (inventory.length - 1);
                prepareDisplay(inventory[bookIndex], textArea);
            }
        });

        JLabel logoLabel = new JLabel(new ImageIcon("bluehills.jpg"));
        JPanel logoPanel = new JPanel();
        logoPanel.add(logoLabel);

        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
        centerPanel.add(prepareDisplay(inventory[bookIndex], textArea)); //show the first employee

        //calculate inventory total
        double inventoryTotal = calculateInventoryTotal(inventory);

        //myTextArea.append("Total Inventory Value: " + money.format(inventoryTotal) + "\n\n");

        //set the GUI window with the parameters
        JFrame frame = new JFrame("Book Store Program");
        frame.setLayout(new BorderLayout()); // set layout
        frame.add(logoPanel, BorderLayout.NORTH); //add logo
        frame.add(buttonPanel, BorderLayout.SOUTH); // add previous and next button to frame
        frame.add(centerPanel, BorderLayout.CENTER); // add center panel to frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }
}// end class 

我尝试使用calculateInventoryTotal方法在总库存字符串后显示GUI中的总库存值。我无法正常显示,所以我正在寻求帮助。我搜索了几个小时但找不到这个问题的答案。提前感谢您帮助我完成此项目。

1 个答案:

答案 0 :(得分:0)

您使用此声明获得inventoryTotal

double inventoryTotal = calculateInventoryTotal(inventory);

但你永远不会将这个值赋予gui元素来显示