Solved

Java method returns to other methods

Posted on 2011-09-30
7
201 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
bitbucket vs gitbucket 3 57
micro services spring boot application error 3 146
Java Timer (static) 9 36
tomcat not starting 6 31
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now