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.
Simple output :
Enter the name of the Item you are Purchasing: Bag
Enter QTY and Price Separated by a space: 3 499.75
You are Purchasing 3 Bag(s) at 499.75 each.
Amount due is 1,499.25
import java.util.Scanner;
public class Purchase {
private String itemName;
private double itemPrice;
private int itemQuantity;
private double amountDue;
public void setItemName(String itemName){
this.itemName = itemName;
}
public void setTotalCost(int quantity, double price){
this.itemQuantity = quantity;
this.itemPrice = price;
this.amountDue = quantity * price;
}
public String getItemName(){
return this.itemName;
}
public double getTotalCost(){
return this.amountDue;
}
public void readInput(){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the name of the Item you are Purchasing: ");
setItemName(scanner.nextLine());
System.out.print("Enter QTY and Price Separated by a space: ");
setTotalCost(scanner.nextInt(),scanner.nextDouble());
}
public void writeOutput(){
System.out.println("You are Purchasing " + this.itemQuantity + " " + getItemName() + "(s) at " + this.itemPrice + " each.");
System.out.println("Amount due is " + getTotalCost());
}
}
public class Main {
public static void main(String args[]){
Purchase purchase = new Purchase();
purchase.readInput();
purchase.writeOutput();
}
}
Comments
Leave a comment