I'm managing a grocery list in a main class called shoppingList
, and I created a new class named Product to handle the String (I get a string full of data on the product and in the class Product I use the .split(";")
and arrays to arrange the data as needed.
On the main class - shoppingList
- there is a method called addProduct
that I need to execute, and this method get a String
. How do I change this String
to be type Product
so I can add this product into an array of products (products[currentNumOfProducts-1] = productLine
)?
Thanks in advance!
public class Product {
private String type;
private double price;
private int id;
private String manifactor;
private ExtraData extraData;
public Product(String item) {
String[] parts = item.split(",");
type = parts[0];
type = parts[1];
switch (type) {
case "ElectricProduct":
extraData = new ElctricExtraData(item);
break;
case "Book":
extraData = new BookExtraData(item);
break;
...
This is the class Product -and the method on Shopping list:
public void addProduct (String productLine){
if (curNumOfProducts < products.length) {
products[curNumOfProducts - 1] = productLine;
curNumOfProducts++;
}