3 Replies - 306 Views - Last Post: 01 July 2019 - 05:15 AM Rate Topic: ***-- 2 Votes

#1 smul   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-June 19

Is it possible to ask for user input from a method?

Posted 28 June 2019 - 02:22 PM

Im doing an assignment which is a simple simulation of a pizza order where the user chooses between 3 sizes, 3 kinds of toppings and number of toppings. We are supposed to allow for any letter case for the size and topping for example small, SMALL or sMAlL. I would like to give an error message if the user enters an invalid size and then ask for a new input. There's no problem to do it from the main class but I want it to be handled in my separate Pizza class if that is possible?

The main class ask for the input of size then it goes to my separate Pizza class:

This is my calculation of cost:
	public double calcCost() {
		a=getSize();
		String A=a.toUpperCase();
		int price=0;
			if(A.equals("SMALL")) {
				price=10;}
			
			if(A.equals("MEDIUM")) {
				price=15;}
			
			if(A.contentEquals("LARGE")) {
				price=20;}

return price;
}


Can I in this method add this error/ask for new input part if the user enters anything other than small medium or large, or should I create a setter method or anything else, all tips are welcome!

This post has been edited by macosxnerd101: 30 June 2019 - 02:25 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Is it possible to ask for user input from a method?

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15160
  • View blog
  • Posts: 60,694
  • Joined: 12-June 08

Re: Is it possible to ask for user input from a method?

Posted 28 June 2019 - 02:53 PM

Depends what your 'pizza class' looks like.
Was This Post Helpful? 0
  • +
  • -

#3 smul   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-June 19

Re: Is it possible to ask for user input from a method?

Posted 29 June 2019 - 07:09 AM

private String size;
	private int nrtop;
	private String top;
	public String a;
	public double storlek;
	
	public Pizza(String S, String t, int nt) {size=S; top=t; nrtop=nt;}



this si my "pizza class", what I simple do in the main is ask for user input and then create a new Pizza(user input, user input, user input). It is no problem to limit the input in the main. example it would look like this:
while(topping.equals("cheese")==false && topping.equals("pepperonir")==false && topping.equals("ham")==false) {
			System.out.print("Please enter a correct alternative of topping: ");
			topping = input.next();
		}


but I want my class to handle this if it is possible

This post has been edited by macosxnerd101: 30 June 2019 - 02:26 PM
Reason for edit:: Please use code tags

Was This Post Helpful? 0
  • +
  • -

#4 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2906
  • View blog
  • Posts: 11,325
  • Joined: 03-December 12

Re: Is it possible to ask for user input from a method?

Posted 01 July 2019 - 05:15 AM

View Postsmul, on 28 June 2019 - 04:22 PM, said:

There's no problem to do it from the main class but I want it to be handled in my separate Pizza class if that is possible?


It's possible, and extremely easy to implement... However, you don't want to do that. I'll explain: So, currently you are doing a console program, what happens when you want to expand this to use a UI? Or Web? Or mobile? Now that class is trying to take in user input from the console, and you no longer have one. So, now you need to rewrite the class/es to take input from a variety of possible interfaces, when you could have/ should have just passed the value into the class.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1