Solved

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

Posted on 2012-12-27
5
199 Views
Last Modified: 2013-01-15
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.
0
Comment
Question by:aspnet-scotland
  • 3
  • 2
5 Comments
 
LVL 20

Expert Comment

by:gatorvip
ID: 38726857
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.
0
 

Author Comment

by:aspnet-scotland
ID: 38727985
Gatorvip,

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

Thanks.
0
 
LVL 20

Expert Comment

by:gatorvip
ID: 38730033
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

0
 

Author Comment

by:aspnet-scotland
ID: 38740788
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.
0
 
LVL 20

Accepted Solution

by:
gatorvip earned 500 total points
ID: 38741365
OK, then I misread your requirement.

Personally I would use a single array to hold the user bets because it's cleaner that way, but it's really up to you. I would set it up in the following way:

a) 1 fixed 1x6 array to hold the actual lottery numbers
b) 1 fixed 20x6 array (or 1 dynamic numBets x 6 array ) to hold the user input

Your P[index] won't really change, you'll just put it inside an additional loop that allows for the extra bets.

So, for example:
...
int numBets; // holds the number of bets
int [][] userBets = new int [20][6]; // holds the user numbers
...
for (int bet=0; bet<numBets; bet++)
{
  for (int pos=0; pos<6; pos++)
  {
    -- get user numbers here
    userBets[bet][pos] = ... read input
  }
}

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to install java on RHEL image on EC2 4 39
hibernate example using maven 12 56
Need Help! Getting a syntax error and don't understand why 3 31
jsp error 6 25
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question