Advertisement
TermSpar

Planned Economy

May 12th, 2017
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.99 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Desktop;
  3. import java.awt.Font;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.MouseAdapter;
  7. import java.awt.event.MouseEvent;
  8. import java.io.IOException;
  9. import java.net.URI;
  10. import java.net.URISyntaxException;
  11. import java.util.Random;
  12.  
  13. import javax.swing.JButton;
  14. import javax.swing.JFrame;
  15. import javax.swing.JLabel;
  16. import javax.swing.JOptionPane;
  17. import javax.swing.JScrollPane;
  18. import javax.swing.JTable;
  19. import javax.swing.JTextField;
  20. import javax.swing.table.DefaultTableModel;
  21.  
  22. public class Main {
  23.  
  24.     static boolean isRunning = false;
  25.     static boolean pricesSet = false;
  26.     static boolean changeWorth = true;
  27.    
  28.     static int numberInvests = 0;
  29.     static int moneyLoss = 100;
  30.    
  31.     static int monthsPassed = 2;
  32.    
  33.     static boolean newInvest = true;
  34.     static boolean indBoost = true;
  35.    
  36.     public static void updateRows(Object[] row, AggregateEconomy ae, DefaultTableModel model){
  37.         for(int i = 0; i < ae.getSize(); i++){
  38.             row[0] = ae.getIndustry(i);
  39.             row[1] = ae.getIndustry(i).getLaborValue();
  40.             row[2] = ae.getIndustry(i).getClearingPrice();
  41.             row[3] = String.format("%.3g%n", ae.getIndustry(i).getError());
  42.             row[4] = "$"+ae.getIndustry(i).getNetWorth();
  43.            
  44.             model.setValueAt(row[0], i, 0);
  45.             model.setValueAt(row[1], i, 1);
  46.             model.setValueAt(row[2], i, 2);
  47.             model.setValueAt(row[3], i, 3);
  48.             model.setValueAt(row[4], i, 4);
  49.         }
  50.     }
  51.    
  52.     @SuppressWarnings("static-access")
  53.     public Main(){
  54.        
  55.         // create JFrame and JTable
  56.         JFrame frame = new JFrame("Planned Economy");
  57.         JTable table = new JTable();
  58.  
  59.         // create a table model and set a Column Identifiers to this model
  60.         Object[] columns = {"Industry","Labor Value (Hours)","Clearing Price ($)","Price Error Margin","Net Worth"};
  61.         DefaultTableModel model = new DefaultTableModel();
  62.         model.setColumnIdentifiers(columns);
  63.        
  64.         // set the model to the table
  65.         table.setModel(model);
  66.              
  67.         // Change A JTable Background Color, Font Size, Font Color, Row Height
  68.         table.setBackground(Color.LIGHT_GRAY);
  69.         table.setForeground(Color.black);
  70.         Font font = new Font("",1,22);
  71.         table.setFont(font);
  72.         table.setRowHeight(30);
  73.        
  74.         // labels
  75.         JLabel lblCredit = new JLabel("Largely based on the work of Allin Cottrell and Paul Cockshott (Click To Read)");
  76.         JLabel lblTime = new JLabel("Months Passed: 0");
  77.        
  78.         // create JTextFields
  79.         JTextField textId = new JTextField();
  80.        
  81.         // create Economy
  82.         AggregateEconomy agEcon = new AggregateEconomy();
  83.        
  84.         // create JButtons
  85.         JButton btnAdd = new JButton("Add");
  86.         JButton btnDelete = new JButton("Delete");
  87.         JButton btnUpdate = new JButton("+Value");  
  88.        
  89.         JButton btnStart = new JButton("Start");
  90.         JButton btnStop = new JButton("Exit");
  91.        
  92.         JButton btnInvest = new JButton("Invest");
  93.        
  94.         JButton btnHow = new JButton("How to Play");
  95.        
  96.         // set locations
  97.         textId.setBounds(20, 220, 100, 25);
  98.  
  99.         btnAdd.setBounds(150, 220, 100, 25);
  100.         btnUpdate.setBounds(780, 220, 100, 25);
  101.         btnDelete.setBounds(150, 265, 100, 25);
  102.        
  103.         btnStart.setBounds(20, 265, 100, 25);
  104.         btnStop.setBounds(20, 310, 100, 25);
  105.        
  106.         btnInvest.setBounds(655, 220, 100, 25);
  107.        
  108.         lblCredit.setBounds(5, 347, 1000, 25);
  109.         lblTime.setBounds(652, 197, 300, 25);
  110.        
  111.         btnHow.setBounds(150, 310, 100, 25);
  112.        
  113.         // create JScrollPane
  114.         JScrollPane pane = new JScrollPane(table);
  115.         pane.setBounds(0, 0, 880, 200);
  116.        
  117.         frame.setLayout(null);
  118.        
  119.         frame.add(pane);
  120.        
  121.         // add JTextFields to the jframe
  122.         frame.add(textId);
  123.        
  124.         // add JButtons to the jframe
  125.         frame.add(btnAdd);
  126.         frame.add(btnDelete);
  127.         frame.add(btnUpdate);
  128.        
  129.         frame.add(btnStart);
  130.         frame.add(btnStop);
  131.        
  132.         frame.add(btnInvest);
  133.        
  134.         frame.add(lblCredit);
  135.         frame.add(lblTime);
  136.        
  137.         frame.add(btnHow);
  138.        
  139.         lblCredit.addMouseListener(new MouseAdapter() {
  140.             @Override
  141.             public void mouseClicked(MouseEvent e) {
  142.                 try {
  143.                     Desktop.getDesktop().browse(new URI("http://ricardo.ecn.wfu.edu/~cottrell/socialism_book/new_socialism.pdf"));
  144.                 } catch (IOException ex) {
  145.                     //It looks like there's a problem
  146.                 } catch (URISyntaxException e1) {
  147.                     // TODO Auto-generated catch block
  148.                     e1.printStackTrace();
  149.                 }
  150.             }
  151.         });
  152.        
  153.         btnHow.addActionListener(new ActionListener(){
  154.             @Override
  155.             public void actionPerformed(ActionEvent arg0) {
  156.                 new How();
  157.             }
  158.         });
  159.        
  160.         // create an array of objects to set the row data
  161.         Object[] row = new Object[6];
  162.        
  163.         // button add row
  164.         btnAdd.addActionListener(new ActionListener(){
  165.  
  166.             @Override
  167.             public void actionPerformed(ActionEvent e) {
  168.                 Industry ind = new Industry(textId.getText());
  169.                
  170.                 row[0] = ind;
  171.                 row[1] = ind.getLaborValue();
  172.                 row[2] = ind.getClearingPrice();
  173.                 row[3] = ind.getError();
  174.                 row[4] = "$"+ind.getNetWorth();
  175.                
  176.                 agEcon.addIndustry(ind);
  177.                
  178.                 // add row to the model
  179.                 model.addRow(row);
  180.                 textId.setText("");
  181.                
  182.             }
  183.         });
  184.        
  185.         // button remove row
  186.         btnDelete.addActionListener(new ActionListener(){
  187.             @Override
  188.             public void actionPerformed(ActionEvent e) {
  189.                 // i = the index of the selected row
  190.                 int i = table.getSelectedRow();
  191.                 if(i >= 0){
  192.                     // remove a row from jtable
  193.                     agEcon.removeIndustry(agEcon.getIndustry(i));
  194.                     model.removeRow(i);
  195.                 }
  196.                 else{
  197.                     JOptionPane.showMessageDialog(null,"Delete Error");
  198.                 }
  199.             }
  200.         });
  201.        
  202.         // get selected row data From table to textfields
  203.         table.addMouseListener(new MouseAdapter(){
  204.        
  205.         @Override
  206.         public void mouseClicked(MouseEvent e){
  207.            
  208.             // i = the index of the selected row
  209.             int i = table.getSelectedRow();
  210.            
  211.             textId.setText(model.getValueAt(i, 0).toString());
  212.         }
  213.         });
  214.        
  215.         // button update value
  216.         btnUpdate.addActionListener(new ActionListener(){
  217.             @Override
  218.             public void actionPerformed(ActionEvent e) {
  219.              
  220.                 // i = the index of the selected row
  221.                 int i = table.getSelectedRow();
  222.                
  223.                 if(i >= 0){
  224.                     // direct labor time:
  225.                     String s1 = JOptionPane.showInputDialog("Enter Direct Labor Time: ");
  226.                     int dL = 0;
  227.                     dL = Integer.parseInt(s1);
  228.                    
  229.                     // indirect labor time:
  230.                     int iL = 0;
  231.                     String s2 = JOptionPane.showInputDialog("Enter Indirect Labor Time: ");
  232.                     iL = Integer.parseInt(s2);
  233.                    
  234.                     // calculate labor value:
  235.                     int value = 0;
  236.                     value = dL + iL;
  237.                    
  238.                     // set values
  239.                     agEcon.getIndustry(i).setDirectL(dL);
  240.                     agEcon.getIndustry(i).setIndirectL(iL);
  241.                    
  242.                     agEcon.getIndustry(i).setLaborValue(value);
  243.                    
  244.                     model.setValueAt(value+"", i, 1);
  245.                    
  246.                 }
  247.                 else{
  248.                     JOptionPane.showMessageDialog(null,"Update Error");
  249.                 }
  250.             }
  251.         });
  252.        
  253.         btnInvest.addActionListener(new ActionListener(){
  254.             @Override
  255.             public void actionPerformed(ActionEvent e) {
  256.                 if(isRunning){
  257.                     int i = table.getSelectedRow();
  258.                     Industry industry = agEcon.getIndustry(i);
  259.                    
  260.                     String sMoney = JOptionPane.showInputDialog("Enter How Much You Want To Invest in $ "
  261.                             + "(This will be used to minimize labor time but willbe taken from the enterprise's net worth | also note"
  262.                             + "that consumer demand (market clearing prices) fluctuates when you do this):");
  263.                     double money = Double.parseDouble(sMoney);
  264.                    
  265.                     // System whereby labor values are changed via investment
  266.                     if(money <= industry.getNetWorth()){
  267.                         industry.setNetWorth((int) (industry.getNetWorth()-money));
  268.                         numberInvests++;
  269.                            
  270.                         // set new labor values to be lower
  271.                         industry.setDirectL(industry.getDirectL()-(industry.getDirectL()-10));
  272.                         industry.setIndirectL(industry.getIndirectL()-(industry.getIndirectL()-10));
  273.                         industry.setLaborValue(industry.getDirectL()+industry.getIndirectL());
  274.                        
  275.                         // reset prices
  276.                         pricesSet = false;
  277.                     }
  278.                     else{
  279.                         JOptionPane.showMessageDialog(null, "You don't have that much");
  280.                     }
  281.                 }
  282.                 else{
  283.                     JOptionPane.showMessageDialog(null, "You must enter value then hit 'start'");
  284.                 }
  285.             }
  286.            
  287.         });
  288.        
  289.         frame.setSize(900,400);
  290.         frame.setLocationRelativeTo(null);
  291.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  292.         frame.setVisible(true);
  293.         frame.setResizable(false);
  294.        
  295.      // button start
  296.         btnStart.addActionListener(new ActionListener(){
  297.             boolean t = true;
  298.             public void actionPerformed(ActionEvent e) {
  299.                 for(int i = 0; i < agEcon.getSize(); i++){
  300.                     if(!(agEcon.getIndustry(i).getDirectL() > 0))
  301.                         t=false;
  302.                 }
  303.                 if(t){
  304.                     isRunning = true;
  305.                     for(int i = 0; i < agEcon.getSize(); i++){
  306.                         agEcon.getIndustry(i).setRunning(isRunning);
  307.                     }
  308.                 }
  309.                 else{
  310.                     JOptionPane.showMessageDialog(null, "Enter all labor values first");
  311.                     t=true;
  312.                 }
  313.             }
  314.         });
  315.        
  316.         // game loop
  317.         while(true){
  318.        
  319.        
  320.        
  321.         // button stop
  322.         btnStop.addActionListener(new ActionListener(){
  323.             @Override
  324.             public void actionPerformed(ActionEvent e) {
  325.                 frame.dispose();
  326.             }
  327.         });
  328.        
  329.         // game loop
  330.         if(isRunning){
  331.            
  332.             // set random prices   
  333.             EconThread et = new EconThread(agEcon, lblTime);
  334.  
  335.             updateRows(row, agEcon, model);
  336.            
  337.             // keep updating table per month
  338.             et.start();
  339.            
  340.             // punish economy for lack of investment
  341.             if(et.getMonthsPassed() > monthsPassed && numberInvests == 0){
  342.                 for(int i = 0; i < agEcon.getSize(); i++){
  343.                     Industry temp = agEcon.getIndustry(i);
  344.                     temp.setNetWorth((temp.getNetWorth()-moneyLoss));
  345.                 }
  346.                 moneyLoss+=1000;
  347.                 updateRows(row, agEcon, model);
  348.             } else if(et.getMonthsPassed() > monthsPassed && monthsPassed > 2 && numberInvests < 3){
  349.                 System.out.println(monthsPassed);
  350.                 for(int i = 0; i < agEcon.getSize(); i++){
  351.                     Industry temp = agEcon.getIndustry(i);
  352.                     temp.setNetWorth((temp.getNetWorth()-moneyLoss));
  353.                 }
  354.                 moneyLoss+=1000;
  355.                 updateRows(row, agEcon, model);
  356.             }
  357.            
  358.             if(indBoost){
  359.                 if(numberInvests >=3){
  360.                     for(int i = 0; i < agEcon.getSize(); i++){
  361.                         Industry temp = agEcon.getIndustry(i);
  362.                         temp.setNetWorth((temp.getNetWorth()+10000));
  363.                     }
  364.                 }
  365.                 indBoost = false;
  366.             }
  367.            
  368.             for(int i = 0; i < agEcon.getSize(); i++){
  369.                 if(agEcon.getIndustry(i).getNetWorth() <= 0){
  370.                     JOptionPane.showMessageDialog(null, "You Lose");
  371.                     frame.dispose();
  372.                     System.exit(0);
  373.                 }
  374.             }
  375.            
  376.             if(numberInvests > 0){
  377.                 if(newInvest){
  378.                     monthsPassed++;
  379.                     newInvest=false;
  380.                 }
  381.             }
  382.             System.out.println(monthsPassed);
  383.            
  384.             // buffer month increments
  385.             try {
  386.                 et.sleep(3500);
  387.             } catch (InterruptedException e1) {
  388.                 e1.printStackTrace();
  389.             }
  390.         }
  391.        
  392.       }
  393.     }
  394.  
  395.     // Main method
  396.     public static void main(String[] args){
  397.         new Main();
  398.     }
  399. }
  400.  
  401.  
  402. ////////////////////////////////////////////// Industry
  403.  
  404.  
  405. import java.util.Random;
  406.  
  407. public class Industry {
  408.  
  409.     private String title = "Industry";
  410.    
  411.     Random rnd = new Random();
  412.    
  413.     double perGDP = 0;
  414.    
  415.     //Used to Calculate % of GDP
  416.     double netWorth = 10000+rnd.nextInt(100000);
  417.    
  418.     //Used to Compare to Clearing Price
  419.     double laborValue = 0;
  420.    
  421.     // labor time
  422.     int directLabor = 0;
  423.     int indirectLabor = 0;
  424.    
  425.     //Used to Compare to Labor Value
  426.     double clearingPrice = 0;
  427.    
  428.     // Check if running
  429.     boolean isRunning = false;
  430.    
  431.     double error = 0;
  432.    
  433.     public Industry(String name) {
  434.         title = name;
  435.     }
  436.    
  437.     public boolean isRunning() {
  438.         return isRunning;
  439.     }
  440.  
  441.     public void setRunning(boolean isRunning) {
  442.         this.isRunning = isRunning;
  443.     }
  444.  
  445.     // getters and setters
  446.     public void setPerGDP(double n, double gdp){
  447.         perGDP = n / gdp;
  448.         perGDP*=100;
  449.         System.out.println(perGDP);
  450.     }
  451.    
  452.     public double getPerGDP(){
  453.         return perGDP;
  454.     }
  455.    
  456.     public double getNetWorth() {
  457.         return netWorth;
  458.     }
  459.  
  460.     public void setNetWorth(double netWorth) {
  461.         this.netWorth = netWorth;
  462.     }
  463.  
  464.     public double getLaborValue() {
  465.         return laborValue;
  466.     }
  467.  
  468.     public void setLaborValue(double laborValue) {
  469.         this.laborValue = laborValue;
  470.     }
  471.  
  472.     public double getClearingPrice() {
  473.         return clearingPrice;
  474.     }
  475.  
  476.     public void setClearingPrice(double rndPrice) {
  477.         this.clearingPrice = rndPrice;
  478.     }
  479.  
  480.     public void setError(double p, double l){
  481.         error = Math.abs((l-p)/l);
  482.     }
  483.    
  484.     public double getError(){
  485.         return Math.abs(error);
  486.     }
  487.    
  488.     public void setDirectL(int l){
  489.         directLabor = l;
  490.     }
  491.    
  492.     public int getDirectL(){
  493.         return directLabor;
  494.     }
  495.    
  496.     public void setIndirectL(int l){
  497.         indirectLabor = l;
  498.     }
  499.    
  500.     public int getIndirectL(){
  501.         return indirectLabor;
  502.     }
  503.    
  504.     public String toString(){
  505.         return title;
  506.     }
  507.  
  508. }
  509.  
  510.  
  511.  
  512. ///////////////////////////////////////////// Economy
  513.  
  514. import java.util.ArrayList;
  515.  
  516. public class AggregateEconomy {
  517.    
  518.     private double GDP = 0;
  519.     private ArrayList<Industry> economy = new ArrayList<Industry>();
  520.    
  521.     public AggregateEconomy() {
  522.        
  523.     }
  524.    
  525.     public void addIndustry(Industry ind){
  526.         economy.add(ind);
  527.     }
  528.    
  529.     public void removeIndustry(Industry ind){
  530.         economy.remove(ind);
  531.     }
  532.    
  533.     public int getSize(){
  534.         return economy.size();
  535.     }
  536.    
  537.     public Industry getIndustry(int index){
  538.         return economy.get(index);
  539.     }
  540.    
  541.     public void setGDP(double gdp){
  542.         GDP = gdp;
  543.     }
  544.    
  545.     public double getGDP(){
  546.         for(int i = 0; i < economy.size(); i++){
  547.             GDP+=economy.get(i).getNetWorth();
  548.         }
  549.         return GDP;
  550.     }
  551.  
  552. }
  553.  
  554.  
  555. ////////////////////////////////// EconThread
  556.  
  557. import java.util.Random;
  558.  
  559. import javax.swing.JLabel;
  560.  
  561. public class EconThread extends Thread{
  562.  
  563.     private AggregateEconomy agEcon;
  564.     private JLabel lbl;
  565.    
  566.     static int days = 0;
  567.    
  568.     public EconThread(AggregateEconomy e, JLabel l) {
  569.         agEcon=e;
  570.         lbl=l;
  571.     }
  572.    
  573.     public void set(){
  574.         for(int i = 0; i < agEcon.getSize(); i++){
  575.             Random rnd = new Random();
  576.             double rndPrice = 0;
  577.            
  578.             if(agEcon.getIndustry(i).getIndirectL() > agEcon.getIndustry(i).getDirectL())
  579.                 rndPrice = agEcon.getIndustry(i).getDirectL()+rnd.nextInt((int) (agEcon.getIndustry(i).getIndirectL()+(0.25*agEcon.getIndustry(i).getLaborValue())));
  580.             else
  581.                 rndPrice = agEcon.getIndustry(i).getIndirectL()+rnd.nextInt((int) (agEcon.getIndustry(i).getDirectL()+(0.25*agEcon.getIndustry(i).getLaborValue())));
  582.            
  583.             agEcon.getIndustry(i).setClearingPrice(rndPrice);
  584.            
  585.             // get error margin
  586.             agEcon.getIndustry(i).setError(rndPrice, agEcon.getIndustry(i).getLaborValue());
  587.         }
  588.     }
  589.    
  590.     public void run(){
  591.         for(int i = 0; i < agEcon.getSize(); i++){
  592.             Random rnd = new Random();
  593.             double rndPrice = 0;
  594.            
  595.             if(agEcon.getIndustry(i).getIndirectL() > agEcon.getIndustry(i).getDirectL())
  596.                 rndPrice = agEcon.getIndustry(i).getDirectL()+rnd.nextInt((int) (agEcon.getIndustry(i).getIndirectL()+(0.25*agEcon.getIndustry(i).getLaborValue())));
  597.             else
  598.                 rndPrice = agEcon.getIndustry(i).getIndirectL()+rnd.nextInt((int) (agEcon.getIndustry(i).getDirectL()+(0.25*agEcon.getIndustry(i).getLaborValue())));
  599.            
  600.             agEcon.getIndustry(i).setClearingPrice(rndPrice);
  601.            
  602.             // get error margin
  603.             agEcon.getIndustry(i).setError(rndPrice, agEcon.getIndustry(i).getLaborValue());
  604.         }
  605.         lbl.setText("Months Passed: "+days);
  606.         days++;
  607.     }
  608.    
  609.     public int getMonthsPassed(){
  610.         return days;
  611.     }
  612.  
  613. }
  614.  
  615.  
  616. //////////////////////////////////////// How
  617.  
  618. import javax.swing.ImageIcon;
  619. import javax.swing.JFrame;
  620. import javax.swing.JLabel;
  621. import javax.swing.SwingUtilities;
  622.  
  623. public class How {
  624.  
  625.     JFrame frame = new JFrame();
  626.    
  627.      private JLabel player = new JLabel();
  628.    
  629.     public How() {
  630.         frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  631.         frame.setSize(1484,1034);
  632.         frame.setVisible(true);
  633.         frame.setResizable(false);
  634.         frame.setLayout(null);
  635.         frame.setTitle("How to Play");
  636.        
  637.         player.setBounds(0, 0, 1484, 1034);
  638.         player.setIcon(new ImageIcon("G:\\Buhot AP Computer Science 16-17 J463-1\\Final Project Drop Off\\Ben Bollinger\\howTo.png"));
  639.        
  640.         frame.add(player);
  641.         SwingUtilities.updateComponentTreeUI(frame);
  642.     }
  643.  
  644.     public static void main(String[] args) {
  645.         new How();
  646.  
  647.  
  648. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement