Java 带2+的arrayList;存储的对象,如果用户搜索它们,如何获取返回
因此,通过该程序,用户在一个文件Inv.java中输入对象的数据,然后该信息存储在Product.java和store.java中。在store.java中有一个arrayList,它保存信息,但是当尝试放入两个项目时,第二个对象会写入第一个项目,我如何解决这个问题,以及如何在Inv.java中调用这些对象 Inv.java的代码Java 带2+的arrayList;存储的对象,如果用户搜索它们,如何获取返回,java,file,arraylist,return-value,Java,File,Arraylist,Return Value,因此,通过该程序,用户在一个文件Inv.java中输入对象的数据,然后该信息存储在Product.java和store.java中。在store.java中有一个arrayList,它保存信息,但是当尝试放入两个项目时,第二个对象会写入第一个项目,我如何解决这个问题,以及如何在Inv.java中调用这些对象 Inv.java的代码 import java.util.Scanner; import java.util.*; import java.util.ArrayList; public c
import java.util.Scanner;
import java.util.*;
import java.util.ArrayList;
public class Inv
{
public static void main(String args[])
{
Scanner console = new Scanner(System.in);
String str;
char c;
int n=0;
System.out.println(" INVENTORY MANAGEMENT SYSTEM");
System.out.println("===============================================");
System.out.println("1. ADD PRODUCT DATA");
System.out.println("2. VIEW PRODUCT DATA");
System.out.println("3. VIEW REPRLENISHMENT STRATEGY");
System.out.println("===============================================");
System.out.println("4. EXIT PROGRAM");
while(n!=4)// Exits the program when 4 is pressed
{
System.out.print("\n Please enter option 1-4 to continue...: ");
n = Integer.parseInt(System.console().readLine());
// Reads user input and takes them to selected code.
if (n>4||n<1)
{
System.out.print("Invalid input, please try again...");
continue;
}
if (n==1)// Takes to option 1 or addItem()
{
str="y";
while(str.equals("y")||str.equals("Y"))
{
Inv.addItem();
System.out.print("Would you like to enter another product ? (Y or N) : ");
str = console.next();
}
continue;
}
if (n==2)// Takes to option 2 or prodData
{
str="y";
while(str.equals("y")||str.equals("Y"))
{
Inv.prodData();
System.out.println("\n***************************************************\n");
System.out.print("Stay viewing this page? (Y or N) ");
str = console.next();
}
continue;
}
else
if (n==3)// Takes to option 3 or replenStrat
{
System.out.print("View Replenishment Strategy.");
continue;
}
}
System.out.print("\nThank you for using this inventory management software.\n");
System.out.print("Developed by Xavier Edwards");
System.out.println("\n***************************************************\n");
}
// Global variables so that any class can call it and use the information in it
public static Product product;
public static Store store;
// Where the user inputs the data for the item
public static void addItem ()
{
Scanner console = new Scanner(System.in);
product = new Product();// initiates the product and store to being empty.
store = new Store();
String desc, id, str="";
double price = 0, sUpPrice = 0, unitCost = 0, inventoryCost = 0;
int stock = 0, demand = 0;
System.out.print("Please enter product description between 3 to 10 characters...: ");
desc = console.next();
desc = desc.toLowerCase();
product.setName(desc);
if ((desc.length() < 3 || desc.length() > 10))
{
System.out.println("\nThis Input is incorrect. Please make description between 3 to 10 characters.\n");
System.out.println("Try again with different input. ");
System.out.println("\n*****************************************\n");
Inv.addItem();
}
System.out.print("Please enter price in $ : ");
price = console.nextDouble();
product.setPrice(price);
if (price < 0)
{
System.out.println("\nThis Input is incorrect. Please make sure attributes are positve numbers\n");
System.out.println("Because of incorrect input, program will restart. ");
System.out.println("\n*****************************************\n");
Inv.addItem();
}
System.out.print("Please enter set up price. $ : ");
sUpPrice = console.nextDouble();
product.setsUpPrice(sUpPrice);
if (sUpPrice < 0)
{
System.out.println("\nThis Input is incorrect. Please make sure attributes are positve numbers\n");
System.out.println("Because of incorrect input, program will restart. ");
System.out.println("\n*****************************************\n");
Inv.addItem();
}
System.out.print("Please enter unit- cost. $ : ");
unitCost = console.nextDouble();
product.setunitCost(unitCost);
if (unitCost < 0)
{
System.out.println("\nThis Input is incorrect. Please make sure attributes are positve numbers\n");
System.out.println("Because of incorrect input, program will restart. ");
System.out.println("\n*****************************************\n");
Inv.addItem();
}
System.out.print("Please enter the inventory cost. $ : ");
inventoryCost = console.nextDouble();
product.setinvCost(inventoryCost);
if (inventoryCost < 0)
{
System.out.println("\nThis Input is incorrect. Please make sure attributes are positve numbers\n");
System.out.println("Because of incorrect input, program will restart. ");
System.out.println("\n*****************************************\n");
Inv.addItem();
}
System.out.print("Please enter the amount in stock : ");
stock = console.nextInt();
product.setstock(stock);
if (stock < 0)
{
System.out.println("\nThis Input is incorrect. Please make sure attributes are positve numbers\n");
System.out.println("Because of incorrect input, program will restart. ");
System.out.println("\n*****************************************\n");
Inv.addItem();
}
System.out.print("Please enter the demand of the product : ");
demand = console.nextInt();
product.setdRate(demand);
if (demand < 0)
{
System.out.println("\nThis Input is incorrect. Please make sure attributes are positve numbers\n");
System.out.println("Because of incorrect input, program will restart. ");
System.out.println("\n*****************************************\n");
Inv.addItem();
}
System.out.println("\n*****************************************\n");
System.out.print(desc +" Product was added successfully ");
System.out.println("\n*****************************************\n");
// stores the item in the array
store.add(product);
}
// Where the product information is being returned to the user
public static void prodData()
{
Scanner console = new Scanner(System.in);
String pOption, str;
System.out.print("\nEnter product description to view the data...\n");
pOption = console.next();//
product = store.getProduct(pOption); //Checks to see if the item is created
// so that data can be displayed
if (product != null){
System.out.println("Product description : "+product.getName());
System.out.println("Price : $ "+product.getPrice());
System.out.println("Set-up Price : $ "+product.getsUpPrice());
System.out.println("Unit Cost : $ "+product.getunitCost());
System.out.println("Inventory Cost : $ "+product.getinvCost());
System.out.println("Amount of Stock : "+product.getstock());
System.out.println("Amount of Stock : "+product.getdRate());
}else{
System.out.println("\nThere is no information on this product.\n");
System.out.println("\nWould you like to try again? (Y or N) \n");
str = console.next();
Inv.prodData();
}
}
}
store.java的代码
import java.util.*;
import java.util.ArrayList;
public class Store{
public ArrayList <Product> ProductList = new ArrayList<Product> ();
public Store()
{
//ArrayList = "";
}
public void add(Product product)
{
ProductList.add(product);
}
public Product getProduct(String prodName) {
for (int i = 0; i < ProductList.size(); i++) {
if (ProductList.get(i).getName().equals(prodName)) {
return ProductList.get(i);
}
}
return null;
}
}
import java.util.*;
导入java.util.ArrayList;
公共类商店{
public ArrayList ProductList=new ArrayList();
公共商店()
{
//ArrayList=“”;
}
公共无效添加(产品)
{
ProductList.add(产品);
}
公共产品getProduct(字符串prodName){
对于(int i=0;i
感谢您的帮助。问题出在
Inv.addItem()
方法中,在该方法中,您每次调用变量时都要实例化变量store
store = new Store();
查看您的代码,您应该在Inv
中实例化它:
public static Store store = new Store();
问题出在
Inv.addItem()
方法中,在该方法中,每次调用变量store
store = new Store();
查看您的代码,您应该在Inv
中实例化它:
public static Store store = new Store();
“from store.java有一个数组,它保存信息,但当试图放入两个项目时,第二个对象会在第一个数组上写上”否!正如我所看到的,您使用的是
ArrayList
而不是array,只有在使用set()
而不是add()
时,才会出现这种情况。如果我错了,请纠正我。请使用格式选项。你是对的,我说的数组不好,它应该是arrayList,因此通过这一点,我可以从store.java中澄清如何解决我的问题。有一个数组保存信息,但当尝试放入两个项目时,第二个对象会在第一个“否”上写入!正如我所看到的,您使用的是ArrayList
而不是array,只有在使用set()
而不是add()
时,才会出现这种情况。如果我错了,请纠正我。请使用格式选项。你是对的,我说的数组不好,它应该是arrayList,因此,澄清了这一点之后,我如何解决我的问题谢谢,如果我把它移到Inv的开头,应该解决它,我还没有看完整的代码,但是是的,这应该解决你的问题,第二个对象写在第一个对象上谢谢,所以如果我把它移到Inv的开头,应该解决它,我没有看完整的代码,但是是的,这应该解决你的问题问题:第二个对象在第一个对象上写入