我为我的班级决赛写了一个自动售货机程序但是我一直遇到堆栈溢出错误。 我相信它与继承有关,但不确定,我已经尝试过改变它,没有任何帮助。
你们的任何帮助都会受到极大的赞赏。感谢。
这是我的计划:
package vendingmachine;
import java.text.NumberFormat;
import javax.swing.JOptionPane;
public class VendingMachine {
public static void main(String[] args) {
VendMachine freestyle = new VendMachine();
int selectedOption = JOptionPane.showConfirmDialog(null, "Would you like to use the vending machine?", "Vending Machine",
JOptionPane.YES_NO_OPTION);
if (selectedOption == JOptionPane.NO_OPTION) {
System.exit(selectedOption);
} else {
freestyle.offerChoice();
}
}
}
class VendMachine {
Snacks temp = new Snacks();
private float moneyPaid = 0;
int maxBevCount = 50;
int maxSnackCount = 100;
float itemPrice = .65f;
private final String coke = "Coke Zero";
private final String sprite = "Sprite Zero";
private final String water = "Dasani";
private float moneyReturned = 0;
private int ask;
NumberFormat formating = NumberFormat.getCurrencyInstance();
public void offerChoice() {
ask = Integer.parseInt(JOptionPane.showInputDialog(null, "Would you like a Snack: 1 or Beverage: 2 ", "Vending Machine",
JOptionPane.INFORMATION_MESSAGE));
if (ask == 1) {
temp.askUser();
} else {
int askBevType = Integer.parseInt(JOptionPane.showInputDialog(null, "What type of beverage would you like? " + coke + ": 1 " + sprite
+ ": 2 " + water + ": 3 " + "Quit: 4 ", "Vending Machine", JOptionPane.INFORMATION_MESSAGE));
switch (askBevType) {
case 1: {
JOptionPane.showMessageDialog(null, "you chose " + coke + " the price is: " + formating.format(itemPrice));
addMoney();
break;
}
case 2: {
JOptionPane.showMessageDialog(null, "you chose " + sprite + " the price is: " + formating.format(itemPrice));
addMoney();
break;
}
case 3: {
JOptionPane.showMessageDialog(null, "you chose " + water + " the price is: " + formating.format(itemPrice));
addMoney();
break;
}
case 4: {
System.exit(0);
}
default: {
break;
}
}
}
}
public float addMoney() {
moneyPaid = Float.parseFloat(JOptionPane.showInputDialog(null, " insert your money", "Vending Machine", JOptionPane.INFORMATION_MESSAGE));
while (true) {
if (moneyPaid == itemPrice) {
vend();
}
if (moneyPaid < itemPrice) {
JOptionPane.showMessageDialog(null, "that's not enough... please insert more money!", "Vending Machine", JOptionPane.ERROR_MESSAGE);
float moreCashIn = Float.parseFloat(JOptionPane.showInputDialog(null, "The items cost " + formating.format(itemPrice)
+ " insert your money.", "Vending Machine", JOptionPane.INFORMATION_MESSAGE));
if (moreCashIn >= itemPrice) {
vend();
}
} else if (moneyPaid > itemPrice) {
moneyReturned = moneyPaid - itemPrice;
vend();
JOptionPane.showMessageDialog(null, "And here is your change of " + formating.format(moneyReturned), "Vending Machine",
JOptionPane.INFORMATION_MESSAGE);
}
return moneyPaid;
}
}
public void vend() {
JOptionPane.showMessageDialog(null, "Vending........Here you go. Have a nice day!", "Vending Machine", JOptionPane.INFORMATION_MESSAGE);
restock();
}
public void restock() {
if (ask == 2) {
maxBevCount -= 1;
JOptionPane.showMessageDialog(null, "Beverage Stock in the machine is now " + maxBevCount, "Vending Machine",
JOptionPane.INFORMATION_MESSAGE);
maxBevCount += 1;
JOptionPane.showMessageDialog(null, "Machine is now restocked with " + maxBevCount + " Beverages ", "Vending Machine",
JOptionPane.INFORMATION_MESSAGE);
} else {
maxSnackCount -= 1;
JOptionPane.showMessageDialog(null, "Stock in the machine is now " + maxSnackCount, "Vending Machine", JOptionPane.INFORMATION_MESSAGE);
maxSnackCount += 1;
JOptionPane.showMessageDialog(null, "Machine is now restocked with " + maxSnackCount + " items", "Vending Machine",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
class Snacks extends VendMachine {
int askSnackType;
EmAndM emAndM = new EmAndM("M&M's", 180, 1.80f);
Snickers snickers = new Snickers("Snickers", 280, 2.50f);
Gum gum = new Gum("Gum", 110, 1.25f);
Popcorn popcorn = new Popcorn("Popcorn", 190, 2.30f);
Crackers crackers = new Crackers("Crackers", 130, 1.90f);
Chips chips = new Chips("Chips", 140, 1.70f);
public void askUser() {
askSnackType = Integer.parseInt(JOptionPane.showInputDialog(null, "Would you like some- " + emAndM.name + ": 1 " + snickers.name + ": 2 "
+ gum.name + ": 3 " + popcorn.name + ": 4 " + crackers.name + ": 5 " + chips.name + ": 6 " + " Quit: 7 ", "Vending Machine",
JOptionPane.INFORMATION_MESSAGE));
switch (askSnackType) {
case 1: {
JOptionPane.showMessageDialog(null,
"You chose " + emAndM.name + " , Calories: " + emAndM.calories + " the price is: " + formating.format(emAndM.price));
addMoney();
break;
}
case 2: {
JOptionPane.showMessageDialog(null,
"You chose " + snickers.name + " , Calories: " + snickers.calories + " the price is: " + formating.format(snickers.price));
addMoney();
break;
}
case 3: {
JOptionPane.showMessageDialog(null,
"You chose " + gum.name + " , Calories: " + gum.calories + " the price is: " + formating.format(gum.price));
addMoney();
break;
}
case 4: {
JOptionPane.showMessageDialog(null,
"You chose " + popcorn.name + " , Calories: " + popcorn.calories + " the price is: " + formating.format(popcorn.price));
addMoney();
break;
}
case 5: {
JOptionPane.showMessageDialog(null,
"You chose " + crackers.name + " , Calories: " + crackers.calories + " the price is: " + formating.format(crackers.price));
addMoney();
break;
}
case 6: {
JOptionPane.showMessageDialog(null,
"You chose " + chips.name + " , Calories: " + chips.calories + " the price is: " + formating.format(chips.price));
addMoney();
break;
}
case 7: {
System.exit(0);
}
default: {
break;
}
}
}
class SugarySnacks {}
class EmAndM extends Snacks {
String name;
int calories;
float price;
EmAndM(String brand, int cal, float cost) {
name = brand;
calories = cal;
price = cost;
}
}
class Snickers extends Snacks {
String name;
int calories;
float price;
Snickers(String brand, int cal, float cost) {
name = brand;
calories = cal;
price = cost;
}
}
class Gum extends Snacks {
String name;
int calories;
float price;
Gum(String brand, int cal, float cost) {
name = brand;
calories = cal;
price = cost;
}
}
class SaltySnacks {}
class Popcorn extends Snacks {
String name;
int calories;
float price;
Popcorn(String brand, int cal, float cost) {
name = brand;
calories = cal;
price = cost;
}
}
class Crackers extends Snacks {
String name;
int calories;
float price;
Crackers(String brand, int cal, float cost) {
name = brand;
calories = cal;
price = cost;
}
}
class Chips extends Snacks {
String name;
int calories;
float price;
Chips(String brand, int cal, float cost) {
name = brand;
calories = cal;
price = cost;
}
}
}
答案 0 :(得分:0)
解决了我的问题,在我的VendMachine类中,我在Snack类中创建了temp对象。这导致堆栈溢出。我通过在offerChoice方法中创建相同的临时对象来解决问题。
这是修改后的区域:
类VendMachine {
Snacks temp = new Snacks();**//removed this object**
private float moneyPaid = 0;
int maxBevCount = 50;
int maxSnackCount = 100;
float itemPrice = .65f;
private final String coke = "Coke Zero";
private final String sprite = "Sprite Zero";
private final String water = "Dasani";
private float moneyReturned = 0;
private int ask;
NumberFormat formating = NumberFormat.getCurrencyInstance();
public void offerChoice(){
Snacks temp = new Snacks(); **//Inserted same object under this method**
ask = Integer.parseInt(JOptionPane.showInputDialog(null, "Would you like a Snack: 1 or Beverage: 2 ", "Vending Machine", JOptionPane.INFORMATION_MESSAGE));
if (ask == 1){
temp.askSnackType();