// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
public class PriceModel {
    private double x_low;
    private double y_low;
    private double x_high;
    private double y_high;
    private double min_delta;
    public PriceModel(int x_low, double y_low, int x_high, double y_high, double min_delta) {
        if(x_low > x_high) throw new IllegalArgumentException("x_low greater than x_high");
        if(y_low < y_high) throw new IllegalArgumentException("y_low less than y_high");
        if(x_high * y_high < x_low * y_low + (x_high - x_low) * min_delta) throw new IllegalArgumentException("Restriction not possible");
        this.x_low = x_low;
        this.y_low = y_low;
        this.x_high = x_high;
        this.y_high = y_high;
        this.min_delta = min_delta;
    }
    public double simpleCalc(int x) {
        if(x <= x_low) {
            return y_low;
        } else if(x >= x_high) {
            return y_high;
        } else {
            return y_low - (x - x_low) / (x_high - x_low) * (y_low - y_high);
        }
    }
    private double limit(int x) {
        return ((x + 1) * restrictedCalc(x + 1) - min_delta) / x;
    }
    public double restrictedCalc(int x) {
        if(x <= x_low) {
            return y_low;
        } else if(x >= x_high) {
            return y_high;
        } else {
            return Math.min(simpleCalc(x), limit(x));
        }
    }
    public static void main(String[] args) {
        PriceModel pm = new PriceModel(3, 100, 10, 70, 35);
        for(int i = 1; i <= 15; i++) {
            System.out.printf("%2d %6.2f %8.2f  %6.2f %8.2f\n",  i, pm.simpleCalc(i), i * pm.simpleCalc(i), pm.restrictedCalc(i), i * pm.restrictedCalc(i));
        }
    }
}
