Java Lottery - relating arrays to each other

Hello all and thank you for taking the time to read my problem.
I am trying to create a program that asks the user for their name, then how man tickets they would like, then they enter 6 numbers for each ticket.
The maximum amount of 6 number tickets that can be bought is 20.
So for example user1 could get three tickets, user2 could get 10 tickets and user3 could get 7 tickets (20 in total).
My main headache is how to relate the ticket numbers entered with the persons username.
I assume that if user1 gets three tickets these are stored in an array [6] [20] but then when user2 puts their lines on how do I print out at the end who has which tickets?
Also the maximum array size for tickets is [6] [20]...but what if only one user buys 3 tickets and does not use up the remaining 17 tickets, does this affect how I define the array?
Thanks again, all help welcome!

This is sort of what they were talking about, this is an object oriented approach.
There should be no 2D arrays, just arrays of Objects that contain data about themselves or perhaps more Objects.
Try to make your code look more like this.
import java.util.*;
public class Lottery {
    private LinkedList<Person> line = new LinkedList<Person>();
    public Person nextInLine() {
        line.add( new Person() );
        return line.getLast();
    public void showTicketsPurchased() {
        System.out.println( "\nTickets Purchased:");
        for( Person p: line ) {
            p.showTicketsPurchased();
    class Person {
        private LinkedList<Ticket> tickets= new LinkedList<Ticket>();
        void buyTicket( int num1, int num2, int num3, int num4, int num5, int num6 ) {
            try {
                tickets.add( Ticket.sellTicket( num1, num2, num3, num4, num5, num6 ) );
                System.out.println( "Bought Ticket: " + tickets.getLast() );
            } catch (NoSuchElementException nsee) {
                System.out.println( nsee.getMessage() );
                System.out.println( "Could not buy ticket, it's sold out");
        public void showTicketsPurchased() {
            for ( Ticket t: tickets ) {
                System.out.println( t );
    public static void main( String[] args ) {
        Lottery lottery = new Lottery();
        Person servingThisPerson;
        //First person in line
        servingThisPerson = lottery.nextInLine();
        servingThisPerson.buyTicket( 1,2,3,4,5,6 );
        servingThisPerson.buyTicket( 2,3,4,5,6,7 );
        //Second person in line
        servingThisPerson = lottery.nextInLine();
        servingThisPerson.buyTicket( 10, 20, 30, 40, 50, 60 );
        servingThisPerson.buyTicket( 20, 30, 40, 50, 60, 70 );
        lottery.showTicketsPurchased();
import java.util.*;
public class Ticket {
    public static final int MAX_NUM_TICKETS = 20;
    private static int numTicketsSold = 0;
    private int[] numbers;
    private Ticket( int num1, int num2, int num3, int num4, int num5, int num6 ) {
        numbers = new int[]{ num1, num2, num3, num4, num5, num6 };
        numTicketsSold++;
    public static Ticket sellTicket( int num1, int num2, int num3, int num4, int num5, int num6 ) {
        if( numTicketsSold < MAX_NUM_TICKETS ) {
            return new Ticket( num1, num2, num3, num4, num5, num6 );
        } else {
            throw new NoSuchElementException("Tickets sold out");
     * Don't want people to change the tickets numbers after it has been created.
     * @return copy of tickets numbers, not the original array
    public int[] getTicketNumbers() {
        return Arrays.copyOf( numbers, numbers.length );
    public String toString() {
        return Arrays.toString( numbers );
}Brushfire,

Similar Messages

  • SES: relate attributes to each other

    Hi,
    I am implementing a SES based index. In this index I store Customer data.
    Most of the customers have multiple addresses, and here my problem is situated.
    When I perform a search, the different addresses get mixed up.
    For example, when I have a customer with the following addresses
    Address 1: street = Baker Street city = London
    Address 2: street = Town Street city = Liverpool
    and another customer with the following addresses
    Address 1: street = Baker Street city = Liverpool
    Address 2: street = Town Street city = London
    I find back both customers when I search for street = Baker Street and city = London
    In my indexing class, in the method GET_ATTRIBUTE_VALUES I have given each address line a different ROW_COUNTER as this parameter suggest it can relate the address data to each other.
    Has anybody got an idea how I can relate my address data to each other, so my search returns accurate results?
    Thanks
    Mario
    PS: I don't want to use joined indexes for the addresses, as I already have build joined indexes with Sales Transactions where I will need to search on Sales Transaction Data combined with address data and customer master data.

    My experiments with SES showed that here is what is needed:
    - you should inform TREX that your attributes (city and street) are related by putting them into a single attribute group
    - attributes' internal(as seen in trexadmin) type should be String (and not Text). By default only quite a short abap string (25 or 40 CHARs) gets converted by SES as String. My values were longer so I had to force String type for them, which is, I'm afraid, unsupported.
    Also there seem to be some bugs with this sort of search in TREX 7.00 standalone (which I have). Release notes for 7.10 promise corrections

  • Help: Problem With Seperate Arrays Overwriting Each Other!

    Hi,
    I have this function that randomly allocates free time to coursework blocks. This method works because I have tested numerous times and it always prints of different allocations of time every time its called.
    The problem that I have is that I have this method that called this method twice to store each different call within a separate ArrayList.
    When I make the first call the block are assigned to the array fine. But when I called it a second time to store the next result within a separate Array the first Array result are overwritten . Any body have any idea why this is happening, I have asked numerous friends and even my supervisor and he can't find anything wrong with it. Any help would be very helpful...
    Code:
    public ArrayList<CourseworkTimeBlock> courseworkAllocation(ArrayList timetable, ArrayList<CourseworkTimeBlock> cCourseworkBlocks){
              // Calculates and stores the free "time blocks" that can be used
              // to allocate coursework.
              ArrayList<Integer> freeTimeBlocks = new ArrayList<Integer>();
              freeTimeBlocks = freeBlocks(timetable);
              Random random = new Random();
              ArrayList<CourseworkTimeBlock> courseworkTimeBlocks = new ArrayList<CourseworkTimeBlock>();
              for(int i = 0; i < cCourseworkBlocks.size(); i++){
                   int allocateTo = -100;
                   int randomPoint = 0;
                   CourseworkTimeBlock courseworkBlock;
                   courseworkBlock = cCourseworkBlocks.get(i);
                   while(allocateTo < 0){
                        randomPoint = random.nextInt(freeTimeBlocks.size());
                        allocateTo = freeTimeBlocks.get(randomPoint);
                   courseworkBlock.setBlockAllocated(allocateTo);
                   courseworkTimeBlocks.add(courseworkBlock);
                   freeTimeBlocks.set(randomPoint, -1 );
              for( int i = 0; i < courseworkTimeBlocks.size(); i++){
                   System.out.println("CW: " + courseworkTimeBlocks.get(i).getBlockAllocated());
              return courseworkTimeBlocks;
         }These are the calls I make in a separate method:
    ArrayList<CourseworkTimeBlock> courseworkTimetableOne = new ArrayList<CourseworkTimeBlock>();
    ArrayList<CourseworkTimeBlock> courseworkTimetableTwo = new ArrayList<CourseworkTimeBlock>();
             courseworkTimetableOne = courseworkAllocation(fixedTimetable, courseworkBlocks);
             courseworkTimetableTwo = courseworkAllocation(fixedTimetable, courseworkBlocks);Edited by: ChrisUK on Mar 1, 2008 9:11 AM

    ChrisUK wrote:
    Thanks for the reply.
    In terms of the:
    courseworkTimetableOne = courseworkAllocation(fixedTimetable, courseworkBlocks);
    courseworkTimetableTwo = courseworkAllocation(fixedTimetable, courseworkBlocks);They are both supposed to pass in the fixedTimetable object, the reason being is that this is a timetable and is passed through in order to find it "free time" blocks.
    The courseworkTimetableOne and courseworkTimetableTwo are just different representations of how the coursework time blocks could be allocated to that given fixedtimetable. You're calling the same method with the same parameters, so both references are going to point to the same object. It's the same as if you just replaced the second line with courseworkTimetableTwo = courseworkTimetableOne.
    You've got two references pointing to the same object.
    The fact that you do this first:
    ArrayList<CourseworkTimeBlock> courseworkTimetableOne = new ArrayList<CourseworkTimeBlock>();
    ArrayList<CourseworkTimeBlock> courseworkTimetableTwo = new ArrayList<CourseworkTimeBlock>();means nothing. You create the two new ArrayLists, but then this
    courseworkTimetableOne = courseworkAllocation(fixedTimetable, courseworkBlocks);
    courseworkTimetableTwo = courseworkAllocation(fixedTimetable, courseworkBlocks);gets rid of them. The new ArrayLists you just created are lost.
    Remember when you assign to a reference variable such as courseworkTimetableOne =, all that does is copy a reference so now courseworkTimetableOne is pointing to the same object that whatever is on the RHS is pointing to.
    If you want two different lists, then either courseworkAllocation should make a copy of its internal list and return that, or else the caller should do it.
    >
    By the way what does "SSCCE" mean?[http://mindprod.com/jgloss/sscce.html|http://mindprod.com/jgloss/sscce.html]
    [http://homepage1.nifty.com/algafield/sscce.html|http://homepage1.nifty.com/algafield/sscce.html]

  • Assigning two dimensional array to each other

    Hi All,
    I have two two-dimensional arrays and I want to assign one's second dimension's values as the other's second dimension's values in this way:
            int userCounter = 0;
            for (int userID : mostCommonUsers) {
                reducedTrainingVectorForSimilarity[userCounter] = trainingVectorForSimilarity[userID];
                userCounter++;
            }In the above code, I have an *"ArrayList<Integer> mostCommonUsers"*, and iterate through this. I'm using the values inside this array list, as a first dimension's index number of the *"trainingVectorForSimilarity"*, and assigning all the values in the second dimension to the left-hand-side array's second dimension. Both array has fixed length of second dimension. So, am I right by doing this, or I should make a for-loop to iterate over the individual values on the second dimensions of both arrays?
    Another similar question: In the below code, *"trainingVectorForSimilarity"* is an *"float[][]"*, so basically I assign all the second dimensions' values into a newly created array named *"trainingUserRatings"*. Again, I assumed that if the corresponding first dimesion's second dimension values will be assign without any iteration over each values.
    for (int i = 0; i < trainingVectorForSimilarity.length; i++) {
                float[] trainingUserRatings = trainingVectorForSimilarity;
    similarity[i] = calculateCosineSimilarity(testingVectorForSimilarity, trainingUserRatings);
    Am I right for both cases?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

    Here is the output for the code that you sent:
    [[1, 2, 3], [4, 5, 6]]
    [[1, 2, 3], [4, 5, 6]]
    [[1, 2, 3], [4, 5, 6]]
    [[1, 2, 3], [4, 5, 6]]
    [[1, 2, 3], [4, 999999, 6]]
    [[1, 2, 3], [4, 999999, 6]]
    [[1, 2, 3], [4, 999999, 6]]
    [[1, 2, 3], [4, 5, 6]]
    [null, [4, 999999, 6]]
    [null, [4, 999999, 6]]
    [[1, 2, 3], [4, 999999, 6]]
    [[1, 2, 3], [4, 5, 6]]I confused that even making an array copy still refers the same object but not a copy of that object. For example:
    float[][] trainingVectorForSimilarity is defined and initialized.
                float[] trainingUserRatings = new float[trainingVectorForSimilarity.length];
    trainingUserRatings = trainingVectorForSimilarity[i];
    is the same withfloat[] trainingUserRatings = new float[trainingVectorForSimilarity[i].length];
    System.arraycopy(trainingVectorForSimilarity[i], 0, trainingUserRatings, 0, trainingUserRatings.length);

  • JAXB - when java beans contain lists of each other

    I have two classes:
    class A {
    private List<B> bList;
    class B {
    private List<A> aList;
    }It is like this due to the relationships they have in the database and our beans are mapped that way. When putting through JAXB , however, I would like the XML to only have A as the parent and then the list of B, not each B to list it's list of A, if that makes any sense.
    So I want
    <A>
    <B>
    <B>
    </A>
    not
    <A>
    <B>
    <A>
    </A>

    I have two classes:
    class A {
    private List<B> bList;
    class B {
    private List<A> aList;
    }It is like this due to the relationships they have in the database and our beans are mapped that way. When putting through JAXB , however, I would like the XML to only have A as the parent and then the list of B, not each B to list it's list of A, if that makes any sense.
    So I want
    <A>
    <B>
    <B>
    </A>
    not
    <A>
    <B>
    <A>
    </A>

  • I can't seem to access new mail on my I pad . I can only see strings of messages that are related to each other. I also have lost the ability to search for messages that are no longer on display on my I pad.

    I cannot seem to access properly any more the new e mail messages on my I pad. I can only see strings of messages that relate to each other.
    Also I cannot search for old messages that have now disappeared . The search bar has disappeared.
    Tom

    Try a reset: hold down the home button along with the power button until you see the Apple, then let go.

  • Is it okay to home share with my roommate who is not related to me if we do not import each other's music?

    Is it okay to home share with my roommate who is not related to me if we do not import each other's music?

    Thanks I will try that . . . . the issue became worse when we recently updated to IOS6 - now all three of us get everything . . . . I was trying to avoid separate Apple ID's and emails but that might be the solution based on what I have read on here . . . .

  • Related emails not folding into each other in one folder / but do in all others

    It really bizarre for me, as my other folders also have mail routed to them via various rules, and related messages fold neatly into each other, with a little triangle next to them, indicating that there is a whole chain in there.
    But in this one particular mail folder, each message shows up one by one, I am not sure why?
    The rules route messages into it by subject line (i.e. if it contains "zee" move it to that folder).
    Can anyone explain?
    Thank you!
    Zebbler

    Hello eporon,
    the only possibility to create a DVD with several movies and have the option to either play the parts separatly or "play all" as in commercial DVDs is to duplicate your movie(s).
    This means you have to import the individual movies (as you already did), then create a new movie where you join the parts into one. Now import that movie as well and name the menu button "play all".
    Important: this can only work if the combined parts are less than 60 min long (DVD-R is max 120 min).
    Alternatively you could create a single movie out of the four parts and place chapter markers for each individual title (ceremony, reception...) the let iDVD create a chapter submenu. Now the separate titles can be accessed from there.
    hope this helps
    mish

  • Are this OSPF LSA relate to each other ???

    OSPF neighbor relationships progress
    1) Down State
    2) Init State
    OSPF routers send Type 1 (hello)
    3) Two-Way State
    4) ExStart State (Type 2)
    5) Exchange State
    6) Loading State (type 3)
    7) Full Adjacency
    ALSO,
    OSPF Area Types also use LSA exchange between routers or area
    Type 1 – generate by each router for each area it belongs to, flood only within particular area, describe the states of the router’s link to the area.
    Type 2 – generate by DR in multi-access networks, flood only within the area that contain the network, describe the set of routes attched to a particular network.
    Type 3 – Orgin by ABR, flood throughout the backbone area to other ABRs, describe the links between ABR and the internal routes of a local area.
    Type 4 – Orgin by ABR, flood throughout the backbone area to other ABRs, describe routes to ASBRs.
    Type 5 – Orgin by ASBR, describe the routes to destinations external to the AS, flood throughout an OSPF AS.
    The questions are this LSA relate to each other or it just happen like this ???

    Hi Friend,
    There is no relation between the type of LSA (1,2,3,4,5,7) exchanged between the routers within an area and between the areas to exchange the link state information and the packets which are used to form an ospf neigh and adjancy relationship.
    Taking an example LSA 5 which is used to carry the external route information into an area is not at all related to type 5 LSA which you are talking for formaing a naighbor relationship.
    I think the neighbor relationship is formed using hello packet, dd packet and LSU and LSR packets which you may name as type of LSA's. but these are not at all related to LSA's which are used to carry route information between the areas and within areas.
    HTH
    Ankur

  • Array elements equal to each other?

    Hi,
    Maybe a very simple question but i haven't found the answer here...
    I have a 1D Array with let's say 4 elements, and if all elements are equal to each other
    (1,1,1,1)  then it should return true
    if one element is other then the rest
    (1,2,1,1)  it should return false...
    How to build this simple?
    Best regards,
    Thijs
    Solved!
    Go to Solution.

    Hi Thijs,
    you need to compare one element of the array with the remaining ones:
    To your own approach:
    - RubeGoldberg due to indexing the same element of the array in each iteration
    - RubeGoldberg due to using a loop where polymorphism will do the same
    - wrong labeling of the output: you need to AND all booleans, but not ORing them
    - the AutoCleanup tool is pretty good at such small snippets…
    Best regards,
    GerdW
    CLAD, using 2009SP1 + LV2011SP1 + LV2014SP1 on WinXP+Win7+cRIO
    Kudos are welcome

  • How to call java method having array as argument from c++ ?

    Hello sir,
    how to call java method having array as arguments from c++;
    here is java code which is called from c++
    class PQR {
         public void xyz(int[] ia) {
         System.out.println("hi");
              for (int i = 0; i < ia.length; i++)
                   System.out.println(ia);
    suppose all jvm invocation is done...                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

    For someone well versed in java, C++ and JNI although tedious that should be obvious.
    For someone not well versed in all three it is going to be very difficult.
    Even for someone that does have knowledge in all of those areas coming up with a C++ interface that reflects that functionality in a dynamic way such that anyone is will to use it is going to be quite an adventure.
    At any rate to start building it you do exactly the same thing that you would in java.
    1. Extract everything in the jar via the zip package
    2. For each found instance extract all of the methods, return types, parameters, etc and build a description tree for each class.
    Doing all of that in C++ is going to take a LOT of code. If someone wanted an estimate from me it would take me 6 months to do it. And before I would even attempt it I would get them to explain to me in detail exactly how they thought they were going to use it when I was done because I can't see any reasonable way to do that.
    I left out the description tree itself. I suppose you could duplicate the entire reflection api in C++.
    Now perhaps if it was much, much more constrained, like to only those classes that implement a single interface then that would be more reasonable.

  • Two ear file communicating with each other

    HI,
    I have one doubt,
    I Have two ear files (two webservices actually) deployed succesfully in the same application server (Weblogic7.0). Let us say them a.ear and b.ear
    In a.ear there are class say A.class and B.class. In B.java file there is a staic HashMap which stores some object based on some id. Also in B.java there is static method which takes the id as a parameter and return the corrospojnding object stored in the static hasmap.
    In b.ear file there class say B.class and C.class. Note that this ear file has its own copy of B.class file. In C.java file i create an object (though i dont need create being it a static method) of B.java and calls the static object passing it the id and expecting it that it will return me the object stored in the Hashmap. it returns me null though it has the objects stored for that id.
    Is it because both ear files has their own copy of B.class. but i though that that they are running in the same jvm in application server so it should not be a problem. if this is the reason then my problem will be solved if i put them in same ear file.
    Can anybody put his insights into his experiences and lemme know what is the cause of problem and solution and for my knowledge why it is happening.
    Best Regards,
    Akhil Nagpal

    You're right that the two have different copies of the class. This is because the EJB container uses a different classloader for different applications, to prevent statics clashing with each other and other related security reasons.
    Usually if you want to communicate from one to the other, you would use the EJB mechanism itself. The JNDI names should be global (from simple tests I did in the past, this seems to be the case) so that one application can look up an EJB from the other.

  • Problem with java applet and array of arrays

    hi!
    i'm passing an array of arrays from java applet using
    JSObject.getWindow(applet).call("jsFunction", array(array(), array()) );
    in every other browser than safari 4.0.2 it's no problem to iterate over this array. in safari "array.length" is undefined. is such construction supported in safari's js engine?
    Message was edited by: quaintpl
    Message was edited by: quaintpl
    Message was edited by: quaintpl
    Message was edited by: quaintpl
    Message was edited by: quaintpl

    Thanks for the answer but the problem is the type of object of method and how from pl/sql is posiblle to call.
    The method waiting a ArrayofAlicIva, but if i define this object is not posible to set the object inside the array because the wsdl not have this functions.
    I need to define array of objects but the object is inside is the diferent type of array.
    If i Define the array of object correct to object inside, the method expect that the other array type.
    Is a Deadlock ??
    The solution in Java is Simple
    AlicIva[] alicIva = new AlicIva[1];
    alicIva[0]= new AlicIva();
    alicIva[0].setId(Short.parseShort(1));
    fedr[0].setIva(alicIva);
    this is the method imported in java class to form
    -- Method: setIva (LArrayOfAlicIva;)V
    PROCEDURE setIva(
    obj ORA_JAVA.JOBJECT,
    a0 ORA_JAVA.JOBJECT) IS
    BEGIN
    args := JNI.CREATE_ARG_LIST(1);
    JNI.ADD_OBJECT_ARG(args, a0, 'ArrayOfAlicIva');
    JNI.CALL_VOID_METHOD(FALSE, obj, 'FECAEDetRequest', 'setIva', '(LArrayOfAlicIva;)V', args);
    END;

  • TS4006 I want to set up "find my ipad" on my sister's ipad, so we can find each other's iPads. However, this seems to require differing individual cloud email addresses, so one ipad wouldn't recognise the other one. How do I get round this?

    I want to set up "find my ipad" on my sister's ipad, so we can find each other's iPads. However, this seems to require differing individual cloud email addresses, so one ipad wouldn't recognise the other one. How do I get round this?

    Many thanks for the quick response.
    I'm fine with using the same ID for iCloud, so I'll give that a try.
    You mention that a lost ipad can be found from any computer. As a relative computer novice, I didn't know this. How would I go about carrying out such a search?
    I don't think I'd use the find friends app. My concern is to find a lost or stolen ipad.
    Thanks

  • Classes are not visible to each other in a same package

    Hi,
    I have a question. How to make all classes in a same package visible to each other? For example, I have two classes below, T1.java and T2.java:
    Below is T1.java
    package thesis;
    public class T1 {
         public T1(){}
         public void func1(){
              System.out.println("This is in T1 class");
    Below is T2.java
    package thesis;
    public class T2 {
         public T2(){}
         public void func2(){
              System.out.println("This is in T2");
         public static void main(String[] args){
              T1 t1 = new T1();
              t1.func1();
              T2 t2 = new T2();
              t2.func2();
    I create a directory named thesis and put T1.java and T2.java under this folder. But when I try to compile T2.java, it give me error message, seems that T2.java cannot recognize T1.class.
    So, can anyone solve this problem for me. Thanks in advance.
    jmling

    Make sure your classpath env variable is set correctly!
    to compile these classes you should have something like:
    C: > javac thesis\T2.java
    if your classpath is set up correctly (default is CLASSPATH=.) then the above command should work.
    If your still having problems, try:
    C: > javac thesis\*.java
    or compile T1.java first.
    Anthony

Maybe you are looking for