Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Desktop;
- import java.awt.Font;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.io.IOException;
- import java.net.URI;
- import java.net.URISyntaxException;
- import java.util.Random;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JScrollPane;
- import javax.swing.JTable;
- import javax.swing.JTextField;
- import javax.swing.table.DefaultTableModel;
- public class Main {
- static boolean isRunning = false;
- static boolean pricesSet = false;
- static boolean changeWorth = true;
- static int numberInvests = 0;
- static int moneyLoss = 100;
- static int monthsPassed = 2;
- static boolean newInvest = true;
- static boolean indBoost = true;
- public static void updateRows(Object[] row, AggregateEconomy ae, DefaultTableModel model){
- for(int i = 0; i < ae.getSize(); i++){
- row[0] = ae.getIndustry(i);
- row[1] = ae.getIndustry(i).getLaborValue();
- row[2] = ae.getIndustry(i).getClearingPrice();
- row[3] = String.format("%.3g%n", ae.getIndustry(i).getError());
- row[4] = "$"+ae.getIndustry(i).getNetWorth();
- model.setValueAt(row[0], i, 0);
- model.setValueAt(row[1], i, 1);
- model.setValueAt(row[2], i, 2);
- model.setValueAt(row[3], i, 3);
- model.setValueAt(row[4], i, 4);
- }
- }
- @SuppressWarnings("static-access")
- public Main(){
- // create JFrame and JTable
- JFrame frame = new JFrame("Planned Economy");
- JTable table = new JTable();
- // create a table model and set a Column Identifiers to this model
- Object[] columns = {"Industry","Labor Value (Hours)","Clearing Price ($)","Price Error Margin","Net Worth"};
- DefaultTableModel model = new DefaultTableModel();
- model.setColumnIdentifiers(columns);
- // set the model to the table
- table.setModel(model);
- // Change A JTable Background Color, Font Size, Font Color, Row Height
- table.setBackground(Color.LIGHT_GRAY);
- table.setForeground(Color.black);
- Font font = new Font("",1,22);
- table.setFont(font);
- table.setRowHeight(30);
- // labels
- JLabel lblCredit = new JLabel("Largely based on the work of Allin Cottrell and Paul Cockshott (Click To Read)");
- JLabel lblTime = new JLabel("Months Passed: 0");
- // create JTextFields
- JTextField textId = new JTextField();
- // create Economy
- AggregateEconomy agEcon = new AggregateEconomy();
- // create JButtons
- JButton btnAdd = new JButton("Add");
- JButton btnDelete = new JButton("Delete");
- JButton btnUpdate = new JButton("+Value");
- JButton btnStart = new JButton("Start");
- JButton btnStop = new JButton("Exit");
- JButton btnInvest = new JButton("Invest");
- JButton btnHow = new JButton("How to Play");
- // set locations
- textId.setBounds(20, 220, 100, 25);
- btnAdd.setBounds(150, 220, 100, 25);
- btnUpdate.setBounds(780, 220, 100, 25);
- btnDelete.setBounds(150, 265, 100, 25);
- btnStart.setBounds(20, 265, 100, 25);
- btnStop.setBounds(20, 310, 100, 25);
- btnInvest.setBounds(655, 220, 100, 25);
- lblCredit.setBounds(5, 347, 1000, 25);
- lblTime.setBounds(652, 197, 300, 25);
- btnHow.setBounds(150, 310, 100, 25);
- // create JScrollPane
- JScrollPane pane = new JScrollPane(table);
- pane.setBounds(0, 0, 880, 200);
- frame.setLayout(null);
- frame.add(pane);
- // add JTextFields to the jframe
- frame.add(textId);
- // add JButtons to the jframe
- frame.add(btnAdd);
- frame.add(btnDelete);
- frame.add(btnUpdate);
- frame.add(btnStart);
- frame.add(btnStop);
- frame.add(btnInvest);
- frame.add(lblCredit);
- frame.add(lblTime);
- frame.add(btnHow);
- lblCredit.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- try {
- Desktop.getDesktop().browse(new URI("http://ricardo.ecn.wfu.edu/~cottrell/socialism_book/new_socialism.pdf"));
- } catch (IOException ex) {
- //It looks like there's a problem
- } catch (URISyntaxException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- });
- btnHow.addActionListener(new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent arg0) {
- new How();
- }
- });
- // create an array of objects to set the row data
- Object[] row = new Object[6];
- // button add row
- btnAdd.addActionListener(new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent e) {
- Industry ind = new Industry(textId.getText());
- row[0] = ind;
- row[1] = ind.getLaborValue();
- row[2] = ind.getClearingPrice();
- row[3] = ind.getError();
- row[4] = "$"+ind.getNetWorth();
- agEcon.addIndustry(ind);
- // add row to the model
- model.addRow(row);
- textId.setText("");
- }
- });
- // button remove row
- btnDelete.addActionListener(new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent e) {
- // i = the index of the selected row
- int i = table.getSelectedRow();
- if(i >= 0){
- // remove a row from jtable
- agEcon.removeIndustry(agEcon.getIndustry(i));
- model.removeRow(i);
- }
- else{
- JOptionPane.showMessageDialog(null,"Delete Error");
- }
- }
- });
- // get selected row data From table to textfields
- table.addMouseListener(new MouseAdapter(){
- @Override
- public void mouseClicked(MouseEvent e){
- // i = the index of the selected row
- int i = table.getSelectedRow();
- textId.setText(model.getValueAt(i, 0).toString());
- }
- });
- // button update value
- btnUpdate.addActionListener(new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent e) {
- // i = the index of the selected row
- int i = table.getSelectedRow();
- if(i >= 0){
- // direct labor time:
- String s1 = JOptionPane.showInputDialog("Enter Direct Labor Time: ");
- int dL = 0;
- dL = Integer.parseInt(s1);
- // indirect labor time:
- int iL = 0;
- String s2 = JOptionPane.showInputDialog("Enter Indirect Labor Time: ");
- iL = Integer.parseInt(s2);
- // calculate labor value:
- int value = 0;
- value = dL + iL;
- // set values
- agEcon.getIndustry(i).setDirectL(dL);
- agEcon.getIndustry(i).setIndirectL(iL);
- agEcon.getIndustry(i).setLaborValue(value);
- model.setValueAt(value+"", i, 1);
- }
- else{
- JOptionPane.showMessageDialog(null,"Update Error");
- }
- }
- });
- btnInvest.addActionListener(new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent e) {
- if(isRunning){
- int i = table.getSelectedRow();
- Industry industry = agEcon.getIndustry(i);
- String sMoney = JOptionPane.showInputDialog("Enter How Much You Want To Invest in $ "
- + "(This will be used to minimize labor time but willbe taken from the enterprise's net worth | also note"
- + "that consumer demand (market clearing prices) fluctuates when you do this):");
- double money = Double.parseDouble(sMoney);
- // System whereby labor values are changed via investment
- if(money <= industry.getNetWorth()){
- industry.setNetWorth((int) (industry.getNetWorth()-money));
- numberInvests++;
- // set new labor values to be lower
- industry.setDirectL(industry.getDirectL()-(industry.getDirectL()-10));
- industry.setIndirectL(industry.getIndirectL()-(industry.getIndirectL()-10));
- industry.setLaborValue(industry.getDirectL()+industry.getIndirectL());
- // reset prices
- pricesSet = false;
- }
- else{
- JOptionPane.showMessageDialog(null, "You don't have that much");
- }
- }
- else{
- JOptionPane.showMessageDialog(null, "You must enter value then hit 'start'");
- }
- }
- });
- frame.setSize(900,400);
- frame.setLocationRelativeTo(null);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- frame.setResizable(false);
- // button start
- btnStart.addActionListener(new ActionListener(){
- boolean t = true;
- public void actionPerformed(ActionEvent e) {
- for(int i = 0; i < agEcon.getSize(); i++){
- if(!(agEcon.getIndustry(i).getDirectL() > 0))
- t=false;
- }
- if(t){
- isRunning = true;
- for(int i = 0; i < agEcon.getSize(); i++){
- agEcon.getIndustry(i).setRunning(isRunning);
- }
- }
- else{
- JOptionPane.showMessageDialog(null, "Enter all labor values first");
- t=true;
- }
- }
- });
- // game loop
- while(true){
- // button stop
- btnStop.addActionListener(new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent e) {
- frame.dispose();
- }
- });
- // game loop
- if(isRunning){
- // set random prices
- EconThread et = new EconThread(agEcon, lblTime);
- updateRows(row, agEcon, model);
- // keep updating table per month
- et.start();
- // punish economy for lack of investment
- if(et.getMonthsPassed() > monthsPassed && numberInvests == 0){
- for(int i = 0; i < agEcon.getSize(); i++){
- Industry temp = agEcon.getIndustry(i);
- temp.setNetWorth((temp.getNetWorth()-moneyLoss));
- }
- moneyLoss+=1000;
- updateRows(row, agEcon, model);
- } else if(et.getMonthsPassed() > monthsPassed && monthsPassed > 2 && numberInvests < 3){
- System.out.println(monthsPassed);
- for(int i = 0; i < agEcon.getSize(); i++){
- Industry temp = agEcon.getIndustry(i);
- temp.setNetWorth((temp.getNetWorth()-moneyLoss));
- }
- moneyLoss+=1000;
- updateRows(row, agEcon, model);
- }
- if(indBoost){
- if(numberInvests >=3){
- for(int i = 0; i < agEcon.getSize(); i++){
- Industry temp = agEcon.getIndustry(i);
- temp.setNetWorth((temp.getNetWorth()+10000));
- }
- }
- indBoost = false;
- }
- for(int i = 0; i < agEcon.getSize(); i++){
- if(agEcon.getIndustry(i).getNetWorth() <= 0){
- JOptionPane.showMessageDialog(null, "You Lose");
- frame.dispose();
- System.exit(0);
- }
- }
- if(numberInvests > 0){
- if(newInvest){
- monthsPassed++;
- newInvest=false;
- }
- }
- System.out.println(monthsPassed);
- // buffer month increments
- try {
- et.sleep(3500);
- } catch (InterruptedException e1) {
- e1.printStackTrace();
- }
- }
- }
- }
- // Main method
- public static void main(String[] args){
- new Main();
- }
- }
- ////////////////////////////////////////////// Industry
- import java.util.Random;
- public class Industry {
- private String title = "Industry";
- Random rnd = new Random();
- double perGDP = 0;
- //Used to Calculate % of GDP
- double netWorth = 10000+rnd.nextInt(100000);
- //Used to Compare to Clearing Price
- double laborValue = 0;
- // labor time
- int directLabor = 0;
- int indirectLabor = 0;
- //Used to Compare to Labor Value
- double clearingPrice = 0;
- // Check if running
- boolean isRunning = false;
- double error = 0;
- public Industry(String name) {
- title = name;
- }
- public boolean isRunning() {
- return isRunning;
- }
- public void setRunning(boolean isRunning) {
- this.isRunning = isRunning;
- }
- // getters and setters
- public void setPerGDP(double n, double gdp){
- perGDP = n / gdp;
- perGDP*=100;
- System.out.println(perGDP);
- }
- public double getPerGDP(){
- return perGDP;
- }
- public double getNetWorth() {
- return netWorth;
- }
- public void setNetWorth(double netWorth) {
- this.netWorth = netWorth;
- }
- public double getLaborValue() {
- return laborValue;
- }
- public void setLaborValue(double laborValue) {
- this.laborValue = laborValue;
- }
- public double getClearingPrice() {
- return clearingPrice;
- }
- public void setClearingPrice(double rndPrice) {
- this.clearingPrice = rndPrice;
- }
- public void setError(double p, double l){
- error = Math.abs((l-p)/l);
- }
- public double getError(){
- return Math.abs(error);
- }
- public void setDirectL(int l){
- directLabor = l;
- }
- public int getDirectL(){
- return directLabor;
- }
- public void setIndirectL(int l){
- indirectLabor = l;
- }
- public int getIndirectL(){
- return indirectLabor;
- }
- public String toString(){
- return title;
- }
- }
- ///////////////////////////////////////////// Economy
- import java.util.ArrayList;
- public class AggregateEconomy {
- private double GDP = 0;
- private ArrayList<Industry> economy = new ArrayList<Industry>();
- public AggregateEconomy() {
- }
- public void addIndustry(Industry ind){
- economy.add(ind);
- }
- public void removeIndustry(Industry ind){
- economy.remove(ind);
- }
- public int getSize(){
- return economy.size();
- }
- public Industry getIndustry(int index){
- return economy.get(index);
- }
- public void setGDP(double gdp){
- GDP = gdp;
- }
- public double getGDP(){
- for(int i = 0; i < economy.size(); i++){
- GDP+=economy.get(i).getNetWorth();
- }
- return GDP;
- }
- }
- ////////////////////////////////// EconThread
- import java.util.Random;
- import javax.swing.JLabel;
- public class EconThread extends Thread{
- private AggregateEconomy agEcon;
- private JLabel lbl;
- static int days = 0;
- public EconThread(AggregateEconomy e, JLabel l) {
- agEcon=e;
- lbl=l;
- }
- public void set(){
- for(int i = 0; i < agEcon.getSize(); i++){
- Random rnd = new Random();
- double rndPrice = 0;
- if(agEcon.getIndustry(i).getIndirectL() > agEcon.getIndustry(i).getDirectL())
- rndPrice = agEcon.getIndustry(i).getDirectL()+rnd.nextInt((int) (agEcon.getIndustry(i).getIndirectL()+(0.25*agEcon.getIndustry(i).getLaborValue())));
- else
- rndPrice = agEcon.getIndustry(i).getIndirectL()+rnd.nextInt((int) (agEcon.getIndustry(i).getDirectL()+(0.25*agEcon.getIndustry(i).getLaborValue())));
- agEcon.getIndustry(i).setClearingPrice(rndPrice);
- // get error margin
- agEcon.getIndustry(i).setError(rndPrice, agEcon.getIndustry(i).getLaborValue());
- }
- }
- public void run(){
- for(int i = 0; i < agEcon.getSize(); i++){
- Random rnd = new Random();
- double rndPrice = 0;
- if(agEcon.getIndustry(i).getIndirectL() > agEcon.getIndustry(i).getDirectL())
- rndPrice = agEcon.getIndustry(i).getDirectL()+rnd.nextInt((int) (agEcon.getIndustry(i).getIndirectL()+(0.25*agEcon.getIndustry(i).getLaborValue())));
- else
- rndPrice = agEcon.getIndustry(i).getIndirectL()+rnd.nextInt((int) (agEcon.getIndustry(i).getDirectL()+(0.25*agEcon.getIndustry(i).getLaborValue())));
- agEcon.getIndustry(i).setClearingPrice(rndPrice);
- // get error margin
- agEcon.getIndustry(i).setError(rndPrice, agEcon.getIndustry(i).getLaborValue());
- }
- lbl.setText("Months Passed: "+days);
- days++;
- }
- public int getMonthsPassed(){
- return days;
- }
- }
- //////////////////////////////////////// How
- import javax.swing.ImageIcon;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.SwingUtilities;
- public class How {
- JFrame frame = new JFrame();
- private JLabel player = new JLabel();
- public How() {
- frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- frame.setSize(1484,1034);
- frame.setVisible(true);
- frame.setResizable(false);
- frame.setLayout(null);
- frame.setTitle("How to Play");
- player.setBounds(0, 0, 1484, 1034);
- player.setIcon(new ImageIcon("G:\\Buhot AP Computer Science 16-17 J463-1\\Final Project Drop Off\\Ben Bollinger\\howTo.png"));
- frame.add(player);
- SwingUtilities.updateComponentTreeUI(frame);
- }
- public static void main(String[] args) {
- new How();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement