Java Inventory Tracker接口程序不从output.txt读取数据并从数组列表

时间:2017-11-23 07:29:37

标签: java file-io interface inventory tracker

所以我正在尝试创建一个库存程序,用户输入库存中的项目信息,程序将其保存到数组,然后使用I / O输出output.txt文件,然后您可以加载库存文件虽然我遇到了问题。每当我尝试加载库存时,都会出现一堆错误。我可以在将项目添加到库存后获取项目信息并保存它但是当我尝试加载它时主菜单只是循环返回,当我退出程序尝试再次加载它时,我得到一堆错误。关于如何让这个程序正确运行的任何想法?

import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;

public class InventoryTrackerInterface {

  //ArrayList to store all the inventory items
  static ArrayList < Item > al = new ArrayList < Item > ();

  public static void main(String[] args) {
    //ArrayList to store all the inventory items
    ArrayList < Item > al = new ArrayList < Item > ();




    FileOutputStream fos;
    while (true) {
      Scanner reader = new Scanner(System.in); // Reading from System.in
      System.out.println("Enter your option: \n1.Add an item to Inventory \n2.Get an item info \n3.Save inventory to file \n4.Load inventory from file\n5.Exit ");
      int option = reader.nextInt();
      //Read input from user and execute accordingly
      switch (option) {

        case 1:
          //Add neew item to the ArrayList
          Item obj = new Item();
          System.out.println("Enter the new items name:");
          String name = reader.nextLine();
          obj.setName(name);
          reader.nextLine();
          System.out.println("Enter the new items quantity:");
          int quantity = reader.nextInt();
          obj.setQuantity(quantity);
          System.out.println("Enter the new items price:");
          float price = reader.nextFloat();
          obj.setPrice(price);
          System.out.println("Enter the new items UPC:");
          int upc = reader.nextInt();
          obj.setUPC(upc);

          //Add the new data to ArrayList
          al.add(obj);
          break;
        case 2:
          //Display the data from the ArrayList
          int itemCount = al.size(); //Get total size of ArrayList
          System.out.println("Enter a number from [0-" + itemCount + ":");
          int num = reader.nextInt();
          Item i = al.get(num);
          System.out.println("Name:" + i.getName());
          System.out.println("Quantity:" + i.getQuantity());
          System.out.println("Price:" + i.getPrice());
          System.out.println("UPC:" + i.getUPC());

          break;
        case 3:
          //Save data from ArrayList to output.txt
          saveData();
          System.out.println("Inventory saved to file output.txt");

          break;
        case 4:
          //Read data from output.txt and load ArrayList
          loadData();
          break;
        case 5:
          //Exit
          System.out.println("Bye");
          System.exit(0);

      }
    }
  }

  private static void saveData() {
    //Save output to output.txt
    FileOutputStream fos = null;

    try {
      fos = new FileOutputStream("output.txt");
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } finally {

      if (fos != null) {

        ObjectOutputStream oos = null;
        try {

          oos = new ObjectOutputStream(fos);

          if (oos != null) {

            oos.writeObject(al);

          }
          assert oos != null;
          oos.close();

        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }

  private static void loadData() {
    //Load data from output.txt

    FileInputStream fis = null;
    try {
      fis = new FileInputStream("output.txt");
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } finally {

      if (fis != null) {

        ObjectInputStream ois = null;
        try {
          ois = new ObjectInputStream(fis);

          if (ois != null) {

            try {

              al = (ArrayList < Item > ) ois.readObject();
            } catch (ClassNotFoundException e) {
              e.printStackTrace();
            }
          } else {

          }
          assert ois != null;
          ois.close();

        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
}

public class Item {
  private String name;
  private int quantity;
  private double price;
  private int upc;

  Item() {
    this.name = "";
    this.quantity = 0;
    this.price = 0;
    this.upc = 0;



  }


  public Item(String name, int qty, double price, String upc) {



  }


  public String getName() {

    return name;

  }


  public int getQuantity() {

    return quantity;

  }

  public double getPrice() {

    return price;

  }

  public int getUPC() {

    return upc;

  }
  void setName(String name) {
    this.name = name;
  }
  void setQuantity(int quantity) {
    this.quantity = quantity;
  }
  void setPrice(double price) {
    this.price = price;
  }
  void setUPC(int upc) {
    this.upc = upc;
  }
}




public class Inventory {
  private Item[] itemArray;
  private int totalItems = 0;

  public int getTotalNumberOfItems() {
    return totalItems;
  }

  public Item getItem(int index) {
    if (index < 0 || index >= totalItems) {
      return null;
    } else {
      return itemArray[index];
    }
  }

  public void addItem(Item newItem) {
    if (newItem == null) {
      System.out.println("Item not added.");
    } else {
      itemArray[totalItems] = newItem;
      totalItems++;
    }
  }

  public void saveInventoryToFile(String fileName) {}

  public void loadInventoryFromFile(String fileName) {}
}

0 个答案:

没有答案