I need help developing a Java program that: Reads input from the keyboard using
ID: 3870340 • Letter: I
Question
I need help developing a Java program that: Reads input from the keyboard using a Scanner Object and its methods, Displays information to the user in various formats, including formatting numbers and, manipulates String objects using various String methods. You need to use methods of Scanner and String class that will allow the user to enter information about two products sold at a Grocery Store and display information about the products. Products will consist of the following data: productName – String, quantity – integer, and unitPrice – double. The program will need to prompt the user to enter the information for two products and store this information in the appropriately named variable.
Explanation / Answer
Note : While displaying the output first time I displayed product info normally.But second time I limited the price value to two Decimal Plces.And Displaying the Product name charaters with spaces.
________________
Products.java
public class Products {
//Declaring instance variables
private String productName;
private int quantity;
private double unitPrice;
//Parameterized constructor
public Products(String productName, int quantity, double unitPrice) {
super();
this.productName = productName;
this.quantity = quantity;
this.unitPrice = unitPrice;
}
//getters and setters
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public double getUnitPrice() {
return unitPrice;
}
public void setUnitPrice(double unitPrice) {
this.unitPrice = unitPrice;
}
}
________________
GroceryStore.java
import java.util.Scanner;
public class GroceryStore {
public static void main(String[] args) {
//Declaring variables
String pname;
int qty;
double price;
//Creating product object array
Products p[] = new Products[2];
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Getting the input entered by the user
for (int i = 0; i < p.length; i++) {
System.out.println("Product#" + (i + 1) + ":");
System.out.print("Enter the Product Name :");
pname = sc.next();
System.out.print("Enter the Quantity :");
qty = sc.nextInt();
System.out.print("Enter the Unit Price :$");
price = sc.nextDouble();
p[i] = new Products(pname, qty, price);
}
//Displaying the Products Info
for (int i = 0; i < p.length; i++) {
System.out.println("Product#" + (i + 1) + ":");
System.out.println("Product Name :" + p[i].getProductName());
System.out.println("Quantity :" + p[i].getQuantity());
System.out.println("Price : $" + p[i].getUnitPrice());
}
//Displaying the Products Info
for (int i = 0; i < p.length; i++) {
System.out.println("Product#" + (i + 1) + ":");
System.out.print("Product Name :");
for (int j = 0; j < p[i].getProductName().length(); j++) {
System.out.print(p[i].getProductName().charAt(j) + " ");
}
System.out.println(" Quantity :" + p[i].getQuantity());
System.out.printf("Price :%.2f $ ", p[i].getUnitPrice());
}
}
}
___________________
Output:
Product#1:
Enter the Product Name :Umbrella
Enter the Quantity :30
Enter the Unit Price :$4.5555
Product#2:
Enter the Product Name :Book
Enter the Quantity :300
Enter the Unit Price :$2.33333
Product#1:
Product Name :Umbrella
Quantity :30
Price : $4.5555
Product#2:
Product Name :Book
Quantity :300
Price : $2.33333
Product#1:
Product Name :U m b r e l l a
Quantity :30
Price :4.56 $
Product#2:
Product Name :B o o k
Quantity :300
Price :2.33 $
____________________Thank YOu
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.