如何在LinkedList中搜索Java中具有特定变量的对象?

如何在LinkedList中搜索Java中具有特定变量的对象?,java,linked-list,Java,Linked List,我有一个问题,我可以用对象填充LinkedList,稍后我可以在我设计的这个商店系统中操作这些对象。问题是,当我用多个对象填充列表,然后在列表中搜索该对象时;我的程序会告诉我,我正在寻找的对象不存在,即使我可以列出列表的内容,它也会显示出来 任何帮助都将不胜感激 import java.io.*; import java.util.*; public class Sales extends addVideoGame implements java.io.Serializable{ priva

我有一个问题,我可以用对象填充LinkedList,稍后我可以在我设计的这个商店系统中操作这些对象。问题是,当我用多个对象填充列表,然后在列表中搜索该对象时;我的程序会告诉我,我正在寻找的对象不存在,即使我可以列出列表的内容,它也会显示出来

任何帮助都将不胜感激

import java.io.*;
import java.util.*;

public class Sales extends addVideoGame implements java.io.Serializable{

private static LinkedList<VideoGames> sellGame = new LinkedList<>();
private static String searchTerm;
private static int searchQ;

public static void sellItem(){
    sellGame = games;
    int listLength = sellGame.size();
    searchTerm = IBIO.inputString("What item would you like to sell: ");
    for(VideoGames v : sellGame){
    if(v.getTitle().contains(searchTerm)){
        IBIO.output("Item found: " + searchTerm);
        searchQ = v.getQuantity();
        IBIO.output("Available Quantity: " + searchQ);
        int sellQ = IBIO.inputInt("How much of this item would you like to sell: ");
        if(sellQ > searchQ){
            IBIO.output("The amount you have specified is greater than the \ncurrent stock.");
            sellItem();
        } else {
            searchQ = searchQ - sellQ;
            v.setQuantity(searchQ);
            double sellP;
            sellP = sellQ * v.getPrice();
            IBIO.output("£"+sellP);
            String confirm = IBIO.input("This is the price you are selling these items for. Type 'Yes' to complete the order or 'No' to reject it. ");
            if(confirm.equalsIgnoreCase("Yes")){
                IBIO.output("Order complete!");
                try{
                    int receiptCount = 0;
                    PrintWriter receipt = new PrintWriter("C:\\Users\\Yemi\\Desktop\\TheStore\\receipt"+ receiptCount +".txt");
                    receipt.println("Item sold: " + v.getTitle());
                    receipt.println("Quantity sold: " + sellQ);
                    receipt.close();
                    receiptCount = receiptCount + 1;
                    IBIO.output("Receipt saved to: C:\\Users\\Yemi\\Desktop\\TheStore");
                } catch(IOException io){
                    io.printStackTrace();
                }
                IBIO.output("Thank you for buying from Gamers Avenue UK!");
            } else if(confirm.equalsIgnoreCase("No") && TheStore.privilege){
                AccessMenus.adminMenu();
            } else {
                AccessMenus.userMenu();
            }
            if(TheStore.privilege){
                AccessMenus.adminMenu();
            } else {
                AccessMenus.userMenu();
            }
        }
    } else {
        IBIO.output("The item you are looking for does not exist.");
        sellItem();
    }
    }
}
}
访问菜单:

public class AccessMenus{

    public static int choice;//Variable which will hold the value, which corresponds to an action depending on what value is entered.

    public AccessMenus(){ //Null argument constructor, to set values to 0.
        AccessMenus.choice = 0;
    }

    public AccessMenus(int c){ //Single argument constructor.
        AccessMenus.choice = c;
    }

    public static void userMenu(){
        IBIO.output("1: Sell a product.");
        IBIO.output("2: Register a customer in the Loyalty programme.");
        IBIO.output("3: Stock check.");
        IBIO.output("4: Log out.");
        IBIO.output(" ");
        IBIO.output("Please make your choice: ");
        choice = IBIO.inputInt();
        if(choice == 1){
            Sales.sellItem();
        } else if(choice == 2){
            CustomerRandom.customerMenu();
        } else if(choice == 3){
            StockCheck.checkStock();
        } else if(choice == 4){
            IBIO.output("Logging out.");
            System.exit(1);
        } else {
            IBIO.output("Invalid choice. Returning to menu.");
            userMenu(); //If the value entered does not correspond to any action, the program will treat it as invalid and return to the menu.
        }//close else
    }//close userMenu

    public static void adminMenu(){
        IBIO.output("1: Sell a product.");
        IBIO.output("2: Go the Videogame management menu.");
        IBIO.output("3: Stock check.");
        IBIO.output("4: Register a customer in the Loyalty programme.");
        IBIO.output("5: Log out.");
        IBIO.output(" ");
        IBIO.output("Please make your choice: ");
        choice = IBIO.inputInt();
        if(choice == 1){
            Sales.sellItem();
        } else if(choice == 2){
            addVideoGame.vgMenu();
        }else if(choice == 3){
            StockCheck.checkStock();
        } else if(choice == 4){
            CustomerRandom.customerMenu();
        } else if(choice == 5){
            IBIO.output("Logging out.");
            System.exit(1);
        } else {
            IBIO.output("Invalid input. Returning to menu.");
            adminMenu();
        } //end else
    }//close AdminMenu
}//close AccessMenus
此类是必需的,因为它允许您填充列表:

import java.util.*;
import java.io.*;

public class addVideoGame extends VideoGames implements java.io.Serializable{

public static VideoGames game = new VideoGames();
public static VideoGames eGame = new VideoGames();
public static LinkedList <VideoGames> games = new LinkedList<>();
public static LinkedList <VideoGames> loadList = new LinkedList<>();
private static int vgChoice = 0;
public static int vgCount = 0;
public static int vgAmount = 0;

public static void vgMenu(){
    IBIO.output("WARNING: USING OPTION 4 TO LOAD IN A LOCAL FILE WILL ERASE EVERYTHING CONTAINED IN THE CURRENT LIST. \nSave everything in the current list using option 3 before loading in data.");
    IBIO.output("1: Add a new videogame to the list.");
    IBIO.output("2: View the contents of the list.");
    IBIO.output("3: Save the contents of the list to the local area.");
    IBIO.output("4: Load in data from a local file.");
    IBIO.output("5: Return to the main menu.");
    vgChoice = IBIO.inputInt("Make your choice: ");

    if(vgChoice == 1){
        vgAmount = IBIO.inputInt("How many games would you like to add to the database?: ");
        for(int x = 0; x < vgAmount; x = x + 1){
        VideoGames vg = new VideoGames(); 
        vg.setTitle(IBIO.inputString("Enter the title of the game: "));
        vg.setPublisher(IBIO.inputString("Enter the publisher of the game: "));
        vg.setDeveloper(IBIO.inputString("Enter the developer of the game: "));
        vg.setAgeRating(IBIO.inputInt("Enter the age rating of the game: "));
        vg.setGenre(IBIO.inputString("Enter the genre of the game: "));
        vg.setQuantity(IBIO.inputInt("Enter the available quantity of the game: "));
        vg.setPrice(IBIO.inputDouble("Enter the recommended retail price: "));
        game = vg;
        games.add(vg);
        IBIO.output(" ");
        }
        vgMenu();
    } else if(vgChoice == 2){
        IBIO.output("Current amount of games in the list: " + games.size());
            Iterator itr = games.iterator();
            while(itr.hasNext()){
                Object g = itr.next();
                IBIO.output(g + " ");
            }
            //System.out.println(Arrays.toString(games.toArray()));
        vgMenu();
    } else if(vgChoice == 3){
        try{
            ListIterator output = games.listIterator();
            while(output.hasNext()){
            Object o = output.next();
            FileOutputStream fileOut = new FileOutputStream("C:\\Users\\Yemi\\Desktop\\TheStore\\games.ser");
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            out.writeObject(game);
            out.close();
            fileOut.close();
            IBIO.output("Data saved to C:\\Users\\Yemi\\Desktop\\TheStore\\games.ser");
            }
        } catch(IOException io){
                io.printStackTrace();
            }
        vgMenu();
    } else if(vgChoice == 4){
        eGame = null;
        try{
            ListIterator input = games.listIterator();
            while(input.hasNext()){
            Object i = input.next();
            FileInputStream fileIn = new FileInputStream("C:\\Users\\Yemi\\Desktop\\TheStore\\games.ser");
            ObjectInputStream in =  new ObjectInputStream(fileIn);
            eGame = (VideoGames) in.readObject();
            in.close();
            fileIn.close();
            games.clear();
            games.add(eGame);
            IBIO.output("Item added to list from local file.");
            }
        } catch (IOException i){
            i.printStackTrace();
            return;
        } catch(ClassNotFoundException c){
            IBIO.output("VideoGames class not found");
            c.printStackTrace();;
            return;
        }
        vgMenu();
    } else if(vgChoice == 5){
        IBIO.output("Returning to main menu: ");
        AccessMenus.adminMenu();
    } 
}     
}
import java.util.*;
导入java.io.*;
公共类addVideoGame扩展视频游戏实现java.io.Serializable{
公共静态视频游戏=新视频游戏();
公共静态视频游戏eGame=新视频游戏();
公共静态LinkedList游戏=新建LinkedList();
public static LinkedList loadList=new LinkedList();
私有静态int vgChoice=0;
公共静态int vgCount=0;
公共静态int vgAmount=0;
公共静态void vgMenu(){
IBIO.output(“警告:使用选项4加载本地文件将删除当前列表中包含的所有内容。\n在加载数据之前,使用选项3保存当前列表中的所有内容。”);
IBIO.output(“1:向列表中添加一个新的视频游戏。”);
IBIO.output(“2:查看列表的内容”);
IBIO.output(“3:将列表内容保存到本地区域”);
输出(“4:从本地文件加载数据”);
IBIO.output(“5:返回主菜单”);
vgChoice=IBIO.inputInt(“做出您的选择:”;
如果(vgChoice==1){
vgAmount=IBIO.inputInt(“您希望向数据库添加多少游戏?:”;
对于(整数x=0;x

再次感谢您的帮助

我猜这个bug是由
else
-block在
sellItem()内的循环中引起的。
试试这个(尽管我仍然认为您应该在最后的if中删除对
sellItem()
的调用):

import java.io.*;
导入java.util.*;
公共类销售扩展addVideoGame实现java.io.Serializable{
private static LinkedList sellGame=new LinkedList();
私有静态字符串搜索项;
私有静态int-searchQ;
公共静态无效项(){
sellGame=游戏;
int listLength=sellGame.size();
searchTerm=IBIO.inputString(“您想卖什么商品:”);
布尔foundItem=false;
用于(视频游戏v:sellGame){
if(v.getTitle().contains(searchTerm)){
foundItem=true;
IBIO.输出(“找到的项目:+searchTerm”);
searchQ=v.getQuantity();
IBIO.输出(“可用数量:+searchQ”);
int sellQ=IBIO.inputInt(“您希望销售多少商品:”);
如果(sellQ>searchQ){
IBIO.output(“您指定的金额大于\n当前库存”);
sellItem();
}否则{
searchQ=searchQ-sellQ;
v、 设定数量(searchQ);
双重销售;
sellP=sellQ*v.getPrice();
IBIO.产量(单位:英镑+销售额);
import java.util.*;
import java.io.*;

public class addVideoGame extends VideoGames implements java.io.Serializable{

public static VideoGames game = new VideoGames();
public static VideoGames eGame = new VideoGames();
public static LinkedList <VideoGames> games = new LinkedList<>();
public static LinkedList <VideoGames> loadList = new LinkedList<>();
private static int vgChoice = 0;
public static int vgCount = 0;
public static int vgAmount = 0;

public static void vgMenu(){
    IBIO.output("WARNING: USING OPTION 4 TO LOAD IN A LOCAL FILE WILL ERASE EVERYTHING CONTAINED IN THE CURRENT LIST. \nSave everything in the current list using option 3 before loading in data.");
    IBIO.output("1: Add a new videogame to the list.");
    IBIO.output("2: View the contents of the list.");
    IBIO.output("3: Save the contents of the list to the local area.");
    IBIO.output("4: Load in data from a local file.");
    IBIO.output("5: Return to the main menu.");
    vgChoice = IBIO.inputInt("Make your choice: ");

    if(vgChoice == 1){
        vgAmount = IBIO.inputInt("How many games would you like to add to the database?: ");
        for(int x = 0; x < vgAmount; x = x + 1){
        VideoGames vg = new VideoGames(); 
        vg.setTitle(IBIO.inputString("Enter the title of the game: "));
        vg.setPublisher(IBIO.inputString("Enter the publisher of the game: "));
        vg.setDeveloper(IBIO.inputString("Enter the developer of the game: "));
        vg.setAgeRating(IBIO.inputInt("Enter the age rating of the game: "));
        vg.setGenre(IBIO.inputString("Enter the genre of the game: "));
        vg.setQuantity(IBIO.inputInt("Enter the available quantity of the game: "));
        vg.setPrice(IBIO.inputDouble("Enter the recommended retail price: "));
        game = vg;
        games.add(vg);
        IBIO.output(" ");
        }
        vgMenu();
    } else if(vgChoice == 2){
        IBIO.output("Current amount of games in the list: " + games.size());
            Iterator itr = games.iterator();
            while(itr.hasNext()){
                Object g = itr.next();
                IBIO.output(g + " ");
            }
            //System.out.println(Arrays.toString(games.toArray()));
        vgMenu();
    } else if(vgChoice == 3){
        try{
            ListIterator output = games.listIterator();
            while(output.hasNext()){
            Object o = output.next();
            FileOutputStream fileOut = new FileOutputStream("C:\\Users\\Yemi\\Desktop\\TheStore\\games.ser");
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            out.writeObject(game);
            out.close();
            fileOut.close();
            IBIO.output("Data saved to C:\\Users\\Yemi\\Desktop\\TheStore\\games.ser");
            }
        } catch(IOException io){
                io.printStackTrace();
            }
        vgMenu();
    } else if(vgChoice == 4){
        eGame = null;
        try{
            ListIterator input = games.listIterator();
            while(input.hasNext()){
            Object i = input.next();
            FileInputStream fileIn = new FileInputStream("C:\\Users\\Yemi\\Desktop\\TheStore\\games.ser");
            ObjectInputStream in =  new ObjectInputStream(fileIn);
            eGame = (VideoGames) in.readObject();
            in.close();
            fileIn.close();
            games.clear();
            games.add(eGame);
            IBIO.output("Item added to list from local file.");
            }
        } catch (IOException i){
            i.printStackTrace();
            return;
        } catch(ClassNotFoundException c){
            IBIO.output("VideoGames class not found");
            c.printStackTrace();;
            return;
        }
        vgMenu();
    } else if(vgChoice == 5){
        IBIO.output("Returning to main menu: ");
        AccessMenus.adminMenu();
    } 
}     
}
import java.io.*;
import java.util.*;

public class Sales extends addVideoGame implements java.io.Serializable{

    private static LinkedList<VideoGames> sellGame = new LinkedList<>();
    private static String searchTerm;
    private static int searchQ;

    public static void sellItem(){
        sellGame = games;
        int listLength = sellGame.size();
        searchTerm = IBIO.inputString("What item would you like to sell: ");
        boolean foundItem = false;
        for(VideoGames v : sellGame){
            if(v.getTitle().contains(searchTerm)){
                foundItem = true;
                IBIO.output("Item found: " + searchTerm);
                searchQ = v.getQuantity();
                IBIO.output("Available Quantity: " + searchQ);
                int sellQ = IBIO.inputInt("How much of this item would you like to sell: ");
                if(sellQ > searchQ){
                    IBIO.output("The amount you have specified is greater than the \ncurrent stock.");
                    sellItem();
                } else {
                    searchQ = searchQ - sellQ;
                    v.setQuantity(searchQ);
                    double sellP;
                    sellP = sellQ * v.getPrice();
                    IBIO.output("£"+sellP);
                    String confirm = IBIO.input("This is the price you are selling these items for. Type 'Yes' to complete the order or 'No' to reject it. ");
                    if(confirm.equalsIgnoreCase("Yes")){
                        IBIO.output("Order complete!");
                        try{
                            int receiptCount = 0;
                            PrintWriter receipt = new PrintWriter("C:\\Users\\Yemi\\Desktop\\TheStore\\receipt"+ receiptCount +".txt");
                            receipt.println("Item sold: " + v.getTitle());
                            receipt.println("Quantity sold: " + sellQ);
                            receipt.close();
                            receiptCount = receiptCount + 1;
                            IBIO.output("Receipt saved to: C:\\Users\\Yemi\\Desktop\\TheStore");
                        } catch(IOException io){
                            io.printStackTrace();
                        }
                        IBIO.output("Thank you for buying from Gamers Avenue UK!");
                    } else if(confirm.equalsIgnoreCase("No") && TheStore.privilege){
                        AccessMenus.adminMenu();
                    } else {
                        AccessMenus.userMenu();
                    }
                    if(TheStore.privilege){
                        AccessMenus.adminMenu();
                    } else {
                        AccessMenus.userMenu();
                    }
                }
                break;
            }
        }
        if(!foundItem) {
            IBIO.output("The item you are looking for does not exist.");
            sellItem();
        }
    }
}