user3624722
user3624722

Reputation: 1

Creating an object by scanning an input file?

I am writing a simple program that keeps track of inventory. I have an input file called "inventory-txt" that needs to be scanned and create an object. The file looks like this:

(item, quanity, price)

pen 1000 2

notepad 1050 5

paint-brush 500 3

scissors 398 4

eraser 199 2

paper-weight 50 3

stapler-small 100 5

stapler-large 50 8

marker 1000 2

This is what I got so far:

public class Item {
    private String name;
    private int quantity;
    private double pricePerUnit;

// Constructor for class Item
Item(String name, int quantity, double pricePerUnit){
    this.name = name;
    this.quantity = quantity;
    this.pricePerUnit = pricePerUnit;
}

// Setter method for name
public void setName(String name){
    this.name = name;
}
// Getter method for name
public String getName(){
    return name;
}

// Setter method for quantity
public void setQuantity(int quantity){
    this.quantity = quantity;
}
// Getter method for quantity
public double getQuantity(){
    return quantity;
}

// Setter method for price per unit
public void setPricePerUnit(double pricePerUnit){
    this.pricePerUnit = pricePerUnit;
}
// Getter method for price per unit
public double getPricePerUnit(){
    return pricePerUnit; 
}

}

 public static void main(String[] args) {
    // To re-factor the Inventory management program
    // Task 2.a.i.
    File inputFile;
    Scanner Input = null;
    try{
        inputFile = new File("Assignment13-inventory.txt");
        Input = new Scanner(inputFile);
        while(Input.hasNext()){
            String name = Input.next();
            int quantity = Input.nextInt();
            double pricePerUnit = Input.nextDouble();
            System.out.println(name);
            System.out.println(quantity);
            System.out.println(pricePerUnit);


        }


    } catch(FileNotFoundException e){
       System.out.println("File does not exist");
      }

  Input.close();

  // Task 2.a.ii.

Item item1 = new Item();
item1.setName(name);
System.out.println(item1.getName());
item1.setPrice(price);
item1.setPricePerUnit(pricePerUnit);  

The file is successfully scanned but I am having a difficult time creating each object. Each object should have it's own name, quantity, and price. Please help me figure out how to create these objects!

Upvotes: 0

Views: 187

Answers (3)

Alexandre Santos
Alexandre Santos

Reputation: 8338

Remember to place the full path to find the file and not just the file name.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Test
{

    public static void main(String[] args)
    {
        // To re-factor the Inventory management program
        // Task 2.a.i.
        File inputFile;
        Scanner Input = null;
        try
        {
            List<Item> items = new ArrayList<Item>();
            inputFile = new File("/full/location/of/the/file/Assignment13-inventory.txt");
            Input = new Scanner(inputFile);
            while (Input.hasNext())
            {
                String name = Input.next();
                int quantity = Input.nextInt();
                double pricePerUnit = Input.nextDouble();
                System.out.println(name);
                System.out.println(quantity);
                System.out.println(pricePerUnit);
                System.out.println("Creating the item and adding it to the list of items:");
                items.add(new Item(name, quantity, pricePerUnit));
            }

            System.out.println("These are the items read:");
            System.out.println(items);

        }
        catch (FileNotFoundException e)
        {
            System.out.println("File does not exist");
        }

        Input.close();
    }
}

class Item
{
    private String name;
    private int quantity;
    private double pricePerUnit;

    // Constructor for class Item
    public Item(String name, int quantity, double pricePerUnit)
    {
        this.name = name;
        this.quantity = quantity;
        this.pricePerUnit = pricePerUnit;
    }

    // Setter method for name
    public void setName(String name)
    {
        this.name = name;
    }

    // Getter method for name
    public String getName()
    {
        return name;
    }

    // Setter method for quantity
    public void setQuantity(int quantity)
    {
        this.quantity = quantity;
    }

    // Getter method for quantity
    public double getQuantity()
    {
        return quantity;
    }

    // Setter method for price per unit
    public void setPricePerUnit(double pricePerUnit)
    {
        this.pricePerUnit = pricePerUnit;
    }

    // Getter method for price per unit
    public double getPricePerUnit()
    {
        return pricePerUnit;
    }

    @Override
    public String toString()
    {
        return "Item [name=" + name + ", quantity=" + quantity + ", pricePerUnit=" + pricePerUnit + "]";
    }


}

This is the output:

pen
1000
2.0
Creating the item and adding it to the list of items:
notepad
1050
5.0
Creating the item and adding it to the list of items:
paint-brush
500
3.0
Creating the item and adding it to the list of items:
scissors
398
4.0
Creating the item and adding it to the list of items:
eraser
199
2.0
Creating the item and adding it to the list of items:
paper-weight
50
3.0
Creating the item and adding it to the list of items:
stapler-small
100
5.0
Creating the item and adding it to the list of items:
stapler-large
50
8.0
Creating the item and adding it to the list of items:
marker
1000
2.0
Creating the item and adding it to the list of items:
These are the items read:
[Item [name=pen, quantity=1000, pricePerUnit=2.0], Item [name=notepad, quantity=1050, pricePerUnit=5.0], Item [name=paint-brush, quantity=500, pricePerUnit=3.0], Item [name=scissors, quantity=398, pricePerUnit=4.0], Item [name=eraser, quantity=199, pricePerUnit=2.0], Item [name=paper-weight, quantity=50, pricePerUnit=3.0], Item [name=stapler-small, quantity=100, pricePerUnit=5.0], Item [name=stapler-large, quantity=50, pricePerUnit=8.0], Item [name=marker, quantity=1000, pricePerUnit=2.0]]

Upvotes: 0

bcorso
bcorso

Reputation: 47128

You want to create the new Items and store them as you are looping through the file.

Check out this Ideone Example using the data that you supply.

Scanner s = new Scanner("Assignment13-inventory.txt");
List<Item> items = new ArrayList<Item>();

while(s.hasNext()){
    String name = s.next();
    int quantity = s.nextInt();
    double pricePerUnit = s.nextDouble();
    items.add(new Item(name, quantity, pricePerUnit));
}
s.close();

As a side note, avoid giving variables a capital letter because those should be reserved for class names (E.g. Input in your code).

Upvotes: 0

merlin2011
merlin2011

Reputation: 75585

Create a List to hold your objects, and then call the constructor after you read the input.

public static void main(String[] args) {
    List<Item> myItems = new ArrayList<Item>();
    // .... Other code.
    String name = Input.next();
    int quantity = Input.nextInt();
    double pricePerUnit = Input.nextDouble();
    myItems.add(new Item(name, quantity, pricePerUnit));
    // ... Other code
}

Upvotes: 1

Related Questions