java - 搜索具有特定名称的项目列表

标签 java

我正在做一个面向对象的程序,其中包含一个目录类和一个产品类。 Catalog 有一种方法,该方法假设搜索从文件中读取的具有特定名称的产品列表。一切正常,但 getProducts 不起作用。

这是具有 getProducts(String name) 的 Catalog 类

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*

public class Catalog{



    private static int MAX_ITEMS = 10;

    private Products[] list;

    private int nextItem;


    /**
     * Default constructor 
     */
    public Catalog(){
        list = new Products[MAX_ITEMS];
        nextItem = 0;
    }

    /**
     * Reads items from an input file.
     */
    public void loadList(String fileName)
            throws FileNotFoundException { 
        if ( (fileName != null) && (!fileName.equals("")) ) { 
            Scanner input = new Scanner(new File(fileName)); 
            String newLine = null; 
            String name = null;
            int quantity = 0;
            double price = 0.0;


            while (input.hasNextLine() && nextItem < MAX_ITEMS) { 
                if (input.hasNext()) { 
                    name = input.next(); 
                } else { 
                    System.err.println("ERROR Not a String"); 
                    System.exit(2); 
                }
                if (input.hasNextInt()) { 
                    quantity = input.nextInt(); 
                } else { 
                    System.err.println("ERROR Not an integer"); 
                    System.exit(2); 
                } 
                if (input.hasNextDouble()) { 
                    price = input.nextDouble(); 
                } else { 
                    System.err.println("ERROR Not a double"); 
                    System.exit(2); 
                }
                list[nextItem] = new Products(name, quantity, price); 
                newLine = input.nextLine(); 
                nextItem += 1; 
            } 
        } 
        return; 
    } 

    /**
     * Calculate the total cost of products.
     */
    public double getTotalPrice(){
        double total = 0.0;
        for(int i=0; i < nextItem; i++){
            Products products = list[i];
            total+=products.getTotalPrice();
        }
        return total;
    }

    /**
     * Search Catalog items with a product name and returns it to caller
     */
    public Products getProducts(String name){
        **//search a list for string equality using the name of the product and returns it to the caller**
        for(int i=0; i<nextItem; i++){
            Products item = list[i];
            if(item.equals(name)){
                return item;  //What is suppose to be returned or how to  
                //return it to the caller
            }

            public static void main(String[] args) 
                    throws FileNotFoundException { 
                Catalog catalog= new Catalog(); 
                catalog.loadList(args[0]);
                System.out.println();
                System.out.format("Total Price = %9.2f\n", catalog.getTotalPrice());
            }
        }

这是产品类

public class Products {

    private String name;
    private int quantity;
    private double price;

    /**
     * Constructor.
     */
    public Products(String name, int quantity, double price){
        this.name = name;
        this.quantity = quantity;
        this.price = price;
    }
    /**
     * Gets name of the product.
     */
    public String getName(){
        return this.name;
    }
    /**
     * Gets the quantity of products.
     */
    public int getQuantity(){
        return this.quantity;
    }
    /**
     * Gets the cost per product.
     */
    public double getPrice(){
        return price;
    }
    /**
     * set quantity of the products.
     */
    public void setQuantity(int quantity){
        this.quantity=quantity;
    }
    /**
     * Calculate the total price.
     */
    public double getTotalPrice(){
        return quantity * price;
    }
    /**
     * Returns a spaced-separated list of the attributes.
     */
    public String toString(){
        toStr="";
        toStr= toStr + getName();
        return toStr;
    }

这是文件

    Football 2 15.50
    Football-Jersey 2 30.95
    ChapStick 1 3.87
    Book 4 10.00
    Book-Journal 1 5.00

最佳答案

你有:

public Products getProducts(String name){
    ...
    for(int i=0; i<nextItem; i++){
        ...
        Products item = list[i];
        if(item.equals(name)) {
            ...

请注意,item 是一个Products,但您将其与Stringname 进行比较。您可能想要比较产品的名称,例如:

        if(item.getName().equals(name)) {

您还可以使用String.equalsIgnoreCase()如果名称不区分大小写,可能使用 trim()首先,如果前导/尾随空格也是一个问题。

关于java - 搜索具有特定名称的项目列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22213496/

相关文章:

java - 如何将一个接口(interface)上的实现者转换为另一个接口(interface)?

java - 如何从 servlet init 方法获取请求参数

java - 如何处理 DDD 聚合内的 getOrCreateIfAbsent?

java - 如何从 React Native 组件中的 Java 模块返回数组?

java - Knime:从 Java 应用程序调用 Knime 工作流程

java - 是否存在简单的基于文档的数据库?

java - lucene 文档字段 setBoost 错误

java - 提高java截图的分辨率

java - 恢复应用程序时尝试写入空对象引用上的字段 'int android.app.Fragment.mNextAnim'

java - glClearColor 无法正常工作(android opengl)