public void work() { // get the first institution if (total < _selected.size()) { int id = _selected.get(total); double firstPrice = 0; double currentPrice = 0; double avgPriceForProgram = (TaxIncome / _selected.size()) * 0.2; // this is a price that is calculate for programs, average 20% of income // start negotiating try { // Get initial price of program LogWriter.writeInfo( "Agent -> Get program price: avgPerProgram = " + avgPriceForProgram + ", program ID = " + id); HTTPConnection connection = new HTTPConnection(storage.getServerURL()); JSONObject response = connection.connectJSON(Engine.AgentRequestProgramPrice(_taxYearID, _taxPayerID, id)); storage.setLastServerResponse(response.toString()); if (!response.getString("result").equals("OK")) { throw new Exception("Invalid response from server = " + response.getString("data")); } // read results JSONObject data = response.getJSONObject("data"); int type = data.getInt("Type"); currentPrice = data.getDouble("Price"); firstPrice = currentPrice; double lowestPrice = firstPrice; double newPrice = 0; if (type == 1) { // this is final offer - we need to set the result - and offer to client LogWriter.writeInfo("Agent -> Final offer = " + currentPrice + " program ID = " + id); setProgramPrice(id, currentPrice); } else { double tries = 0.5; while (true) { if (type == 1) { // this is final offer - we need to set the result - and offer to client LogWriter.writeInfo( "Agent -> Final offer = " + currentPrice + " program ID = " + id); setProgramPrice(id, currentPrice); break; } // we can try to continue more - to get a better deal if it is more than we want if (newPrice > firstPrice) { // obiously we cannot get betterp rice LogWriter.writeInfo( "Agent -> Cannot go higher = " + lowestPrice + " program ID = " + id); setProgramPrice(id, lowestPrice); break; } else if (currentPrice > avgPriceForProgram) { // Get initial price of program connection = new HTTPConnection(storage.getServerURL()); newPrice = avgPriceForProgram * 0.1 + avgPriceForProgram * (tries); LogWriter.writeInfo( "Agent -> Askin better price = " + newPrice + " program ID = " + id); // lowe the price by 90% response = connection.connectJSON( Engine.AgentRequestProgramLowerPrice( _taxYearID, _taxPayerID, id, newPrice)); storage.setLastServerResponse(response.toString()); if (!response.getString("result").equals("OK")) { throw new Exception( "Invalid response from server = " + response.getString("data")); } // read results data = response.getJSONObject("data"); type = data.getInt("Type"); currentPrice = data.getDouble("Price"); LogWriter.writeInfo( "Agent -> currentPrice = " + currentPrice + " lowestPrice = " + lowestPrice + " program ID = " + id); if (currentPrice < lowestPrice) { lowestPrice = currentPrice; } tries += 0.1; } else { // this is ok, we can accept that LogWriter.writeInfo( "Agent -> Best offer = " + currentPrice + " program ID = " + id); setProgramPrice(id, currentPrice); break; } } } } catch (Exception ex) { // this will exit Message msg = mHandler.obtainMessage(); Bundle b = new Bundle(); b.putString("error", "Connection failed: " + ex.getMessage()); msg.setData(b); mHandler.sendMessage(msg); LogWriter.writeException(TaxProgramsManageActivity.this, ex.getMessage(), ex); } } }