0

I have an assignment for uni where I have to create a Java program that produces a random pairing customers and their transaction totals.

It comes in two parts where the first part produces the customers and their transactions like so:

Customer ID, Transaction Value
1, 74.36 
1, 44.98 
3, 6.44
0, 19.13
3, 59.44
2, 81.56
0, 87.21
4, 40.9
1, 42.11
3, 66.05

the second summarises the total number of transactions for each customer like so:

Customer: 1 Transactions: 3.0
Customer: 1 Transactions: 3.0
Customer: 3 Transactions: 3.0
Customer: 0 Transactions: 2.0
Customer: 3 Transactions: 3.0
Customer: 2 Transactions: 1.0
Customer: 0 Transactions: 2.0
Customer: 4 Transactions: 1.0
Customer: 1 Transactions: 3.0
Customer: 3 Transactions: 3.0

My problem is the second section should only produce each customer ID once i.e 1,3,0,2,4. I can only do this using int and double variables and without creating any other arrays or structures. My code can be found below.

 import java.util.*;

 public class Assignment3 {
   public static long studentNumber=1234567; 

   public static int customerID[];
   public static double transactionValue[];

   public static void initialiseData(int size) {
     customerID = new int[size];
     transactionValue = new double[size];

     Random rand = new Random(studentNumber);
     for (int i=0; i<size; i++) {
       customerID[i] = rand.nextInt(size / 2);
       transactionValue[i] = rand.nextInt(10000) / 100.0;
     }
   }

   public static void main(String args[]) {
     int size=10;

    initialiseData(size);

    // Your code should only be below this line
    double transaction = 0;
    int customer = 0;
    int customer_Total = 0;
    int count = 0;
    int customer_count = 0;
    double transaction_Total = 0;

    System.out.println("Customer ID, Transaction Value");

    for (size= 0; size < customerID.length; size++) {
    customer= customerID[size];
    transaction= transactionValue[size];
    System.out.println(customer + ", " + transaction);

    }

    System.out.println("");

    for(customer_count = 0; customer_count < customerID.length; customer_count++) {
      transaction_Total= 0;
      customer_Total = customerID[customer_count];
      count = customerID[customer_count];
      //System.out.println(count);

      for (int customer_count2 = 0;
           customer_count2 < customerID.length;
           customer_count2++) {                         
        if (customer_Total == customerID[customer_count2]) {
          transaction_Total++;

          //System.out.println(customer_count2);
        }
      }

      System.out.println("Customer: "+ customer_Total + " " +
                         "Transactions: " + transaction_Total); 
    }

    // Your code should not be below this line

  }
}

3 Answers 3

2

I suggest you look at how Collections.shuffle works. In your case you can create an array of all possible values and "shuffle" then in a random order.

Sign up to request clarification or add additional context in comments.

5 Comments

i cant use anything but ints and doubles so no extra arrays
You can't even use the two arrays in your code, I am confused.
the arrays were given to me and i then have to process the information stored in them only using int and double variables. I can't create any arrays aside from the ones given to me. Does that make sense?
Yes. So I would put in the customer ids you want to have, into the array you and shuffle them so they are in a random order. They will be random but unique (or occur any specific number you want)
True, but its very simple and you can do the same thing in your code.
1

EDIT: in this code:

for(customer_count = 0; customer_count < customerID.length; customer_count++) {
  transaction_Total= 0;
  customer_Total = customerID[customer_count];
  count = customerID[customer_count];
  //System.out.println(count);

  for (int customer_count2 = 0;
       customer_count2 < customerID.length;
       customer_count2++) {                         
    if (customer_Total == customerID[customer_count2]) {
      transaction_Total++;

      //System.out.println(customer_count2);
    }
  }

  System.out.println("Customer: "+ customer_Total + " " +
                     "Transactions: " + transaction_Total); 
}

add the following after 'transaction_Total= 0;':

int wasBefore = 0; //are you allowed to use boolean variables?
for (int customer_count3 = 0;
       customer_count3 < customer_count;
       customer_count3++) {                         
    if (customer_Total == customerID[customer_count3]) {
      wasBefore = 1;
    }
}
if (wasBefore==1) {continue;}

In this way you still get random order of customers without repreating them

4 Comments

Then in your solution add one more loop to find whether this particulkar customer_id exists on a position before customerID[customer_count], and if so, 'continue' the loop with next customer_count value
unfortunately the customer ID's cant be sorted. They have to remain like this Customer: 1, Transactions: 3 Customer: 3, Transactions: 3 Customer: 0, Transactions: 2 Customer: 2, Transactions: 1 Customer: 4, Transactions: 1
Thanks for you're help but i cant use boolean variables only int and double
OK sorry i didn't see your comment just after you edited your first post so i have it working correctly. Thank you for your help! It's so obvious as well, i have been staring at the code so much i got stuck!
0

so your homework is a little bit irritating, since it is unusual to make such a restriction as don't use arrays.

But you could do the following: Your customer ids go from 0 to size/2 so it is possible to write a loop which counts the transactions for every possible customer. Like this

for(int customer_id = 0; customer_id <= size /2 ; customer_id++){
    int transaction_sum = 0;
    for (j= 0; j < customerID.length; j++)
        if(customerID[j] == i)
             transaction_sum++;
    if( transaction_sum > 0)
        System.out.println("Customer: " + customer_id +
                           "  Transactions: " + transaction_sum);
}

but this is not a nice solution, beacuse this is slow for large number of possible ids.

3 Comments

thanks for the comment. when i ran this it produced Customer: 0 Transactions: 2 Customer: 1 Transactions: 2 Customer: 2 Transactions: 2 Customer: 3 Transactions: 2 Customer: 4 Transactions: 2
unfortunately its not the answer im looking for. Its ridiculously annoying
Customer: 1, Transactions: 3 Customer: 3, Transactions: 3 Customer: 0, Transactions: 2 Customer: 2, Transactions: 1 Customer: 4, Transactions: 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.