使用数组创建软饮料程序

时间:2014-02-26 05:11:24

标签: java

我正在为我的班级做一个软饮料计划,但我无法弄清楚为什么我的主要班级告诉我有错误。该程序假设打印出以下内容:

Soft drink    ID     Starting Inventory    Final Inventory    # transactions
Coke          123           100                  70                  2
Pepsi         345           50                   80                  3
CanadaDry     678           75                   83                  1
DrPepper      444           120                  130                 3

这是我到目前为止所做的:

SoftDrinkTester(主类)

/**
 * This program tests the functionality of a the SoftDrinkInventory class.
 * A datafile containing initial data is used to construct a SoftDrink object.
 * Then transactions are processed where each transaction contains how
 * cases are bought or sold. A function displays a report of the drink name,
 * ID number, starting inventory, final inventory, and the number of
 * transactions processed. The largest and smallest transaction value
 * is displayed.
 */
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class SoftDrinkTester {
    public static void main (String[] args) {
        Scanner inventoryFile = null; // inventory data file
        Scanner transFile = null; // transaction data file
        // open the inventory initialization file
        try {
            inventoryFile = new Scanner(new FileInputStream("data6.txt"));
        }
        catch (FileNotFoundException e){
            System.out.println("File not found or not opened.");
            System.exit(0);
        }
        // open the file containing the buy/sell transactions
        try {
            transFile = new Scanner(new FileInputStream("data6trans.txt"));
        }
        catch (FileNotFoundException e) {
            System.out.println("File not found or not opened.");
            System.exit(0);
        }
        // instantiate the soft drink distributorship object
        // and process the transactions by updating the inventory totals
        SoftDrinkInventory softDrinks = new SoftDrinkInventory(inventoryFile);
        softDrinks.processTransactions(transFile);
        softDrinks.displayReport();
    }
}

SoftDrinkInventory

import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class SoftDrinkInventory {

    static final int MAXSIZE = 100;     // maximum size of 100
    private String[] names;             // softdrink names
    private String[] ids;               // softdrink identifications
    private int[] startingInventory;    // starting inventory of the softdrinks
    private int[] finalInventory;       // final inventory of the softdrinks
    private int[] transactionCounts;    // number of transactions per softdrink
    private int trueSize;               // total number of softdrinks

    /**-----------------------------------------------------------------------
     * constructor  
     * 
    .
     */
    public SoftDrinkInventory() {

        initializeString(names);
        initializeString(ids);
        initializeInt(startingInventory);
        initializeInt(finalInventory);
        initializeInt(transactionCounts);

    }


    /**-----------------------------------------------------------------------
     * displayReport  
     */

    public void displayReport() {
        System.out.printf("%-22s %-16s %-23s %-23s %s %n", "Soft Drink", "ID",
            "Starting Inventory", "Final Inventory",
            "# transaction");
        for(int i = 0; i < MAXSIZE; i++) {
            //change "%-22s %-16s %-23f %-23f %f %n" to the below
            //and names etc. to names[i] etc.
            System.out.printf("%-22s %-16s %-23d %-23d %d %n", names[i], ids[i],
                startingInventory[i], finalInventory[i], 
                transactionCounts[i]);

        }

    }
    /**-----------------------------------------------------------------------
     * initializeInt 
     * 
     */
    private void initializeInt(int[] a) {

        for(int i = 0; i < a.length; i++) {
            a[i] = 0;
        }

    }
    /**-----------------------------------------------------------------------
     * initializeString 
     */
    private void initializeString(String[] s) {

        for(int i = 0; i < s.length; i++) {
            s[i] = "";

        }

    }
}

我仍然需要在SoftDrinkInventory中创建公共processTransactions和private findID类。 我获取姓名和身份证件的文件如下:

的库存:

Coke       123 100
Pepsi      345 50
CanadaDry  678 75
DrPepper   444 120
交易的

345   10
123   -5
345   10
678    8
444   20
444  -20
444   10
999    5
345   10
123  -25

我曾尝试向我的教授寻求帮助,但他拒绝在课外教我们任何东西。我感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

密切注意您为实例化对象而进行的调用:

SoftDrinkInventory softDrinks = new SoftDrinkInventory(inventoryFile);

现在,观察构造函数将接受的正式参数:

public SoftDrinkInventory()

这里的问题是没有Scanner作为参数的构造函数(实际上,唯一的构造函数,如上所示,不带任何参数)。

暂且不说:您的数组未初始化。您应该使用您希望用它初始化它们的值初始化它们。

例如:

private String[] names = new String[MAXSIZE];

另外,作为一点思考:你的initializeInt方法是无用的,因为Java保证数组被初始化to a default value;因为那是设置原始数组的值,所以所有元素都已经为零。

答案 1 :(得分:0)

中创建SoftDrinkInventory对象时
SoftDrinkInventory softDrinks = new SoftDrinkInventory(inventoryFile);

您正在传递一个类型为`Scanner的参数,但您的构造函数实现不需要任何参数:

public SoftDrinkInventory() {
    ...
}

,您想检查阵列初始化。它们没有被初始化。你应该做点什么:

startingInventory = new int[someLength];