Solved

Java method returns to other methods

Posted on 2011-09-30
7
209 Views
Last Modified: 2012-05-12
Dear Experts,

I need some help with my project.  My code works a little, but I can't get the methods to returns what I required.  I have to fix my methods so only the orderNum and amount is returned, but I'm stuck.  I can't figure out how to make it work.  When I call my methods it prints the string again.  Once I get it to work I will code the other menu items.


Welcome to Walter's Sushi Shop.  Our menu is in English and our food is delicious.

1    Tuna (Maguro)                 5.45  
2    Kappa Maki (cucumber)         7.65  
3    Sake (Salmon)                 4.49  
4    Tako (Octopu)                 7.79  
5    Oolong Tea                    0.95  

Enter 1, 2, 3, 4, or 5.
1

Enter the amount required.
3

Enter 1, 2, 3, 4, or 5.
1

Enter the amount required.
3
You ordered 3 Tuna (Maguro)

Enter the amount required.
3
$15.00
Enter the amount required.
3

The tax is: 1.05
 
import java.util.Scanner;

public class Assignment3 {

	
	public static void main(String[] args) {

   		// Create a Scanner object
		Scanner input = new Scanner (System.in);
   	
	  printWelcome();
   	  printMenu(itemNumber, itemName, itemPrice);
   	  getOrderNum(0);
   	  //checkOrderNum();
   	  getAmount();
	  printsubAndTotal();  
	  
 } // ends main
	   
	public static final double P_TUNA = 5.00; 
	public static final double P_MAKI = 4.50;
	public static final double P_SAKE = 3.50; 
	public static final double P_TAKO = 3.00; 
	public static final double P_TEA = 2.00;
	   
	public static final double TAXRATE = 0.07; 
	   
	static  int[] itemNumber = {1, 2, 3, 4, 5};
    static String[] itemName = {"Tuna (Maguro)","Kappa Maki (cucumber)","Sake (Salmon)","Tako (Octopu)", "Oolong Tea"};
    static double[] itemPrice = {5.45, 7.65, 4.49, 7.79, 0.95};	

    static void printMenu(int[] itemNumber, String[] itemName, double[]itemPrice){
     	   for(int i = 0;i < itemNumber.length;i++) 
     		System.out.printf("%-5s%-30s%-6s\n", itemNumber[i], itemName[i], itemPrice[i]);
     }	// ends printMenu     
      
      
    static void printWelcome() {
	    System.out.println("Welcome to Walter's Sushi Shop.  "
		 	+ "Our menu is in English and our food is delicious.\n");

    } // ends printWelcome

	static int getOrderNum(int order1){
    Scanner input = new Scanner (System.in);
    	System.out.println("\nEnter 1, 2, 3, 4, or 5.");
		 int order = input.nextInt();
		  return order;	
	} // ends getOrderNum
	
	//static void checkOrderNum(){
 	//	if(order < 1 || order > 5){
	//	  		System.out.println("You entered " + order1 +
	//			" which is not a valid order number.");
	// 	} // ends if
			
//	} // ends checkOrderNum	
	
	
	 static int getAmount(){
	    Scanner input = new Scanner (System.in);
		
			System.out.println("\nEnter the amount required.");
			  int amount1 = input.nextInt();
			  if(amount1 < 1 || amount1 > 5){
				  	System.out.println("You entered " + amount1 +
					" please order between 1 and 5 of each item.");
			  } // ends if

			return amount1;	
		} // ends getAmount	

	static void printsubAndTotal(){
		
		if(getOrderNum(0) == 1 ){
			System.out.println("You ordered " + getAmount() +" "+ itemName[0]);
			//System.out.printf("\nPrice before tax: "+ P_TUNA * getAmount());		
			System.out.printf("$%4.2f",P_TUNA * getAmount());
			System.out.printf("\nThe tax is: " + P_TUNA * getAmount() * TAXRATE);
		
		} // ends if
				
	}	// ends printsubAndTotal
	} // ends Assignment3

Open in new window

0
Comment
Question by:WalterAPO
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
7 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36895393
So the output above - is what you want to see?

Not how it is working now yet?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36895398
import java.util.Scanner;

public class Assignment3 {


	public static void main(String[] args) {

   		// Create a Scanner object
		Scanner input = new Scanner (System.in);

	  printWelcome();
   	  printMenu(itemNumber, itemName, itemPrice);
   	 int j = getOrderNum(0);
   	  //checkOrderNum();
   	 int am=  getAmount();
	  printsubAndTotal(j, am);

 } // ends main

	public static final double P_TUNA = 5.00;
	public static final double P_MAKI = 4.50;
	public static final double P_SAKE = 3.50;
	public static final double P_TAKO = 3.00;
	public static final double P_TEA = 2.00;

	public static final double TAXRATE = 0.07;

	static  int[] itemNumber = {1, 2, 3, 4, 5};
    static String[] itemName = {"Tuna (Maguro)","Kappa Maki (cucumber)","Sake (Salmon)","Tako (Octopu)", "Oolong Tea"};
    static double[] itemPrice = {5.45, 7.65, 4.49, 7.79, 0.95};

    static void printMenu(int[] itemNumber, String[] itemName, double[]itemPrice){
     	   for(int i = 0;i < itemNumber.length;i++)
     		System.out.printf("%-5s%-30s%-6s\n", itemNumber[i], itemName[i], itemPrice[i]);
     }	// ends printMenu


    static void printWelcome() {
	    System.out.println("Welcome to Walter's Sushi Shop.  "
		 	+ "Our menu is in English and our food is delicious.\n");

    } // ends printWelcome

	static int getOrderNum(int order1){
    Scanner input = new Scanner (System.in);
    	System.out.println("\nEnter 1, 2, 3, 4, or 5.");
		 int order = input.nextInt();
		  return order;	
	} // ends getOrderNum

	//static void checkOrderNum(){
 	//	if(order < 1 || order > 5){
	//	  		System.out.println("You entered " + order1 +
	//			" which is not a valid order number.");
	// 	} // ends if

//	} // ends checkOrderNum


	 static int getAmount(){
	    Scanner input = new Scanner (System.in);

			System.out.println("\nEnter the amount required.");
			  int amount1 = input.nextInt();
			  if(amount1 < 1 || amount1 > 5){
				  	System.out.println("You entered " + amount1 +
					" please order between 1 and 5 of each item.");
			  } // ends if

			return amount1;
		} // ends getAmount

	static void printsubAndTotal(int j, int am){

		if(j == 1 ){
			System.out.println("You ordered " + am +" "+ itemName[0]);
			//System.out.printf("\nPrice before tax: "+ P_TUNA * getAmount());
			System.out.printf("$%4.2f",P_TUNA * am);
			System.out.printf("\nThe tax is: " + P_TUNA * am * TAXRATE);

		} // ends if

	}	// ends printsubAndTotal
	} // ends Assignment3

Open in new window


Output:
Welcome to Walter's Sushi Shop.  Our menu is in English and our food is delicious.

1    Tuna (Maguro)                 5.45  
2    Kappa Maki (cucumber)         7.65  
3    Sake (Salmon)                 4.49  
4    Tako (Octopu)                 7.79  
5    Oolong Tea                    0.95  

Enter 1, 2, 3, 4, or 5.
1

Enter the amount required.
3
You ordered 3 Tuna (Maguro)
$15.00
The tax is: 1.05
Process finished with exit code 0

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36895402

These are the changes:
   	 int j = getOrderNum(0);
   	  //checkOrderNum();
   	 int am=  getAmount();
	  printsubAndTotal(j, am);

Open in new window


	static void printsubAndTotal(int j, int am){

		if(j == 1 ){
			System.out.println("You ordered " + am +" "+ itemName[0]);
			//System.out.printf("\nPrice before tax: "+ P_TUNA * getAmount());
			System.out.printf("$%4.2f",P_TUNA * am);
			System.out.printf("\nThe tax is: " + P_TUNA * am * TAXRATE);

		} // ends if

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Expert Comment

by:for_yan
ID: 36895405
your methods getOrderNum(int) and getAmount() are interactive methods which ask the questions
and they should return value - the result which user enetered.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36895410
You correctly specified that they should return integer - but in your calling code you should
get these returtned numbers and
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36895413
and then feed them as parameters to the printing method. You should feed only the results of te methods (the numbers) to
the printing method, instead of putiing the actual method in there, becaue every time java encounters getAmount() it will
go back and start excuting this method - so it wil again ask question of the user; in fact at this point it should be already
parameter captured in previous questjion, rather than once again call of the method.

Let me know if this is understandable.
0
 

Author Closing Comment

by:WalterAPO
ID: 36895585
Yan,

Thank you for the explanation of what I wasn't doing correctly.  Your explanation was easy to follow and very complete.  I know that I was returning the complete method and not just the return, but I couldn't figure out how to fix it.  Again, thank you.

Walter
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
eclipse buid path vs tomcat lib path 10 49
maven disable workspace resolution 1 77
ejb entity bean example 2 57
spring maven example issues 3 102
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question