【问题标题】:How do you return items from ArrayList that is stored in a different file?如何从存储在不同文件中的 ArrayList 返回项目?
【发布时间】:2016-08-14 07:46:33
【问题描述】:

所以我目前拥有的代码可以返回对数组中对象的引用,但不会显示其中的数据。代码的第一位是用户输入数据的主文件,但对象引用只返回用户输入的最后一个对象,我需要的是要搜索的数组,如果找到该项目显示数据与该对象一起存储。

第一个文件。

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

public class Inv
{


    public static void main(String args[])
    {

        Scanner console = new Scanner(System.in);

        String str;
        char c;
        int n=0;


        //Product product  = new Product();


        System.out.println("        INVENTORY MANAGEMENT SYSTEM");
        System.out.println("===============================================");
        System.out.println("1. ADD PRODUCT DATA");
        System.out.println("2. VIEW PRODUCT DATA");
        System.out.println("3. VIEW REPRLENISHMENT STRATEGY");
        System.out.println("===============================================");
        System.out.println("4. EXIT PROGRAM");

        while(n!=4)
        {
            System.out.print("\n Please enter option 1-4 to continue...: ");
            n = Integer.parseInt(System.console().readLine());

            if (n>4||n<1)
            {
                System.out.print("Invalid input, please try again...");
                continue;
            }
            if (n==1)
            {
                str="y";
                while(str.equals("y")||str.equals("Y"))
                {

                    Inv.addItem();
                    System.out.print("Would you like to enter another product ? (Y or N) : ");
                    str = console.next();
                }   
                continue;
            }
            if (n==2)
            {
                str="y";
                while(str.equals("y")||str.equals("Y"))
                {
                    Inv.prodData();
                    System.out.println("\n***************************************************\n");
                    System.out.print("Stay viewing this page? (Y or N) ");
                    str = console.next();

                }
                continue;
            }
            else

            if (n==3)
            {
                System.out.print("View Replenishment Strategy.");
                continue;
            }
        }
        System.out.print("\nThank you for using this inventory management software.\n");
        System.out.print("Developed by Xavier Edwards");
        System.out.println("\n***************************************************\n");

    }

    public static Product product;
    public static Store store;
    public static void addItem ()
    {
        Scanner console = new Scanner(System.in);
        product = new Product();
        store = new Store();

        String desc, id, str="";
        double price = 0, sUpPrice = 0, unitCost = 0, inventoryCost = 0;
        int stock = 0, demand = 0;

        //if (product == null) //If product 1 is empty
        //{
            System.out.print("Please enter product description between 3 to 10 characters...: ");
            desc = console.next();
            desc = desc.toLowerCase();
            product.setName(desc);

            if ((desc.length() < 3 || desc.length() > 10))
            {
                System.out.println("\nThis Input is incorrect. Please make description between 3 to 10 characters.\n");
                System.out.println("Try again with different input. ");
                System.out.println("\n*****************************************\n");
                Inv.addItem();
            }

            System.out.print("Please enter price in $ : ");
            price = console.nextDouble();
            product.setPrice(price);

            if (price < 0)
            {
                System.out.println("\nThis Input is incorrect. Please make sure attributes are positve numbers\n");
                System.out.println("Because of incorrect input, program will restart. ");
                System.out.println("\n*****************************************\n");
                Inv.addItem();
            }

            System.out.print("Please enter set up price. $ : ");
            sUpPrice = console.nextDouble();
            product.setsUpPrice(sUpPrice);

            if (sUpPrice < 0)
            {
                System.out.println("\nThis Input is incorrect. Please make sure attributes are positve numbers\n");
                System.out.println("Because of incorrect input, program will restart. ");
                System.out.println("\n*****************************************\n");
                Inv.addItem();
            }

            System.out.print("Please enter unit- cost. $ : ");
            unitCost = console.nextDouble();
            product.setunitCost(unitCost);

            if (unitCost < 0)
            {
                System.out.println("\nThis Input is incorrect. Please make sure attributes are positve numbers\n");
                System.out.println("Because of incorrect input, program will restart. ");
                System.out.println("\n*****************************************\n");
                Inv.addItem();
            }

            System.out.print("Please enter the inventory cost. $ : ");
            inventoryCost = console.nextDouble();
            product.setinvCost(inventoryCost);

            if (inventoryCost < 0)
            {
                System.out.println("\nThis Input is incorrect. Please make sure attributes are positve numbers\n");
                System.out.println("Because of incorrect input, program will restart. ");
                System.out.println("\n*****************************************\n");
                Inv.addItem();
            }

            System.out.print("Please enter the amount in stock : ");
            stock = console.nextInt();
            product.setstock(stock);

            if (stock < 0)
            {
                System.out.println("\nThis Input is incorrect. Please make sure attributes are positve numbers\n");
                System.out.println("Because of incorrect input, program will restart. ");
                System.out.println("\n*****************************************\n");
                Inv.addItem();
            }

            System.out.print("Please enter the demand of the product : ");
            demand = console.nextInt();
            product.setdRate(demand);

            if (demand < 0)
            {
                System.out.println("\nThis Input is incorrect. Please make sure attributes are positve numbers\n");
                System.out.println("Because of incorrect input, program will restart. ");
                System.out.println("\n*****************************************\n");
                Inv.addItem();
            }

            System.out.println("\n*****************************************\n");
            System.out.print(desc +" Product was added successfully ");
            System.out.println("\n*****************************************\n");
            store.add(product);

        //}
    }

    public static void prodData()
    {
        Scanner console = new Scanner(System.in);
        String pOption, str;

        System.out.print("\nEnter product description to view the data...\n");
        pOption = console.next();

        if (product == null)
        {
            System.out.println("\nThere is no information on this product.\n");

            System.out.println("\nWould you like to try again? (Y or N) \n");
            str = console.next();
            Inv.prodData();
        }

        System.out.println("The information for the product is..... ");
        System.out.println("\n*****************************************\n");
        System.out.println(store.ProductList.get(0));
        if (product.equals(store.ProductList.get(0)))
        {
            System.out.println("Product description : "+product.getName());
            System.out.println("Price : $ "+product.getPrice());
            System.out.println("Set-up Price : $ "+product.getsUpPrice());
            System.out.println("Unit Cost : $ "+product.getunitCost());
            System.out.println("Inventory Cost : $ "+product.getinvCost());
            System.out.println("Amount of Stock : "+product.getstock());
            System.out.println("Amount of Stock : "+product.getdRate());
        }*/
    }

}

创建数组列表和存储对象的第二个文件。

import java.util.*;
import java.util.ArrayList;

public class Store{
    public ArrayList <Product> ProductList = new ArrayList<Product> ();

    public Store()
    {
        //ArrayList = "";

    }

    public void add(Product product)
    {           
        ProductList.add(product);
    }

    public Product getProduct(String prodName) {
        for (int i = 0; i < ProductList.size(); i++) {
            if (ProductList.get(i).getName().equals(prodName)) {
                return ProductList.get(i);
            }
        }
        return null;
 }

}

用于存储特定用户条目的代码

public class Product
{
    public String name;
    public double price, sUpPrice, unitCost, invCost;
    public int stock, demand;


    public Product()
    {
        name = "";
        price = 0;
        sUpPrice = 0;
        unitCost = 0;
        invCost = 0;
        stock = 0;
        demand = 0;

    }

        public void setName(String name) {
            this.name = name;
        }
        public String getName() {
            return this.name;
        }

        public void setPrice(double price) {
            this.price = price;
        }
        public double getPrice() {
            return this.price;
        }   
        public void setsUpPrice(double sUpPrice) {
            this.sUpPrice = sUpPrice;
        }
        public double getsUpPrice() {
            return this.sUpPrice;
        }   
        public void setunitCost(double unitCost) {
            this.unitCost = unitCost;
        }
        public double getunitCost() {
            return this.unitCost;
        }   
        public void setinvCost(double invCost) {
            this.invCost = invCost;
        }
        public double getinvCost() {
            return this.invCost;
        }
        public void setstock(int stock) {
            this.stock = stock;
        }
        public int getstock() {
            return this.stock;
        }
        public void setdRate(int demand) {
            this.demand = demand;
        }
        public int getdRate() {
            return this.demand;
        }

}

这就是另一个名为 product.java 的文件中的代码

【问题讨论】:

  • 您的主题是返回一个存储在文件中的数组列表,但我在您的代码中没有看到任何与读取或写入文件相关的实例
  • 对不起,我的意思是数组列表的位置在一个名为 store.java 的文件中,而主要的代码位在一个名为 inv.java 的文件中,如果这样更有意义的话
  • 所以你需要将你的数据存储在类存储中并从类存储中取回数据,这样你就可以从你项目中任何类的存储类中获取数据
  • 是的,基本上就是这样,正如您从代码的第一位看到的那样,一直到底部,我需要在调用时在该方法中返回它

标签: java file arraylist return-value


【解决方案1】:

如果我理解正确,您希望用户输入产品名称,然后显示所有产品数据。

您可能应该使用您的getProduct 方法。

product = store.getProduct(pOption);
if (product != null){
    System.out.println("Product description : "+product.getName());
    System.out.println("Price : $ "+product.getPrice());
    System.out.println("Set-up Price : $ "+product.getsUpPrice());
    System.out.println("Unit Cost : $ "+product.getunitCost());
    System.out.println("Inventory Cost : $ "+product.getinvCost());
    System.out.println("Amount of Stock : "+product.getstock());
    System.out.println("Amount of Stock : "+product.getdRate());
}else{
    System.out.println("\nThere is no information on this product.\n");
    System.out.println("\nWould you like to try again? (Y or N) \n");
    str = console.next();
    Inv.prodData();
}

【讨论】:

  • 谢谢您的代码可用于返回产品信息,但仅当列表中有单个产品时,当我在列表中输入 2 个项目时,第一个项目被擦除,我将如何去关于解决这个问题?
  • 这两个项目是否同名?
  • 我已将整个项目的最后部分添加到原始问题中,这是另一个文件
  • 你的意思是,如果你输入2个产品,你只能检索最新的一个?
【解决方案2】:
public class Inv {


    //read data from any way you prefer and call the methods on store class
    //since the class methods and the productlist are marked as static
    //every class on your project is accessing and changing the same productlist

    Store.add("your product object");
    Store.getProduct("your name");


}


class Store{
    public static ArrayList <product> ProductList = new ArrayList<product> (); 

    public static void add(product product){
    ProductList.add(product);
    }

    public static product getProduct(String prodName) {
        for (int i = 0; i < ProductList.size(); i++) {
            if (ProductList.get(i).getName().equals(prodName)) {
                return ProductList.get(i);
            }
        }
        return null;
    }

       //implement new static methods to remove products from productlist
    public static removeProduct(String name){
       //code to remove product
    }


    public static removeAll(){
    //code to clear the productlist
    }


}

你需要在你的代码中遵循这种方法,在 cmets 中提供了解释

【讨论】:

    猜你喜欢
    • 2014-09-02
    • 2016-03-13
    • 2023-03-22
    • 2017-12-21
    • 1970-01-01
    • 1970-01-01
    • 2017-02-07
    • 2023-03-19
    • 2019-05-22
    相关资源
    最近更新 更多