java program to search item from name in class

Solutions on MaxInterview for java program to search item from name in class by the best coders in the world

showing results for - "java program to search item from name in class"
Pietro
27 Sep 2017
1import java.io.File;
2import java.io.FileNotFoundException;
3import java.util.*
4
5public class Catalog{
6
7
8
9    private static int MAX_ITEMS = 10;
10
11    private Products[] list;
12
13    private int nextItem;
14
15
16    /**
17     * Default constructor 
18     */
19    public Catalog(){
20        list = new Products[MAX_ITEMS];
21        nextItem = 0;
22    }
23
24    /**
25     * Reads items from an input file.
26     */
27    public void loadList(String fileName)
28            throws FileNotFoundException { 
29        if ( (fileName != null) && (!fileName.equals("")) ) { 
30            Scanner input = new Scanner(new File(fileName)); 
31            String newLine = null; 
32            String name = null;
33            int quantity = 0;
34            double price = 0.0;
35
36
37            while (input.hasNextLine() && nextItem < MAX_ITEMS) { 
38                if (input.hasNext()) { 
39                    name = input.next(); 
40                } else { 
41                    System.err.println("ERROR Not a String"); 
42                    System.exit(2); 
43                }
44                if (input.hasNextInt()) { 
45                    quantity = input.nextInt(); 
46                } else { 
47                    System.err.println("ERROR Not an integer"); 
48                    System.exit(2); 
49                } 
50                if (input.hasNextDouble()) { 
51                    price = input.nextDouble(); 
52                } else { 
53                    System.err.println("ERROR Not a double"); 
54                    System.exit(2); 
55                }
56                list[nextItem] = new Products(name, quantity, price); 
57                newLine = input.nextLine(); 
58                nextItem += 1; 
59            } 
60        } 
61        return; 
62    } 
63
64    /**
65     * Calculate the total cost of products.
66     */
67    public double getTotalPrice(){
68        double total = 0.0;
69        for(int i=0; i < nextItem; i++){
70            Products products = list[i];
71            total+=products.getTotalPrice();
72        }
73        return total;
74    }
75
76    /**
77     * Search Catalog items with a product name and returns it to caller
78     */
79    public Products getProducts(String name){
80        **//search a list for string equality using the name of the product and returns it to the caller**
81        for(int i=0; i<nextItem; i++){
82            Products item = list[i];
83            if(item.equals(name)){
84                return item;  //What is suppose to be returned or how to  
85                //return it to the caller
86            }
87
88            public static void main(String[] args) 
89                    throws FileNotFoundException { 
90                Catalog catalog= new Catalog(); 
91                catalog.loadList(args[0]);
92                System.out.println();
93                System.out.format("Total Price = %9.2f\n", catalog.getTotalPrice());
94            }
95        }