I made it this far but I\'m not sure what else this is asking for...Include a me
ID: 3602947 • Letter: I
Question
I made it this far but I'm not sure what else this is asking for...Include a method called productsBuilder that takes no parameters and returns a String. In the method body construct a String (name it productsCatalog) and populate it with items, each of which must be 10 characters long. For any product less than 10 characters, pad the remainder with spaces. You don't need to get the values from the user, just populate the String with 3 product names. For example, product 1 might be "Desk ", product 2 might be "Table ", and product 3 might be "Pen ". All 3 of these products are within the productsCatalog String.
package ecommerceapp;
import java.util.Scanner;
import java.util.Random;
public class ECommerceApp {
public static String productsBuilder()
{
return "Table, Desk, Chair";
}
public static boolean getOrder(String name) //Getting product name from the user
{
return productsBuilder().contains(name); //If found returns true, if not returns false
}
public static double getPrice()
{
Random rand = new Random(); //Getting a random price between 1 and 100
return rand.nextInt((100 - 1 + 1)) +1;
}
public static double getTax(double price)
{
return (price * 10.0 / 100.0); //10% tax of the price
}
public static double getTotal(double price, double tax)
{
return price + tax; //Returns the sum of price and the tax
}
public static void printTotal(double total)
{
System.out.println(String.format("Your sale total is $%.2f",total)); //Prints out the total of the item in the catalog
}
public static void main(String[] args)
{
//Printing the banner
String productsCatalog = productsBuilder();
System.out.print("Enter the product name for order: "); //Getting input from the user
String orderName = new Scanner(System.in).next();
System.out.println("****************************************** " +
"====== Welcome to my eCommerce app! ====== " +
"******************************************");
System.out.println();
if(getOrder(orderName))
{
double price = getPrice();
double tax = getTax(price);
double total_sale = getTotal(price, tax);
printTotal(total_sale);
}
else
{
System.out.println("The product was not found.");
}
}
}
Explanation / Answer
package ecommerceapp;
import java.util.Scanner;
import java.util.Random;
public class ECommerceApp {
public static String productsBuilder()
{
String productsCatalog="Desk ,Table ,Pen ";
return productsCatalog;
}
public static boolean getOrder(String name) //Getting product name from the user
{
return productsBuilder().contains(name); //If found returns true, if not returns false
}
public static double getPrice()
{
Random rand = new Random(); //Getting a random price between 1 and 100
return rand.nextInt((100 - 1 + 1)) +1;
}
public static double getTax(double price)
{
return (price * 10.0 / 100.0); //10% tax of the price
}
public static double getTotal(double price, double tax)
{
return price + tax; //Returns the sum of price and the tax
}
public static void printTotal(double total)
{
System.out.println(String.format("Your sale total is $%.2f",total)); //Prints out the total of the item in the catalog
}
public static void main(String[] args)
{
//Printing the banner
String productsCatalog = productsBuilder();
System.out.print("Enter the product name for order: "); //Getting input from the user
String orderName = new Scanner(System.in).next();
System.out.println("****************************************** " +
"====== Welcome to my eCommerce app! ====== " +
"******************************************");
System.out.println();
if(getOrder(orderName))
{
double price = getPrice();
double tax = getTax(price);
double total_sale = getTotal(price, tax);
printTotal(total_sale);
}
else
{
System.out.println("The product was not found.");
}
}
}
/*
Enter the product name for order: Table
******************************************
====== Welcome to my eCommerce app! ======
******************************************
Your sale total is $64.90
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.