我需要编写一个从.txt文件中读取的方法,并创建一个新的“InventoryItem”对象并将其添加到“Warehouse”对象中。 Warehouse类创建InventoryItem的arraylist。我使用扫描程序从文本文件中读取,但不了解如何将这些值放入我创建的新Warehouse对象中。
文本文件如下:
Purple Widget
1234 17.95 150
我的方法:
public Warehouse readInventoryFile(File inputFile) throws FileNotFoundException {
Warehouse warehouse1 = new Warehouse();
Scanner in = new Scanner(inputFile);
while(in.hasNext()) {
String item = in.nextLine();
int sku = in.nextInt();
double price = in.nextDouble();
int stock = in.nextInt();
String nextLine = in.nextLine();
}
return warehouse1;
}
InventoryItem类:
public class InventoryItem{
private final int sku;
private final String item;
private double price;
private int stock;
public InventoryItem(int sku, String item, double price, int stock){
this.sku = sku;
this.item = item;
this.price = price;
this.stock = stock;
}//InventoryItem constructor
public int getSku(){
return sku;
}//getSKU
public String getItem(){
return item;
}//getItem
public void setPrice(double price){
this.price = price;
}//setPrice
public double getPrice(){
return price;
}//getPrice
public void setStock(int stock){
this.stock = stock;
}//setStock
public int getStock(){
return stock;
}//getStock
@Override
public String toString(){
return String.format("[%d, %s, %1.2f, %d]", sku, item, price, stock);
}//toString
public static void main(String[] args){
InventoryItem itemName = new InventoryItem(1234, "crackers", 2.50, 4);
itemName.toString();
}//main
}//InventoryItem
仓库类:
import java.util.ArrayList;
public class Warehouse {
private final ArrayList<InventoryItem> inventory;
public Warehouse() {
inventory = new ArrayList<>();
}//constructor
public void addItem(InventoryItem item) {
inventory.add(item);
}//addItem
private int findHelper(int sku) {
int index = 0;
while (index < inventory.size()) {
if (sku == inventory.get(index).getSku()) {
return index;
}//if
index++;
}//while
return -1;
}//findHelper
public InventoryItem findItem(int sku) {
int index = findHelper(sku);
return inventory.get(index);
}//findItem
public InventoryItem removeItem(int sku) {
int index = findHelper(sku);
return inventory.remove(index);
}//removeItem
public void updateItemQuantity(int sku, int stock) {
int index = findHelper(sku);
inventory.get(index).setStock(stock);
}//updateItemQuantity
public void updateItemPrice(int sku, double price) {
int index = findHelper(sku);
inventory.get(index).setPrice(price);
}//updateItemPrice
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < inventory.size(); i++) {
sb.append(inventory.get(i).toString()).append(", ");
}//for
return this.inventory.toString();
}//toString
private int seqSearchRecHelper(int sku, int index) {
if (index < inventory.size()) {
if (sku == inventory.get(index).getSku()) {
return index;
}//if sku ==
return seqSearchRecHelper(sku, index + 1);
}//if index
return -1;
}//seqSearchHelper
public InventoryItem seqSearchRec(int sku) {
int i = seqSearchRecHelper(sku, 0);
if (i == -1) {
return null;
}//if
return inventory.get(i);
}//seqSearchRec
public void selectSort() {
int largestIndex;
int largest;
//for (int i = inventory.size() - 1; i > 0; i--) {
for(int i = 0; i<inventory.size(); i++){
largest = inventory.get(0).getSku();
largestIndex = i;
for (int j = i+1; j < inventory.size(); j++) {
if (largest < inventory.get(j).getSku()) {
largest = inventory.get(j).getSku();
largestIndex = j;
//largest++;
}//if
}//for j
InventoryItem temp = inventory.get(i);
inventory.set(i, inventory.get(largestIndex));
inventory.set(largestIndex, temp);
}//for i
}//selectSort
public InventoryItem binSearch(int sku) {
return binSearchHelper(sku, 0, inventory.size()-1);
}//binSearch
private InventoryItem binSearchHelper(int sku, int start, int end) {
if(start>end){return null;}
int mid=(start+end)/2;
if(sku == inventory.get(mid).getSku()){return inventory.get(mid);}
if(sku<inventory.get(mid).getSku()){return binSearchHelper(sku, start, mid-1);}
return binSearchHelper(sku, mid+1, end);
}//binSearchHelper
public int size() {
return inventory.size();
}
/**
*
* @param index
* @return
*/
public InventoryItem getItem(int index) {
return inventory.get(index);
}
public static void main(String[] args) {
Warehouse item = new Warehouse();
Warehouse item2 = new Warehouse();
item.addItem(new InventoryItem(10, "crackers", 2.00, 10));
item.addItem(new InventoryItem(11, "chips", 4.99, 300));
item.addItem(new InventoryItem(1, "toothpicks", 0.39, 600));
System.out.println("All items (toString): " + item);
System.out.println("Item sku 10: " + item.findItem(10));
System.out.println("Item sku 11: " + item.findItem(11));
System.out.println("Item sku 1: " + item.findItem(1));
item.updateItemQuantity(10, 11);
item.updateItemQuantity(11, 400);
item.updateItemQuantity(1, 599);
System.out.println();
System.out.println("Quantity updated (sku 10): " + item.findItem(10));
System.out.println("Quantity updated (sku 11): " + item.findItem(11));
System.out.println("Quantity updated (sku 1): " + item.findItem(1));
item.updateItemPrice(10, 3.00);
item.updateItemPrice(11, 3.99);
item.updateItemPrice(1, 0.49);
System.out.println();
System.out.println("Price updated - sku 10: " + item.findItem(10));
System.out.println("Price updated - sku 11: " + item.findItem(11));
System.out.println("Price updated - sku 1: " + item.findItem(1));
System.out.println();
System.out.println("seqSearchRec - sku 10: " + item.seqSearchRec(0));
System.out.println("seqSearchRec - sku 11: " + item.seqSearchRec(1));
System.out.println("seqSearchRec - sku 1: " + item.seqSearchRec(2));
System.out.println();
System.out.println("All items updated (toString): " + item);
//System.out.println("Items to remove: " + item.removeItem(10) + item.removeItem(11));
//System.out.println("Items left in inventory: " + item);
//item.selectSort();
//System.out.println("SelectSort: " + item);
System.out.println("seqSearchRec (expected null): " + item.seqSearchRec(2));
System.out.println("seqSearchRec - sku 1: " + item.seqSearchRec(1));
System.out.println("seqSearchRec - sku 10: " + item.seqSearchRec(10));
System.out.println("seqSearchRec - sku 11: " + item.seqSearchRec(11));
System.out.println("binSearch 10: " + item.binSearch(10));
System.out.println("binSearch 11: " + item.binSearch(11));
item.selectSort();
System.out.println("selectSort: " + item);
System.out.println(item.size());
System.out.println(item.getItem(1));
}//main
}//Warehouse
答案 0 :(得分:1)
您需要在将InventoryItem添加到Warehouse对象之前创建它:
public Warehouse readInventoryFile(File inputFile) throws FileNotFoundException {
Warehouse warehouse1 = new Warehouse();
Scanner in = new Scanner(inputFile);
while(in.hasNext()) {
String item = in.nextLine();
int sku = in.nextInt();
double price = in.nextDouble();
int stock = in.nextInt();
String nextLine = in.nextLine();
InventoryItem myItem = new InventoryItem(sku, item, price, stock);
warehouse1.addItem(myItem);
}