Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • Last Modified:

Java method returns to other methods

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
WalterAPO
Asked:
WalterAPO
  • 6
1 Solution
 
for_yanCommented:
So the output above - is what you want to see?

Not how it is working now yet?
0
 
for_yanCommented:
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
 
for_yanCommented:

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
for_yanCommented:
your methods getOrderNum(int) and getAmount() are interactive methods which ask the questions
and they should return value - the result which user enetered.
0
 
for_yanCommented:
You correctly specified that they should return integer - but in your calling code you should
get these returtned numbers and
0
 
for_yanCommented:
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
 
WalterAPOAuthor Commented:
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

Independent Software Vendors: 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!

  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now