Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /**
- * A demo program that demonstate the use of arrays, loop and functions
- * Stores the data in two arrays - one array for item name and one for prices
- */
- public class ItemsPricesArrayFunctions {
- static String itemNames[] = new String[5];
- static double itemPrices[] = new double[5];
- public static void main(String args[]) {
- Scanner scanner = new Scanner(System.in);
- int option;
- while (true) {
- System.out.println("===== MENU ======");
- System.out.println("1. Enter data");
- System.out.println("2. View data");
- System.out.println("3. Show most expensive item");
- System.out.println("4. Show cheapest item");
- System.out.println("5. Search for an item");
- System.out.println("6. Exit");
- System.out.println("Enter your option:");
- option = scanner.nextInt();
- switch (option) {
- case 1:
- takeInput();
- break;
- case 2:
- printItemData();
- break;
- case 3:
- printExpensiveItem();
- break;
- case 4:
- printCheapestItem();
- break;
- case 5:
- searchItems();
- break;
- case 6:
- System.exit(0);
- break;
- default:
- System.out.println("Enter an option between 1 to 5");
- }
- }
- }
- //take input from the user for item name and price
- public static void takeInput() {
- Scanner scanner = new Scanner(System.in);
- for (int i= 0; i < itemNames.length; i++) {
- System.out.print("Enter name for item " + (i+1) + ":");
- itemNames[i] = scanner.next();
- //loop until user enters a valid price between 1-100
- while (true) {
- System.out.print("Enter price for item " + (i+1) + ":");
- double itemPrice = scanner.nextDouble();
- //do validation to check if item price is between 1 and 100
- if (itemPrice >= 1 && itemPrice <=100) {
- itemPrices[i] = itemPrice;
- break; //from the while loop
- }
- else {
- System.out.println("Error! Enter a valid price between 1 and 100");
- }
- }
- }
- }
- //print all item names and their prices
- public static void printItemData() {
- for (int i= 0; i < itemNames.length; i++) {
- System.out.println(itemNames[i] + " Price:AED "+ itemPrices[i]);
- }
- }
- //search for an item and if found print its price
- public static void searchItems() {
- Scanner scanner = new Scanner(System.in);
- System.out.print("Enter name of item you want search:");
- String itemToSearch = scanner.next();
- //iterate over the array to find the item, find its next and
- //the print its price which is at the same index in the itemPrices array
- boolean wasSearchItemFound = false;
- for (int i = 0; i < itemNames.length; i++) {
- //check if there is an itemName that matches the itemToSearch
- if (itemToSearch.equals(itemNames[i])) {
- //item was found - print its price also at the same index i
- System.out.println("Item found." +itemNames[i] + " Price:AED" + itemPrices[i]);
- //set wasSearchItemFound to true
- wasSearchItemFound = true;
- //item was found do continue the loop - break
- break;
- }
- }
- //if wasSearchItemFound is still false print item not found
- if (wasSearchItemFound == false) {
- System.out.println(itemToSearch + " NOT found!");
- }
- }
- //find the item with the highest price
- public static void printExpensiveItem() {
- int indexOfExpensiveItem = -1;
- double mostexpensive = itemPrices[0];
- //iterate over the itemPrices array to find the highest price
- for (int i = 0; i < itemPrices.length; i++ ) {
- if (itemPrices[i] > mostexpensive) {
- mostexpensive = itemPrices[i];
- indexOfExpensiveItem = i;
- }
- }
- //print the most expensive item's name and price
- System.out.println(itemNames[indexOfExpensiveItem]
- + " Price is " + itemPrices[indexOfExpensiveItem]);
- }
- //find the item with the lowest price
- public static void printCheapestItem() {
- int indexOfCheapestItem = 0;
- double mostCheap = itemPrices[0];
- for (int i = 0; i < itemPrices.length; i++ ) {
- if (itemPrices[i] < mostCheap) {
- mostCheap = itemPrices[i];
- indexOfCheapestItem = i;
- }
- }
- //print the most cheap item's name and price
- System.out.println(itemNames[indexOfCheapestItem]
- + " Price is " + itemPrices[indexOfCheapestItem]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment