【问题标题】:Vending Machine Program and printing array自动售货机程序和打印阵列
【发布时间】:2016-03-18 06:30:42
【问题描述】:

我正在为一个应该读取文本文件并从所述文本文件中获取项目以填充项目对象数组的类创建一个自动售货机程序。然而,当我去打印数组时,这就是 JVM 发出的结果。

物品@3d4eac69

我已经调试了程序,看看文件是否被正确读取,值是否被正确输入,并且确实如此。我已经尝试了几乎所有我能想到的东西,作为一个初学者,我不知道该怎么做。我将在下面包含我的代码。 VendingMachine 构造函数由讲师提供给我们。

自动售货机.java 导入 java.io.*;

import java.util.Scanner;


public class VendingMachine {

//data members
private Item[] stock;  //Array of Item objects in machine
private double money;  //Amount of revenue earned by machine
private Item[] vendor;

/*********************************************************************
 * This is the constructor of the VendingMachine class that take a
 * file name for the items to be loaded into the vending machine.
 *
 * It creates objects of the Item class from the information in the 
 * file to populate into the stock of the vending machine.  It does
 * this by looping the file to determine the number of items and then
 * reading the items and populating the array of stock. 
 * 
 * @param filename Name of the file containing the items to stock into
 * this instance of the vending machine. 
 * @throws FileNotFoundException If issues reading the file.
 *********************************************************************/
public VendingMachine(String filename) throws FileNotFoundException{
    //Open the file to read with the scanner
    File file = new File(filename);
    Scanner scan = new Scanner(file);

    //Determine the total number of items listed in the file
    int totalItem = 0;
    while (scan.hasNextLine()){
        scan.nextLine();
        totalItem++;
    } //End while another item in file
    //Create the array of stock with the appropriate number of items
    stock = new Item[totalItem];
    scan.close();

    //Open the file again with a new scanner to read the items
    scan = new Scanner(file);
    int itemQuantity = -1;
    double itemPrice = -1;
    String itemDesc = "";
    int count = 0;
    String line = "";

    //Read through the items in the file to get their information
    //Create the item objects and put them into the array of stock
    while(scan.hasNextLine()){
        line = scan.nextLine();
        String[] tokens = line.split(",");
        try {
            itemDesc = tokens[0];
            itemPrice = Double.parseDouble(tokens[1]);
            itemQuantity = Integer.parseInt(tokens[2]);

            stock[count] = new Item(itemDesc, itemPrice, itemQuantity);
            count++;
        } catch (NumberFormatException nfe) {
            System.out.println("Bad item in file " + filename + 
                    " on row " + (count+1) + ".");
        }
    } //End while another item in file

    scan.close();

    //Initialize the money data variable.
    money = 0.0;

} //End VendingMachine constructor

//To run the successful transaction
public void vend() {

}

//To determine whether or not the transaction was successful 
public void outputMessage() {


}

//To print the items in held in stock
public void printMenu()  {
    vendor = stock;
    System.out.println(this.vendor[0]);

}




} //End VendingMachine class definition

VendingMachineDriver.java

import java.util.*;

import java.io.*;

public class VendingMachineDriver {



public static void main(String args[]) throws FileNotFoundException {
    Scanner input = new Scanner(System.in);
    //String vendingSelect = input.next();
    String a = new String("a");
    String b = new String("b");
    String x = new String("x");



    System.out.println("Welcome to Jeremy's Super Vending Machines!");
    System.out.println("Please enter how much money you have:");
    System.out.println("Press A to select Drinks");
    //String vendingSelect = input.next();
    VendingMachine drinks = new VendingMachine("vending_machines/drinks");
    drinks.printMenu();



 }
}

Item.java(我们只被告知在这个文件中有数据成员,老实说我不知道​​为什么)

import java.util.*;

public class Item {
  private String itemDesc;
  private double itemPrice;
  private int itemQuantity;

  public Item (String itemDesc, double itemPrice, int itemQuantity){
      this.itemDesc = itemDesc;
      this.itemPrice = itemPrice;
      this.itemQuantity = itemQuantity;
}

}

编辑:忘记添加文本文件和相应的位置

饮料(位置是 vending_machines/drinks)

    Milk,2.00,1
    OJ,2.50,6
    Water,1.50,10
    Soda,2.25,6
    Coffee,1.25,4
    Monster,3.00,5

零食(位置是 vending_machines/snacks)

    Gummies,1.50,6
    Chips,1.00,6
    Raisins,1.25,5
    Pretzels,1.50,6
    Cookie,1.75,5
    Peanut,1.25,4
    Gum,0.75,2

【问题讨论】:

  • 如果您希望它为您打印“有用”信息,则需要覆盖toString in item,如果您直接打印对象。

标签: java arrays class methods


【解决方案1】:

添加/覆盖 Items 类的 toString 方法

@Override
    public String toString() {
        return "c1 [itemDesc=" + itemDesc + ", itemPrice=" + itemPrice + ", itemQuantity=" + itemQuantity + "]";
    }

否则 java 会打印对象的哈希码而不是人类可读信息...

【讨论】:

  • 只是想说声谢谢。这可能在讲座中有所涉及,但我错过了几天。这解决了我非常沮丧的问题,几乎让我流泪了哈哈
【解决方案2】:

您需要重写toString() 方法才能正确打印对象的内容。

【讨论】:

    【解决方案3】:

    当您尝试print an Item 时,您看到的乱码实际上是对象的内部表示和哈希码。如果Item 有自己的 toString 方法,就可以解决这个问题。这可以通过将以下内容添加到您的 Item 类来实现。

    public String toString() {
        return "Description: " + this.itemDesc + 
               " | Price: " + this.itemPrice + 
               " | Quantity: " +  this.itemQuantity;
    }
    

    注意:在这个方法中添加@Override 标签是很好的风格。不过,没有它也可以工作。

    如果您想了解发生这种情况的原因,请记住 Java 中的所有类都扩展了 Object 类。如果一个类没有定义它自己的toString() 方法,它会退回到使用Object 类的方法,看起来像这样.. [注意那里的@ - 这正是你所拥有的:) ]

    public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多