Solved

Java method returns to other methods

Posted on 2011-09-30
7
198 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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…

762 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

18 Experts available now in Live!

Get 1:1 Help Now