Answer to Question #316619 in Java | JSP | JSF for denz

Question #316619

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



1
Expert's answer
2022-03-23T13:44:46-0400
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();
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS