Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java program to search item from name in class

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
", catalog.getTotalPrice());
            }
        }
Comment

PREVIOUS NEXT
Code Example
Java :: Java Find the greatest common divisor of two positive integers. The integers can be large, so you need to find a clever solution. 
Java :: creating jdbc connection in java using service name oracle 
Java :: my maven project give error when adding javax.xml.bind dependency 
Java :: The superclass "jakarta.servlet.http.HttpServlet" was not found on the Java Build Path 
Java :: marshalling in java 
Java :: spring-boot-maven-plugin not found 
Java :: java swing on close 
Java :: update java windows 
Java :: Java 8 merge 2 collections. 
Java :: android studio space 
Java :: stream filter java 8 
Java :: command to create a Hashmap in Java 
Java :: java random number generator 
Java :: sending a excel in an attachment in email java 
Java :: Java peek() Method 
Java :: random suffling java 
Java :: Java Access superclass attribute 
Java :: insertion sort java 
Java :: java stream index 
Java :: java for loop syntax 
Java :: java creating an arraylist 
Java :: java to kotlin converter android studio 
Java :: java comment 
Java :: hash map 
Java :: dates in java 8 
Java :: nested class in java 8 
Java :: java mahout get clusters centers 
Java :: java 8 iterating and manipulating list 
Java :: Algorithms - sum 
Java :: efficient java code to reverse array elements 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =