// 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) 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Estimator extends JFrame {
    private static QQ[] languages = { new QQ(20, "4GL"),
                                      new QQ(32, "Basic"),
                                      new QQ(49, "Ada"),
                                      new QQ(53, "Java"),
                                      new QQ(55, "C++"),
                                      new QQ(71, "Fortran"),
                                      new QQ(80, "PL/I"),
                                      new QQ(91, "Pascal"),
                                      new QQ(91, "COBOL"),
                                      new QQ(128, "C"),
                                      new QQ(213, "Assembler") };
    private static QQ[] productComplexities = { new QQ(4, "Embedded"),
                                         new QQ(8, "Real time & OS close"),
                                         new QQ(12, "OS close"),
                                         new QQ(15, "Normal"),
                                         new QQ(21, "Simple logic & IO"),
                                         new QQ(28, "Simple logic") };
    private static QQ[] maturityLevels = { new QQ(0, "Project start"),
                                           new QQ(52, "Requirements complete"),
                                           new QQ(84, "Design complete"),
                                           new QQ(100, "Development complete") };
    private static QQ[] analystCapabilities = { new QQ(0.71, "Motivated & Experience"),
                                         new QQ(0.86, "Motivated or experienced"),
                                         new QQ(1.00, "Traditional"),
                                         new QQ(1.19, "Demotivated or inexperienced"),
                                         new QQ(1.46, "Demotivated and inexperienced") };
    private static QQ[] programmerCapabilities = { new QQ(0.70, "Motivated & Experience"),
                                            new QQ(0.86, "Motivated or experienced"),
                                            new QQ(1.00, "Traditional"),
                                            new QQ(1.17, "Demotivated or inexperienced"),
                                            new QQ(1.42, "Demotivated and inexperienced") };
    private static QQ[] modernPracticesLevels = { new QQ(0.83, "All (CMMI level 5)"),
                                           new QQ(0.91, "Some (CMMI level 4)"),
                                           new QQ(1.00, "Most (CMMI level 3)"),
                                           new QQ(1.10, "Few (CMMI level 2)"),
                                           new QQ(1.21, "None (CMMI level 1)") };
    private static QQ[] toolsLevels = { new QQ(0.83, "GUI IDE"),
                                 new QQ(0.91, "Text IDE"),
                                 new QQ(1.00, "Full screen editor"),
                                 new QQ(1.10, "Line editor"),
                                 new QQ(1.24, "Punched cards") };
    private static QQ[] toolsVolatilities = { new QQ(1.00, "Major: none, Minor: every year"),
                                       new QQ(1.07, "Major: every year, Minor: every month"),
                                       new QQ(1.13, "Major: every 6 months, Minor: every 2 weeks"),
                                       new QQ(1.19, "Major: every 2 months, Minor: every week"),
                                       new QQ(1.25, "Major: every 2 weeks, Minor: every 2 days") };
    private static QQ[] practicesVolatilities = { new QQ(1.00, "Major: none, Minor: every year"),
                                           new QQ(1.07, "Major: every year, Minor: every month"),
                                           new QQ(1.15, "Major: every 6 months, Minor: every 2 weeks"),
                                           new QQ(1.23, "Major: every 2 months, Minor: every week"),
                                           new QQ(1.30, "Major: every 2 weeks, Minor: every 2 days") };
    private static QQ[] multiOrganizationLevels = { new QQ(1.00, "Single organization"),
                                             new QQ(1.09, "Multiple organizations same company"),
                                             new QQ(1.15, "Multiple companies same locations"),
                                             new QQ(1.25, "Multiple companies multiple locations") };
    private static QQ[] multiLocationLevels = { new QQ(1.00, "Single location"),
                                         new QQ(1.07, "Multiple locations same time zone"),
                                         new QQ(1.13, "Multiple locations 1 hour difference"),
                                         new QQ(1.20, "Multiple locations more than 1 hour difference") };
    public static double calculateSizeGrowth(int productComplexity, int maturity) {
        if(productComplexity < 8) {
            return 1 + 0.50 * (1 - maturity/100.0);
        } else if(8 <= productComplexity && productComplexity <= 15) {
            return 1 + (0.90 - productComplexity * 0.05)  * (1 - maturity/100.0);
        } else {
            return 1 + 0.15 * (1 - maturity/100.0);
        }
    }
    public static double calculateConstantTechnologyBasic(double analystCability, double programmerCapability, double modernPracticesLevel, double toolsLevel) {
        double T = analystCability * programmerCapability * modernPracticesLevel * toolsLevel;
        double v = -3.70945 * Math.log(T / 4.11);
        double a = v / 5;
        return 2000 * Math.exp(a);
    }
    public static double calculateConstantTechnologyEffective(double constantTechnologyBasic, double toolsVolatility, double practicesVolatility, double multiOrganizationLevel, double multiLocationLevel) {
        return constantTechnologyBasic / (toolsVolatility * practicesVolatility * multiOrganizationLevel * multiLocationLevel);
    }
    public static double calculateSourceLinesEffective(int sourceLinesNew, int sourceLinesModified, int sourceLinesReused) {
        return 1.00 * sourceLinesNew + 0.75 * sourceLinesModified + 0.20 * sourceLinesReused;  
    }
    public static double calculateEffortDevelopment(double productComplexity, double constantTechnologyEffective, double sourceLinesEffective) {
        return 4.72 * Math.pow(productComplexity, 0.4) / Math.pow(constantTechnologyEffective, 1.2) * Math.pow(sourceLinesEffective, 1.2);
    }
    public static double calculateTimeDevelopment(double productComplexity, double effortDevelopment) {
        return 7.15 / Math.cbrt(productComplexity) * Math.cbrt(effortDevelopment);
    }
    public static double calculateTimeTotal(double timeDevelopment) {
        return 1.28 * timeDevelopment;
    }
    private JTextField newloc;
    private JTextField modloc;
    private JTextField reuseloc;
    private JComboBox estlang;
    private JComboBox impllang;
    private JComboBox complexity;
    private JComboBox maturity;
    private JComboBox analyst;
    private JComboBox programmer;
    private JComboBox practices;
    private JComboBox tools;
    private JComboBox changeTools;
    private JComboBox changePractices;
    private JComboBox organization;
    private JComboBox location;
    private JLabel personmonths;
    private JLabel months;
    private JLabel persons;
    private JLabel productivity;
    private JLabel total;
    private boolean lang;
    private void add2(JPanel p, String lbl, JComponent c) {
        JPanel p2 = new JPanel();
        p2.setBorder(BorderFactory.createEmptyBorder(2, 10, 2, 10));
        p2.setLayout(new GridLayout(1, 2));
        p2.add(new JLabel(lbl + ":"));
        p2.add(c);
        p.add(p2);
    }
    public Estimator() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(new BorderLayout());
        JPanel locpanel = new JPanel();
        locpanel.setLayout(new GridLayout(1, 3));
        newloc = new JTextField();
        add2(locpanel, "New lines", newloc);
        modloc = new JTextField();
        add2(locpanel, "Modified lines", modloc);
        reuseloc = new JTextField();
        add2(locpanel, "Reused lines", reuseloc);
        getContentPane().add(locpanel, BorderLayout.NORTH);
        JPanel optpanel = new JPanel();
        optpanel.setLayout(new GridLayout(10, 1));
        complexity = new JComboBox(productComplexities);
        complexity.setSelectedIndex(3);
        add2(optpanel, "Complexity", complexity);
        maturity = new JComboBox(maturityLevels);
        maturity.setSelectedIndex(0);
        add2(optpanel, "Project maturity", maturity);
        analyst = new JComboBox(analystCapabilities);
        analyst.setSelectedIndex(2);
        add2(optpanel, "Analyst", analyst);
        programmer = new JComboBox(programmerCapabilities);
        programmer.setSelectedIndex(2);
        add2(optpanel, "Programmer", programmer);
        practices = new JComboBox(modernPracticesLevels);
        practices.setSelectedIndex(4);
        add2(optpanel, "Best practices", practices);
        tools = new JComboBox(toolsLevels);
        tools.setSelectedIndex(0);
        add2(optpanel, "Tools", tools);
        changeTools = new JComboBox(toolsVolatilities);
        changeTools.setSelectedIndex(0);
        add2(optpanel, "Tools change", changeTools);
        changePractices = new JComboBox(practicesVolatilities);
        changePractices.setSelectedIndex(0);
        add2(optpanel, "Practices change", changePractices);
        organization = new JComboBox(multiOrganizationLevels);
        organization.setSelectedIndex(0);
        add2(optpanel, "Organization", organization);
        location = new JComboBox(multiLocationLevels);
        location.setSelectedIndex(0);
        add2(optpanel, "Location", location);
        getContentPane().add(optpanel, BorderLayout.CENTER);
        JPanel respanel = new JPanel();
        respanel.setBorder(BorderFactory.createEmptyBorder(2, 10, 2, 10));
        respanel.setLayout(new BorderLayout());
        JPanel respanel2 = new JPanel();
        respanel2.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        respanel2.setLayout(new GridLayout(3, 2));
        personmonths = new JLabel();
        add2(respanel2, "Development person months", personmonths);
        months = new JLabel();
        add2(respanel2, "Development months", months);
        persons = new JLabel();
        add2(respanel2, "Development persons", persons);
        productivity = new JLabel();
        add2(respanel2, "Lines/month/person", productivity);
        total = new JLabel();
        add2(respanel2, "Total person months", total);
        respanel.add(respanel2, BorderLayout.CENTER);
        JButton calc = new JButton("Calculate");
        calc.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                double ac = analystCapabilities[analyst.getSelectedIndex()].getValue();
                double pc = programmerCapabilities[programmer.getSelectedIndex()].getValue();
                double mpl = modernPracticesLevels[practices.getSelectedIndex()].getValue();
                double tl = toolsLevels[tools.getSelectedIndex()].getValue();
                double ctb = calculateConstantTechnologyBasic(ac, pc, mpl, tl);
                double tv = toolsVolatilities[changeTools.getSelectedIndex()].getValue();
                double pv = practicesVolatilities[changePractices.getSelectedIndex()].getValue();
                double mol = multiOrganizationLevels[organization.getSelectedIndex()].getValue();
                double mll = multiLocationLevels[location.getSelectedIndex()].getValue();
                double cte = calculateConstantTechnologyEffective(ctb, tv, pv, mol, mll);
                int nl = Integer.parseInt(newloc.getText());
                int ml = Integer.parseInt(modloc.getText());
                int rul = Integer.parseInt(reuseloc.getText());
                int c = (int)productComplexities[complexity.getSelectedIndex()].getValue();
                int m = (int)maturityLevels[maturity.getSelectedIndex()].getValue();
                double le = calculateSizeGrowth(c, m) * calculateSourceLinesEffective(nl, ml, rul);
                if(lang) {
                    le = languages[impllang.getSelectedIndex()].getValue() / languages[estlang.getSelectedIndex()].getValue() * le;
                }
                double pemo = calculateEffortDevelopment(c, cte, le);
                double mo = calculateTimeDevelopment(c, pemo);
                double pe = pemo / mo;
                double p = le / pemo;
                double tot = calculateTimeTotal(pemo);
                personmonths.setText(Long.toString(Math.round(pemo)));
                months.setText(Long.toString(Math.round(mo)));
                persons.setText(Long.toString(Math.round(pe)));
                productivity.setText(Long.toString(Math.round(p)));
                total.setText(Long.toString(Math.round(tot)));
            }} );
        respanel.add(calc, BorderLayout.EAST);
        getContentPane().add(respanel, BorderLayout.SOUTH);
        lang = false;
        newloc.addKeyListener(new KeyListener() {
            public void keyPressed(KeyEvent e) {
                if(!lang && e.getKeyCode() ==  'A' && (e.getModifiersEx() & KeyEvent.CTRL_DOWN_MASK) != 0) {
                    JPanel optpanel = (JPanel)getContentPane().getComponent(1);
                    optpanel.setLayout(new GridLayout(12, 1));
                    estlang = new JComboBox(languages);
                    estlang.setSelectedIndex(8);
                    add2(optpanel, "Language estimation", estlang);
                    impllang = new JComboBox(languages);
                    impllang.setSelectedIndex(8);
                    add2(optpanel, "Language implementation", impllang);
                    optpanel.repaint();
                    pack();
                    lang = true;
                }
            }
            public void keyReleased(KeyEvent e) {
            }
            public void keyTyped(KeyEvent e) {
            }
        });
        setTitle("Software Development Estimation");
        pack();
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame f = new Estimator();
                f.setVisible(true);
            }
        });
    }
}

class QQ {
    private double value;
    private String description;
    public QQ(double value, String description) {
        this.value = value;
        this.description = description;
    }
    public double getValue() {
        return value;
    }
    public void setValue(double value) {
        this.value = value;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String toString() {
        return description;
    }
}
