You are on page 1of 3

//FINAL OUTPUT :

//WHOLE OUTPUT

//Documentation
-- I have two classes, 1st Class [“LabExer2.java”] handles the MAIN METHOD & writeOutput() which I
fetch all the data coming from the 2nd class.

-- 2ND [“_extension.java”] Contains the Encapsulation Method of get&setItemName() and


get&setTotalCost() also readInput()
Here are the source code of both two classes.
-- LabExer2.java
-- _extension.java

------------------------------
LabExer2.java
------------------------------
public class LabExer2{
//-write the code
public void writeOutput(){
_extension ext = new _extension();
ext.readInput();
}
//-run the code
public static void main(String[] args) {
LabExer2 lab = new LabExer2();
lab.writeOutput();
}
}
------------------------------
_extension.java
------------------------------
import java.util.*;
class _extension{

//-class variables
private String itemName;
private double itemPrice;
private double amountDue;
private int itemQuantity;
String item;
int item_amount;
double price;

//-item name
public String getItemName(){return itemName;}
public void setItemName(String newItemName)
{itemName = newItemName;}

//-total cost
public double getTotalCost(){return amountDue;}
public void setTotalCost(int quantity, double price)
{itemQuantity = quantity; itemPrice = price; amountDue = quantity * price;}
public void readInput(){
_extension ext = new _extension();
Scanner sc = new Scanner(System.in);
//-item name
System.out.println("\n-----Output-----\nEnter the name of the item you purchase : ");
item = sc.nextLine(); ext.setItemName(item);
//-amount
System.out.println("Enter quantity and price separated by space : ");
item_amount = sc.nextInt();price = sc.nextDouble();
ext.setTotalCost(item_amount,price);
//-output
System.out.println("\nYou are purchasing " + item_amount + " " + ext.getItemName()
+ "(s) at " + price + " each. \nAmount Due is : " + ext.getTotalCost());

You might also like