简体   繁体   中英

Carry over variable value to new method

I'm new to java and I can't seem to get my method working without having to reset the variable to a value of 'null' or new Object. What I need is to have the user input carry over into the next method.

public static void addItem ()
{
    Scanner console = new Scanner(System.in);

    String desc, id, str="";
    double price = 0, setUpPrice = 0, unitCost = 0, inventoryCost = 0;
    int stock = 0, demand = 0;
    Product product  = new Product();


    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.print("This Input is incorrect, Please try again.");
    }*/
    System.out.print("Please enter price in $ : ");
    price = console.nextDouble();
    System.out.print("Please enter set up price. $ : ");
    setUpPrice = console.nextDouble();
    System.out.print("Please enter unit- cost. $ : ");
    unitCost = console.nextDouble();
    System.out.print("Please enter the inventory cost. $ : ");
    inventoryCost = console.nextDouble();
    System.out.print("Please enter the amount in stock : ");
    stock = console.nextInt();
    System.out.print("Please enter the demand of the product : ");
    demand = console.nextInt();


    // need code here to repeat the fuction again if y is pressed.
    // if n is pressed need to return to inventory management interface.

}

public static void prodData ()
{
    Product product = new Product();

    System.out.println("The information for the product is : ");
    System.out.println("Product description : "+product.getName());
}

Declare your product var as global and access as follows

    public static  Product product; 

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

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

    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.print("This Input is incorrect, Please try again.");
    }*/
    System.out.print("Please enter price in $ : ");
    price = console.nextDouble();
    System.out.print("Please enter set up price. $ : ");
    setUpPrice = console.nextDouble();
    System.out.print("Please enter unit- cost. $ : ");
    unitCost = console.nextDouble();
    System.out.print("Please enter the inventory cost. $ : ");
    inventoryCost = console.nextDouble();
    System.out.print("Please enter the amount in stock : ");
    stock = console.nextInt();
    System.out.print("Please enter the demand of the product : ");
    demand = console.nextInt();

    // need code here to repeat the fuction again if y is pressed.
    // if n is pressed need to return to inventory management interface.

}

public static void prodData () {
    System.out.println("The information for the product is : ");
    System.out.println("Product description : "+product.getName());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM