DEV Community

Gourav Kadu
Gourav Kadu

Posted on

TCS Digital QUESTION

Question 2 Statement

A store has different categories of products in stock as shown below.
Item Number=[101, 102, 103, 108]
Item Name=[Milk, Cheese , Ghee, Bread]
Price=[42, 50, 500, 40]
Stock =[10, 20, 15, 16]

When user give input with 2 values as

  1. Item number for item which user wish to buy
  2. Quantity for the item entered above

When user enters above input, the code should check the stock

  1. If quantity is less than stock and item is available display a notification message showing Output Line1 [prices] INR Calculate total price in float with precision. Output Line2 quantity LEFT // updated stock for item after after purchase

2.If the quantity and stocks less than quantity entered by the user while placing order, then display message
Output Line1 NO STOCK

Output Line2 quantity LEFT

3.If user enter character as input for item number and quantity or enter item number which is not available then display following message and stop Output Line1- INVALID INPUT Make sure the string message displayed in output.

Sample INPUT :-

101
4
Enter fullscreen mode Exit fullscreen mode

Sample OUTPUT:-

168.0 INR
6 LEFT
Enter fullscreen mode Exit fullscreen mode

Sample INPUT :-

104
20
Enter fullscreen mode Exit fullscreen mode

Sample OUTPUT:-

NO STOCK
16 LEFT
Enter fullscreen mode Exit fullscreen mode

Solution

To Run the code or Tinker with code Click here

import java.util.*;
public class codechef {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int[] item_number = {101,102,103,104};
        String[] item_name = {"Milk", "Cheese", "Ghee", "Bread"};
        int[] price = {42,50,500,40};
        int[] stock = {10,20,15,16};

    int item_num = sc.nextInt();
    int quant = sc.nextInt();
    int temp =0,flag=0;;
    for(int i = 0; i<4 ;i++) {
        if(item_num ==item_number[i]) {             
        temp = i;   
        flag=1;
        }
        }


    if(flag== 0){
    System.out.println("INVALID INPUT");
    System.exit(0);
    }
    if(quant > stock[temp]) {
        System.out.println("NO STOCK");
        System.out.println(stock[temp]+" "+"LEFT");
    }else if(quant <= stock[temp]){
    System.out.println((float)(price[temp]*quant)+" "+"INR");
    System.out.println(stock[temp]-quant+" "+"LEFT");
    }
    }
    }

Enter fullscreen mode Exit fullscreen mode

Top comments (0)