LabExer2
+ setItemName(String newItemName): void
+ setTotalCost (int quantity, double price): void
+ getItemName(): String
+ getTotalCost(): double
+ readInput(): void
+ writeOutput(): void
Note: The readInput() method will be used to accept user input through the Scanner class. This is done by:
For int: s.nextInt()
For double: s.nextDouble()
You are purchasing 3 bag(s) at 1,745.5 each.
import java.util.Scanner;
public class LabExer2 {
private String itemName;
private double itemPrice;
private int itemQuality;
private double amountDue;
public void setItemName(String newItemName) {
itemName = newItemName;
}
public void setTotalCost(int quantity, double price) {
amountDue = quantity * price;
}
public String getItemName() {
return itemName;
}
public double getTotalCost() {
return amountDue;
}
public void readInput() {
Scanner s = new Scanner(System.in);
System.out.println("Name:");
itemName = s.nextLine();
System.out.println("Item price:");
itemPrice = s.nextDouble();
System.out.println("Item quantity:");
itemQuality = s.nextInt();
}
public void writeOutput() {
System.out.println("You are purchasing " + itemQuality + " " + itemName + "(s) at " + itemPrice + " each.");
}
}
Comments
Leave a comment