这是一个允许用户向系统添加数据的程序。这具体是程序的库存类,我们的任务是将对象库存转换为数组列表的对象...我是数组列表的新手,我需要一些帮助来解决这个问题。
我已经尝试过几次,但到目前为止还没有成功,任何帮助!
public class Inventory {
/**
* List of FoodItems that represents our inventory
*/
private FoodItem[] inventory;
/**
* Number of items that a user has entered
*/
private int numItems;
/**
* Default Constructor
*/
public Inventory() {
inventory = new FoodItem[20];
}
/**
* Reads from the Scanner object passed in and fills the data member fields of the class with valid data.
* @param scanner - Scanner to use for input
* @return <code>true</code> if all data members were successfully populated, <code>false</code> otherwise
*/
public boolean addItem(Scanner scanner) {
if(numItems == 20)
{
System.out.println("Inventory full");
return false;
}
boolean valid = false;
FoodItem item = null;
while(!valid)
{
System.out.print("Do you wish to add a fruit(f), vegetable(v) or a preserve(p)? ");
if(scanner.hasNext(Pattern.compile("[fFvVpP]")))
{
String choice = scanner.next();
switch(choice.toLowerCase())
{
case "f":
item = new Fruit();
break;
case "v":
item = new Vegetable();
break;
case "p":
item = new Preserve();
break;
default: // Should not get here.
item = new FoodItem();
break;
}
valid = true;
}
else
{
System.out.println("Invalid entry");
scanner.next();
valid = false;
}
}
if(item.inputCode(scanner))
{
if(alreadyExists(item)<0)
{
if(item.addItem(scanner))
{
inventory[numItems] = item;
numItems++;
return true;
}
return false;
}
else
{
System.out.println("Item code already exists");
return false;
}
}
return true;
}
/**
* Search for a food item and see if it is already stored in the inventory
* @param item - FoodItem to look for
* @return - The index of item if it is found, -1 otherwise
*/
public int alreadyExists(FoodItem item) {
for(int i=0;i<numItems;i++)
{
if(inventory[i].isEqual(item))
return i;
}
return -1;
}
/**
* Update the quanity stored in the food item
* @param scanner - Input device to use
* @param buyOrSell - If we are to add to quantity (<code>true</code>) or remove (<code>false</code>)
* @return
*/
public boolean updateQuantity(Scanner scanner, boolean buyOrSell) {
// If there are no items then we can't update, return
if(numItems == 0)
return false;
FoodItem temp = new FoodItem();
temp.inputCode(scanner);
int index = alreadyExists(temp);
if(index != -1)
{
String buySell = buyOrSell?"buy":"sell";
System.out.print("Enter valid quantity to "+buySell+": ");
if(scanner.hasNextInt())
{
int amount = scanner.nextInt();
if(amount > 0)
{
return inventory[index].updateItem(buyOrSell? amount: amount*-1);
}
else
{
System.out.println("Invalid quantity...");
}
}
else
{
System.out.println("Invalid quantity...");
}
}
return false;
}
@Override
public String toString() {
String returnString = "Inventory:\n";
for(int i=0;i<numItems;i++)
returnString += inventory[i].toString() + "\n";
return returnString;
}
}
最佳答案
我可能误解了这个问题,但我猜您想将 Inventory 转换为使用 ArrayList,而不是将其转换为 ArrayList。这是正确的吗?
如果我正确理解了这个问题,那么您要做的第一件事就是将 inventory
字段的声明更改为 ArrayList:
private List<FoodItem> inventory;
请注意,我已将其声明为 List 接口(interface),而不是具体的 ArrayList 实现。除非您需要的 ArrayList 实现的特定功能在通用接口(interface)中不可用,否则通常最好在通用接口(interface)级别进行操作,以便为自己提供最大的灵 active 。
但是,在构造函数中,您实际上将实例化具体的 ArrayList:
public Inventory() {
inventory = new ArrayList<>(20);
}
通过从裸数组切换到 ArrayList,您将发现使用 Collection 的第一个优点,因为您不再需要 alreadyExists()
方法。 List 及其 super 接口(interface) Collection 支持 contains()
方法。
这个:
if(alreadyExists(item)<0)
变成:
if (!inventory.contains(item))
...您可以删除 alreadyExists()
方法。
第二个优点是您不再需要将库存中的商品数量作为单独的字段进行跟踪。 List 已经为您提供了这一点。
这个:
if(numItems == 20)
变成:
if (inventory.size() == 20)
..您可以从类中删除 numItems
字段。
第三个优点是 List/Collection 支持与上面的实现非常相似的 toString()
。
这个:
@Override
public String toString() {
String returnString = "Inventory:\n";
for(int i=0;i<numItems;i++)
returnString += inventory[i].toString() + "\n";
return returnString;
}
变成:
@Override
public String toString() {
return "Inventory:\n" + inventory.toString();
}
toString() 和 List 提供的 toString() 之间的主要区别在于每个项目之间有换行符,而 List 只是在每个项目之间放置逗号。如果你想保留新行,那么列表/集合上的循环比数组稍微麻烦一点:
@Override
public String toString() {
String returnString = "Inventory:\n";
for(FoodItem item : inventory) {
returnString += item.toString() + "\n";
}
return returnString;
}
关于java - 如何将数组字段重构为 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58469954/