[Other] How should this program flow?

Discussion in 'Programming' started by Nozumu, Apr 27, 2012.

  1. Nozumu

    Nozumu Well-Known Member

    Hey guys, I'm having some trouble with this program. The program should work like this

    [1] It asks the user to input 3 data, # of records, total quantity and total cost

    [2] From an existing database (with ID, recordname, price) it will select random records until it has achieved the number of records inputted by the user (records already picked can be picked again)

    [3] each selected record is then paired with a random quantity, the sum of all the quantities shall be equal to the amount inputted by the user

    [4] the price is multiplied by the amount paired to the record and will be the cost, the sum of all the cost should be equal to the one inputted by the user.

    So that's it. I have tried so many times but to no avail. I don't need codes, I just need someone to guide me on how the program should flow for it to work.
  2. XaTrIxX

    XaTrIxX Moderator

    I'd say, partially it depends on the programming language used, but generally I'd start off with designing DAOs (Data Access Objects), which provide basic database queries. I guess you won't really need much more than the record select.

    You then have the layer which receives this result and does your calculations. Save your result Record in a class and work with a list of (for example) RecordClasses then.

    It's important to abstract layers. You can code each of them individually and put them together after, or whatever way you like, but assure one layer works (maybe with Tests).

    Let me know if that helped you!
  3. Nozumu

    Nozumu Well-Known Member

    It did, thanks! Now I'm having problems with the conditions. As of now it can select the records and generate the numbers which sums up to the number by the user, my problem is i have no idea on how to make the total costs equal to the input.
  4. XaTrIxX

    XaTrIxX Moderator

    I'm not sure if i understood correctly, but you have to sum up to a certain value?

    This depends on how well distributed your values are, e.g. if your values make it possible, sum up as long as you get over your value, then remove a value to get to the exact sum, or remove to get lower and add another one to sum it up.

    I hope this helps, but maybe i misunderstood totally.
  5. Nozumu

    Nozumu Well-Known Member

    Let's say that I input the following:

    # Records: 5
    Quantity: 15
    Total Cost: 800

    Then It should Output something like this: (Record Name and Price is from a database while Quantity is randomly generated)

    TOTAL| |15|800

    As of now the random selection of records and quantity works, but I have no idea on how to make the total cost equal up too. Will looping the quantity randomization work or will it just make an endless loop? Does it have to use a really complex selection system or what?
  6. XaTrIxX

    XaTrIxX Moderator

    I described a way of doing it. Randomization loop will work also if you have a half-decent random function!