Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class LabExer2 {
- // Creating private variable for encapsulation
- private String itemName;
- private Double itemPrice;
- private int itemQuantity;
- private double amountDue;
- // Creating setter for itemName variable
- public void setItemName(String newItemName)
- {
- itemName = newItemName;
- }
- // Creating setter for total or amountDue variable
- public void setTotalCost(int quantity, double price)
- {
- itemQuantity = quantity;
- itemPrice = price;
- amountDue = (itemQuantity * itemPrice);
- }
- // Creating getter for itemName variable
- public String getItemName()
- {
- return itemName;
- }
- // Creating getter for totalCost or amountDue variable
- public Double getTotalCost()
- {
- return amountDue;
- }
- // Read input method for the main class but the method should be inside the main class
- // since the encpasulation will be useless
- public void readInput()
- {
- Scanner scanner = new Scanner(System.in);
- System.out.println("Enter the name of the item you are purchasing.");
- setItemName(scanner.nextLine());
- System.out.println("Enter the quantity and price separated by a space.");
- setTotalCost(scanner.nextInt(), scanner.nextDouble());
- }
- // output method for the main class but just like the read input, it should not be
- // in the same class as encapsulated variable
- public void writeOutput()
- {
- System.out.println("You are purchasing " + itemQuantity + " " + getItemName()
- + "(s) at " + itemPrice + " each.");
- System.out.println("Amount due is " + getTotalCost());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement