Please open the menu to show more

Thursday, July 20, 2017

Solution for the assignment given for 2 to 4 batch on Wednesday 19th July

This code is implemented by Pankaj kumar Gupta/BRANCH ECE/NIT, Arunachal pradesh



import java.util.*;
public class StringFruit{
 
  public static void main(String[] arg){

   Scanner sc = new Scanner(System.in);
   System.out.println("Please Enter five friut name and their respective prices in format 'name,price:name,price and so on' ");
    String data=sc.nextLine();
 String[] splited=data.split(",|\\:");
 int i=0,j=0,k=0;
 String[] names={"","","","",""};
 int[] prices={0,0,0,0,0};
 for(String c:splited)
 {
  i++;
       if(i%2!=0) 
      {
        names[j++]=c;
      }
     else   if(i%2==0) 
      {
       prices[k++]=Integer.parseInt(c); 
       }
 
System.out.println("Fruit\t\tPrice");
 for(int t=0;t<5;t++){
 System.out.print(names[t]+"\t\t"+prices[t]);
    System.out.println(""); 
 }
 int[] mrp={0,0,0,0,0};
 int x=0;
 for(int pri:prices)
 {
  mrp[x++]=pri;
 }
    
         Arrays.sort(prices);
  System.out.println("Please press 1 to sort by price in increasing order");
  System.out.println("Please press 2 to sort by price in decreasing order");
  
         int input=sc.nextInt();
  if(input==1){
        System.out.println("Sorted in increasing order according to price");
     System.out.print("Fruit\t\tPrice");
     for(int it=0;it<5;it++){
  System.out.println("");
  for(int s=0;s<5;s++){
  if(mrp[s]==prices[it])
   System.out.print(names[s]+"\t\t"+prices[it]); 
  } 
     }
  }
  else 
   if(input==2)
   {
    System.out.println("Sorted in decreasing order according to price");
          System.out.print("Fruit\t\tPrice");
          for(int it=4;it>=0;it--){
       System.out.println("");
        for(int s=0;s<5;s++){
       if(mrp[s]==prices[it])
     System.out.print(names[s]+"\t\t"+prices[it]); 
         } 
            }
   }
   else{
   System.out.print("Please input correct choice");
           System.exit(0);   
   }
  }
  }
  

1 comment: