Create a Java program named PE2Lastname. (ex. PE2Dela_Cruz)
Construct a simple purchasing program based on the UML Class Diagram below.
The (-) symbol represents private variables, while(+) represent public.
PE2Lastname
-itemName: String
-itemPrice:double
-itemQuantity:int
-amountDue:double
+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 doing the following:
a.
Importing Scanner from the java.util package.
b.
Instantiating an object of the Scanner class, Scanner s = new
Scanner(System.in);
c.
Storing the input to the variable name based on data type:
a.
For String: s.nextLine()
b.
For int: s.nextInt()
c.
For double: s.nextDouble()
d.
import java.util.*;
class PE2Lastname {
private String itemName;
private double itemPrice;
private int itemQuantity;
private double amountDue;
public void setItemName(String newItemName) {
this.itemName = newItemName;
}
public void setTotalCost(int quantity, double price) {
this.itemQuantity = quantity;
this.amountDue = price;
}
public String getItemName() {
return itemName;
}
public double getTotalCost() {
return amountDue;
}
public void readInput() {
Scanner s = new Scanner(System.in);
System.out.print("Enter item name: ");
this.itemName = s.nextLine();
System.out.print("Enter item price: ");
this.itemPrice = s.nextDouble();
System.out.print("Enter item quantity: ");
this.itemQuantity = s.nextInt();
this.amountDue = itemPrice * itemQuantity;
s.close();
}
public void writeOutput() {
System.out.println("The item name: " + this.itemName);
System.out.println("The item price: " + this.itemPrice);
System.out.println("The item quantity: " + this.itemQuantity);
System.out.println("The item amount due: " + this.amountDue);
}
}
class App {
public static void main(String[] args) {
PE2Lastname PE2Lastname = new PE2Lastname();
PE2Lastname.readInput();
PE2Lastname.writeOutput();
}
}
Comments
Leave a comment