[text] jfood

Viewer

  1. import java.util.*;
  2. public class RestaurantDemo {
  3.  
  4.         public static void main(String[] args) {
  5.                
  6.                
  7.                
  8.                 Scanner sc=new Scanner(System.in);
  9.                 int foodId,quantity;
  10.                 String name;
  11.                 double price;
  12.                 int n=sc.nextInt();
  13.                 Food []fd=new Food[n];
  14.                  
  15.                 for(int i=0;i<n;i++)
  16.                 {
  17.                 foodId=sc.nextInt();
  18.                 name=sc.next();
  19.                 price=sc.nextDouble();
  20.                 quantity=sc.nextInt();
  21.                 fd[i]=new Food(foodId,name,price,quantity);
  22.                  
  23.                 }
  24.                 System.out.println("Enter quantity");
  25.                 int qt=sc.nextInt();
  26.                 Food []result=findFoodByQuantity(qt,fd);
  27.                 System.out.println("Find food by quantity");
  28.                 System.out.println("---------------------------");
  29.                 for(int i=0;i<result.length;i++)
  30.                 System.out.println(result[i].getFoodId()+" "+result[i].getName()+" "+result[i].getPrice()+" "+result[i].getQuantity());
  31.                  
  32.                 Food food=findFoodWithHighestBill(fd);
  33.                 System.out.println("Find food with highest bill");
  34.                 System.out.println("---------------------------");
  35.                 System.out.println(food.getFoodId()+" "+food.getName()+" "+food.getPrice()+" "+food.getQuantity());
  36.                 }
  37.  
  38.                  
  39.  
  40.                 // Method find food by the quantity
  41.                 // Implement
  42.                 public static Food[] findFoodByQuantity(int qt, Food[] fd) {
  43.                 int count=0;
  44.                 for(int i=0;i<fd.length;i++)
  45.                 {
  46.                 if(fd[i].getQuantity()>qt)
  47.                 {
  48.                  
  49.                 count++;
  50.                 }
  51.                 }
  52.                 Food []result= new Food[count];
  53.                 int j=0;
  54.                  
  55.                 for(int i=0;i<fd.length;i++)
  56.                 {
  57.                 if(fd[i].getQuantity()>qt)
  58.                 {
  59.                  
  60.                 result[j++]=fd[i];
  61.                 }
  62.                 }
  63.                 return result;
  64.                 }
  65.                  
  66.                 //Method Find food with highest Bill
  67.                 //Implement
  68.                  
  69.                 public static Food findFoodWithHighestBill(Food[] fd)
  70.                 {
  71.                 double bill;
  72.                 Food max=fd[0];
  73.                 double maxbill=fd[0].getPrice()*fd[0].getQuantity();
  74.                 for(int i=0;i<fd.length;i++)
  75.                 {
  76.                 bill=fd[i].getPrice()*fd[i].getQuantity();
  77.                 if(bill>maxbill)
  78.                 {
  79.                  
  80.                 max=fd[i];
  81.                 maxbill=bill;
  82.                 }
  83.                  
  84.                 }
  85.                 return max;
  86.                 }
  87.  
  88.         }
  89.  
  90.  
  91. ######  FOOD  ###
  92.  
  93. public class Food {
  94.        
  95.         int foodId;
  96.         String name;
  97.         double price;
  98.         int quantity;
  99.          
  100.         public Food(int foodId, String name, double price, int quantity)
  101.         {
  102.          
  103.         this.foodId = foodId;
  104.         this.name = name;
  105.         this.price = price;
  106.         this.quantity = quantity;
  107.         }
  108.  
  109.         public int getFoodId() {
  110.         return foodId;
  111.         }
  112.  
  113.         public void setFoodId(int foodId) {
  114.         this.foodId = foodId;
  115.         }
  116.  
  117.         public String getName() {
  118.         return name;
  119.         }
  120.  
  121.         public void setName(String name) {
  122.         this.name = name;
  123.         }
  124.  
  125.         public double getPrice() {
  126.         return price;
  127.         }
  128.  
  129.         public void setPrice(double price) {
  130.         this.price = price;
  131.         }
  132.  
  133.         public int getQuantity() {
  134.         return quantity;
  135.         }
  136.  
  137.         public void setQuantity(int quantity) {
  138.         this.quantity = quantity;
  139.         }
  140.  
  141.  
  142. }

Editor

You can edit this paste and save as new:


File Description
  • jfood
  • Paste Code
  • 29 Jan-2020
  • 2.61 Kb
You can Share it: