Construct a simple purchasing program based on the Requirement Below.
Main.java should only contain the main method and a Reference of Purchase Object. Purchase.java should contain this fields & Methods:
Fields/Instance Variables
-itemName : String
-itemPrice : double
-itemQuantity : int
-amountDue :double
Methods
setItemName(String itemName) : 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.
import java.util.*;
class Purchase {
private String itemName;
private double itemPrice;
private int itemQuantity;
private double amountDue;
public Purchase() {
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public void setTotalCost(int quantity, double price) {
this.itemQuantity = quantity;
this.itemPrice = price;
}
public String getItemName() {
return itemName;
}
public double getTotalCost() {
return this.itemQuantity * this.itemPrice;
}
public void readInput() {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter item name: ");
itemName = keyboard.nextLine();
System.out.print("Enter item price: ");
itemPrice = keyboard.nextDouble();
System.out.print("Enter item quantity: ");
itemQuantity = keyboard.nextInt();
amountDue = getTotalCost();
keyboard.close();
}
public void writeOutput() {
System.out.println("The item name: " + itemName);
System.out.println("The item price: " + itemPrice);
System.out.println("The item quantity: " + itemQuantity);
System.out.println("The item total cost: " + amountDue);
}
}
class App {
public static void main(String[] args) {
Purchase p = new Purchase();
p.readInput();
p.writeOutput();
}
}
Comments
Leave a comment