Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Trying to get this to work...

Here is the main java file

import java.util.*;
import java.io.*;

//Class
public class Project3 {

//Main function
public static void main(String[] args) throws java.io.IOException {

//declaring our variables and array
int choice, numberOfItems;
final double cheeseSteakSub = 3.25;
final double turkeysub = 3.00;
final double frenchFries = 1.50;
final double soda = 1.00;
final double applePie = 1.25;
final double fruit = .75;

//beginning the loop for user to select menu items
do {

//describing the output of the program
System.out.println("\n--Rusty's Sub Shop and Bingo Parlor--\n");
System.out.println("* 1. Cheese Steak Subs $3.25 *\n");
System.out.println("* 2. Turkey Sub $3.00 *\n");
System.out.println("* 3. French Fries $1.50 *\n");
System.out.println("* 4. Soda $1.00 *\n");
System.out.println("* 5. Apple Pie $1.25 *\n");
System.out.println("* 6. Fruit $ .75 *\n");
System.out.println("* 7. Finished Ordering *\n");
System.out.println("------------\n");
System.out.print("Choose an item from the menu above: ");

//setting our input for use
Scanner stdin = new Scanner(System.in);
choice = stdin.nextInt();

//switch to process menu selections from user
switch (choice) {

case 1:

{
System.out.print("\nHow many Cheese Steak Subs would you like to purchase: ");
numberOfItems = stdin.nextInt();
MenuOrder itemTotal[0] = new MenuOrder.calculateItemCost(numberOfItems, cheeseSteakSub);
System.out.printf("\n" + numberOfItems + " Cheese Steak Subs = $%1.2f" ,itemTotal[0]);
System.out.println("");
}

break;

case 2:

{
System.out.print("\nHow many Turkey Subs would you like to purchase: ");
numberOfItems = stdin.nextInt();
MenuOrder itemTotal[1] = new MenuOrder.calculateItemCost(numberOfItems, turkeysub);
System.out.printf("\n" + numberOfItems + " Turkey Subs = $%1.2f" ,itemTotal[1]);
System.out.println("");
}

break;

case 3:

{
System.out.print("\nHow many orders of French Fries would you like to purchase: ");
numberOfItems = stdin.nextInt();
MenuOrder itemTotal[2] = new MenuOrder.calculateItemCost(numberOfItems, frenchFries);
System.out.printf("\n" + numberOfItems + " orders of French Fries = $%1.2f" ,itemTotal[2]);
System.out.println("");
}

break;

case 4:

{
System.out.print("\nHow many Sodas would you like to purchase: ");
numberOfItems = stdin.nextInt();
MenuOrder itemTotal[3] = new MenuOrder.calculateItemCost(numberOfItems, soda);
System.out.printf("\n" + numberOfItems + " sodas = $%1.2f" ,itemTotal[3]);
System.out.println("");
}

break;

case 5:

{
System.out.print("\nHow many Apple Pies would you like to purchase: ");
numberOfItems = stdin.nextInt();
MenuOrder itemTotal[4] = new MenuOrder.calculateItemCost(numberOfItems, applePie);
System.out.printf("\n" + numberOfItems + " Apple Pies = $%1.2f" ,itemTotal[4]);
System.out.println("");
}

break;

case 6:

{
System.out.print("\nHow many pieces of Fruit would you like to purchase: ");
numberOfItems = stdin.nextInt();
MenuOrder itemTotal[5] = new MenuOrder.calculateItemCost(numberOfItems, fruit);
System.out.printf("\n" + numberOfItems + " orders of Fruit = $%1.2f" ,itemTotal[5]);
System.out.println("");
}

break;

case 7:

{

MenuOrder grandTotal = new grandTotal.getTotalOrderCost();

System.out.printf(" Grand total = $%1.2f" ,grandTotal);
System.out.println("");
}

default:

{
System.out.println("\nInvalid Menu item, please select item again.\n");
}

break;

}

}while (choice != 7);
}
}

Here is the class file

public class MenuOrder {

public static double orderSubtotal;
public static double orderTotalCost;
public static double itemTotalCost;
public static double taxes;
public static final double salesTax = .05;

public static double itemTotal[];
itemTotal = new double[7];

public MenuOrder() {

orderTotalCost = 0.0;
orderSubtotal = 0.0;

}

public double calculateItemCost(int itemNumber, double itemCost) {
itemTotalCost += itemNumber * itemCost;
return itemTotalCost;
}

public double calculateOrderTotal() {
orderSubtotal = (itemTotal[0] + itemTotal[1] + itemTotal[2] + itemTotal[3] + itemTotal[4] + itemTotal[5]);
taxes = orderSubtotal * salesTax;
orderTotalCost = orderSubtotal + taxes;
return orderTotalCost;
}

public double getTotalOrderCost() {
return orderTotalCost;
}
}

Gettting many errors and everytime I try to fix an error I get more errors or different errors, can someone give me a good direction to go in or help

L:\College Info\CMIS141\Projects\Project3.java:54: ']' expected
MenuOrder itemTotal[0] = new MenuOrder.calculateItemCost(numberOfItems, cheeseSteakSub);
^
L:\College Info\CMIS141\Projects\Project3.java:66: ']' expected
MenuOrder itemTotal[1] = new MenuOrder.calculateItemCost(numberOfItems, turkeysub);
^
L:\College Info\CMIS141\Projects\Project3.java:78: ']' expected
MenuOrder itemTotal[2] = new MenuOrder.calculateItemCost(numberOfItems, frenchFries);
^
L:\College Info\CMIS141\Projects\Project3.java:90: ']' expected
MenuOrder itemTotal[3] = new MenuOrder.calculateItemCost(numberOfItems, soda);
^
L:\College Info\CMIS141\Projects\Project3.java:102: ']' expected
MenuOrder itemTotal[4] = new MenuOrder.calculateItemCost(numberOfItems, applePie);
^
L:\College Info\CMIS141\Projects\Project3.java:114: ']' expected
MenuOrder itemTotal[5] = new MenuOrder.calculateItemCost(numberOfItems, fruit);
^
6 errors

Tool completed with exit code 1
[7365 byte] By [Zahrber] at [2007-11-11 8:35:36]
# 1 Re: Trying to get this to work...
Your statements would make sense if you had an array of MenuOrder elements named itemTotal created in your Project3 class file. It appears that you are trying to access the array which is a static data member of the MenuOrder class. In order to access that field, try using MenuOrder.itemTotal[0], etc. If this does not work, provide setter methods which will put the value into the appropriate index location in the array.
nspils at 2007-11-11 22:34:54 >
# 2 Re: Trying to get this to work...
I had my array in my Project3 file at first but never got past the error I get now.

import java.util.*;
import java.io.*;

//Class
public class Project3 {

//Main function
public static void main(String[] args) throws java.io.IOException {

//declaring our variables and array
int choice, numberOfItems;
final double cheeseSteakSub = 3.25;
final double turkeysub = 3.00;
final double frenchFries = 1.50;
final double soda = 1.00;
final double applePie = 1.25;
final double fruit = .75;
double itemTotal[];
itemTotal = new double[7];

//beginning the loop for user to select menu items
do {

//describing the output of the program
System.out.println("\n--Rusty's Sub Shop and Bingo Parlor--\n");
System.out.println("* 1. Cheese Steak Subs $3.25 *\n");
System.out.println("* 2. Turkey Sub $3.00 *\n");
System.out.println("* 3. French Fries $1.50 *\n");
System.out.println("* 4. Soda $1.00 *\n");
System.out.println("* 5. Apple Pie $1.25 *\n");
System.out.println("* 6. Fruit $ .75 *\n");
System.out.println("* 7. Finished Ordering *\n");
System.out.println("------------\n");
System.out.print("Choose an item from the menu above: ");

//setting our input for use
Scanner stdin = new Scanner(System.in);
choice = stdin.nextInt();

//switch to process menu selections from user
switch (choice) {

case 1:

{
System.out.print("\nHow many Cheese Steak Subs would you like to purchase: ");
numberOfItems = stdin.nextInt();
MenuOrder itemTotal[] = new MenuOrder.calculateItemCost(numberOfItems, cheeseSteakSub);
System.out.printf("\n" + numberOfItems + " Cheese Steak Subs = $%1.2f" ,itemTotal[0]);
System.out.println("");
}

break;

case 2:

{
System.out.print("\nHow many Turkey Subs would you like to purchase: ");
numberOfItems = stdin.nextInt();
MenuOrder itemTotal[] = new MenuOrder.calculateItemCost(numberOfItems, turkeysub);
System.out.printf("\n" + numberOfItems + " Turkey Subs = $%1.2f" ,itemTotal[1]);
System.out.println("");
}

break;

case 3:

{
System.out.print("\nHow many orders of French Fries would you like to purchase: ");
numberOfItems = stdin.nextInt();
MenuOrder itemTotal[] = new MenuOrder.calculateItemCost(numberOfItems, frenchFries);
System.out.printf("\n" + numberOfItems + " orders of French Fries = $%1.2f" ,itemTotal[2]);
System.out.println("");
}

break;

case 4:

{
System.out.print("\nHow many Sodas would you like to purchase: ");
numberOfItems = stdin.nextInt();
MenuOrder itemTotal[] = new MenuOrder.calculateItemCost(numberOfItems, soda);
System.out.printf("\n" + numberOfItems + " sodas = $%1.2f" ,itemTotal[3]);
System.out.println("");
}

break;

case 5:

{
System.out.print("\nHow many Apple Pies would you like to purchase: ");
numberOfItems = stdin.nextInt();
MenuOrder itemTotal[] = new MenuOrder.calculateItemCost(numberOfItems, applePie);
System.out.printf("\n" + numberOfItems + " Apple Pies = $%1.2f" ,itemTotal[4]);
System.out.println("");
}

break;

case 6:

{
System.out.print("\nHow many pieces of Fruit would you like to purchase: ");
numberOfItems = stdin.nextInt();
MenuOrder itemTotal[] = new MenuOrder.calculateItemCost(numberOfItems, fruit);
System.out.printf("\n" + numberOfItems + " orders of Fruit = $%1.2f" ,itemTotal[5]);
System.out.println("");
}

break;

case 7:

{

MenuOrder grandTotal = new grandTotal.getTotalOrderCost();

System.out.printf(" Grand total = $%1.2f" ,grandTotal);
System.out.println("");
}

default:

{
System.out.println("\nInvalid Menu item, please select item again.\n");
}

break;

}

}while (choice != 7);
}
}

public class MenuOrder {

public static double orderSubtotal;
public static double orderTotalCost;
public static double itemTotalCost;
public static double taxes;
public static final double salesTax = .05;

public MenuOrder() {

}

public double calculateItemCost(int itemNumber, double itemCost) {
itemTotalCost += itemNumber * itemCost;
return itemTotalCost;
}

public double calculateOrderTotal() {
orderSubtotal = (itemTotal[0] + itemTotal[1] + itemTotal[2] + itemTotal[3] + itemTotal[4] + itemTotal[5]);
taxes = orderSubtotal * salesTax;
orderTotalCost = orderSubtotal + taxes;
return orderTotalCost;
}

public double getTotalOrderCost() {
return orderTotalCost;
}
}

Then I got these errors

.\MenuOrder.java:17: <identifier> expected
itemTotal = new double[7];
^
.\MenuOrder.java:17: cannot find symbol
symbol : class itemTotal
location: class MenuOrder
itemTotal = new double[7];
^
L:\College Info\CMIS141\Projects\Project3.java:57: itemTotal is already defined in main(java.lang.String[])
MenuOrder itemTotal[] = new MenuOrder.calculateItemCost(numberOfItems, cheeseSteakSub);
^
L:\College Info\CMIS141\Projects\Project3.java:57: cannot find symbol
symbol : class calculateItemCost
location: class MenuOrder
MenuOrder itemTotal[] = new MenuOrder.calculateItemCost(numberOfItems, cheeseSteakSub);
^
L:\College Info\CMIS141\Projects\Project3.java:69: itemTotal is already defined in main(java.lang.String[])
MenuOrder itemTotal[] = new MenuOrder.calculateItemCost(numberOfItems, turkeysub);
^
L:\College Info\CMIS141\Projects\Project3.java:69: cannot find symbol
symbol : class calculateItemCost
location: class MenuOrder
MenuOrder itemTotal[] = new MenuOrder.calculateItemCost(numberOfItems, turkeysub);
^
L:\College Info\CMIS141\Projects\Project3.java:81: itemTotal is already defined in main(java.lang.String[])
MenuOrder itemTotal[] = new MenuOrder.calculateItemCost(numberOfItems, frenchFries);
^
L:\College Info\CMIS141\Projects\Project3.java:81: cannot find symbol
symbol : class calculateItemCost
location: class MenuOrder
MenuOrder itemTotal[] = new MenuOrder.calculateItemCost(numberOfItems, frenchFries);
^
L:\College Info\CMIS141\Projects\Project3.java:93: itemTotal is already defined in main(java.lang.String[])
MenuOrder itemTotal[] = new MenuOrder.calculateItemCost(numberOfItems, soda);
^
L:\College Info\CMIS141\Projects\Project3.java:93: cannot find symbol
symbol : class calculateItemCost
location: class MenuOrder
MenuOrder itemTotal[] = new MenuOrder.calculateItemCost(numberOfItems, soda);
^
L:\College Info\CMIS141\Projects\Project3.java:105: itemTotal is already defined in main(java.lang.String[])
MenuOrder itemTotal[] = new MenuOrder.calculateItemCost(numberOfItems, applePie);
^
L:\College Info\CMIS141\Projects\Project3.java:105: cannot find symbol
symbol : class calculateItemCost
location: class MenuOrder
MenuOrder itemTotal[] = new MenuOrder.calculateItemCost(numberOfItems, applePie);
^
L:\College Info\CMIS141\Projects\Project3.java:117: itemTotal is already defined in main(java.lang.String[])
MenuOrder itemTotal[] = new MenuOrder.calculateItemCost(numberOfItems, fruit);
^
L:\College Info\CMIS141\Projects\Project3.java:117: cannot find symbol
symbol : class calculateItemCost
location: class MenuOrder
MenuOrder itemTotal[] = new MenuOrder.calculateItemCost(numberOfItems, fruit);
^
L:\College Info\CMIS141\Projects\Project3.java:128: package grandTotal does not exist
MenuOrder grandTotal = new grandTotal.getTotalOrderCost();
^
15 errors

Tool completed with exit code 1
Zahrber at 2007-11-11 22:35:59 >
# 3 Re: Trying to get this to work...
You've declared itemTotal[] as both an array of doubles and MenuOrders.

Have you tried to call the array by using MenuOrder.itemTotal[i]? Notice the "dot" between MenuOrder and itemTotal[i], rather than a space? Same with your grandTotal field ...
nspils at 2007-11-11 22:36:52 >
# 4 Re: Trying to get this to work...
I have managed to get to this point so far

import java.util.*;
import java.io.*;

//Class
public class Project3 {

//Main function
public static void main(String[] args) throws java.io.IOException {

//declaring our variables and array
int choice, numberOfItems;
final double cheeseSteakSub = 3.25;
final double turkeysub = 3.00;
final double frenchFries = 1.50;
final double soda = 1.00;
final double applePie = 1.25;
final double fruit = .75;

//beginning the loop for user to select menu items
do {

//describing the output of the program
System.out.println("\n--Rusty's Sub Shop and Bingo Parlor--\n");
System.out.println("* 1. Cheese Steak Subs $3.25 *\n");
System.out.println("* 2. Turkey Sub $3.00 *\n");
System.out.println("* 3. French Fries $1.50 *\n");
System.out.println("* 4. Soda $1.00 *\n");
System.out.println("* 5. Apple Pie $1.25 *\n");
System.out.println("* 6. Fruit $ .75 *\n");
System.out.println("* 7. Finished Ordering *\n");
System.out.println("------------\n");
System.out.print("Choose an item from the menu above: ");

//setting our input for use
Scanner stdin = new Scanner(System.in);
choice = stdin.nextInt();

//switch to process menu selections from user
switch (choice) {

case 1:

{
System.out.print("\nHow many Cheese Steak Subs would you like to purchase: ");
numberOfItems = stdin.nextInt();
MenuOrder item1 = new MenuOrder.calculateItemCost(numberOfItems, cheeseSteakSub);
System.out.printf("\n" + numberOfItems + " Cheese Steak Subs = $%1.2f" ,MenuOrder.itemTotal[0]);
System.out.println("");
}

break;

case 2:

{
System.out.print("\nHow many Turkey Subs would you like to purchase: ");
numberOfItems = stdin.nextInt();
MenuOrder item2 = new MenuOrder.calculateItemCost(numberOfItems, turkeysub);
System.out.printf("\n" + numberOfItems + " Turkey Subs = $%1.2f" ,MenuOrder.itemTotal[1]);
System.out.println("");
}

break;

case 3:

{
System.out.print("\nHow many orders of French Fries would you like to purchase: ");
numberOfItems = stdin.nextInt();
MenuOrder item3 = new MenuOrder.calculateItemCost(numberOfItems, frenchFries);
System.out.printf("\n" + numberOfItems + " orders of French Fries = $%1.2f" ,MenuOrder.itemTotal[2]);
System.out.println("");
}

break;

case 4:

{
System.out.print("\nHow many Sodas would you like to purchase: ");
numberOfItems = stdin.nextInt();
MenuOrder item4 = new MenuOrder.calculateItemCost(numberOfItems, soda);
System.out.printf("\n" + numberOfItems + " sodas = $%1.2f" ,MenuOrder.itemTotal[3]);
System.out.println("");
}

break;

case 5:

{
System.out.print("\nHow many Apple Pies would you like to purchase: ");
numberOfItems = stdin.nextInt();
MenuOrder item5 = new MenuOrder.calculateItemCost(numberOfItems, applePie);
System.out.printf("\n" + numberOfItems + " Apple Pies = $%1.2f" ,MenuOrder.itemTotal[4]);
System.out.println("");
}

break;

case 6:

{
System.out.print("\nHow many pieces of Fruit would you like to purchase: ");
numberOfItems = stdin.nextInt();
MenuOrder item6 = new MenuOrder.calculateItemCost(numberOfItems, fruit);
System.out.printf("\n" + numberOfItems + " orders of Fruit = $%1.2f" ,MenuOrder.itemTotal[5]);
System.out.println("");
}

break;

case 7:

{

System.out.printf(" Grand total = $%1.2f" ,MenuOrder.getTotalOrderCost());
System.out.println("");
}

default:

{
System.out.println("\nInvalid Menu item, please select item again.\n");
}

break;

}

}while (choice != 7);
}
}

public class MenuOrder {

public static double orderSubtotal;
public static double orderTotalCost;
public static double itemTotalCost;
public static double taxes;
public static final double salesTax = .05;

public static double itemTotal[] = new double[7];

public MenuOrder() {

}

public static double calculateItemCost(int itemNumber, double itemCost) {
itemTotalCost += itemNumber * itemCost;
return itemTotalCost;
}

public static double calculateOrderTotal() {
orderSubtotal = itemTotal[0] + itemTotal[1] + itemTotal[2] + itemTotal[3] + itemTotal[4] + itemTotal[5];
taxes = orderSubtotal * salesTax;
orderTotalCost = orderSubtotal + taxes;
return orderTotalCost;
}

public static double getTotalOrderCost() {
return orderTotalCost;
}
}

only have 6 erros left so far until more come up after next change but these have to do with the same problem

L:\College Info\CMIS141\Projects\Project3.java:54: cannot find symbol
symbol : class calculateItemCost
location: class MenuOrder
MenuOrder item1 = new MenuOrder.calculateItemCost(numberOfItems, cheeseSteakSub);
^
L:\College Info\CMIS141\Projects\Project3.java:66: cannot find symbol
symbol : class calculateItemCost
location: class MenuOrder
MenuOrder item2 = new MenuOrder.calculateItemCost(numberOfItems, turkeysub);
^
L:\College Info\CMIS141\Projects\Project3.java:78: cannot find symbol
symbol : class calculateItemCost
location: class MenuOrder
MenuOrder item3 = new MenuOrder.calculateItemCost(numberOfItems, frenchFries);
^
L:\College Info\CMIS141\Projects\Project3.java:90: cannot find symbol
symbol : class calculateItemCost
location: class MenuOrder
MenuOrder item4 = new MenuOrder.calculateItemCost(numberOfItems, soda);
^
L:\College Info\CMIS141\Projects\Project3.java:102: cannot find symbol
symbol : class calculateItemCost
location: class MenuOrder
MenuOrder item5 = new MenuOrder.calculateItemCost(numberOfItems, applePie);
^
L:\College Info\CMIS141\Projects\Project3.java:114: cannot find symbol
symbol : class calculateItemCost
location: class MenuOrder
MenuOrder item6 = new MenuOrder.calculateItemCost(numberOfItems, fruit);
^
6 errors

Tool completed with exit code 1
Zahrber at 2007-11-11 22:38:02 >
# 5 Re: Trying to get this to work...
Let's take a look at this line of code:

MenuOrder item1 = new MenuOrder.calculateItemCost(numberOfItems, cheeseSteakSub);

The difficulty you are getting now is that your code is telling the compiler that you are trying to construct an instance of class MenuOrder, called item1, by running a static method of the class which returns a double, not an instance of the class.

It appears to me that you have your interaction of the two classes mixed up and you do not have a clear idea of what you are trying to accomplish. I think that if I were writing this, I would not make everything in MenuOrder a static method or field. I would create a new instance for every order, then my code would use the methods exposed by that instance as selections are made by that customer (or table) - each order would have its own itemTotal[] array and would calculate its own totals.

Makes sense to me ... how about you?
nspils at 2007-11-11 22:38:56 >
# 6 Re: Trying to get this to work...
In the line of code MenuOrder item1 = new MenuOrder.calculateItemCost(numberOfItems, cheeseSteakSub); I am creating an object item1 from class MenuOrder to calculate the cost of each item selected then storing the total cost in an array position ten at the end total the array to get grandtotal

I made them static because if I don't make the methods static I get the errors non-static variable xxxxxxx cannot be referenced from a static context, so making them static is the only way I can get this to pass
Zahrber at 2007-11-11 22:40:00 >
# 7 Re: Trying to get this to work...
You cannot construct an instance of the class in the manner you are trying. I think this is why you were getting your "calling a non-static method in a static environment" error - you were not constructing an instance of the class.

First, construct an instance of MenuOrder called table3, for the customers sitting at table3:

MenuOrder table3 = new MenuOrder();

Then take table3's orders. As you enter the orders, let's say for CheeseSteak Subs, assign a value for numberOfItems, then call calculateItemCost like this:

table3.itemTotal[1] = table3.calculateItemCost(numberOfItems, 4.95);

Once your customers have entered their orders, now calculate the total for table3's order by summing the values held in each element of the array, perform sales tax calculation (if applicable) and spit out the info.
nspils at 2007-11-11 22:40:58 >
# 8 Re: Trying to get this to work...
OK thanks I got it working, you don't know how much I appreciate the help. Even after reading the textbook I was still not seeing my errors I was so frustrated.
Zahrber at 2007-11-11 22:42:06 >
# 9 Re: Trying to get this to work...
That's the benefit of a forum like this. Glad you got it to work.
nspils at 2007-11-11 22:43:04 >