storing the input to the next variable name based on data type
For String s.nextLine()
For int s.nextInt()
For double: s.nextDouble()
import java.util.Scanner;
class SecondClass{
String itemName;
double itemPrice;
int itemQuantity;
double amountDue;
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 itemPrice*itemQuantity;
}
public void readInput(){
}
public void writeOutput(){
System.out.print( "Name: " + getItemName() +
"\nTotalCost: "+ getTotalCost());
}
}
public class LabExer2 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
SecondClass secClass = new SecondClass();
System.out.print("Enter name: ");
secClass.setItemName(s.nextLine());
System.out.print("Enter quantity: ");
int a = s.nextInt();
System.out.print("Enter price: ");
secClass.setTotalCost(a, s.nextDouble());
secClass.writeOutput();
}
}
Comments
Leave a comment