⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inventory.java.txt

📁 Code for the classical inventory program
💻 TXT
字号:
import java.util.Arrays;      // program uses arrays
import java.awt.*;              
import java.awt.event.*;      
import javax.swing.*;         // Import the java swing package 
import javax.swing.Icon;
import javax.swing.ImageIcon;

       
public class Inventory5 {

    // main method begins execution of java application
    public static void main(String[] args) {
        System.out.println("\nCheckPoint: <strong class="highlight">Inventory</strong> Part 5");
        System.out.println("My DVD Collection\n");

        Movie dvd = null;
        <strong class="highlight">Inventory</strong> <strong class="highlight">inventory</strong> = new <strong class="highlight">Inventory</strong>();        

        dvd = new Movie(1, "Live Free or Die Hard", 18, 19.99f, 2001);
        System.out.println(dvd);
        <strong class="highlight">inventory</strong>.addMovie(dvd);                    
        
        dvd = new Movie(2, "The Patriot", 14, 20.99f, 1998);
        System.out.println(dvd);
        <strong class="highlight">inventory</strong>.addMovie(dvd);                    
        
        dvd = new Movie(3, "Full Metal Jacket", 1, 21.99f, 1995);
        System.out.println(dvd);
        <strong class="highlight">inventory</strong>.addMovie(dvd);                    

        dvd = new Movie(4, "Coyote Ugly", 15, 18.99f, 2003);
        <strong class="highlight">inventory</strong>.addMovie(dvd);                    
        System.out.println(dvd);
 
        dvd = new Movie(5, "Apocalypto", 10, 21.99f, 2006);
        System.out.println(dvd);
        <strong class="highlight">inventory</strong>.addMovie(dvd);                    
        
        dvd = new Movie(6, "Monster in Law", 1, 7.99f, 2005);
        System.out.println(dvd);
        System.out.println("\nEnd of DVD collection!\n");
        <strong class="highlight">inventory</strong>.addMovie(dvd);                   

        <strong class="highlight">inventory</strong>.printInventory();
        new InventoryGUI(<strong class="highlight">inventory</strong>);                          
    } // end main

} // end Inventory5


class DVD {
    private int itemNo;
    private String title;
    private int inStock;
    private float unitPrice;

    DVD(int itemNo, String title, int inStock, float unitPrice) {
        this.itemNo    = itemNo;
        this.title     = title;
        this.inStock   = inStock;
        this.unitPrice = unitPrice;
    }

    public int getItemNo()      { return itemNo; }
    public String getTitle()    { return title; }
    public int getInStock()     { return inStock; }
    public float getUnitPrice() { return unitPrice; }

    public float value() {
        return inStock * unitPrice;
    }

    public String toString() {
        return String.format("itemNo=%2d   title=%-22s   inStock=%3d   price=$%7.2f   value=$%8.2f",
                              itemNo, title, inStock, unitPrice, value());
    }

} // end DVD


class <strong class="highlight">Inventory</strong> {
    // Setup an array of Movies (set it to hold 30 items)
    private final int INVENTORY_SIZE = 30;
    private DVD[] items;
    private int numItems;
     
    <strong class="highlight">Inventory</strong>() {
        items = new Movie[INVENTORY_SIZE];
        numItems = 0;
    }

    public int getNumItems() {
        return numItems;
    }

    public DVD getDVD(int n) {
        return items[n];
    }

    // Adds a Movie to the array of Movies. Adds to first empty slot found.
    public void addMovie(DVD item) {
        items[numItems] = item;    
        ++numItems;
    }

    // Loop through our array of Movies and add up the total value.
    // Go item by item adding the quantity on hand x its price.
    // Add that value to a running total accumulator variable.
    public double value() {
        double sumOfInventory = 0.0;

        for (int i = 0; i < numItems; i++)
            sumOfInventory += items[i].value();
                        
        return sumOfInventory;
    }

    // Prints the <strong class="highlight">inventory</strong> list including name, quantity, price, 
    // and total stock value for each item.
    public void printInventory() {
        System.out.println("\nShelly's DVD Inventory\n");
       
        // If no items were found, print a message saying the <strong class="highlight">inventory</strong> is empty.
        if (numItems <= 0) {
            System.out.println("Inventory is empty at the moment.\n");
        } else {
            for (int i = 0; i < numItems; i++)
                System.out.printf("%3d   %s\n", i, items[i]);
            System.out.printf("\nTotal value in <strong class="highlight">inventory</strong> is $%,.2f\n\n", value());
        }
    }
    
} // end <strong class="highlight">Inventory</strong>


// Extends DVD class from the base class DVD
class Movie extends DVD {
    // Holds movie year and adds restocking fee
    private int movieYear;

    // Constructor, calls the constructor of Movie first
    public Movie(int MovieID, String itemName, int quantityOnHand, float itemPrice, int year) {
        super(MovieID, itemName, quantityOnHand, itemPrice);    
    	// Pass on the values needed for creating the Movie class first thing
        this.movieYear = movieYear;		
    }

    // To set the year manually
    public void setYear(int year) {
        movieYear = year;
    }

    // Get the year of this DVD Movie
    public int getMovieYear() {
        return movieYear;
    }

    // Overrides value() in Movie class by calling the base class version and
    // adding a 5% restocking fee on top
     public float value() {
        return super.value() + restockingFee();
    } 

    // Simply gets the base class's value, and figures out the 5% restocking fee only
    public float restockingFee() {
        return super.value() * 0.05f;
    }

} // end Movie


// GUI for the <strong class="highlight">Inventory</strong>
// Contains an <strong class="highlight">inventory</strong> of DVD's and lets the user step through them one by one
class InventoryGUI extends JFrame 
{
    // access <strong class="highlight">inventory</strong> for DVD Collection
    private <strong class="highlight">Inventory</strong> theInventory; 
    
    // index in the <strong class="highlight">inventory</strong> of the currently displayed DVD. 
    // the index starts at 0, goes to the number of DVDs in the <strong class="highlight">inventory</strong> minus 1 
    private int index = 0; 
    
    // GUI elements to display currently selected DVD information 
    private final JLabel itemNumberLabel = new JLabel("  Item Number:"); 
    private JTextField itemNumberText; 
    
    private final JLabel prodnameLabel = new JLabel("  Product Name:"); 
    private JTextField prodnameText; 
    
    private final JLabel prodpriceLabel = new JLabel("  Price:"); 
    private JTextField prodpriceText; 
    
    private final JLabel numinstockLabel = new JLabel("  Number in Stock:"); 
    private JTextField numinstockText; 
    
    private final JLabel valueLabel = new JLabel("  Value:"); 
    private JTextField valueText; 
    
    private final JLabel restockingFeeLabel = new JLabel("  Restocking Fee:"); 
    private JTextField restockingFeeText; 
    
    private final JLabel totalValueLabel = new JLabel("  <strong class="highlight">Inventory</strong> Total Value:"); 
    private JTextField totalValueText; 

	private JPanel centerPanel;
	private JPanel buttonPanel;


    // constructor for the GUI, in charge of creating all GUI elements 
	InventoryGUI(<strong class="highlight">Inventory</strong> <strong class="highlight">inventory</strong>) {
        super("Shelly's Movie Inventory");
        final Dimension dim = new Dimension(140, 20);
        final FlowLayout flo = new FlowLayout(FlowLayout.LEFT);
        JPanel jp;

        // create the <strong class="highlight">inventory</strong> object that will hold the product information 
        theInventory = <strong class="highlight">inventory</strong>;        
        
        // setup the GUI
        
        // product information 
        // setup a panel to collect all the components.
        centerPanel = new JPanel(); 
        centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));

 
        buttonPanel = new JPanel();

        JButton firstButton = new JButton("First");    
        firstButton.addActionListener(new FirstButtonHandler());
        buttonPanel.add(firstButton);
        
        JButton previousButton = new JButton("Previous");
        previousButton.addActionListener(new PreviousButtonHandler());
        buttonPanel.add(previousButton);

        JButton nextButton = new JButton("Next");    
        nextButton.addActionListener(new NextButtonHandler());
        buttonPanel.add(nextButton);
        
        JButton lastButton = new JButton("Last");
        lastButton.addActionListener(new LastButtonHandler());
        buttonPanel.add(lastButton);
        
        JButton addButton = new JButton("Add");
        addButton.addActionListener(new AddButtonHandler());
        buttonPanel.add(addButton);
        
        JButton deleteButton = new JButton("Delete");
        deleteButton.addActionListener(new DeleteButtonHandler());
        buttonPanel.add(deleteButton);
        
        JButton modifyButton = new JButton("Modify");
        modifyButton.addActionListener(new ModifyButtonHandler());
        buttonPanel.add(modifyButton);
        
        JButton saveButton = new JButton("Save");
        saveButton.addActionListener(new SaveButtonHandler());
        buttonPanel.add(saveButton);
        
        JButton searchButton = new JButton("Search");
        searchButton.addActionListener(new SearchButtonHandler());
        buttonPanel.add(searchButton);

        centerPanel.add(buttonPanel);
       
        jp = new JPanel(flo);
        itemNumberLabel.setPreferredSize(dim);
        jp.add(itemNumberLabel); 
        itemNumberText = new JTextField(3); 
        itemNumberText.setEditable(false); 
        jp.add(itemNumberText); 
        centerPanel.add(jp);
        
        jp = new JPanel(flo);
        prodnameLabel.setPreferredSize(dim);
        jp.add(prodnameLabel); 
        prodnameText = new JTextField(17); 
        prodnameText.setEditable(false); 
        jp.add(prodnameText); 
        centerPanel.add(jp);
        
        jp = new JPanel(flo);
        prodpriceLabel.setPreferredSize(dim);
        jp.add(prodpriceLabel); 
        prodpriceText = new JTextField(17); 
        prodpriceText.setEditable(false); 
        jp.add(prodpriceText); 
        centerPanel.add(jp);
        
        jp = new JPanel(flo);
        numinstockLabel.setPreferredSize(dim);
        jp.add(numinstockLabel); 
        numinstockText = new JTextField(5); 
        numinstockText.setEditable(false); 
        jp.add(numinstockText);  
        centerPanel.add(jp);
        
        jp = new JPanel(flo);
        restockingFeeLabel.setPreferredSize(dim);
        jp.add(restockingFeeLabel); 
        restockingFeeText = new JTextField(17); 
        restockingFeeText.setEditable(false); 
        jp.add(restockingFeeText); 
        centerPanel.add(jp);
        
        jp = new JPanel(flo);
        valueLabel.setPreferredSize(dim);
        jp.add(valueLabel); 
        valueText = new JTextField(17); 
        valueText.setEditable(false); 
        jp.add(valueText); 
        centerPanel.add(jp);
        
        // add the overall <strong class="highlight">inventory</strong> information to the panel
        jp = new JPanel(flo);
        totalValueLabel.setPreferredSize(dim);
        jp.add(totalValueLabel); 
        totalValueText = new JTextField(17);
        totalValueText.setEditable(false);
        jp.add(totalValueText); 
        centerPanel.add(jp);

        // add the panel to the GUI display
        setContentPane(centerPanel);

        repaintGUI();

        setDefaultCloseOperation(EXIT_ON_CLOSE); 
        setSize(420, 480); 
        setResizable(false); 
        setLocationRelativeTo(null); 
        setVisible(true); 
    } 

    // (re)display the GUI with current product's information 
    public void repaintGUI() { 
        Movie temp = (Movie) theInventory.getDVD(index); 
        
        if (temp != null) { 
            itemNumberText.setText("" + temp.getItemNo());     
            prodnameText.setText(temp.getTitle()); 
            prodpriceText.setText(String.format("$%.2f", temp.getUnitPrice())); 
            restockingFeeText.setText(String.format("$%.2f", temp.restockingFee())); 
            numinstockText.setText("" + temp.getInStock()); 
            valueText.setText(String.format("$%.2f", temp.value())); 
        }
        totalValueText.setText(String.format("$%.2f", theInventory.value())); 
    }


    class FirstButtonHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            index = 0;
            repaintGUI();
        }
    }

    class PreviousButtonHandler implements ActionListener {
        public void actionPerformed(ActionEvent e){
	        int numItems = theInventory.getNumItems();
	        index = (--index) % numItems;
            repaintGUI();
        }
     }

    class NextButtonHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
	        int numItems = theInventory.getNumItems();
            index = (++index) % numItems;
            repaintGUI();
        }
    }
    class LastButtonHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
	        int numItems = theInventory.getNumItems();
            index = (numItems -1) % numItems;
            repaintGUI();

        }
    }

      class AddButtonHandler implements ActionListener {
      public void actionPerformed(ActionEvent e){
            repaintGUI();
        }
     }
    	                      
      class SaveButtonHandler implements ActionListener {
      public void actionPerformed(ActionEvent e){
            repaintGUI();
        }
     }
     
      class ModifyButtonHandler implements ActionListener {
      public void actionPerformed(ActionEvent e){
            repaintGUI();
        }
     }
     
      class DeleteButtonHandler implements ActionListener {
      public void actionPerformed(ActionEvent e){
            repaintGUI();
        }
     }
     
       class SearchButtonHandler implements ActionListener {
      public void actionPerformed(ActionEvent e){
            repaintGUI();
        }
     }
     
     
  } // End InventoryGUI class  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -