Avatar of aspnet-scotland
aspnet-scotland
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Using Java how do I use a class type array to store user details

Hi,

I have the attached code.

I have purposly only used one class and arrays as this was a requirement. How can I modify the code to allow the user to place up to 20 bets (each bet is 6 different numbers between 1 and 49) at a cost of £1 per bet. Basically I need to modify my class to also prompt the user for his/her name and how many bets they would like to make (up to 20 bets) keeping track of how much they are due for the amount of bets they have made. I also need to give the user the option of a lucky dip where the program chooses 6 random numbers for them.

import java.util.Scanner;
import java.util.Random;

public class LotteryNumbers
{
 public static void main(String[] args)
 {
     System.out.println( "Play Lottery : 6/49" + "\n");

  // Part 1: Pick six  numbers, between 1 and 49.
  // and put them in an arry of six numbers

  int [] P = new int [6];

  // Create a Scanner input from keyboard
      System.out.println("Part1:  \n");
  System.out.println( "£1 for a line of six numbers. Play!" + "\n");

  System.out.println("Enter your lottery numbers- enter one number at a time.");

   for (int index =0; index <6; index++)
    {
     Scanner keyboard = new Scanner (System.in);
     System.out.print("# " + (index+1) + ": ");
     P[index] = keyboard.nextInt();
       }
         while (
        (P[0] < 1|| P[0] >49)||
              (P[1] < 1|| P[1] >49)||
              (P[2] < 1|| P[2] >49)||
              (P[3] < 1|| P[3] >49)||
              (P[4] < 1|| P[4] >49)||
              (P[5] < 1|| P[5] >49)

              ||

              P[0] == P[1] || P[0] == P[2] || P[0] ==P[3] || P[0]== P[4] || P[0] ==P[5]||

              P[1] == P[2] || P[1] ==P[3] || P[1] ==P[4] || P[1]== P[5] ||

              P[2] == P[3] || P[2] ==P[4] || P[2] ==P[5] ||

              P[3] == P[4] || P[3] ==P[5] ||
              P[4] == P[5]
              )

              {
                  System.out.println( "Error: Please use numbers between 1 and 49 inclusive, and no repititions");

       for (int index =0; index <6; index++)
         {

        Scanner keyboard = new Scanner (System.in);
        System.out.print(" Pick # " + (index+1) + ": ");
           P[index] = keyboard.nextInt();

         }

          }


       // Part 1B": Put your   numbers pick in inmcreasing order. Sort the numbers from lowest to highest.

       int startScan, index, minIndex, minValue;

       for ( startScan= 0; startScan <(P.length-1); startScan++)

       {
     minIndex = startScan;
     minValue = P[startScan];

     for ( index = startScan +1; index <P.length; index++)
     {
      if (P[index] < minValue)
      {
       minValue = P[index];
       minIndex =index;
      }

     }
      P[minIndex] = P[startScan];
      P[startScan] =minValue;

     }

  System.out. println ( "This is the sorted list of your  numbers:");

  for (int j =0; j <P.length; j++)

   System.out. print (  P[j] + "  ");

  System.out.println ("End of part 1 \n\n");
        System.out.println();

       // Part 2: Generate 6 random numbers for lottery draw
        System.out.println( "Part2:");

       // Create a Random class object.


        // Let the lottery numbers be:

  int N1, N2, N3, N4, N5, N6 ;   // random numbers

  Random randomNumber = new Random();

        // Let

        N1=1;  N2 = 1;  N3 = 1; N4 = 1; N5=1;  N6= 1;


        while ( N1 <1 ||  N2 <1 || N3 <1 || N4 <1 ||N5 < 1 ||  N6 <1 ||

                 N1==N2 || N1==N3 || N1==N4 || N1==N5 || N1==N6||
                 N2==N3 || N2==N4 || N2==N5 || N2==N6 ||
                 N3==N4 || N3==N5 || N3==N6 ||
                 N4==N5 || N4==N6 ||
                 N5==N6
               )
        {

  N1 = randomNumber.nextInt(49);   N2 = randomNumber.nextInt(49);       N3 = randomNumber.nextInt(49);

        N4 = randomNumber.nextInt(49);     N5 = randomNumber.nextInt(49);        N6 = randomNumber.nextInt(49);

     }

       // Create an array of the Lottery random numbers

       int [] lotteryNumbers  = new int [6];

        lotteryNumbers[0] = N1;    lotteryNumbers[1] = N2;      lotteryNumbers[2] = N3;
        lotteryNumbers[3] = N4;    lotteryNumbers[4] = N5;      lotteryNumbers[5] = N6;

       System.out. println ("The Random-Draw Lottery  Numbers are:");
           for (int j =0; j <lotteryNumbers.length; j++)

           System.out. println ( "#  " + (j+1) + "\t"  + lotteryNumbers[j]);


           System.out. println ();

        // sort the numbers from lowest to highest.

       for ( startScan= 0; startScan <(lotteryNumbers.length-1); startScan++)

       {
     minIndex = startScan;
     minValue = lotteryNumbers[startScan];

     for ( index = startScan +1; index <lotteryNumbers.length; index++)
     {
      if (lotteryNumbers[index] < minValue)
      {
       minValue = lotteryNumbers[index];
       minIndex =index;
      }

     }
      lotteryNumbers[minIndex] = lotteryNumbers[startScan];
      lotteryNumbers[startScan] =minValue;

     }
          System.out. println( "This is the  sorted list of lottery-draw numbers:");

          for (int j =0; j <lotteryNumbers.length; j++)

           System.out. print ( lotteryNumbers[j] + "  ");

           //

        System.out.println( "End of part 2 \n\n");

        System.out.println();

          // Part 3: check lottery numbers - see if P[] == lotteryNumbers[]
          // Let

         System.out.println( "Part3: lottery check!"+ "\n\n");

         int count = 0;

         for ( index = 0; index <6;index++)

          {
     for (int L = 0; L <6; L++)

           if ( P[index] == lotteryNumbers[L])

            count++;
      }

         System.out.println("You matched:  " + count + " number(s)!");

          if( count == 0 )
          System.out.println( "Sorry, You are not a winner this time."+ "\n" );

          if( count == 1 )
          System.out.println( "You  win £1."+"\n" );

          if( count == 2 )
          System.out.println( "You win £2."+"\n");


          if( count == 3)

          System.out.println( "You win £3."+"\n");

          if( count == 4)

          System.out.println( "You win £4."+"\n");

          if( count == 5)

          System.out.println( "You win £50."+"\n");

          if( count == 6)

          System.out.println( "You  win the jackpot of £1000."+"\n");

 }
}

Open in new window


Thanks.
Java

Avatar of undefined
Last Comment
gatorvip

8/22/2022 - Mon
gatorvip

Use a String to hold the user name,  an int to hold the number of bets and change your array to a matrix of 20x6. Populate it based on how many bets the user wants to place.
aspnet-scotland

ASKER
Gatorvip,

Can you alter my code to show me what you mean?

Thanks.
gatorvip

Well, you currently have:

  int [] P = new int [6];
...
      int [] lotteryNumbers  = new int [6];

Open in new window


This portion of code looks to me like you're using two arrays instead of your requirement of one, unless I misread what you wrote.

What I would do is create several class variables:
String clientName;
int numBets;
int [][] lotteryNumbers = new int [20][6];

Open in new window

Where numBets will hold a user-inputted value and can be any integer from 1 to 20. The lotteryNumbers array is fixed size, but you can change that to a dynamic one and initialize it with the numBets value too.

For example, you could do this:
public static void main(String[] args)
{
 
...
	// get the user's name
	System.out.println("What is your name?");
	clientName = keyboard.next();
	
	// find the number of bets
	System.out.println("How many bets would you like to make (up to 20)?");
	numBets = keyboard.nextInt();
...

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
aspnet-scotland

ASKER
Hi gatorvip,

My requirements state to use only one class but I can use multiple arrays.

Am I best still to use your 20x6 array? If so, I'm stuggling to combine my two arrays into one as my existing code is dependant on P[index] which I am struggling to find with your approach.

Thanks.
ASKER CERTIFIED SOLUTION
gatorvip

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question