2

Hey so for this program I have 3 files all together, what I'm trying to achieve is to get what the user inputs from one file, and then store it in an array in another file, so that I can then call it back later on.

The first program is the bit that the user inputs.

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();


    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("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());

}

}

The second program is the bit with the array list, I cant seem to declare the array list, and I am unable to get the data to populate the array list.

public class Store{ private ArrayList ProductList = new ArrayList ();

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

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

public Product getProduct(String prodName){
    for(int i = 0; i< ProductList.length; i++){
        if(ProductList(i).name == prodName){
            return ProductList(i);
        }
    }
}

}

Any help would be great, thanks.

2 Answers 2

1

In ArrayList, you need to call get method with index of object that you want access. Instead of ProductList(i) in getProduct method, do ProductList.get(i)

getProduct function should now look like below:

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; }

in ProductList.get(i).getName().equals(prodName) line instead of using ==, use equals method for comparison. It will compare value of string. If you use ==, it will compare object reference of both the string and in your case it will always gives false even if its values are same because both the String Objects you are using for comparison are different.

Also just an advice, in product class create public getter setter and access name variable by getName() method.

Sign up to request clarification or add additional context in comments.

9 Comments

okay, thankyou for that ive updated the code but now it returns an error of cannot find symbol. symbol: method length
Where exactly its giving the error? Without proper log its not possible to help
location: variable ProductList of type ArrayList<Product>
Please find the updated code, for List there is no length property like array. You have to use size() method to get number of elements in list.
import java.util.*; import java.util.ArrayList; public class Store{ private 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.length; i++) { if (ProductList.get(i).name == prodName) { return ProductList.get(i); } } } } updated code is that
|
1

When declaring an ArrayList, you need to specify the data type it holds.

In your example, as far as I can see, productList should be instantiated using private ArrayList<Product> productList = new ArrayList<>();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.