我一直在试图弄清楚如何获得“管理员”按钮以显示自动售货机中产品的当前库存以及单击时的现金余额,但是我似乎无法弄清楚如何。我最初尝试使用JOptionPane.showMessageDialog,但是尝试使用的所有方法均无效。
错误:
找不到符号符号:方法getInv(Dispenser)位置:类VendingMachine.ButtonHandler ----(Alt-Enter显示提示)
主班
package vendingmachine;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class VendingMachine extends JFrame {
//Constants
private static final int WIDTH = 500;
private static final int HEIGHT = 850;
//Object Variables
private JLabel headingL, selectionL;
private JButton candyB, chipsB, gumB, cookiesB, exitB, adminB;
private ButtonHandler bHandler;
//Instance Variables
private Register register = new Register();
private static Dispenser candy = new Dispenser();
private static Dispenser chips = new Dispenser(5,2);
private static Dispenser gum = new Dispenser(20,1);
private static Dispenser cookies = new Dispenser(5,3);
//Constructor
public VendingMachine() {
setTitle("Brennan's Vending Machine");
setSize(WIDTH,HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
//Layout
Container pane = getContentPane();
pane.setLayout(new GridLayout(8,1)); //GRID LAYOUT (Button Spaces)
//Create Lebels
headingL = new JLabel("WELCOME TO BRENNAN'S VENDING MACHINE", SwingConstants.CENTER);
selectionL = new JLabel("Click a button for your selection", SwingConstants.CENTER);
//Create Buttons
bHandler = new ButtonHandler();
candyB = new JButton("candy");
candyB.addActionListener(bHandler);
chipsB = new JButton("chips");
chipsB.addActionListener(bHandler);
gumB = new JButton("gum");
gumB.addActionListener(bHandler);
cookiesB = new JButton("cookies");
cookiesB.addActionListener(bHandler);
adminB = new JButton("Admin");
adminB.addActionListener(bHandler);
exitB = new JButton("EXIT");
exitB.addActionListener(bHandler);
//Add objects
pane.add(headingL);
pane.add(selectionL);
pane.add(candyB);
pane.add(chipsB);
pane.add(gumB);
pane.add(cookiesB);
pane.add(adminB);
pane.add(exitB);
} //end Constructor
//Listener Event for Buttons
private class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("EXIT"))
{
System.exit(0);
}
else if(e.getActionCommand().equals("candy")) {
sellProduct(candy, "Candy");
}
else if(e.getActionCommand().equals("chips")) {
sellProduct(chips, "Chips");
}
else if(e.getActionCommand().equals("gum")) {
sellProduct(gum, "Gum");
}
else if(e.getActionCommand().equals("cookies")) {
sellProduct(cookies, "Cookies");
}
else if(e.getActionCommand().equals("Admin")) {
JOptionPane.showMessageDialog(null, "Cookie Inventory: " + getInv(cookies));
}
}//end actionPerformed
}//end ButtonHandler
//Admin Output
private void sellProduct(Dispenser product, String productName) {
int itemPrice;
if(product.getInv() > 0) {
product.invOut();
itemPrice = product.getProductCost();
register.cashIn(itemPrice);
JOptionPane.showMessageDialog(null, "Get Your " + productName, ". Item Dispensed", JOptionPane.PLAIN_MESSAGE);
}
else {
JOptionPane.showMessageDialog(null,"This item is out of stock.");
}
}
public static void main(String[] args) {
VendingMachine vending = new VendingMachine();
System.out.println(candy.getInv());
}//end main
}//end class
库存处理程序类
package vendingmachine;
public class Dispenser {
//Instance Vars
private int productInv = 10;
private int productCost;
//Constructors
public Dispenser() {
productInv = 10;
productCost = 1;
}//end Inv()
public Dispenser(int productInv, int productCost) {
this.productInv = productInv;
this.productCost = productCost;
}//end Overloaded
//get Method
public int getInv() {
return productInv;
}//end getInv()
public int getProductCost() {
return productCost;
}
//Increment Inventory
public void invOut() {
productInv -= 1;
}
}//end class
现金处理员类别
package vendingmachine;
public class Register {
//Instance Vars
private int cashBalance = 500;
//Constructor
public Register() {
cashBalance = 500;
}//end Register()
public Register(int cashBalance) {
this.cashBalance = cashBalance;
}//end overloaded
//get method
public int getBalance() {
return cashBalance;
}
//Increment balance for sale
public void cashIn(int cashDepo) {
cashBalance =+ cashDepo;
}
}//end class
最佳答案
好吧,错误本身很明显。
它找不到以getInv
作为参数的名为Dispenser
的方法。
您在这里调用getInv
方法:JOptionPane.showMessageDialog(null, "Cookie Inventory: " + getInv(cookies));
在getInv
类中定义了Dispenser
(entory?)方法时,不带任何参数。因此,cookies.getInv();
应该是正确的调用。