Write each of these methods in the Unit6ProblemSet.java class. 1. Write a method
ID: 3689252 • Letter: W
Question
Write each of these methods in the Unit6ProblemSet.java class.
1. Write a method called bannerPrinter that takes no parameter and has no return value. bannerPrinter outputs a greeting to the command line as shown below. ******************************************
****** Welcome to my eCommerce app! ******
******************************************
(print a blank line after the banner, all * must line up at the end)
2. Write a method called productBuilder that takes no parameters and returns an array of Strings. In the method body construct and return an array of 5 Strings (call the array products) and populate the array with 5 product names. You don't need to get the values from the user, just populate with 5 product names.
3. Write a method called getOrder that takes an array of Strings as a parameter (the products array) and returns a boolean. In the method body, prompt the user to enter a product name (a String), then check whether the product name exists in the array of strings. If it exists, return true, otherwise return false.
4. Write a method called getPrice that returns a double. In the method body generate a random number between 1 and 100. Use Math.random() multiplied by a scaling factor and return this value as the price.
5. Write a method called getTax that takes a double as a parameter (the price) and returns a double. Compute the return value as 10% of the parameter value (i.e. 10% of the price). This is the tax.
6. Write a method called getTotal that takes two doubles as parameters (the price and the tax) and returns a double. The return value is computed as the sum of the two parameters. The return value is the sale total.
7. Write a method called printTotal that takes a double as a parameter (the sale total) and has no return value. This method outputs the following to the command line: "Your sale total is: $X" where X is the sale total parameter value.
8. Call each of the methods above from the main method. The pseudocode for the main method will be as given below.
Print the banner message
Get the product array
Get the product order from the user and check it exists in the product array
If the product exists
Get the product price
Compute the product tax
Compute the total sale
Output the total sale
Else
Output "Product not found."
Explanation / Answer
import java.util.Scanner;
/*
@author SRINIVAS PALLI
*/
public class Unit6ProblemSet {
public static void main(String args[]) {
bannerPrinter();
String[] productEntry = productBuilder();
boolean productRef = getOrder(productEntry);
if (productRef) {
double priceGen = getPrice();
double taxGen = getTax(priceGen);
double totalAmt = getTotal(priceGen, taxGen);
System.out.println("Total Sale Price: $"
+ String.format("%.2f", totalAmt));
} else {
System.out.println("Product not found.");
}
}
// banner message
public static void bannerPrinter() {
System.out.println("******************************************");
System.out.println("****** Welcome to my eCommerce app! ******");
System.out.println("******************************************");
System.out.println(); // instructions requested blank line after banner
}
// array of 5 predefined strings, not from user entry
public static String[] productBuilder() {
String[] products = new String[5];
products[0] = "PS4";
products[1] = "XBOX One";
products[2] = "iPhone 6";
products[3] = "Macbook";
products[4] = "Surface Pro";
return products;
}
// getOrder method
public static boolean getOrder(String[] products) {
String productEntry = "";
Scanner scnr = new Scanner(System.in);
System.out.println("Enter a product name Human: ");
productEntry = scnr.nextLine();
for (int i = 0; i < products.length; i++) {
if (productEntry.equals(products[i])) {
scnr.close();
return true;
}
}
scnr.close();
return false;
}
// get Price method
public static double getPrice() {
double priceGen = Math.random() * 100.0 + 1.0;
return priceGen;
}
// getTax Method
public static double getTax(double priceGen) {
return (priceGen * 0.10);
}
// getTotal Method
public static double getTotal(double priceGen, double taxGen) {
double totalAmt = priceGen + taxGen;
return totalAmt;
}
// printTotal Method
public static void printTotal(double totalAmt) {
System.out.println("Your sale total is: $" + totalAmt);
}
}
OUTPUT 1:
******************************************
****** Welcome to my eCommerce app! ******
******************************************
Enter a product name Human:
Box
Product not found.
OUTPUT 2:
******************************************
****** Welcome to my eCommerce app! ******
******************************************
Enter a product name Human:
PS4
Total Sale Price: $76.62
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.