tauk

Items Data with Arrays and Functions and Menu

Jun 21st, 2020
1,207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.35 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /**
  4.  * A demo program that demonstate the use of arrays, loop and functions
  5.  * Stores the data in two arrays - one array for item name and one for prices
  6.  */
  7. public class ItemsPricesArrayFunctions {
  8.     static String itemNames[] = new String[5];
  9.     static double itemPrices[] = new double[5];
  10.    
  11.     public static void main(String args[]) {
  12.         Scanner scanner = new Scanner(System.in);
  13.         int option;
  14.         while (true) {
  15.             System.out.println("===== MENU ======");
  16.             System.out.println("1. Enter data");
  17.             System.out.println("2. View data");
  18.             System.out.println("3. Show most expensive item");
  19.             System.out.println("4. Show cheapest item");
  20.             System.out.println("5. Search for an item");
  21.             System.out.println("6. Exit");
  22.             System.out.println("Enter your option:");
  23.             option = scanner.nextInt();
  24.             switch (option) {
  25.                 case 1:
  26.                     takeInput();
  27.                     break;
  28.                 case 2:
  29.                     printItemData();
  30.                     break;
  31.                 case 3:
  32.                     printExpensiveItem();
  33.                     break;
  34.                 case 4:
  35.                     printCheapestItem();
  36.                     break;
  37.                 case 5:
  38.                     searchItems();
  39.                     break;
  40.                 case 6:
  41.                     System.exit(0);
  42.                     break;
  43.                 default:
  44.                     System.out.println("Enter an option between 1 to 5");
  45.             }
  46.         }
  47.     }
  48.    
  49.     //take input from the user for item name and price
  50.     public static void takeInput() {
  51.         Scanner scanner = new Scanner(System.in);
  52.         for (int i= 0; i < itemNames.length; i++) {
  53.             System.out.print("Enter name for item " + (i+1) + ":");
  54.             itemNames[i] = scanner.next();
  55.             //loop until user enters a valid price between 1-100
  56.             while (true) {
  57.                 System.out.print("Enter price for item " + (i+1) + ":");
  58.                 double itemPrice = scanner.nextDouble();
  59.                 //do validation to check if item price is between 1 and 100
  60.                 if (itemPrice >= 1 && itemPrice <=100) {
  61.                     itemPrices[i] = itemPrice;
  62.                     break; //from the while loop
  63.                 }
  64.                 else {
  65.                     System.out.println("Error! Enter a valid price between 1 and 100");
  66.                 }
  67.             }
  68.         }
  69.     }
  70.    
  71.     //print all item names and their prices
  72.     public static void printItemData() {
  73.         for (int i= 0; i < itemNames.length; i++) {
  74.             System.out.println(itemNames[i] + " Price:AED "+ itemPrices[i]);
  75.         }
  76.     }
  77.    
  78.     //search for an item and if found print its price
  79.     public static void searchItems() {
  80.         Scanner scanner = new Scanner(System.in);
  81.         System.out.print("Enter name of item you want search:");
  82.         String itemToSearch = scanner.next();
  83.         //iterate over the array to find the item, find its next and
  84.         //the print its price which is at the same index in the itemPrices array
  85.         boolean wasSearchItemFound = false;
  86.         for (int i = 0; i < itemNames.length; i++) {
  87.             //check if there is an itemName that matches the itemToSearch
  88.             if (itemToSearch.equals(itemNames[i])) {
  89.                 //item was found - print its price also at the same index i
  90.                 System.out.println("Item found." +itemNames[i] + " Price:AED" + itemPrices[i]);
  91.                 //set wasSearchItemFound to true
  92.                 wasSearchItemFound = true;
  93.                 //item was found do continue the loop - break
  94.                 break;
  95.             }
  96.         }
  97.         //if wasSearchItemFound is still false print item not found
  98.         if (wasSearchItemFound == false) {
  99.             System.out.println(itemToSearch + " NOT found!");
  100.         }
  101.     }
  102.    
  103.     //find the item with the highest price
  104.     public static void printExpensiveItem() {
  105.         int indexOfExpensiveItem = -1;
  106.         double mostexpensive = itemPrices[0];
  107.         //iterate over the itemPrices array to find the highest price
  108.         for (int i = 0;  i < itemPrices.length; i++ ) {
  109.             if (itemPrices[i] > mostexpensive) {
  110.                 mostexpensive = itemPrices[i];
  111.                 indexOfExpensiveItem = i;
  112.             }
  113.         }
  114.        
  115.         //print the most expensive item's name and price
  116.         System.out.println(itemNames[indexOfExpensiveItem]
  117.                         + " Price is " + itemPrices[indexOfExpensiveItem]);
  118.     }
  119.    
  120.     //find the item with the lowest price
  121.     public static void printCheapestItem() {
  122.         int indexOfCheapestItem = 0;
  123.         double mostCheap = itemPrices[0];
  124.         for (int i = 0;  i < itemPrices.length; i++ ) {
  125.             if (itemPrices[i] < mostCheap) {
  126.                 mostCheap = itemPrices[i];
  127.                 indexOfCheapestItem = i;
  128.             }
  129.         }
  130.        
  131.         //print the most cheap item's name and price
  132.         System.out.println(itemNames[indexOfCheapestItem]
  133.                         + " Price is " + itemPrices[indexOfCheapestItem]);
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment