0% found this document useful (0 votes)
247 views3 pages

Codes

The document defines a LabExer2 class that calculates item costs. It contains private fields to store the item name, price, quantity, and amount due. Methods set the item name and total cost based on quantity and price. The readInput method prompts the user for input and the writeOutput method displays the purchase details and amount due.

Uploaded by

Ashley Aquino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
247 views3 pages

Codes

The document defines a LabExer2 class that calculates item costs. It contains private fields to store the item name, price, quantity, and amount due. Methods set the item name and total cost based on quantity and price. The readInput method prompts the user for input and the writeOutput method displays the purchase details and amount due.

Uploaded by

Ashley Aquino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

public class LabExer2 {

private String itemName;


private double itemPrice, amountDue;
private int itemQuantity;

public void setItemName(String newItemName) {


itemName = newItemName;
}

public void setTotalCost(int quantity, double price) {


itemPrice = price;
itemquantity = quantity;
}

public String getItemName() {


return itemName;
}

public double getTotalCost () {


amountDue = itemPrice * itemquantity;
return amountDue;
}

public void readInput () {


Scanner a = new Scanner(System.in);
System.out.println("Enter the name of the item you are purchasing.");
itemName = a.nextLine();

System.out.println("Enter the quality and price seperated by a space.");


itemquantity = a.nextInt();
itemPrice = a.nextDouble();
}

public void writeOutput() {


System.out.println("You are purchasing " + "(s) at" + itemPrice + "each.");
System.out.println("Amount due is" + amountDue);
}
}

//package labexer2; import java.util.*;


import java.text.*; public class LabExer2 { //Variable initiation (private access modifier:
since there will be no other classes which will access this)
private static int itemQuantity;
private static double itemPrice;
private static double amountDue;
private static String itemName; //main method declared public static void main(String[] arg) {
//try catch to handle exceptions when an invalid value is entered try { readInput(); } //Throw
system defined exceptions: catch (Exception e) { System.out.println(e); System.exit(0); } }
//(private access modifier for methods: since there will be no other classes which will access
this) private void setItemName(String newItemName) { //print all results itemName =
newItemName; } private void setTotalCost(int qty, double price) { itemPrice = price;
itemQuantity = qty; amountDue = price * qty; } private String getItemName() { return
itemName; } private double getTotalCost() { return amountDue; } private static void
readInput() { Scanner s = new Scanner(System.in); System.out.println("Enter the name of the
item you are purchasing: "); itemName = s.nextLine(); System.out.println("Enter the quantity
and the price of the item separated by a space: "); itemQuantity = s.nextInt(); itemPrice =
s.nextDouble(); LabExer2 obj = new LabExer2(); obj.setItemName(itemName);
obj.setTotalCost(itemQuantity, itemPrice); obj.writeOutput(); } private void writeOutput()
{ System.out.println("You are purchasing " + itemQuantity + " " + itemName + "(s) at " +
String.format("%.2f", itemPrice) + " each."); System.out.println("Amount Due is " +
String.format("%.2f", getTotalCost()) + "."); } }

import java.util.Scanner;

public class LabExer2 {


private String itemName;
private double itemPrice;
private int itemQuantity;
private double amountDue;

public void setItemName(String newItemName) {


itemName = newItemName;
}

public void setTotalCost(int quantity, double price) {


itemQuantity = quantity;
itemPrice = price;
amountDue = quantity*price;
}

public String getItemName() {


return itemName;
}

public double getTotalCost () {


return amountDue;
}

public static void readInput () {


Scanner ash = new Scanner(System.in);
System.out.println("Enter the name of the item you are purchasing.");
String itemName = ash.nextLine(); //ash is my nickname

System.out.println("Enter the quality and price seperated by a space.");


int itemQuantity = ash.nextInt();
double itemPrice = ash.nextDouble();

LabExer2 obj = new LabExer2();


obj.setItemName(itemName);
obj.setTotalCost(itemQuantity, itemPrice);

obj.writeOuput();

public void writeOutput() {


System.out.println("You are purchasing " + itemQuantity + "(s)" + "at" + itemPrice +
"each.");
System.out.println("Amount due is" + getTotalCost() );
}

public static void main(String[] args) {


readInput();
}
}

You might also like