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
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
}
}
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