import java.util.Scanner; /** * * @author Introduction to Java Programming I */
ID: 3555665 • Letter: I
Question
import java.util.Scanner;
/**
*
* @author Introduction to Java Programming I
*/
public class tshirts {
public static int getValidProductId(Scanner input){
int productId;
while(true){
String s = input.next();
try {
productId = Integer.parseInt(s);
if(productId < 0) {
System.out.println ("Please Enter Positive number for Product ID:");
continue;
}
} catch(NumberFormatException e) {
System.out.println ("Please Enter Positive number for Product ID:");
continue;
}
break;
}
return productId;
}
public static double getValidProductPrice(Scanner input){
double productPrice;
while(true){
String s = input.next();
try {
productPrice = Double.parseDouble(s);
if(productPrice < 0) {
System.out.println ("Please Enter valid Positive floating point number for Product Price:");
continue;
}
} catch(NumberFormatException e) {
System.out.println ("Please Enter valid Positive floating point number for Product Price:");
continue;
}
break;
}
return productPrice;
}
public static void main(String[] args) {
// TODO code application logic here
// variables to hold product details
int productId;
String productName;
double productPrice;
/**After getting all of my variables declared,it's on to the new scanner*/
Scanner input=new Scanner(System.in);
/** prompt user to enter the product ID*/
System.out.println ("Please Enter Product ID:");
productId = getValidProductId(input);
System.out.printf ("Product ID : %s ",productId);
/** prompt user to enter product name*/
System.out.println ("Please Enter Product Name:" );
productName = input.next();
/** prompt system to display user entered information for verification*/
System.out.printf ("Product Name: %s ",productName);
/** prompt user to enter Product Price*/
System.out.println ("Please Enter Product Price:");
productPrice = getValidProductPrice(input);
System.out.printf ("Product Price: %s ",productPrice);
/** prompt system to display user entered information for verification*/
System.out.println("---------------------------------");
System.out.println("Product Details:");
System.out.printf ("Product ID : %s ",productId);
System.out.printf ("Product Name: %s ",productName);
System.out.printf ("Product Price: %s ",productPrice);
}
}
Explanation / Answer
/************************** Pseudocode **********************************/
function getValidProductId( input )
while true
set s to input.next;
try
set productId to s
if productId less than 0
print "Please Enter Positive number for Product ID:"
endif
catch e
print "Please Enter Positive number for Product ID:"
continue;
break;
endwhile
return productId;
end_function
function getValidProductPrice( input )
while true
set s to input.next;
try
set productPrice to s
if productPrice less than 0
print "Please Enter valid Positive floating point number for Product Price:"
endif
catch e
print "Please Enter valid Positive floating point number for Product Price:"
continue;
break;
endwhile
return productPrice;
end_function
function main(args)
set input to Scanner(system.in);
print "Please Enter Product ID:"
set productId to getValidProductId(input)
print "Product ID : "+productId
print "Please Enter Product Name:"
set productName to input.next()
print "Product Name: "+productName
print "Please Enter Product Price:"
set productPrice to getValidProductPrice(input)
print "Product Price: "+productPrice
print "---------------------------------"
print "Product Details:"
print "Product ID : "+productId
print "Product Name: "+productName
print "Product Price: "+productPrice
end_function
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.