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

Question #309174

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.


1
Expert's answer
2022-03-10T18:07:59-0500


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();
	}
}

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