自动售货机程序和打印阵列

时间:2016-03-18 06:30:43

标签: java arrays class methods

我正在为一个应该读取文本文件的类创建一个Vending Machine程序,并从所述文本文件中获取项目以填充项目对象的数组。然而,当我打印阵列时,这就是JVM拍摄的内容。

  

物品@ 3d4eac69

我调试了程序以查看文件是否正确读取并且值正确输入并确定它们是否正确。我已经尝试了我能想到的一切,作为初学者,我不知道该怎么做。我将在下面包含我的代码。 VendingMachine构造函数由教师提供给我们。

VendingMachine.java     import 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 / drink)

    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 / snack)

    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

3 个答案:

答案 0 :(得分:1)

添加/覆盖Items Class的toString方法

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

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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

注意:将@Override标记添加到此方法是一种好方法。但是,没有它也可以工作。

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

@