gpt4 book ai didi

java - 修改库存程序以使用 GUI

转载 作者:行者123 更新时间:2023-12-01 15:52:18 25 4
gpt4 key购买 nike

GUI 应显示库存中的所有商品,并包括商品编号、产品名称、库存单位数量、每个单位的价格以及该产品的库存值(value)。此外,GUI 应显示整个库存的值(value)、附加属性和补货费用。所有美元值均应显示为货币(即 $D,DDD.CC)。

当我编译代码时,我收到以下错误消息,我不知道如何纠正。

C:\Documents and Settings\AdminUser\My Documents\InventoryPart4.java:162: class, interface, or enum expected
import java.awt.BorderLayout;
^
C:\Documents and Settings\AdminUser\My Documents\InventoryPart4.java:163: class, interface, or enum expected
import java.awt.FlowLayout;
^
C:\Documents and Settings\AdminUser\My Documents\InventoryPart4.java:164: class, interface, or enum expected
import java.awt.GridLayout;
^
C:\Documents and Settings\AdminUser\My Documents\InventoryPart4.java:165: class, interface, or enum expected
import java.awt.event.ActionEvent;
^
C:\Documents and Settings\AdminUser\My Documents\InventoryPart4.java:166: class, interface, or enum expected
import java.awt.event.ActionListener;
^
C:\Documents and Settings\AdminUser\My Documents\InventoryPart4.java:167: class, interface, or enum expected
import java.text.DecimalFormat;
^
C:\Documents and Settings\AdminUser\My Documents\InventoryPart4.java:169: class, interface, or enum expected
import javax.swing.BorderFactory;
^
C:\Documents and Settings\AdminUser\My Documents\InventoryPart4.java:170: class, interface, or enum expected
import javax.swing.JButton;
^
C:\Documents and Settings\AdminUser\My Documents\InventoryPart4.java:171: class, interface, or enum expected
import javax.swing.JFrame;
^
C:\Documents and Settings\AdminUser\My Documents\InventoryPart4.java:172: class, interface, or enum expected
import javax.swing.JLabel;
^
C:\Documents and Settings\AdminUser\My Documents\InventoryPart4.java:173: class, interface, or enum expected
import javax.swing.JPanel;
^
C:\Documents and Settings\AdminUser\My Documents\InventoryPart4.java:174: class, interface, or enum expected
import javax.swing.JTextField;
^
12 errors

Tool completed with exit code 1

这是我的代码:

/**
* Represents a product.
*/
public class Television {
private int itemNumber;
private String productName;
private int unitsInStock;
private double price;

/**
* Default constructor
*/
public Television() {
}

/**
* Handy constructor to initialize all attributes of the product.
*/
public Television(int itemNumber, String productName, int unitsInStock,
double price) {
this.itemNumber = itemNumber;
this.productName = productName;
this.unitsInStock = unitsInStock;
this.price = price;
}

/**
* @return The value of the inventory (the number of units in stock
* multiplied by the price of each unit).
*/
public double calculateInventory() {
return this.unitsInStock * this.price;
}

/**
* @return the itemNumber
*/
public int getItemNumber() {
return itemNumber;
}

/**
* @return the product name
*/
public String getProductName() {
return productName;
}

/**
* @return the unitsInStock
*/
public int getUnitsInStock() {
return unitsInStock;
}

/**
* @return the price
*/
public double getPrice() {
return price;
}

/**
* @param itemNumber
* the itemNumber to set
*/
public void setItemNumber(int itemNumber) {
this.itemNumber = itemNumber;
}

/**
* @param name
* the product name to set
*/
public void setName(String productName) {
this.productName = productName;
}

/**
* @param unitsInStock
* the unitsInStock to set
*/
public void setUnitsInStock(int unitsInStock) {
this.unitsInStock = unitsInStock;
}

/**
* @param price
* the price to set
*/
public void setPrice(double price) {
this.price = price;
}
}


/**
* This class should inherit from the Product class. Recall that the extends
* keyword is used in java to represent inheritance
*/
public class Supplier extends Computer {
/** Supplier Name */
private String supplierName;

/**
* Constructor should have 5 parameters:
*
* - Item Number
*
* - Product Name
*
* - Number of Units in Stock
*
* - Price of each Unit
*
* - Supplier Name
*/
public Supplier(int itemNumber, String productName, int unitsInStock,
double price, String supplierName) {
/*
* Note you will use the super keyword to invoke the constructor in your
* Product class.
*/
super(itemNumber, productName, unitsInStock, price);
this.supplierName = supplierName;
}

/**
* This method returns the product of price and available units multiplied
* by 5% ((price * product) * .05);
*/
public double calculateRestockFee() {
return super.calculateInventory() * .05;
}

/**
* This method returns the product of price and available units plus the
* restock fee.
*/
public double calculateInventory() {
return super.calculateInventory() + calculateRestockFee();
}

/**
* @return the supplierName
*/
public String getSupplierName() {
return supplierName;
}

/**
* @param supplierName
* the supplierName to set
*/
public void setSupplierName(String supplierName) {
this.supplierName = supplierName;
}

}


import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

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

@SuppressWarnings("serial")
public class InventoryPart4 extends JFrame {
private static DecimalFormat currency = new DecimalFormat("$#,##0.00");

// Declares the TextFields used to display the value of each attribute of
// the current product.
private JTextField itemNumberTF;
private JTextField productNameTF;
private JTextField unitsInStockTF;
private JTextField priceTF;
private JTextField supplierNameTF;
private JTextField restockFeeTF;
private JTextField valueOfInventoryTF;

// Declares a TextField to display the value of the entire inventory
private JTextField totalValueOfInventoryTF;

// Declare buttons to navigate through the products in the inventory
private JButton priorBT;
private JButton nextBT;

// This array holds all products in the inventory.
private Supplier[] products;

// Indicates the index of the product displayed onto the screen.
private int current = 0;

/**
* Starts the application
*
* @param args
* Not used by this application
*/
public static void main(String[] args) {
// Creates and displays the GUI
new InventoryPart4();
}

/**
* Creates a new instance of the GUI and displays the frame.
*/
public InventoryPart4() {
super("Inventory Part 4");
setSize(500, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);

// Creates the array with 5 elements
products = new Supplier[5];

// Creates some products
products[0] = new Supplier(0001, " Samsung UN46D6400",9,1599.99);
products[1] = new Supplier(0002, " Vizio XVT553SV",6,1299.00);
products[2] = new Supplier(0003, " Panasonic Viera TC-P50VT25",2,2079.99);
products[3] = new Supplier(0004, " Sony Bravia KDL-55EX720",8, 1889.99);
products[4] = new Supplier(0005, " LG Infinia 47LX9500",2,2099.00);

// Sorts products by name
sortArray();

// Creates the visual components
createComponents();

// Shows the GUI
setVisible(true);

// Displays the first product
updateFields();
}

private void createComponents() {
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

p.add(createFieldsPanel(), BorderLayout.CENTER);
p.add(createButtonsPanel(), BorderLayout.SOUTH);

setContentPane(p);
}

private JPanel createButtonsPanel() {
JPanel p = new JPanel();
p.setLayout(new FlowLayout(FlowLayout.RIGHT));

priorBT = new JButton("Prior");
priorBT.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (current > 0) {
current--;
updateFields();
}
}
});
p.add(priorBT);

nextBT = new JButton("Next");
nextBT.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (current < products.length - 1) {
current++;
updateFields();
}
}
});
p.add(nextBT);

return p;
}

/**
* Updates the fields to reflect the current product.
*/
protected void updateFields() {
Supplier s = products[current];

itemNumberTF.setText(String.valueOf(s.getItemNumber()));
productNameTF.setText(s.getProductName());
unitsInStockTF.setText(String.valueOf(s.getUnitsInStock()));
priceTF.setText(currency.format(s.getPrice()));
supplierNameTF.setText(s.getSupplierName());
restockFeeTF.setText(currency.format(s.calculateRestockFee()));
valueOfInventoryTF.setText(currency.format(s.calculateInventory()));

totalValueOfInventoryTF.setText(currency.format(calculateInventory()));
}

private JPanel createFieldsPanel() {
JPanel p = new JPanel();
p.setLayout(new GridLayout(0, 2, 5, 5));

p.add(new JLabel("Item Number"));
itemNumberTF = new JTextField();
p.add(itemNumberTF);

p.add(new JLabel("Product Name"));
productNameTF = new JTextField();
p.add(productNameTF);

p.add(new JLabel("Units In Stock"));
unitsInStockTF = new JTextField();
p.add(unitsInStockTF);

p.add(new JLabel("Unit Price"));
priceTF = new JTextField();
p.add(priceTF);

p.add(new JLabel("Supplier Name"));
supplierNameTF = new JTextField();
p.add(supplierNameTF);

p.add(new JLabel("Restock Fee"));
restockFeeTF = new JTextField();
p.add(restockFeeTF);

p.add(new JLabel("Value Of Inventory"));
valueOfInventoryTF = new JTextField();
p.add(valueOfInventoryTF);

p.add(new JLabel(""));
p.add(new JLabel(""));

p.add(new JLabel("Value Of The Entire Inventory"));
totalValueOfInventoryTF = new JTextField();
p.add(totalValueOfInventoryTF);

return p;
}

/**
* A method to calculate the value of the entire inventory. This method
* should take in an array of type Television and should
* traverse through all the elements of the array and calculate the
* inventory.
*
* @return The value of the entire inventory.
*/
public double calculateInventory() {
double value = 0;
for (int i = 0; i < products.length; i++) {
value += products[i].calculateInventory();
}
return value;
}

/**
* Sorts the products by name, using the Bubble Sort algorithm.

*/
public void sortArray() {
int n = products.length; // size;
boolean swapped;
do {
swapped = false;
for (int i = 0; i < n - 1; i++) {
String name1 = products[i].getProductName();
String name2 = products[i + 1].getProductName();
if (name1.compareToIgnoreCase(name2) > 0) {
// swap
Supplier temp = products[i];
products[i] = products[i + 1];
products[i + 1] = temp;
swapped = true;
}
}
n = n - 1;
} while (swapped);
}
}

最佳答案

看起来您正在尝试在文件中进行导入。导入应该位于文件的顶部。我认为这将解决您遇到的特定错误。

关于java - 修改库存程序以使用 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5775209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com