在我的程序中,我有2个商店位置Callahan和Lambton,在我当前的代码中只有一个数组列表。我的问题是,有没有办法保留一个arraylist但是当用户输入不同的商店名称时它是分开的? 以下代码是菜单系统,它将调用arraylist大小并显示它。
import java.util.Scanner;
import java.util.*;
import java.util.ArrayList;
public class StarberksInterface
{
public static void main(String args[])
{
Scanner console = new Scanner(System.in);
store = new Store();
String str, sName1, sName2, name;
char c;
int n=0;
sName1 = "Callahan";
sName2 = "Lambton";
while(n!=5)// Exits the program when 5 is pressed
{
//This is the main menu that will be displayed first.
System.out.println(" MAIN MENU FOR MANAGEMENT SYSTEM");
System.out.println("===============================================");
System.out.println("1. CHOOSE STORE");
System.out.println("2. DISPLAY STORES");
System.out.println("3. LOAD STORE VIA FILE");
System.out.println("4. SAVE STORE TO FILE ");
System.out.println("5. EXIT PROGRAM");
System.out.println("===============================================");
System.out.print("\n Please enter option 1-5 to continue...: ");
n = Integer.parseInt(System.console().readLine());
// Reads user input and takes them to selected code.
if (n>5||n<1)
{
System.out.print("Invalid input, please try again...");
continue;
}
if (n==1)// Takes to option 1 or sub menu
{
str="y";
while(str.equals("y")||str.equals("Y"))
{
System.out.println("Enter a store name [Callahan or Lambton] ");
name = console.next();
if (sName1.equals(name)|| sName2.equals(name))
{
StarberksInterface.subMenu();
break;
}
else
{
System.out.println("There is no store under this name. Please try again.");
}
}
continue;
}
if (n==2)// Gathers products in stores and displays the number of products
{
System.out.println(" Store data is being displayed.");
System.out.println("===============================");
System.out.println("Store: Callahan");
System.out.println(" Number of products: "+store.getProductListSize());
System.out.println("===============================\n");
System.out.println("Store: Lambton");
System.out.println(" Number of Products: "+store.getProductListSize());
System.out.println("===============================\n");
}
}
}
public static void subMenu()
{
Scanner console = new Scanner(System.in);
String str;
char c;
int n=0;
// this will be the sub menu that gets displayed.
System.out.println(" INVENTORY MANAGEMENT SYSTEM");
System.out.println("===============================================");
System.out.println("1. ADD PRODUCT DATA");
System.out.println("2. VIEW SINGLE PRODUCT DATA");
System.out.println("3. DELETE PRODUCT");
System.out.println("4. DISPLAY ALL PRODUCTS IN STORE");
System.out.println("===============================================");
System.out.println("5. EXIT SUB MENU");
while(n!=5)// Exits the program when 4 is pressed
{
System.out.print("\n Please enter option 1-5 to continue...: ");
n = Integer.parseInt(System.console().readLine());
// Reads user input and takes them to selected code.
if (n>5||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"))
{
StarberksInterface.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"))
{
StarberksInterface.prodData();
System.out.println("\n***************************************************\n");
System.out.print("Stay viewing this page? (Y or N) ");
str = console.next();
}
continue;
}
if (n==3)// Takes to option 3 or delete item
{
System.out.print("Delete a product");
continue;
}
if (n==4)// Takes to option 4 or view all products in store
{
System.out.print("Displaying all products in store");
continue;
}
}
if (product != null)// If there is information on the system
// then the user will have the option to view data, before the program quits
{
System.out.println("\n***************************************************\n");
System.out.println("\nAre you sure you want to quit? There is information stored on a product. ");
System.out.println("\nWould you like to view if information? (Y / N) ");
str="";
str = console.next();
while(str.equals("y")||str.equals("Y"))
{
StarberksInterface.prodData();
return;
}
}
else
{
System.out.print("\nThank you for using this inventory management software.\n");
System.out.print("Developed by Xavier Edwards");
System.out.println("\n***************************************************\n");
}
}
以下代码是arraylist的填充位置
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.
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");
StarberksInterface.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");
StarberksInterface.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");
StarberksInterface.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");
StarberksInterface.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");
StarberksInterface.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");
StarberksInterface.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");
StarberksInterface.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
//Checks to see if item is already in the list
/*while (product != null)
{
if (product.equals(store.getProduct(desc)))
{
System.out.println(desc +" is already a product.");
System.out.println("Input for data will restart");
StarberksInterface.addItem();
}
}*/
store.add(product);
}
最后一段代码是实际的arraylist
import java.util.ArrayList;
public class Store{
// stores the product information in an array list
//allows for numerous products and each can be called in the Starberks Interface
public ArrayList <Product> ProductList = new ArrayList<Product> ();
public Store()
{
}
public int getProductListSize()
{
return ProductList.size();
}
public void add(Product product)
{
// Adds the product and all details entered by user to the list.
ProductList.add(product);
}
public Product getProduct(String prodName) {
//
for (int i = 0; i < ProductList.size(); i++) {
//searches through list of products to find a specific name entered in
// from the Starberks Interface
if (ProductList.get(i).getName().equals(prodName)) {
return ProductList.get(i);
}
}
return null;
}
}
答案 0 :(得分:2)
创建两个商店
Store callahan = new Store();
Store lambton = new Store();
根据您的输入
将产品添加到相应的商店 if(storeName.equals("callhan")) {
callahan.add(product);
} else {
lambton.add(product);
}
Map<String, Store> stores = new HashMap<>();
stores.put("callahan", new Store());
stores.put("lambton", new Store()));
因此,您的产品添加代码将是
stores.get(storeName).add(product);
答案 1 :(得分:0)
据我所知,您正在尝试根据 Store 从 ProductList 中检索产品。
我建议将 Store 类之外的 ProductList 填充到 MasterProductsclass 。此 ProductList 将用作主产品列表。
因此,只要您想将 addItem()添加到商店,就可以将产品添加到 ProductList 将自定义方法用作 setStoreHavingProduct 方法,并为其添加商店名称值。在 addItem()之前,验证 ProductList 是否包含相同的产品,以及是否附加了商店名称。因此 ProductList 将包含所有产品,并且其中包含具有此产品的所有 Store 。
这样你最终可以覆盖 ProductList 的方法,并且只显示基于商店的subList。
答案 2 :(得分:0)
您可以尝试ArrayList的ArrayList,它不会将其保留在一个列表中,但应该具有您想要的灵活性。
答案 3 :(得分:0)
你想将一个ArrayList拆分为2.不幸的是,如果你想做类似的事情,你需要查看另一个数据结构。您可以使用Map>
来将产品的每个ArrayList映射到其商店。
虽然,最好的方法是使用更多的OO方法,并在Store类中有一个ArrayList。