gpt4 book ai didi

java - arrayList 存储了 2 个以上的对象,如果用户搜索它们如何获得返回

转载 作者:行者123 更新时间:2023-12-02 03:41:44 25 4
gpt4 key购买 nike

因此,在该程序中,用户在一个文件 Inv.java 中输入对象的数据,然后该信息存储在 Product.java 和 store.java 中。从 store.java 有一个 arrayList 保存信息,但是当尝试放入 2 个项目时,第二个对象会覆盖第一个对象,我该如何解决这个问题以及如何能够在 Inv 中调用对象。 java

Inv.java 代码

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

}

Product.java 代码

public class Product
{
public String name;
public double price, sUpPrice, unitCost, invCost;
public int stock, demand;


public Product()
{
name = "";
price = 0;
sUpPrice = 0;
unitCost = 0;
invCost = 0;
stock = 0;
demand = 0;

}

public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}

public void setPrice(double price) {
this.price = price;
}
public double getPrice() {
return this.price;
}
public void setsUpPrice(double sUpPrice) {
this.sUpPrice = sUpPrice;
}
public double getsUpPrice() {
return this.sUpPrice;
}
public void setunitCost(double unitCost) {
this.unitCost = unitCost;
}
public double getunitCost() {
return this.unitCost;
}
public void setinvCost(double invCost) {
this.invCost = invCost;
}
public double getinvCost() {
return this.invCost;
}
public void setstock(int stock) {
this.stock = stock;
}
public int getstock() {
return this.stock;
}
public void setdRate(int demand) {
this.demand = demand;
}
public int getdRate() {
return this.demand;
}

}

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

}

如有任何帮助,我们将不胜感激。

最佳答案

问题出在 Inv.addItem() 方法中,每次调用它时都会实例化变量 store

store = new Store();

查看您的代码,您应该在 Inv 中实例化它:

public static Store store = new Store();

关于java - arrayList 存储了 2 个以上的对象,如果用户搜索它们如何获得返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36765988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com