n00bfest.com
Home Forum phpBB3 Shoutbox Servers Teamspeak Admins Donate FAQ
Sign Up
Stats Contact Us!

 

View unanswered posts | View active topics It is currently Mon Sep 07, 2026 7:11 am

Forum rules


- n00bfest is not the special olympics. It is a gaming community. Do not act like n00bfest is a retard daycare or you will be punished.
- Outright flames and flamebaiting will be punished.
- Even if you are not an adult, do your best to act like one.
- *NEW* Political/Religious News and NSFW posts do not belong here. Use the Videos, Links, Political/Religious News Discussion Forum
- Punishments range from warnings to permanent forum/gameserver bans.





Reply to topic  [ 13 posts ] 
 Java assignment question 
Author Message
n00bfest Ancient, Senior Admin
User avatar

Joined: Sun Aug 07, 2005 11:00 pm
Posts: 2629
Location: Cocoa, Florida
Post Java assignment question
So I'm in a class that requires you to know Java and I pretty much don't. My teacher for OOP (Object Oriented Programming, IE Java) was hella middle-eastern with a terrible accent that hardly knew Java herself, so I didn't learn anything. Oh, and Java sucks anyways.

Basically I have a GUI here that lets you put in # of items you'd like to order, what you'd like to order (CD ID from an inventory file named "inventory.txt", which is 11111, 22222, etc. through 99999) and how many you'd like to order. I have buttons that do stuff that you most likely don't care about.

The thing I need help with is the newOrder button. I basically need to find some way to reset everything to what it was before the user starts messing with stuff/inputting stuff/etc.. basically a restart of the GUI. I'd like to be able to do this without manually going in and setting all the variables and stuff to their original values.

Ideas? I know one of you guys have to know Java. :(

There's always extra credit for doing some sort of faggy drag and drop listener.. but I'll leave that for later!

The code:

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
   
public class Invoice implements ActionListener {
    //constants
    private static final int WINDOW_WIDTH = 640; //pixels
    private static final int WINDOW_HEIGHT = 200; //pixels
    private static final int FIELD_WIDTH = 33;   //characters
       
    //window for GUI
    private JFrame window = new JFrame("Adam's World of Music");
   
    //other vars
    int counter = 0;
    int intNumItems = 1;
    int intSubItems = 0;
    double discount = 0;
    double dtotal = 0;
    double dsubTotal = 0;

    //for use in viewOrder, because java is a baby
    String[] mBox = new String[10];
    String output = null;
    String outstring = null;
   
    //entry spaces and labels, the retarded spacing is to give the buttons room
    private JLabel numOrderItemsTAG = new JLabel("               Enter number of items in this order:");
    private JTextField numOrderItems = new JTextField(FIELD_WIDTH);
    private JLabel cdIDTAG = new JLabel("               Enter CD ID for Item #" + intNumItems + ":");
    private JTextField cdID = new JTextField(FIELD_WIDTH);
    private JLabel numItemsTAG = new JLabel("               Enter quantity for Item #" + intNumItems + ":");
    private JTextField numItems = new JTextField(FIELD_WIDTH);
    private JLabel itemInfoTAG = new JLabel("               Item #" + intNumItems + " info:");
    private JTextField itemInfo = new JTextField(FIELD_WIDTH);
    private JLabel subTotalTAG = new JLabel("               Order subtotal for " + intSubItems + " item(s):");
    private JTextField subTotal = new JTextField(FIELD_WIDTH);
   
    //lots of buttons
    private JButton processButton = new JButton("Process Item #" + intNumItems);
    private JButton confirmButton = new JButton("Confirm Item #" + intNumItems);
    private JButton viewOrderButton = new JButton("View Order");
    private JButton finishOrderButton = new JButton("Finish Order");
    private JButton newOrderButton = new JButton("New Order");
    private JButton exitButton = new JButton("Exit");
   
    //Invoice() constructor
    public Invoice() {
        //configure GUI
        window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setLocationRelativeTo(null);
       
        //actionListeners for buttons
        processButton.addActionListener(this);
        confirmButton.addActionListener(this);
        viewOrderButton.addActionListener(this);
        finishOrderButton.addActionListener(this);
        newOrderButton.addActionListener(this);
        exitButton.addActionListener(this);
       
        //add components to JPanel p1
        //Container c = window.getContentPane();
        //c.setLayout(new FlowLayout(FlowLayout.RIGHT, 0, 0));
        JPanel p1 = new JPanel();
        p1.setLayout(new FlowLayout(FlowLayout.RIGHT, 0, 0));
        p1.add(numOrderItemsTAG);
        p1.add(numOrderItems);
        p1.add(cdIDTAG);
        p1.add(cdID);
        p1.add(numItemsTAG);
        p1.add(numItems);
        p1.add(itemInfoTAG);
        p1.add(itemInfo);
        p1.add(subTotalTAG);
        p1.add(subTotal);
       
        //add components to JPanel p2
        JPanel p2 = new JPanel();
        p2.add(processButton);
        p2.add(confirmButton);
        p2.add(viewOrderButton);
        p2.add(finishOrderButton);
        p2.add(newOrderButton);
        p2.add(exitButton);
       
        //make a container holding the two JPanels with BorderLayout
        Container c = window.getContentPane();
        c.setLayout(new BorderLayout(5, 10));
        c.add(p1);
        c.add(p2, BorderLayout.SOUTH);
       
        //set some elements uneditable/inactive
        itemInfo.setEditable(false);
        subTotal.setEditable(false);
        confirmButton.setEnabled(false);
        viewOrderButton.setEnabled(false);
        finishOrderButton.setEnabled(false);
       
        window.setVisible(true);   
    }
   
    //actionPerformers
    public void actionPerformed (ActionEvent e) {
        //get user input
        String response1 = numOrderItems.getText();
        String response2 = cdID.getText();
        String response3 = numItems.getText();
       
        //if processButton is pressed
        if (e.getSource() == processButton) {
            try {
                //read in the inventory text file
                BufferedReader in = new BufferedReader(new FileReader("inventory.txt"));
                String str = null;
                //line by line, tokenize by comma
                for (int i = 1; i <= Integer.parseInt(response1); i++) {
                    while ((str = in.readLine()) != null) {
                        StringTokenizer st = new StringTokenizer(str, ",");
                        String itemNum = st.nextToken().trim();
                        if (itemNum.equals(response2)) {
                            String albName = st.nextToken().trim();
                            String price = st.nextToken().trim();
                            //if # of CDs > 4 and <=9, give 10% discount
                            if (Integer.parseInt(response3) > 4 && Integer.parseInt(response3) <= 9) {
                                dtotal = (Double.parseDouble(price) * Integer.parseInt(response3) * (1 - discount));
                                discount = 0.1;
                                output = String.format("%s %s $%s %.0f%% $%.02f", itemNum, albName, price, discount*100, dtotal);
                                itemInfo.setText(output);
                            }
                            //if # of CDs > 9 and <= 14, give 15% discount
                            else if (Integer.parseInt(response3) > 9 && Integer.parseInt(response3) <= 14) {
                                dtotal = (Double.parseDouble(price) * Integer.parseInt(response3) * (1 - discount));
                                discount = 0.15;
                                output = String.format("%s %s $%s %.0f%% $%.02f", itemNum, albName, price, discount*100, dtotal);
                                itemInfo.setText(output);
                            }
                            //if # of CDs >= 15, give 20% discount
                            else if (Integer.parseInt(response3) >= 15) {
                                dtotal = (Double.parseDouble(price) * Integer.parseInt(response3) * (1 - discount));
                                discount = 0.2;
                                output = String.format("%s %s $%s %.0f%% $%.02f", itemNum, albName, price, discount*100, dtotal);
                                itemInfo.setText(output);
                            }
                            //if they are only buying one, discount doesn't matter
                            else {
                                dtotal = (Double.parseDouble(price) * Integer.parseInt(response3) * (1 - discount));
                                output = String.format("%s %s $%s %.0f%% $%.02f", itemNum, albName, price, discount*100, dtotal);
                                itemInfo.setText(output);
                            }
                        //get subTotal, break out of while loop
                       
                        dsubTotal += dtotal;
                        break;
                        }
                    }
                    System.out.print("i is " + i + "\n");
                    mBox[i] = output;
                    //System.out.print("output is " + output + "\n");
                    //System.out.print("mBox[i] is " + mBox[i] + "\n");
                }
                in.close();            
            }
            catch (IOException e1) {
                e1.printStackTrace();
            }
            counter++;
            processButton.setEnabled(false);
            confirmButton.setEnabled(true);
        }
        if (e.getSource() == confirmButton) {
            JOptionPane.showMessageDialog(window,"Item #" + intNumItems + " accepted");
            String totalText = String.format("$%.02f", dsubTotal);
            subTotal.setText(totalText);
            //if user has completed order, set process, confirm buttons inactive
            if (Integer.parseInt(response1) == counter) {
                processButton.setEnabled(false);
                confirmButton.setEnabled(false);
                finishOrderButton.setEnabled(true);
                viewOrderButton.setEnabled(true);
            }
            else {
                //clear user input and activate/deactivate buttons
                cdID.setText(null);
                numItems.setText(null);
                confirmButton.setEnabled(false);
                processButton.setEnabled(true);
                finishOrderButton.setEnabled(true);
                viewOrderButton.setEnabled(true);
               
                //set all buttons with numbers
                intNumItems++;
                intSubItems++;
                cdIDTAG.setText("               Enter CD ID for Item #" + intNumItems + ":");
                numItemsTAG.setText("               Enter quantity for Item #" + intNumItems + ":");
                itemInfoTAG.setText("               Item #" + intNumItems + " info:");
                subTotalTAG.setText("               Order subtotal for " + intSubItems + " item(s):");
                processButton.setText("Process Item #" + intNumItems);
                confirmButton.setText("Confirm Item #" + intNumItems);
            }
        }
        if (e.getSource() == viewOrderButton) {
            for (int i = 1; i < intNumItems; i++) {
                if (outstring == null) {
                    outstring = mBox[i];
                }
                else
                    outstring += "\n" + mBox[i] ;
            }
                JOptionPane.showMessageDialog(window, outstring);
        }
        if (e.getSource() == finishOrderButton) {
            try {
                BufferedWriter out = new BufferedWriter(new FileWriter("transactions.txt"));
                out.write("a string");
                out.close();
            }
            catch (IOException e2) {
                e2.printStackTrace();
            }
        }
        if (e.getSource() == newOrderButton) {
            //NEED HELP HERE!
        }
        if (e.getSource() == exitButton) {
            System.exit(0);
        }         
    }

    public static void main(String[] args) {
        Invoice gui = new Invoice();
    }
}

_________________
R.I.P. Image Stanislav "Lord_Macros" Krivosheev
The noblest of noble.


Wed Feb 03, 2010 11:02 am
Profile WWW
Game Server Admin
User avatar

Joined: Thu Apr 29, 2004 11:00 pm
Posts: 5609
Location: Yo mama's room, Bitch!
Post Re: Java assignment question
*cough* fomenta as F*CK question, shoulda just pmed his ass *cough*

_________________
Image

icemaN: i was droppin wards like they were turds


Wed Feb 03, 2010 2:08 pm
Profile
[n00b] Member

Joined: Thu May 21, 2009 11:00 pm
Posts: 2117
Location: Sagittarius A*
Post Re: Java assignment question
Call a new function called clear(), which will reset all the text values.

It would look something like this.

Code:
public void clear()
{
variable1.setText("");
variable2.setText("");
variable3.setText("");
}


So it will call the clear() function, which will reset all the values.


Wed Feb 03, 2010 3:22 pm
Profile
n00bfest Ancient, Senior Admin
User avatar

Joined: Sun Aug 07, 2005 11:00 pm
Posts: 2629
Location: Cocoa, Florida
Post Re: Java assignment question
Reborn wrote:
Call a new function called clear(), which will reset all the text values.

It would look something like this.

Code:
public void clear()
{
variable1.setText("");
variable2.setText("");
variable3.setText("");
}


So it will call the clear() function, which will reset all the values.


I could just do that in the if (e.getSource() == newOrderbutton), couldn't I?

I'm looking for a way where I won't have to do that, because I'll have to reset all of my counters and other stuff, too. Do you know if there's a way I can start a new instance of the program and kill this instance? Or something like that?

_________________
R.I.P. Image Stanislav "Lord_Macros" Krivosheev
The noblest of noble.


Wed Feb 03, 2010 3:45 pm
Profile WWW
Game Server Admin
User avatar

Joined: Sun May 11, 2008 11:00 pm
Posts: 840
Post Re: Java assignment question
Tucker wrote:
Reborn wrote:
Call a new function called clear(), which will reset all the text values.

It would look something like this.

Code:
public void clear()
{
variable1.setText("");
variable2.setText("");
variable3.setText("");
}


So it will call the clear() function, which will reset all the values.


I could just do that in the if (e.getSource() == newOrderbutton), couldn't I?

I'm looking for a way where I won't have to do that, because I'll have to reset all of my counters and other stuff, too. Do you know if there's a way I can start a new instance of the program and kill this instance? Or something like that?



Quit being lazy and just hardcode that stuff. Initial states are SUPPOSE to be hard coded somewhere, either in a configuration file or in the program itself. You NEVER want to depend on the compiler to set your initial values for you.


Wed Feb 03, 2010 9:24 pm
Profile
n00bfest Elder, Lead Developer
User avatar

Joined: Sun May 24, 2009 11:00 pm
Posts: 2766
Location: Gettin it in
Post Re: Java assignment question
So many options here we could go on for hours...

The best would probably be to create a ui class that includes the ui elements but not the window and write a constructor that initializes the variables how you want -- the idea would be to destroy the ui instantiation and simply create a new one (which would initialize the variables as you expect). If this is just a stupid project don't do it this way and do it the next way.

The easiest would probably be to do as reborn says and have a ui_initialize function that sets the variables as you want -- you'd really only have to move the initialization stuff you already have into it, add some additional code to ensure that text fields you relied on the be blank and so far skipped are, in fact, blank. Then call that during your initial initialization, and then again anytime the reset button is pushed.

_________________

Image


Wed Feb 03, 2010 9:55 pm
Profile
[HNIC] Stзamroller ω

Joined: Sun Apr 25, 2004 11:00 pm
Posts: 13453
Post Re: Java assignment question
I ui_initialized your mom, fomenta.

_________________
Image

I hate to advocate drugs, alcohol, violence, or insanity to anyone, but they've always worked for me.
-- Hunter S Thompson


Wed Feb 03, 2010 10:20 pm
Profile WWW
n00bfest Elder, Lead Developer
User avatar

Joined: Sun May 24, 2009 11:00 pm
Posts: 2766
Location: Gettin it in
Post Re: Java assignment question
Like this.


Attachments:
inventory.txt [73 Bytes]
Downloaded 83 times
Invoice.java.txt [484 Bytes]
Downloaded 85 times
InvoiceUI.java.txt [10.8 KiB]
Downloaded 103 times

_________________

Image
Wed Feb 03, 2010 10:51 pm
Profile
n00bfest Ancient, Senior Admin
User avatar

Joined: Sun Aug 07, 2005 11:00 pm
Posts: 2629
Location: Cocoa, Florida
Post Re: Java assignment question
Jet wrote:
Tucker wrote:
Reborn wrote:
Call a new function called clear(), which will reset all the text values.

It would look something like this.

Code:
public void clear()
{
variable1.setText("");
variable2.setText("");
variable3.setText("");
}


So it will call the clear() function, which will reset all the values.


I could just do that in the if (e.getSource() == newOrderbutton), couldn't I?

I'm looking for a way where I won't have to do that, because I'll have to reset all of my counters and other stuff, too. Do you know if there's a way I can start a new instance of the program and kill this instance? Or something like that?



Quit being lazy and just hardcode that stuff. Initial states are SUPPOSE to be hard coded somewhere, either in a configuration file or in the program itself. You NEVER want to depend on the compiler to set your initial values for you.


They are hard coded, unless I'm not understanding your definition of hard coded. Java requires me to initialize and declare every value.

Thanks for the help fomenta. I just wrapped my invoice class in some other class and called a reset() function that started a new instance of Invoice() and hid the other one.

_________________
R.I.P. Image Stanislav "Lord_Macros" Krivosheev
The noblest of noble.


Wed Feb 03, 2010 10:59 pm
Profile WWW
Retired n00bfest O.G.

Joined: Fri Dec 31, 2004 12:00 am
Posts: 2737
Post Re: Java assignment question
On a side note, I'll be taking Java sometime after I get back from deployment-- Along with C++.


Thu Feb 04, 2010 3:33 am
Profile
Game Server Admin
User avatar

Joined: Tue Apr 01, 2008 11:00 pm
Posts: 5190
Location: Vegas Baby
Post Re: Java assignment question
i want to learn shit like this sooo bad, but i don't have the time or money :( maybe i'll try to hijack some texts...

good luck acid. i took c++ in college and thought it was easy, but i think it was an older version

_________________
Image


Thu Feb 04, 2010 7:16 am
Profile
[n00b] Member

Joined: Thu May 21, 2009 11:00 pm
Posts: 2117
Location: Sagittarius A*
Post Re: Java assignment question
-Purple- wrote:
i want to learn shit like this sooo bad, but i don't have the time or money :( maybe i'll try to hijack some texts...

good luck acid. i took c++ in college and thought it was easy, but i think it was an older version


There are some awesome ebooks and video tutorials out there. They are so good, most classroom based training don't even come close.

For example, http://www.3dbuzz.com/vbforum/sv_home.php - They have video tutorials on every kind of 3d modeling and animation programs out there, along with programming in c++ and c# (with xna) with a heavy emphasis on game development.

How about Java? go here - http://www.javavideotutes.com/index/lessons/ - Those are made with complete beginners in mind.

Both sides are made with complete beginners in mind. Then there are the thousands of e-books to complement the video tutorials. All you need is the time :)


Thu Feb 04, 2010 10:37 am
Profile
Game Server Admin
User avatar

Joined: Tue Apr 01, 2008 11:00 pm
Posts: 5190
Location: Vegas Baby
Post Re: Java assignment question
Thanks reborn, that's some real good shit

_________________
Image


Thu Feb 04, 2010 3:21 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 13 posts ] 

Who is online

Users browsing this forum: Google [Bot] and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.