Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.78 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.InputStreamReader;
  3.  
  4. import java.lang.String;
  5.  
  6. class ibm {
  7.  
  8.  public static void main(String args[]) {
  9.   String[] d = new String[100];
  10.   int[] a = new int[100];
  11.   int[] c = new int[100];
  12.   String input;
  13.   int count = 0;
  14.   int ik = 0;
  15.   System.out.println("NOTE:To exit the infinite loop type exit");
  16.  
  17.   Scanner scanner = new Scanner(new InputStreamReader(System.in));
  18.   do {
  19.  
  20.    String[] temp = new String[100];
  21.  
  22.    input = scanner.nextLine(); //to read the lines of text entered by the user
  23.    if ((input.equalsIgnoreCase("exit")) == true) //to check whethe the user typed exit
  24.     break;
  25.  
  26.    String[] words = input.split(","); //split the lines of text based on cama
  27.  
  28.    int dflag = 0; //flag variable to check same date already exists
  29.    int flag = 0; //flag variable to check same product id already exists in same date
  30.  
  31.    if (count == 0) {
  32.     d[count] = words[0]; //store the date
  33.     // System.out.println(d[count]);
  34.     a[count] = Integer.parseInt(words[1]); //store the quantity
  35.     // System.out.println("total"+a[count]);
  36.  
  37.     temp[ik] = words[2]; //store the producct id
  38.     c[count]++; //increment uinque product count
  39.     count++; //incremetn unique date count
  40.     ik++; //increment unique date count pointer
  41.    } else {
  42.     int i;
  43.     for (i = 0; i < count; i++) {
  44.      if (words[0].equals(d[i]) == true) //to check date alreadyu exists in array a
  45.      {
  46.  
  47.       a[i] += Integer.parseInt(words[1]); //sum up the qauntites on same date                        
  48.       //System.out.println("same1"+a[i]);
  49.  
  50.       dflag = 1; //set flag varible as date already exists
  51.       break;
  52.      }
  53.     }
  54.  
  55.     if (dflag == 1) //if date exists
  56.     {
  57.      int j;
  58.      for (j = 0; j < ik; j++) //check same priduct id exists
  59.      {
  60.       if (words[2].equals(temp[j]) == true) {
  61.        flag = 1;
  62.        break;
  63.       }
  64.      }
  65.      if (flag == 0) //if same product id does not exists
  66.      {
  67.       temp[ik] = words[2]; //store product id
  68.       //System.out.println("same2"+temp[ik++]);
  69.       c[i]++; //increment unique product count
  70.       ik++; //increment product count pointer
  71.  
  72.      }
  73.     } else {
  74.      //if same does not exists
  75.      d[count] = words[0]; //store date
  76.      //System.out.println(d[count]);
  77.      a[count] = Integer.parseInt(words[1]); //store the quantity sold in first time on that date
  78.      temp[ik] = words[2]; //store the product id
  79.      c[count]++; //increment unique product count
  80.      count++; //increment date count
  81.  
  82.     }
  83.    }
  84.   } while (true);
  85.  
  86.  
  87.   //the following lines of code is to display the output
  88.   for (int i = 0; i < count; i++) {
  89.    System.out.print(d[i] + ",");
  90.    double p = a[i];
  91.    double k = c[i];
  92.    System.out.printf("%.2f", (p / k));
  93.    System.out.println("," + c[i]);
  94.  
  95.   }
  96.  
  97.  }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement