Solved

Java method returns to other methods

Posted on 2011-09-30
7
214 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

635 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