我正在编写一个编程项目,你打算为在线购物商店编写一个代码,我有两个文件,其中一个包含产品,另一个包含帐户信息,我需要阅读这两个并将它们存储在ArrayList中和另一个类型帐户,如下所示在商店类,我也想检查登录信息是否匹配
我已经尝试将这两个文件读取到list1和list2(这是String类型的数组列表)并且它完全正常工作但是当我到达我想要检查用户名和密码是否有效的点时我尝试了以下内容:
int accountIndex=-1;
for (int i = 0; i < list2.size(); i++) {
if((list2.get(i).contains(username))&& (list2.get(i).contains(password))){
accountIndex=0;
}
}
if(accountIndex<0)
System.out.println("Login Failed! Invalid Username and/or Password.\n");
但它没有适用于所有情况,并有一些错误,所以我的老师告诉我将文件读入已经存在的产品和帐户的数组列表,但我还没有找到合适的方法...
这是帐户文件包含的示例:
0001,Salam,1234,AbdulSalam Ahmad,1223 Sultanah - Medina,05555343535,[email protected]
0002,Rawan,1111,Rawan Khaled,1223 Alaziziah - Medina,05555343435,[email protected]
//主要:
public static void main(String[] args) throws FileNotFoundException {
int ans;
// Craete a Store object
Store store = new Store("Camera Online Store");
// Read all products from the products file
ArrayList<String>list1=new ArrayList<String>();
File products = new File("Products.txt");
try(Scanner input = new Scanner(products);)
{
input.useDelimiter(",");
while(input.hasNext()){
list1.add(input.nextLine());
}
input.close();
}
// Read all accounts from the account file
File customerFile = new File("Accounts.txt");
ArrayList<String>list2=new ArrayList<String>();
try(Scanner input = new Scanner(customerFile);)
{
input.useDelimiter(",");
while(input.hasNext()){
list2.add(input.nextLine());
} input.close();
}
System.out.println("^^^^^^ Welcome to our "+store.getName()+" ^^^^^");
System.out.println("*****************************************");
while(true)
{
System.out.println("Are you a customer or an admin?\n (1) for user \n (2) for admin\n (3) to exit");
Scanner sc = new Scanner (System.in);
int choice = sc.nextInt();
switch (choice) {
case 1: // customer mode
System.out.println("Enter your login information.");
System.out.print("Username:");
String username = sc.next();
System.out.print("Password:");
String password = sc.next();
int accountIndex=-1;
for (int i = 0; i < list2.size(); i++) {
if((list2.get(i).contains(username))&& (list2.get(i).contains(password))){
accountIndex=0;
}
}
/*
get the account index for this customer
*/
if(accountIndex<0)
System.out.println("Login Failed! Invalid Username and/or Password.\n");
else{
do
{
System.out.println("Choose the required operations from the list below:\n (1) Display all products \n (2) Add a product to your shopping cart by id \n (3) View the products in your shopping cart \n (4) Go to checkout\n (5) Go back to main menu");
//Store class:
class Store{
private String name;
private ArrayList<Account> arracc;
private ArrayList<Products> arrprod;
public Store(){
}
public void setArrAcc(Account x){
arracc.add(x);
}
public void setArrProd(Products x){
arrprod.add(x);
}
public Store(String name){
this.name=name;
}
public void addProduct(Products p){
arrprod.add(p);
}
public void deleteProduct(int id){
arrprod.remove(id);//id is the product index
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
}
//products class:
class Products{
private int productID;
private String name;
private String supplier;
private double price;
public Products(){
}
public Products(int productID,String name,String supplier, double price){
this.productID=productID;
this.name=name;
this.supplier=supplier;
this.price=price;
}
public void setProductID(int ID){
productID=ID;
}
public void setName(String newName){
name=newName;
}
public void setSupplier(String newSupplier){
supplier=newSupplier;
}
public void setPrice(double newPrice){
price=newPrice;
}
public int getID(){
return productID;
}
public String getSupplier(){
return supplier;
}
public String getNAme(){
return name;
}
public double getPrice(){
return price;
}
public String toString(){
return" Product ID: "+productID+"\n -Product Name: "+name+"\n -Product Supplier: "+supplier+
"\n -Product Price: "+price+"\n ******************************************";
}
}
任何帮助将非常感激!!
使用Scanner读取数据文件中的文本行时,请不要使用Scanner#next()方法,除非您特别需要文件中的每个标记(单词)。它专门用于在与Scanner#hasNext()方法一起使用时从文件中检索标记(单词)。将Scanner#hasNextLine()与Scanner#nextLine()方法结合使用。这是一个例子:
File customerFile = new File("Accounts.txt");
ArrayList<String> accountsList = new ArrayList<>();
// Try With Resources...
try (Scanner input = new Scanner(customerFile)) {
while (input.hasNextLine()) {
String line = input.nextLine().trim(); // Trim each line as they are read.
if (line.equals("")) { continue; } // Make sure a data line is not blank.
accountsList.add(line); // Add the data line to Account List
}
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
您会注意到我没有将帐户列表命名为list2。虽然这是一个偏好问题,但它根本就没有足够的描述性。我方便地将其命名为accountsList,这样可以更容易地查看变量的用途。
虽然你可以逃脱它,但我不会使用contains()方法来检查用户名和密码。当账户清单变大时,你会发现它不够准确。而是将每个帐户行拆分为一个字符串数组,并访问用户名和密码所在的特定索引,例如:
int accountIndex = -1;
for (int i = 0; i < accountsList.size(); i++) {
// Split the current accounts data line into a String Array
String[] userData = accountsList.get(i).split(",");
// Check User Name/Password valitity.
if (userData[1].equals(username) && userData[2].equals(password)) {
accountIndex = i;
break; // Success - We found it. Stop Looking.
}
}
现在,这当然是假设在每个帐户数据行中第一个项目是ID号码,第二个项目是用户名称,口渴项目是密码。如果将此行拆分为String数组,然后使第一个项(ID)驻留在Array Index 0处,则第二个项(User Name)驻留在Array Index 1处,第三个项(Password)驻留在Array Index 2处, 等等。
您还有一个名为accountIndex(商品名称)的变量,但在成功找到用户名和密码后,您可以为此变量提供0。通过这样做,您有效地告诉您的代码,无论用户名和密码的有效性如何,成功的帐户实际上是帐户列表中包含的第一个帐户。您应该传递给此变量的是i中包含的值,如上例所示,因为这是传递用户名/密码有效性的Accounts List元素的真实索引值。
您的其余代码尚未提供,因此您可以从此处独立完成。