-6

我有一个我一直在尝试创建的代码。到目前为止,它不会编译和运行,阵列关闭,GUI 将无法工作。就修复此代码而言,我迷失了方向。恐怕我将不得不重新开始并再次迷失。如果有人可以帮助我,这是我的烂摊子...

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mobile.phones;

/**
*
* @author Parents
*/
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.PopupMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class MobilePhones {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

}
private String ItemNumber; //item # of product
private String ProductName; //product name
private String Department; //department name
private double UnitStock; //units in stock
private double ItemPrice; //Price per unit
private double InventoryValue; //The dollar value of the inventory in stock
private double CalculateInventory; //The total value of all of the inventory in stock
private double value;
private double UnitPrice;
int getValue;
private MobilePhones myPhone;
/**
 *
 */

//constructor
public MobilePhones (String item, String product, String department, double units,      double price) {
    String unit = null;
    ItemNumber = unit;
    ProductName = product;
    String dept = null;
Department = dept;
    double stock = 0;
    UnitStock = stock;
    double Price = price;
} //end constructor

//getter and setter methods for MobilePhones

public void setItemNumber (String item) { String unit = null;
//setter for item number
    this.ItemNumber = unit;
} //end setter item number

public String getItemNumber() { //getter for item number
    return ItemNumber;
} //end getter item number

public void setProductName (String product) { //setter for product name
    this.ProductName = product;
} //end setter product name

public String getProductName() { //getter for product name
    return ProductName;
} //end getter product name

public void setDepartment (String department) { String dept = null;
//setter for department name
    this.Department = dept;
} //end setter department name

public String getDepartment() { //getter for department name
    return Department;
} //end getter department name

public void setUnitStock (double units) { double stock = 0;
//setter for units in stock
    this.UnitStock = stock;
} //end setter units in stock

public double getUnitStock() { //getter for units in stock
    return UnitStock;
} //end getter units in stock

public void setUnitPrice (double price) { //setter for unit price
    this.UnitPrice = price;
} //end setter unit price

public double getUnitPrice() { //getter for unit price
    return UnitPrice;
} //end getter for unit price

//calculate inventory value
public double getInventoryValue(){
    return UnitStock * UnitPrice;
}//end calculate inventory value

public void setCalculateInventory (double value){
    this.CalculateInventory = value;
}

public double getCalculateInventory(Phone[] myPhones){
    double revenue = 0.0;
    for(int i = 0; i < 5; i++){
        revenue = getInventoryValue();
        {

    myPhones[0] = new Phone ("0001");
    myPhones[1] = new Phone ("0002");
    myPhones[2] = new Phone ("0003");
    myPhones[3] = new Phone ("0004");
    myPhones[4] = new Phone ("0005");

    //output
        System.out.println("Product Number: \t\t" + myPhones[i].getItemNumber());

        System.out.println("Item Name: \t\t\t" + myPhones[i].getProductName());

        System.out.println("Number of Items currently in Stock: \t" +     myPhones[i].getUnitsStock());

        System.out.println("Cost per Unit: \t\t$%.2f\n" + myPhones[i].getUnitPrice());            

        System.out.printf("Total Value of Inventory is: \t$%.2f\n", revenue);

        System.out.println();

    } // end output
    return 0.0;

/**
*
* @author Parents
*/

}
    return 0;
}
private static class Phone {

    public Phone() {
    }

    private Phone(String string) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body  of generated methods, choose Tools | Templates.
    }

    private String getItemNumber() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private String getProductName() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private String getUnitsStock() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private String getUnitPrice() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
class SmartPhone extends MobilePhones{
    private String MobileOS;
    private double restockingfee;
    double rate = 0.05;
    int getValue;
    public SmartPhone( String item, String product, String department, double units,    double price, String MobileOS )
{
super(item, product, department, units, price);
this.MobileOS = MobileOS;
}

public String getMobileOS() {
return MobileOS;
}
public void setMobileOS(String MobileOS) {
this.MobileOS = MobileOS;
}
public double getValue()
{
return getValue() * 0.05;
}
public double getRestockingFee()
{
return getValue() * 0.05;
}
    @Override
    public String toString()
{
String formatString = "MobilePhone: %s";
formatString += "Restocking Fee: $%.2f";
formatString = String.format( formatString, MobilePhones,
getValue() * 0.05 );
return( formatString + super.toString() );
}      
}
import java.awt.GridLayout
import java.awt.event.ActionEvent
import java.awt.event.ActionListener

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 */
@SuppressWarnings("serial")
public final class MobilePhonesGUI extends JFrame {

JTextField itemNumber = new JTextField();
JTextField name = new JTextField();
JTextField unitsInStock = new JTextField();
JTextField unitPrice = new JTextField();
JTextField valueOfInventory = new JTextField();
JTextField restockingFee = new JTextField();

JButton next = new JButton("Next Product");
JButton prior = new JButton("Prior Product");

JTextField valueOfEntireInventory = new JTextField();

private MobilePhones[] MobilePhones = new MobilePhones[] {};
private int productIndex = 0;

private void showNext() {
    if (productIndex < products.length - 1) {
        productIndex++;
        updateGUI();
    }
}

private void showPrior() {
    if (productIndex > 0) {
        productIndex--;
        updateGUI();
    }
}

public void updateGUI() {
    Product currentProduct = products[productIndex];

    itemNumber.setText(currentProduct.getItemNumber().toString());
    name.setText(currentProduct.getName());
    unitsInStock.setText(currentProduct.getUnitsInStock().toString());
    unitPrice.setText(currentProduct.getUnitPrice().toString());
    valueOfInventory.setText(currentProduct.getValueOfInventory()
            .toString());
    restockingFee.setText(currentProduct.getRestokingFee().toString());

    valueOfEntireInventory.setText(calcValueOfEntireInventory().toString());

}

public Double calcValueOfEntireInventory() {
    double value = 0;

    for (Product p : products) {
        value += p.getValueOfInventory();
    }

    return value;
}

public MobilePhonesGUI(Product[] products) {
    super();
    this.products = products;

    setTitle("Inventory GUI");
    setBounds(0, 0, 400, 300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    createGUI();
    pack();

    prior.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            showPrior();
        }
                    throw new ("Not supported yet."); //To change body of generated     methods, choose Tools | Templates.

            });
            }
    next.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            showNext();
        }
                        return null;
    });

    MobilePhonesGUI();
}

private void createGUI(PopupMenu valueOfEntireInventory, PopupMenu next, PopupMenu  prior, PopupMenu restockingFee, PopupMenu valueOfInventory, PopupMenu unitPrice, PopupMenu  unitsInStock, PopupMenu name, PopupMenu itemNumber) {
    JPanel productPanel = new JPanel(new GridLayout(0, 2, 5, 5));
        Component add;
        add = productPanel.add(new JLabel("Item Number:"));
    productPanel.add(itemNumber);

    productPanel.add(new JLabel("Product Name:"));
    productPanel.add(name);

    productPanel.add(new JLabel("Units In Stock:"));
    productPanel.add(unitsInStock);

    productPanel.add(new JLabel("Unit Price:"));
    productPanel.add(unitPrice);

    productPanel.add(new JLabel("Value of Inventory:"));
    productPanel.add(valueOfInventory);

    productPanel.add(new JLabel("Restocking Fee (5%):"));
    productPanel.add(restockingFee);

    productPanel.add(prior);
    productPanel.add(next);

    productPanel.add(new JLabel("Value of Entire Inventory:"));
    productPanel.add(valueOfEntireInventory);

    JPanel inventoryPanel = new JPanel();
    inventoryPanel.add(productPanel);

    setContentPane(inventoryPanel);
}

/**
     *
     * @param args
     */
    public void main(String[] args) {
    Product[] products;
        products = new Product[] { new Product(1, "Mouse", 100, 5.0),
              new Product(2, "Keyboard", 130, 15.9),
              new Product(3, "Notebook", 10, 800.5),
              new Product(4, "Memory Key", 20, 20.5) };

    new InventoryGUI(products).setVisible(true);
}

 }
4

1 回答 1

-1

我注意到的一件事是您在尝试使用数组之前没有声明数组的大小。如果您知道在您的 getClaculateInventory 方法中只有五个手机,您需要声明手机数组的大小。

public double getCalculateInventory(Phone[] myPhones){
    double revenue = 0.0;
    myPhones = new Phone[5];

如果你愿意,你甚至可以使用 for 循环来用你想要的值填充数组。

于 2013-09-18T23:06:22.737 回答