// Create scanner to obtain input from user Scanner input = new Scanner(System.i
ID: 3865280 • Letter: #
Question
// Create scanner to obtain input from user
Scanner input = new Scanner(System.in);
int ItemsPurchased = 0;
double ItemPrice;
System.out.printf("%n%s", "Enter a part number: ");
String PartNumber = input.nextLine(); // Accepts user input
System.out.printf("%n%s", "Enter a description: ");
String PartDescription = input.nextLine(); // Accepts user input
System.out.printf("%n%s", "Enter quantity purchased: ");
ItemsPurchased = input.nextInt(); // Accepts user input
System.out.printf("%n%s", "Please Enter the item price: ");
How do I get the Input to recieve the ItemPrice which is a double value??
// declare invoice
Invoice invoice = new Invoice(PartNumber, PartDescription, ItemsPurchased, ItemPrice);
Explanation / Answer
Invoice.java
public class Invoice {
private String PartNumber;
private String PartDescription;
private int ItemsPurchased;
private double ItemPrice;
public Invoice(String partNumber, String partDescription, int itemsPurchased,
double itemPrice) {
super();
PartNumber = partNumber;
PartDescription = partDescription;
ItemsPurchased = itemsPurchased;
ItemPrice = itemPrice;
}
public String getPartNumber() {
return PartNumber;
}
public void setPartNumber(String partNumber) {
PartNumber = partNumber;
}
public String getPartDescription() {
return PartDescription;
}
public void setPartDescription(String partDescription) {
PartDescription = partDescription;
}
public int getItemsPurchased() {
return ItemsPurchased;
}
public void setItemsPurchased(int itemsPurchased) {
ItemsPurchased = itemsPurchased;
}
public double getItemPrice() {
return ItemPrice;
}
public void setItemPrice(double itemPrice) {
ItemPrice = itemPrice;
}
@Override
public String toString() {
return "Invoice [PartNumber=" + PartNumber + ", PartDescription=" + PartDescription + ", ItemsPurchased=" + ItemsPurchased + ", ItemPrice=" + ItemPrice + "]";
}
}
__________________________
TestInvoice.java
import java.util.Scanner;
public class TestInvoice {
public static void main(String[] args) {
// Create scanner to obtain input from user
Scanner input = new Scanner(System.in);
int ItemsPurchased = 0;
String PartNumber, PartDescription;
double ItemPrice;
System.out.print("Enter a part number: ");
PartNumber = input.next(); // Accepts user input
input.nextLine();
System.out.print("Enter a description: ");
PartDescription = input.nextLine(); // Accepts user input
System.out.print("Enter quantity purchased: ");
ItemsPurchased = input.nextInt(); // Accepts user input
System.out.print("Enter the item price: ");
ItemPrice = input.nextDouble();
// declare invoice
Invoice invoice = new Invoice(PartNumber, PartDescription, ItemsPurchased, ItemPrice);
System.out.println(invoice.toString());
}
}
_____________________________
Output:
Enter a part number: A123SD
Enter a description: Cooker
Enter quantity purchased: 20
Please Enter the item price: 150
Invoice [PartNumber=A123SD, PartDescription=Cooker, ItemsPurchased=20, ItemPrice=150.0]
_____________Could you rate me well.Plz .Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.