Results 1 to 2 of 2
  1. #1
    soccer_kid_6 is offline Member
    Join Date
    Feb 2010
    Posts
    35
    Rep Power
    0

    Default Finding a the max value of the array using a for loop

    I want to write a code that will allow the user to find the most expensive book in inventory Print all available info about this book.

    here are the two classes I am using
    Java Code:
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    
    class VictorHW12
    {
    	public static void main(String[] args)
    	{
    		Scanner readit = new Scanner(System.in);
    		String msg = " ";
    
    		Book[] bookInventory = new Book[100];
    
    		bookInventory[0] = new Book ("Preston", "The Hot Zone", 10.95);
    		bookInventory[1] = new Book ("Meyer", "Breaking Dawn", 5.95);
    		bookInventory[2] = new Book ("Coben", "Caught", 12.99);
    		bookInventory[3] = new Book ("Kate", "Fallen", 19.50);
    		bookInventory[4] = new Book ("Stockett", "Help", 9.99);
    		bookInventory[5] = new Book ("Howard", "Ice", 10.99);
    		bookInventory[6] = new Book ("Liang", "Java", 59.93);
    		bookInventory[7] = new Book ("Wu", "Java", 45.00);
    		bookInventory[8] = new Book ("Liang", "C Programming", 275.00);
    		bookInventory[9] = new Book ("Wu", "Kung Fu", 15.95);
    
    		for(int i=0; i < Book.getNumberOfBooks(); i++)
    				{
    					System.out.println(bookInventory[i].toString());
    				} // end for loop
    
    
    		System.out.println("Search for book by title?(Enter Y for yes and a N for no.)");
    		String s = readit.nextLine();
    
    		if (s.equalsIgnoreCase("Y"))
    				{//open if
    				System.out.println("Title of Book:");
    				String searchTitle = readit.nextLine();
    							for (int i = 0; i < Book.getNumberOfBooks(); i++)
    							{//open for loop
    								if (searchTitle.equalsIgnoreCase(bookInventory[i].getTitle()))
    								{//open inner if
    								msg = msg + bookInventory[i].toString();
    								} // end inner if
    							} // end for loop
    							System.out.println("Here are the books Titled " + searchTitle + ":\n" + msg);
    							msg = " ";
    				}
    			else if (s.equalsIgnoreCase("N"))
    				{
    					System.out.println("Not searching for books by Title.");
    				}// end else
    			else
    				{
    					System.out.println("Invalid Character.");
    				}
    
    
    				System.out.println("Search for book by Author?(Enter Y for yes and a N for no.)");
    				s = readit.nextLine();
    
    				if (s.equalsIgnoreCase("Y"))
    						{//open if
    						System.out.println("Author of Book:");
    						String searchAuthor = readit.nextLine();
    									for (int i = 0; i < Book.getNumberOfBooks(); i++)
    									{//open for loop
    										if (searchAuthor.equalsIgnoreCase(bookInventory[i].getAuthor()))
    										{//open inner if
    										msg = msg + bookInventory[i].toString();
    										} // end inner if
    									} // end for loop
    									System.out.println("Here are the books written by " + searchAuthor + ":\n" + msg);
    									msg = " ";
    						}// end if
    					else if (s.equalsIgnoreCase("N"))
    						{
    							System.out.println("Not searching for books by Author.");
    						}// end else if
    					else
    						{
    							System.out.println("Invalid Character.");
    						}// end else
    
    		double sum = 0;
    		for (int i = 0; i < Book.getNumberOfBooks(); i++)
    		{
    			sum = sum + bookInventory[i].getPrice();
    		}
    		double average = sum / Book.getNumberOfBooks();
    		System.out.println("The average price of all the books is: $ " + average + ":\n");
    
    		System.out.println("Search for book by Inventory Number?(Enter Y for yes and a N for no.)");
    						s = readit.nextLine();
    
    						if (s.equalsIgnoreCase("Y"))
    							{//open if
    								System.out.println("Inventory Number:");
    								int searchID = readit.nextInt();
    								msg = bookInventory[searchID-1000].toString();
    								System.out.println("Book inventory # " + searchID + ":\n" + msg);
    								msg = "";
    							}//end if
    
    							else if (s.equalsIgnoreCase("N"))
    								{
    									System.out.println("Not searching for books by Inventory Number.");
    								}// end else if
    							else
    								{
    									System.out.println("Invalid Character.");
    								}// end else
    	}
    }
    Java Code:
    public class Book {
    
        private String author;
        private String title;
        private double price;
        private int inventoryNumber = 999;
    
        private static int numberOfBooks = 0;
    
        public Book(String a, String t, double p)
        {
            author = a;
            title = t;
            price = p;
            numberOfBooks++;
            inventoryNumber = numberOfBooks + inventoryNumber;
        }
    
        public String getAuthor() {return author;}
        public void setAuthor(String a){author = a;}
    
    	public String getTitle() {return title;}
    	public void setTitle(String t) {title = t;}
    
    	public double getPrice() {return price;}
    	public void setPrice(double p) {price = p;}
    
    	public int getInventoryNumber() {return inventoryNumber;}
        public static int getNumberOfBooks() {return numberOfBooks;}
    
        public String toString ()
        {
    		StringBuffer sb = new StringBuffer();
    		sb.append("\n======= Begin MyClass object =======\n");
    		sb.append("\nTitle: " + title);
    		sb.append("\nAuthor: " + author);
    		sb.append("\nPrice: " + price);
    		sb.append("\nInventory #: " + inventoryNumber);
    		sb.append("\n\n======= End MyClass object ========\n");
    		return (new String(sb));
    
    	} // end toString method
    
    } // end class

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by soccer_kid_6 View Post
    I want to write a code that will allow the user to find the most expensive book in inventory Print all available info about this book.

    hallo soccer_kid_6,

    i made a little modification in your code. instead of an array i used an arraylist because i find this collection more convenient if you have to store an unknown number of objects. furthermode for getting the number of books you don't have to loop through the whole list but simple call the method size(). here is the code

    Java Code:
    import java.util.ArrayList;
    
    public class BookMax {
    	public static void main(String[] args) {
    		ArrayList<Book> bookInventory = new ArrayList<Book>();
    
    		bookInventory.add(new Book("Preston", "The Hot Zone", 10.95));
    		bookInventory.add(new Book("Meyer", "Breaking Dawn", 5.95));
    		bookInventory.add(new Book("Coben", "Caught", 12.99));
    		bookInventory.add(new Book("Kate", "Fallen", 19.50));
    		bookInventory.add(new Book("Stockett", "Help", 9.99));
    		bookInventory.add(new Book("Howard", "Ice", 10.99));
    		bookInventory.add(new Book("Liang", "Java", 59.93));
    		bookInventory.add(new Book("Wu", "Java", 45.00));
    		bookInventory.add(new Book("Liang", "C Programming", 275.00));
    		bookInventory.add(new Book("Wu", "Kung Fu", 15.95));
    
    		System.out.println("You are the owner of " + bookInventory.size()
    				+ " books and the most expensive is "
    				+ getMostExpensive(bookInventory).toString());
    	}
    
    	public static Book getMostExpensive(ArrayList<Book> list) {
    		double max = 0;
    		Book book = null; 
    
    		for (Book b : list) {
    			if (b.getPrice() > max) {
    				// remember the book with the highest price
    				book = b;
    				max = b.getPrice();
    			}
    		}
    		return book;
    	}
    }

    the method you are looking for is in the getMostExpensive method. Note, if two or more books have the same price then the first one in the list will be selected as the most expensive of them. the code is quite easy to understand but feel free to ask.
    Last edited by j2me64; 04-11-2010 at 11:47 PM.

Similar Threads

  1. convert byte array into char array
    By kgkamaraj in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 11:32 AM
  2. Convert Char Array to String Array
    By Mayur in forum New To Java
    Replies: 8
    Last Post: 10-12-2009, 11:41 AM
  3. Array length and printing out uninitialized array.
    By nicolek808 in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 09:12 AM
  4. How to transfer 1D array in JAVA to 3D array in C
    By fishwater00 in forum New To Java
    Replies: 0
    Last Post: 07-31-2009, 06:24 PM
  5. Replies: 1
    Last Post: 03-31-2009, 06:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •