2

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++;
                }
Anton Balaniuc
  • 10,889
  • 1
  • 35
  • 53
S B
  • 23
  • 2
  • Possible duplicate of [How to add new elements to an array?](https://stackoverflow.com/questions/2843366/how-to-add-new-elements-to-an-array) – Capricorn Sep 11 '18 at 15:37
  • 3
    You'll have to show us the relevant code before we can give any kind of meaningful answer. – mypetlion Sep 11 '18 at 15:37
  • 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; – S B Sep 11 '18 at 15:43
  • 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++; } – S B Sep 11 '18 at 15:45
  • 1
    Post the code in the question. Code in comments is not readable at all – Kaushal28 Sep 11 '18 at 15:50

2 Answers2

1

So if you have a string which represents a product info:

String productInfo = "product;info;here";

You can convert it into Product by passing it to the Product constructor:

Product product = new Product(productInfo); 

so the original line

 products[curNumOfProducts - 1] = productLine;

becomes

 products[curNumOfProducts - 1] = new Product(productLine); 
Anton Balaniuc
  • 10,889
  • 1
  • 35
  • 53
0

... by going to the file "ShoppingList.java" and editing that source code to read public void addProduct(Product product) { instead.

If you cannot change that file, then what you're trying to do is not possible; that's just not how java works.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72