Newbie question.

Hello,

Thanks for you help.

Is it possible to have a seperate method within the main method?

I have created an array that I am trying to total. I can total the items just fine by using item[0] + item[1] + item[2], but the assignment calls for using a method to total the items.

Here is the code and the class Main first. class second.

import java.util.Scanner;// Calls scanner for input

publicclass Inventory

{

// Begin main method

publicstaticvoid main( String args[] )

{

int i;

double invTotal;

//Instantiate Iteminfo array

Iteminfo[] myItem =new Iteminfo[3];

// Create Item Info object

myItem[0] =new Iteminfo( 01,"Laptop", 15, 2000 );

myItem[1] =new Iteminfo( 02,"Desktop", 25, 1500 );

myItem[2] =new Iteminfo( 03,"Server", 10, 5000 );

// Display Program title and description.

System.out.println("Inventory Program Part 2" );

System.out.println();//outputs blank line

System.out.println();

// Display item info

System.out.printf("The first item is: \n\n" );

myItem[0].displayInfo();

System.out.println();

System.out.printf("The second item is: \n\n" );

myItem[1].displayInfo();

System.out.println();

System.out.printf("The third item is: \n\n" );

myItem[2].displayInfo();

System.out.println();

//method to total inventory

publicdouble getInvTotal()

{

return myItem[0].getTotal() + myItem[1].getTotal() + myItem[2].getTotal();

}//end method

// method to display invetory total

privatedouble displayTotal()

{

System.out.printf("The total of all inventory is: $%.2f. \n\n", getInvTotal() );

}//end method

myItem[0].displayTotal();

}// End method main

//method to get total of inventory

and the class

publicclass Iteminfo

{

privateint itemNum;// item number

private String itemName;// name of the item

privateint itemUnits;// Number of items

privatedouble itemCost;//Cost of items

privatedouble invTotal;//total of inventory

//constructor initializes iteminfo

public Iteminfo(int number, String name,int units,double cost)

{

itemNum = number;

itemName = name;

itemUnits = units;

itemCost = cost;

}

//method to set itemNum

publicvoid setItemNum(int number )

{

itemNum = number;

}//end method

// method to set the itemName

publicvoid setItemName( String name )

{

itemName = name;//store the empName

}//end method

//method to set itemUnits

publicvoid setItemUnits (int units )

{

itemUnits = units;//store empHours

}//end method

//method to set itemCost

publicvoid setItemCost (double cost )

{

itemCost = cost;// store itmRate

}//end method

//method to retrieve itemNum

publicint getItemNum()

{

return itemNum;

}//end method

// method to retrieve itemName

public String getItemName()

{

return itemName;

}//end method

// method to retrieve itemUnits

publicint getItemUnits()

{

return itemUnits;

}//end method

// method to retrieve itemCost

publicdouble getItemCost()

{

return itemCost;

}// end method

//method to calculate total item value

publicdouble getTotal()

{

return itemUnits * itemCost;

}//end method

// Display the item info

publicvoid displayInfo()

{

System.out.printf("The item number is : %s\n", getItemNum() );

System.out.printf("The name of the item is: %s\n", getItemName() );

System.out.printf("There are %s %s's in inventory.\n", getItemUnits(), getItemName() );

System.out.printf("%s's cost $%.2f per unit\n", getItemName(), getItemCost() );

System.out.printf("The total value of %s's in inventory is $%.2f\n", getItemName(), getTotal() );

System.out.println();

System.out.println();

}//end method displayInfo

}//end class empinfo

Rgds,

Paul

[8720 byte] By [PaulDoca] at [2007-11-26 20:18:31]
# 1

> Is it possible to have a seperate method within the main method?

> I have created an array that I am trying to total. I can total the items just fine by

> using item[0] + item[1] + item[2], but the assignment calls for using a method to total

> the items.

You can have a totalling method, but it must be alongside, not within the main()

method. Like this:import java.util.Scanner; // Calls scanner for input

public class Inventory

{

// Begin main method

public static void main( String args[] )

{

int i;

// not needed

//double invTotal;

//Instantiate Iteminfo array

Iteminfo[] myItem = new Iteminfo[3];

// etc

System.out.printf("The total of all inventory is: $%.2f. \n\n", getInvTotal());

}

static double getInvTotal(Iteminfo[] items)

{

// go through the array calling getTotal() for

// each element, add them up and return the total

}

}

pbrockway2a at 2007-7-10 0:42:16 > top of Java-index,Java Essentials,New To Java...
# 2

Thank you, that makes sense. However, I cannot print getInvTotal before it's executed right? Here is what I have now and the error is...

D:\School\IT315\Programs\Inventory.java:66: <identifier> expected

System.out.printf( "The total of all inventory is: $%.2f. \n\n", getInvTotal() );

with the pointer at the open paren after printf...Here is the code. Thanks again for your help...

import java.util.Scanner; // Calls scanner for input

public class Inventory

{

// Begin main method

public static void main( String args[] )

{

int i;

double invTotal;

//Instantiate Iteminfo array

Iteminfo[] myItem = new Iteminfo[3];

// Create Item Info object

myItem[0] = new Iteminfo( 01, "Laptop", 15, 2000 );

myItem[1] = new Iteminfo( 02, "Desktop", 25, 1500 );

myItem[2] = new Iteminfo( 03, "Server", 10, 5000 );

// Display Program title and description.

System.out.println( "Inventory Program Part 2" );

System.out.println(); //outputs blank line

System.out.println();

// Display item info

System.out.printf( "The first item is: \n\n" );

myItem[0].displayInfo();

System.out.println();

System.out.printf( "The second item is: \n\n" );

myItem[1].displayInfo();

System.out.println();

System.out.printf( "The third item is: \n\n" );

myItem[2].displayInfo();

System.out.println();

} // End method main

//method to total inventory

static double getInvTotal(Iteminfo[] myItem)

{

return myItem[0].getTotal() + myItem[1].getTotal() + myItem[2].getTotal();

} //end method

System.out.printf( "The total of all inventory is: $%.2f. \n\n", getInvTotal() );

} // End class Payroll

PaulDoca at 2007-7-10 0:42:16 > top of Java-index,Java Essentials,New To Java...
# 3

That's because that particular line of code isn't inside of any method.

You probably want it as the very last line of main().

Exactly as pbrockway2 showed you.

Do you understand the difference between defining a method and invoking a method?

Message was edited by:

paulcw

paulcwa at 2007-7-10 0:42:16 > top of Java-index,Java Essentials,New To Java...
# 4

package forums;

/**

* Immutable inventory-item data class.

*

* Instances of this Item class are read-only-after-construction, so they're thread safe.

*

* see also: thread safe design:

*http://www-128.ibm.com/developerworks/java/library/j-jtp09263.html

* see also: suns concurrency tutorial:

*http://java.sun.com/docs/books/tutorial/essential/concurrency/

*/

class Item

{

// the next integer value

private static volatile int nextId;

private intid;// item identifier

private String name; // name of the item

private intunits; // number of these in stock

private double cost; // our purchase price

//privatise the default no-arg constructor, so can't create an empty Item.

private Item() {

}

/**

* all-args constructor

* @param String name

* @param int units

* @param double cost

* the id attribute is assigned a serial number automatically.

*/

public Item(String name, int units, double cost) {

this.setId(this.getNextId());

this.setName(name);

this.setUnits(units);

this.setCost(cost);

}

/**

* gets the name of this item in English

*/

public int getId() { return this.id; }

private void setId(int id) { this.id = id; }

//private getNextId()

private synchronized int getNextId() {

return ++nextId;

}

/**

* gets the name of this item in English

*/

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

private void setName(String name) { this.name = name; }

/**

* gets the number of units of this utim currently held in stock

*/

public int getUnits() { return this.units; }

private void setUnits(int units) { this.units = units; }

/**

* gets our purchase price of this item as double in Australian Dollars (AUD)

*/

public double getCost() { return this.cost; }

private void setCost ( double cost ) { this.cost = cost; }

/**

* returns total item value

*/

public double getTotal() {

return this.getCost() * this.getUnits();

}

/**

* returns this items details as a String

*/

public String toString() {

return "id="+ qq( ""+this.getId() )

+" name=" + qq( this.getName() )

+" units=" + qq( ""+this.getUnits() )

+" cost=" + qq( String.format("%.2f",this.getCost()) )

+" total=" + qq( String.format("$%.2f",this.getTotal()) )

;

}

// Double Quotes s

private static String qq(String s) {

return "\""+s+"\"";

}

}

/**

* Inventory - Item manager class

*/

public class Inventory

{

public static void main( String args[] ) {

//populate inventory items array

Item[] items = {

new Item("Laptop", 15, 2000)

, new Item("Desktop", 25, 1500)

, new Item("Server", 10, 5000)

};

//display inventory items

System.out.println( "Inventory:" );

double totalCost = 0.0;

for(Item item : items) {

System.out.printf("Item "+item+"\n");

totalCost += item.getCost();

}

System.out.printf("The total of all inventory is: $%.2f%n", totalCost);

}

}

Message was edited by: corlettk

corlettka at 2007-7-10 0:42:16 > top of Java-index,Java Essentials,New To Java...
# 5

Thank You. This is an interesting (and better) approach. I will have to analyze this as it is so different from my own approach, and I can barely get my head around that!

I have moved on to the next issue, but I will open a new thread for that.

Thanks again for eveyone's help, I would not be making it through this class without this site.

Rgds,

Paul

PaulDoca at 2007-7-10 0:42:17 > top of Java-index,Java Essentials,New To Java...