0

GUI.java

package ccc;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class GUI implements ActionListener {

JFrame f;
JPanel bg, s, a;
JLabel ca, £, cav, co, cov, co2, pr, error;
JTextField tf;
JButton b1, b2;

public GUI(){

    f = new JFrame();

    bg = new JPanel();
    bg.setSize(650, 500);
    bg.setBackground(Color.darkGray);
    bg.setLayout(null);
    bg.setLocation(0, 0);

    s = new JPanel();
    s.setSize(180, 445);
    s.setBackground(Color.white);
    s.setLayout(null);
    s.setLocation(10, 10);

    a = new JPanel();
    a.setSize(430, 445);
    a.setBackground(Color.white);
    a.setLayout(null);
    a.setLocation(202, 10);

    ca = new JLabel("CASH:");
    ca.setSize(70,25);
    ca.setLocation(10, 25);

    £ = new JLabel("£");
    £.setSize(10, 25);
    £.setLocation(80, 25);

    cav = new JLabel("128.23");
    cav.setSize(80, 25);
    cav.setLocation(90, 25);

    co = new JLabel("COCAINE:");
    co.setSize(70, 25);
    co.setLocation(10, 50);

    cov = new JLabel("6 units");
    cov.setSize(70, 25);
    cov.setLocation(80, 50);

    co2 = new JLabel("COCAINE");
    co2.setSize(70, 25);
    co2.setLocation(10, 25);

    pr = new JLabel("£39.95");
    pr.setSize(60, 25);
    pr.setLocation(90, 25);

    tf = new JTextField();
    tf.setSize(70, 25);
    tf.setLocation(160, 25);

    b1 = new JButton("BUY");
    b1.setSize(70, 25);
    b1.setLocation(270, 25);
    b1.addActionListener(this);

    b2 = new JButton("SELL");
    b2.setSize(70, 25);
    b2.setLocation(350, 25);
    b2.addActionListener(this);

    error = new JLabel();
    error.setSize(200, 25);
    error.setLocation(50, 50);




    s.add(ca);
    s.add(£);
    s.add(cav);
    s.add(co);
    s.add(cov);

    a.add(co2);
    a.add(pr);
    a.add(tf);
    a.add(b1);
    a.add(b2);
    a.add(error);

    bg.add(s);  
    bg.add(a);

    f.add(bg);




    f.setSize(650, 500);
    f.setLayout(null);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}




public static void main(String[] args) {

    new GUI();
}




@Override
public void actionPerformed(ActionEvent e) {

    if (e.getSource() == b1){
        Buy a = new Buy();
    a.Buy();
    }
    if (e.getSource() == b2){

    }
}

}

Buy.java

package ccc;

public class Buy extends GUI {

String pri, qua, cas, uni, aaa;
double price, quantity, cash, units, total, deduction;

public Buy(){

    pri = pr.getText();
    price = Double.parseDouble(pri);

    qua = tf.getText();
    quantity = Double.parseDouble(qua);

    cas = cav.getText();
    cash = Double.parseDouble(cas);

    uni = cov.getText();
    units = Double.parseDouble(uni);    

    total = price * quantity;



    if (cash >= total){

        deduction = cash - total;
        aaa = String.valueOf(deduction);        
        cav.setText(aaa);
    }

    else if (cash < total){
        error.setText("Sorry, you don't currently have the funds");
    }       
}
}

Hi folks,

I am currently learning java and wanted to give myself a project that was both challenging and fun. I decided to build a game that I remember playing when I was a kid called dopewars.

This is my second attempt at this game. When I began my first attempt, all went well. After a short while my source code began to fill wildly out of control until I could continue no more as I kept getting lost within mountains of code.

I then decided to begin again, only this time I wanted to seperate the gui from the logic (2 different .java files). This is where my problem lies. Previously this would work fine. Since seperating my java files the functionality has stopped.

When I press jbutton b1, my program is supposed to take the price value of cocaine and the units value entered into the jtextfield by the user, perform a calculation by accessing a method within Buy.java, and then update the appropriate JLabels within the s jpanel of GUI.java.

For example, user x wants to buy cocaine at the price indicated, so he enters a value representing the quantity he would like. He then presses the buy button which ultimately deducts the money from his pocket which is shown on the left side of the program window by using a method within the Buy class.

I hope you can understand my explanation and I hope to hear from you soon. Thanks.in advance. My Source code is below.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
dazbrad
  • 182
  • 1
  • 12
  • Having real problems uploading question and sourc code together. There should ne two .java files, but it appears some has been cut off. Atleast it looks that way on my phone?!? – dazbrad Jan 16 '14 at 10:01
  • 1
    See also this [example](http://stackoverflow.com/a/3072979/230513). – trashgod Jan 16 '14 at 11:43

2 Answers2

0

The following answer in no shape or form encourages real dope wars ;)

MVC is your friend. The way I do this usually is to write the data and data manipulation parts of the code first, without any thought whatsoever as to how it's going to look on screen.

Try to separate out the "data" part of your code first. Keep that neatly elsewhere; simple pojo objects representing real world objects, for example.

Then write the operations you can do on those objects into different classes. Make these completely independent of UI, as I said before. This ensures that the functionality part of your code is nice and separate, and essentially test-able.

Then write your UI part. A button click routes to a method call in your operations part of the code, for example.

Hope that helps.

Manish Patel
  • 4,411
  • 4
  • 25
  • 48
0

Your problems begin right here:

public class Buy extends GUI {

Try to rewrite you game logic so that it does not know a single thing about GUI. Assume you want to make your game playable via command line. Your game logic should be written in a way that it does not have to be changed at all to make this possible.

Try to identify the interface through which the user interacts with model of the game. Then implement this interface.

Here are some ideas (I don't know DopeWars, so I'll give you some generalized examples)

public class MyGameEngine {

    public PlayerData getPlayerData() { ... }

    public void sellSomeStuff(int numberOfStuff) throws InvalidPlayerOperation {
        if (numberOfStuff > getPlayerData().getAmountOfStuff() {
            throw new InvalidPlayerOperation("Not enough stuff");
        }
        ....
    }
}

public class PlayerData {

    public int getAmountOfStuff() { ... }
}

public class InvalidPlayerOperation extends RuntimeException { ... }

There are a lot of patterns in this area. But if you want really to learn something, try it without looking up patterns first. When you got it working, there will be enough time to learn about patterns and then you check your code if you used them intuitively or if you could introduce them to improve its quality.

In particular, the MVC pattern might be overkill for your simple game, especially if you don't want to use an MVC framework (you don't).
The Singleton pattern, on the other hand, seems simple at first but is generally bad style and will only cause problems later on.

Cephalopod
  • 14,632
  • 7
  • 51
  • 70
  • Give me a while, ima try something like you have described and see how it goes, thanks for your time. – dazbrad Jan 16 '14 at 10:59