Back to The tech awesomeness
Table of contents
Java chapters

The article for today.

So the person was travelling in different country from the most spent time one for not so long.

There at some moment of time that person took his mobile device and started to investigate the application store with available applications for that country. While seeing no differences from the variety of applications in that application store, that person changed the mobile device's setting for "Country" into the country of travel.

New variety of applications appeared in that application store at the mobile device's application store at once after refreshing.

While trying to purchase one application, the application store prompted to enter the bank cards' details for payment. The previoiusly accepted bank cards' details was not accepted for this country of travel for some specific reason for this time by application store. Only the bank card details' from this country was accepted by application store, mobile payment and gift card account.

That person went and purchased the corresponding specific gift card for the application store of that country of travel. While the person found out later that it could be done online as well.

The person entered that gift card's information for own mobile device's application store's account, purchased that application of choice by that gift card's entered value and enjoyed that application's experience itself.

The person travelled back and returned to the country where spends most of the time after awhile.

Once trying to change mobile device's setting "Country" into the country of spenging the most of the time again, where the person is currently trying to achieve that, the person saw that it was not possible to do that when the balance of the gift card's value at the application store account was non-zero. That was the rule of corresponding application store at that time.

The balance of gift card's value account was at that time as of '0.05'. Too low level of gift card's value for account. No access to buy gift cards in shops for that country for corresponding application store other than online, where the person just has travelled from. And the applications' prices in application store are ending with '.,09' and '.,99' and so on.

First price lock-in?

No, because the account balance for gift card's value can be increased by purchasing another gift card in online web shop for that corresponding country again.

Well done, and now how to calculate how many new applications to purchase and which should be their corresponding prices? Should it be done manually? How much time will it take to achieve that list?

Second price lock-in?

No, because all of that data are available online and at hand and the only activity which could take unreasonable amount of time was manual calculation, which for this time for that person would consume definitely enormous amount of time if done manually and if even achieved. So then what could be missing? It could be some kind of algorithm for this task.

How to tackle the creation of such algoritm?

Initially the person attempted to search the approach for how to get around with this case using methods from combinatorics only.

Then that person collected the unique distinct prices of the applications in the application store, which would be preferrable to buy an extra one and in order to calculate the amount of another value for gift card to buy.

Now the input data for this task are at hand: price values for the extra prefferable applications to buy and the available online gift card values for one extra purchase at best case.

If the idea is to save time in cost of using several central processing unit's cycles of optimized computing device, then sophisticated algorithms as well as simple loop-and-randomize algorithms can be suitable for such case.

In this case the person chose Java programming language with its BigDecimal user-friendly interface and predictable behaviour and SecureRandom for making a suprise for choosing preferrable copies of applications by their prices to buy from.

public static void main(String[] args) {
        BigDecimal[] inputPrices = new BigDecimal[] {
            new BigDecimal("0.49") , 
            new BigDecimal("4.99") , 
            new BigDecimal("5.49") , 
            new BigDecimal("4.49") , 
            new BigDecimal("3.49") , 
            new BigDecimal("0.99") ,
            new BigDecimal("2.99") ,
            new BigDecimal("1.09") ,
            new BigDecimal("10.99") 
            //new BigDecimal("3") , 
            //new BigDecimal("2") , 
            //new BigDecimal("1") 
        };
        BigDecimal inputGiftCardValue = new BigDecimal("25.");
        BigDecimal inputLeftoverAccountValue = new BigDecimal(".07");
        BigDecimal outputPrice = inputLeftoverAccountValue.add(inputGiftCardValue);

        boolean done = findPrefferableApplicationPrices(inputPrices, outputPrice);
        
        while (!done) {
            done = findPrefferableApplicationPrices(inputPrices, outputPrice);
        }
        
    }
    
    private static boolean findPrefferableApplicationPrices(BigDecimal[] inputPrices, BigDecimal outputPrice) {
        boolean going = true;
        
        String result = "";
        BigDecimal finalBillPrice = new BigDecimal(0);
        
        BigDecimal max = findMaxElement(inputPrices);
        
        BigDecimal min = findMinElement(inputPrices);
        
        while(going) {
            BigDecimal x = inputPrices[new java.security.SecureRandom().nextInt(inputPrices.length)];
            finalBillPrice = finalBillPrice.add(x);
            result = "" + result + " + " + x;
            System.out.println(result + " = " + finalBillPrice);
            
            BigDecimal leftoverAmountToSpend = outputPrice.subtract(finalBillPrice);
            
            if(leftoverAmountToSpend.compareTo(BigDecimal.ZERO) == 1 &&
                    leftoverAmountToSpend.compareTo(max) <= 0
                    
                    && leftoverAmountToSpend.compareTo(min) >= 0 ) {
                for (int i = 0; i < inputPrices.length; i++ ) {
                    BigDecimal lastSum = finalBillPrice.add(inputPrices[i]);
                    
                    System.out.println(" left = " + leftoverAmountToSpend);
                    
                    if (lastSum.equals(outputPrice) ) {
                        System.out.println(" LASTSUM = " + lastSum);
                        going = false;
                        return true;
                    }
                }
                 
                System.out.println(" max = " + max);
                System.out.println(" min = " + min);
                System.out.println( " NOT YET " );
                    
            }
            
            if(finalBillPrice.equals(outputPrice)) {
                going = false;
                return true;
            }
            
            if(finalBillPrice.compareTo(outputPrice) == 1) {
                going = false;
            }
        }
        
        return false;
    }
    
    private static BigDecimal findMaxElement(BigDecimal[] inputPrices) {
        BigDecimal max = BigDecimal.ZERO;
        for (int i = 0; i < inputPrices.length; i++ ) {
            if(inputPrices[i].compareTo(max) == 1) {
                max = inputPrices[i];
            }
        }
        return max;
    }
    private static BigDecimal findMinElement(BigDecimal[] inputPrices) {
        BigDecimal max = findMaxElement(inputPrices);
        BigDecimal min = BigDecimal.valueOf(Double.MAX_VALUE);
        for (int i = 0; i < inputPrices.length; i++ ) {
            if(inputPrices[i].compareTo(new BigDecimal("0")) == 1 &&
                    inputPrices[i].compareTo(max) == -1
                    && inputPrices[i].compareTo(min) == -1) {
                min = inputPrices[i];
            }
        }
        return min;
    }

The person received the output as alike:


 + 0.49 + 0.49 + 2.99 + 2.99 + 2.99 + 0.49 + 2.99 + 1.09 + 2.99 + 0.99 + 0.99 + 4.49 = 23.98

So this list was full of summands, which were the prices for preferred applications for corresponding application store, where that person should buy application with those suggested prices plus one hidden price for one additional application, which the person would have a chance to find out as the last one for a purchase in order to spend the gift card's account value for getting the ability to change mobile device's setting "Country" into another country again without the contacting customer support and spending their representatives' precious time and attention to reset the accounts' balance and "Country" settings and instead in favour of spending even more in the value of the applications with regards to their prices.

The trademarks, application names, application store name and logo were hidden intentionally from this story. No monetary income gain was collected in course of progression in this story. That is because I have no idea how to achieve so.

There is no web service for this algorithm after several attempts to find in web search engines online, and this algorithm is available for tryout via editing in text editor, IDE, and launching at REPL for JRE runtime.

Price lock-out?

No.