?
Solved

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

Posted on 2012-12-27
5
Medium Priority
?
205 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month10 days, 17 hours left to enroll

770 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