Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.InputStreamReader;
- import java.lang.String;
- class ibm {
- public static void main(String args[]) {
- String[] d = new String[100];
- int[] a = new int[100];
- int[] c = new int[100];
- String input;
- int count = 0;
- int ik = 0;
- System.out.println("NOTE:To exit the infinite loop type exit");
- Scanner scanner = new Scanner(new InputStreamReader(System.in));
- do {
- String[] temp = new String[100];
- input = scanner.nextLine(); //to read the lines of text entered by the user
- if ((input.equalsIgnoreCase("exit")) == true) //to check whethe the user typed exit
- break;
- String[] words = input.split(","); //split the lines of text based on cama
- int dflag = 0; //flag variable to check same date already exists
- int flag = 0; //flag variable to check same product id already exists in same date
- if (count == 0) {
- d[count] = words[0]; //store the date
- // System.out.println(d[count]);
- a[count] = Integer.parseInt(words[1]); //store the quantity
- // System.out.println("total"+a[count]);
- temp[ik] = words[2]; //store the producct id
- c[count]++; //increment uinque product count
- count++; //incremetn unique date count
- ik++; //increment unique date count pointer
- } else {
- int i;
- for (i = 0; i < count; i++) {
- if (words[0].equals(d[i]) == true) //to check date alreadyu exists in array a
- {
- a[i] += Integer.parseInt(words[1]); //sum up the qauntites on same date
- //System.out.println("same1"+a[i]);
- dflag = 1; //set flag varible as date already exists
- break;
- }
- }
- if (dflag == 1) //if date exists
- {
- int j;
- for (j = 0; j < ik; j++) //check same priduct id exists
- {
- if (words[2].equals(temp[j]) == true) {
- flag = 1;
- break;
- }
- }
- if (flag == 0) //if same product id does not exists
- {
- temp[ik] = words[2]; //store product id
- //System.out.println("same2"+temp[ik++]);
- c[i]++; //increment unique product count
- ik++; //increment product count pointer
- }
- } else {
- //if same does not exists
- d[count] = words[0]; //store date
- //System.out.println(d[count]);
- a[count] = Integer.parseInt(words[1]); //store the quantity sold in first time on that date
- temp[ik] = words[2]; //store the product id
- c[count]++; //increment unique product count
- count++; //increment date count
- }
- }
- } while (true);
- //the following lines of code is to display the output
- for (int i = 0; i < count; i++) {
- System.out.print(d[i] + ",");
- double p = a[i];
- double k = c[i];
- System.out.printf("%.2f", (p / k));
- System.out.println("," + c[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement