Java 使用数组列表从不同文件添加项目并将其存储在另一个文件中
嘿,对于这个程序,我总共有3个文件,我试图实现的是从一个文件中获取用户输入的内容,然后将其存储在另一个文件中的数组中,这样我可以稍后调用它 第一个程序是用户输入的位Java 使用数组列表从不同文件添加项目并将其存储在另一个文件中,java,arrays,file,arraylist,Java,Arrays,File,Arraylist,嘿,对于这个程序,我总共有3个文件,我试图实现的是从一个文件中获取用户输入的内容,然后将其存储在另一个文件中的数组中,这样我可以稍后调用它 第一个程序是用户输入的位 import java.util.Scanner; 导入java.util.*; 导入java.util.ArrayList 公共类投资 { publicstaticvoidmain(字符串参数[]) { 扫描仪控制台=新扫描仪(System.in); 字符串str; 字符c; int n=0; //产品=新产品(); Syste
import java.util.Scanner;
导入java.util.*;
导入java.util.ArrayList
公共类投资
{
publicstaticvoidmain(字符串参数[])
{
扫描仪控制台=新扫描仪(System.in);
字符串str;
字符c;
int n=0;
//产品=新产品();
System.out.println(“库存管理系统”);
System.out.println(“======================================================================”);
System.out.println(“1.添加产品数据”);
System.out.println(“2.查看产品数据”);
System.out.println(“3.视图重新完成策略”);
System.out.println(“======================================================================”);
System.out.println(“4.退出程序”);
而(n!=4)
{
System.out.print(“\n请输入选项1-4以继续…:”;
n=Integer.parseInt(System.console().readLine());
如果(n>4 | | n 10))
{
System.out.println(“\n此输入不正确。请在3到10个字符之间进行说明。\n”);
System.out.println(“使用不同的输入重试”);
System.out.println(“\n******************************************************\n”);
存货附加项();
}
System.out.print(“请输入美元价格:”;
price=console.nextDouble();
产品定价(价格);
如果(价格<0)
{
System.out.println(“\n此输入不正确。请确保属性为正数\n”);
System.out.println(“由于输入错误,程序将重新启动”);
System.out.println(“\n******************************************************\n”);
存货附加项();
}
System.out.print(“请输入设置价格。$:”;
sUpPrice=console.nextDouble();
产品。设置供应(供应);
如果(供应量<0)
{
System.out.println(“\n此输入不正确。请确保属性为正数\n”);
System.out.println(“由于输入错误,程序将重新启动”);
System.out.println(“\n******************************************************\n”);
存货附加项();
}
System.out.print(“请输入单位成本.$:”;
unitCost=console.nextDouble();
产品。设置单位成本(单位成本);
如果(单位成本<0)
{
System.out.println(“\n此输入不正确。请确保属性为正数\n”);
System.out.println(“由于输入错误,程序将重新启动”);
System.out.println(“\n******************************************************\n”);
存货附加项();
}
System.out.print(“请输入库存成本。$:”;
inventoryCost=console.nextDouble();
产品成本(存货成本);
如果(库存成本<0)
{
System.out.println(“\n此输入不正确。请确保属性为正数\n”);
System.out.println(“由于输入错误,程序将重新启动”);
System.out.println(“\n******************************************************\n”);
存货附加项();
}
系统输出打印(“请输入库存金额:”);
stock=console.nextInt();
产品.固定库存(库存);
如果(库存<0)
{
System.out.println(“\n此输入不正确。请确保属性为正数\n”);
System.out.println(“由于输入错误,程序将重新启动”);
System.out.println(“\n******************************************************\n”);
存货附加项();
}
系统输出打印(“请输入产品需求:”);
demand=console.nextInt();
产品设定值(需求);
如果(需求<0)
{
System.out.println(“\n此输入不正确。请确保属性为正数\n”);
System.out.println(“由于输入错误,程序将重新启动”);
System.out.println(“\n******************************************************\n”);
存货附加项();
}
System.out.println(“\n******************************************************\n”);
系统输出打印(desc+“产品添加成功”);
System.out.println(“\n******************************************************\n”);
存储。添加(产品);
}
}
公共静态数据()
{
扫描仪控制台=新扫描仪(System.in);
字符串pOption,str;
System.out.print(“\n输入产品说明以查看数据…\n”);
pOption=console.next();
如果(产品==null)
{
System.out.println(“\n没有关于此产品的信息。\n”);
System.out.println(“\N是否重试?(Y或N)\N”);
str=console.next();
投资产品数据();
}
System.out.println(“产品信息为…”);
System.out.println(“\n******************************************************\n”);
System.out.println(“产品描述:+Product.getName());
System.out.println(“Price:$”+product.getPrice());
System.out.println(“设置价格:$”+product.getsUpPrice());
System.out.println(“单位成本:$”+product.getunitCost());
System.out.println(“库存成本:$”+product.getinvCost());
System.out.println(“库存量:+product.getstock());
System.out.println(“库存量:+product.getdRate());
}
}
第二个程序是数组列表的位,我似乎无法声明数组列表,也无法获取填充数组列表的数据
公共类商店{
私有ArrayList产品列表
public static void main(String args[])
{
Scanner console = new Scanner(System.in);
String str;
char c;
int n=0;
//Product product = new Product();
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)
{
System.out.print("\n Please enter option 1-4 to continue...: ");
n = Integer.parseInt(System.console().readLine());
if (n>4||n<1)
{
System.out.print("Invalid input, please try again...");
continue;
}
if (n==1)
{
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)
{
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)
{
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");
}
public static Product product;
public static Store store;
public static void addItem ()
{
Scanner console = new Scanner(System.in);
product = new Product();
String desc, id, str="";
double price = 0, sUpPrice = 0, unitCost = 0, inventoryCost = 0;
int stock = 0, demand = 0;
if (product == null) //If product 1 is empty
{
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");
store.add(product);
}
}
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();
if (product == null)
{
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();
}
System.out.println("The information for the product is..... ");
System.out.println("\n*****************************************\n");
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());
}
public Store()
{
//ArrayList = "";
}
public void add(Product product)
{
ProductList.add(product);
}
public Product getProduct(String prodName){
for(int i = 0; i< ProductList.length; i++){
if(ProductList(i).name == prodName){
return ProductList(i);
}
}
}