I have a bookstore application, but I cannot show the maximum and the minimum price of the book. Please see my codes below.
import java.util.*; public class Book{ public static void main(String args[]){ int numOfBook; double totalCost = 0; String expBookTitle = ""; double expBookPrice = 0; String cheapBookTitle = ""; double cheapBookPrice = 0; Scanner input = new Scanner(System.in); System.out.println("Welcome to SAJID's Book Shop"); System.out.print("How many book would you like to store: "); numOfBook = input.nextInt(); String[] book = new String[numOfBook]; double[] bookPrice = new double[numOfBook]; for(int i=0;i<book.length;i++) { System.out.print("Please enter book name: "); book[i] = input.next(); System.out.print("Please enter book price: "); bookPrice[i] = input.nextDouble(); System.out.println(); totalCost += bookPrice[i]; if(bookPrice[i] == expBookPrice) { expBookPrice = bookPrice[i]; expBookTitle = book[i]; System.out.println("Expensive book price: " + expBookPrice); System.out.println("Expensive book title: " + expBookTitle); if(bookPrice[i] <= expBookPrice) { cheapBookPrice = bookPrice[i]; cheapBookTitle = book[i]; System.out.println("Cheap book price: " + cheapBookPrice); System.out.println("Cheap book title: " + cheapBookTitle); } } } } }
The given code accepts the user to enter the number of books. According to that, user enter the name and book and price. These values will get stored into respective arrays. Then it will determine the expensive book and cheap book. Here is the code:
import java.util.*; public class Book{ public static void main(String args[]){ int numOfBook; double totalCost = 0; String expBookTitle = ""; double expBookPrice = 0; String cheapBookTitle = ""; double cheapBookPrice = 0; Scanner input = new Scanner(System.in); System.out.println("Welcome to SAJID's Book Shop"); System.out.print("How many book would you like to store: "); numOfBook = input.nextInt(); String[] book = new String[numOfBook]; double[] bookPrice = new double[numOfBook]; for(int i=0;i<book.length;i++) { System.out.print("Please enter book name: "); book[i] = input.next(); System.out.print("Please enter book price: "); bookPrice[i] = input.nextDouble(); System.out.println(); } double max = bookPrice[0]; double min = bookPrice[0]; int a=0,b=0; for (int i=1; i<bookPrice.length; i++) { if (bookPrice[i] > max) { max = bookPrice[i]; a=i; } if (bookPrice[i]< min) { min = bookPrice[i]; b=i; } } System.out.println("Expensive book price: " + max); System.out.println("Expensive book title: " + book[a]); System.out.println(); System.out.println("Cheap book price: " + min); System.out.println("Cheap book title: " + book[b]); } }
Ads