I want to pass all the input values to one element in an array. I have tried to play around with arrays, but don't quite understand how they work with multiple input values.
I have read on Oracle and here but nothing really stands out or they resort to array lists.
Is there a way to encapsulate the values, then store them to an array?
Any help is much appreciated.
import java.util.Scanner;
class Mobile
{
private String name;
private int number;
private int quantity;
private double cost;
public Mobile(String name, int number, int quantity, double cost)
{
this.name = name;
this.number = number;
this.quantity = quantity;
this.cost = cost;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getNumber()
{
return number;
}
public void setNumber(int number)
{
this.number = number;
}
public int getQuantity()
{
return quantity;
}
public void setQuantity(int quantity)
{
this.quantity = quantity;
}
public double getCost()
{
return cost;
}
public void setCost(double cost)
{
this.cost = cost;
}
public double getValue()
{
return cost*quantity;
}
}
public class MobilePhone
{
public static void main (String args[])
{
String itemName;
int itemNum;
int itemQuan;
double unitCost;
for(int i=0;i<5;i++)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter Item Name: ");
itemName = input.nextLine();
System.out.print("Enter Item Number: ");
itemNum = input.nextInt();
System.out.print("Enter Quantity of Item: ");
itemQuan = input.nextInt();
System.out.print("Enter Price of Single Unit: ");
unitCost = input.nextDouble();
Mobile m = new Mobile(itemName, itemNum, itemQuan, unitCost);
}
}
}
Mobile[]enough for this ?