2014-05-11 47 views
0

我在寫一個簡單的程序來跟蹤庫存。我有一個名爲「inventory-txt」的輸入文件需要掃描並創建一個對象。文件看起來像這樣:通過掃描輸入文件創建對象?

(項目,控管數量,價格)

筆1000 2

記事本1050 5

油漆刷500 3

剪刀398 4

橡皮擦199 2

紙張重量50 3

訂書機小100 5

訂書機大50 8

標記1000 2

這是我走到這一步:

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); 

f ile已成功掃描,但我在創建每個對象時遇到困難。每個對象應該有自己的名稱,數量和價格。請幫我弄清楚如何創建這些對象!

回答

1

創建一個List來保存您的對象,然後在讀取輸入後調用構造函數。

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 
} 
0

您希望創建新項目並在循環瀏覽文件時存儲它們。

使用您提供的數據查看此Ideone Example

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(); 

作爲一個側面說明,避免給變量一個大寫字母,因爲這些應爲類名(在你的代碼例如Input)保留。

0

請記住放置完整路徑來查找文件,而不僅僅是文件名。

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 + "]"; 
    } 


} 

這是輸出:

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]]