PDA

View Full Version : Sigh, Java HW Help


Pages : [1] 2

Marvel_us
01-25-07, 05:12 PM
So basically I have to design a program that carries out banking operations on a customers' account.

Now what I need to do is create a class for the customers Name, Address, Bank Account, Customer and Bank database.

I'm having trouble with creating the class Customer. I want my constructor to accept a Name object, Address object and Bank Account object however I don't remember how to do that. The Name, Address and Bank Account classes are set up already.

This is what I have for the constructor but I'm stuck.

public Customer( Name customerName, Address customerAddress, BankAccount customerBankAccount )
{

}

Did I set the constructor up right? If so, what the hell goes in the body?

Suggestions please.

rhink
01-25-07, 06:09 PM
If so, what the hell goes in the body?

You probably want to store the objects you're passing in somewhere...

Marvel_us
01-25-07, 06:19 PM
So basically just declare variables of those types and assign them to the parameter variables in the body?

rhink
01-25-07, 06:24 PM
yup.

public class blah {
private SomeObject obj = null;

public blah(SomeObject input) {
obj = input;
}
}

Marvel_us
01-25-07, 06:26 PM
Ah thanks, I think I got it now.


public class Customer
{
private Name name ;
private Address address ;
private BankAccount account ;

public Customer(Name n, Address add, BankAccount ba)
{
name = n;
address = add;
account = ba;
}

Correct?

Marvel_us
01-25-07, 06:42 PM
Another question...

All my classes compile, now it's time for a test class. The user will enter the information for each "customer". Would I just need to create one Customer object and then in the parameters create objects for name, address, account etc?

ie:

Customer cust1 = new Customer(new Name(args), new Addres(args), new BankAccount(args)) ;

Is that the best way to do it or does anyone have any suggestions?

Cinema4DXL
01-25-07, 10:47 PM
that wouldnt be the best way to do it. if your going to have a multiple customers, your going to need to create a list or array possibly to store each unique customer. the way you have it now everytime the user enters information it will recreate the cust1 Customer object (destory anything you previously entered)

but i cant be sure since i dont know the scope of your project

rhink
01-25-07, 10:56 PM
I agree, you probably want a single customer object per customer you have in memory, and store them in whatever your favorite type of list or collection is.

Marvel_us
01-25-07, 10:56 PM
that wouldnt be the best way to do it. if your going to have a multiple customers, your going to need to create a list or array possibly to store each unique customer. the way you have it now everytime the user enters information it will recreate the cust1 Customer object (destory anything you previously entered)

but i cant be sure since i dont know the scope of your project

Thanks but I've actually got past that. I created an array to store the objects and looped the prompts so the user controls when they are done entering customers.

Problem I'm having now is how to print the array using jOptionPane.

Cinema4DXL
01-25-07, 11:14 PM
try converting what you want to output into a string and print the string into the jOptionPane

Marvel_us
01-25-07, 11:16 PM
try converting what you want to output into a string and print the string into the jOptionPane

Makes sense, I'll give that a shot.

Thanks.

Tuork
01-25-07, 11:25 PM
I can actually hold my ground pretty well in Java.

What's your problem with JOptionPane?

Marvel_us
01-25-07, 11:31 PM
I can actually hold my ground pretty well in Java.

What's your problem with JOptionPane?

Well this is my first time using it and I'm basically self teaching myself. I'm required to use it over the Scanner class and so far I'm coming along. However right now I have an ArrayList with bank customers that I want to print but no luck so far.

Is there like an easy method or something in JOption that allows this?

rhink
01-25-07, 11:42 PM
JOptionPane.showMessageDialog(null,"stuff!");

replace "stuff" with a string object, or an object that has overriden object.toString(), so it actually prints something useful.

Marvel_us
01-25-07, 11:54 PM
Gah, my head is a bowl of mush right now. I keep reading the same lines over and over. I'm going to leave that part for the morning since this isn't due till tomorrow midnight.

Thanks for the responses. I'll try those suggestions and let you guys know how it goes.

Tuork
01-26-07, 09:16 AM
Go over to the API and check out the different variations on how to use JOptionPane.

the .showMessageDialogue(...) methos mentioned by rhink is the obvious choice.

You can also print out the info as an actual table, but that's a little more complicated as far as I remember.

Marvel_us
01-26-07, 11:25 AM
Okay so I created toString methods for my Name, Address and BankAccount classes that hold the various info pertaining their respective classes.

I'm thinking I should create another one in the Customer class that calls the toString from those three so when I need to display it in the test class I only call the toString from class Customer. If any one sees a problem with that speak up, please.

How would I display all the info for each object on the list now?

rhink
01-26-07, 06:07 PM
Go over to the API and check out the different variations on how to use JOptionPane.

the .showMessageDialogue(...) methos mentioned by rhink is the obvious choice.

You can also print out the info as an actual table, but that's a little more complicated as far as I remember.

Heh JTables can be a real pain in the ass, though this would be a relatively simple application of them. Swing is the worst part of working with java, imo. Extremely flexible, can do a lot with it, but it's also very complex if you do more than scratch the surface, and can be slow.

Marvel, you could certainly create a toString in your customer class that concatenated everything in the objects contained within.

As far as displaying them all.... well, you could pop up a new dialog using joptionpane for every single one, but that would require the user to click through each and every one. If you don't have a requirement to use a GUI, it's very straightforward just to iterate through a loop and do a System.out for each record you want to print. JTables would be a nice, neat way to list them, but they can be difficult to work with. Could also use a JTextArea. It really depends on what you're most familiar with, and exactly what the requirements for this project are.

Marvel_us
01-26-07, 06:23 PM
Heh JTables can be a real pain in the ass, though this would be a relatively simple application of them. Swing is the worst part of working with java, imo. Extremely flexible, can do a lot with it, but it's also very complex if you do more than scratch the surface, and can be slow.

Marvel, you could certainly create a toString in your customer class that concatenated everything in the objects contained within.

As far as displaying them all.... well, you could pop up a new dialog using joptionpane for every single one, but that would require the user to click through each and every one. If you don't have a requirement to use a GUI, it's very straightforward just to iterate through a loop and do a System.out for each record you want to print. JTables would be a nice, neat way to list them, but they can be difficult to work with. Could also use a JTextArea. It really depends on what you're most familiar with, and exactly what the requirements for this project are.

I've got the toString methods and right now it's currently looping through the items on the list but like you said the user has to click through every item. There's nothing in my assignment objective that doesn't allow that but I think it sucks to have to click through.

How would you recommend displaying everything at once using a scroll window with joptionpane?

rhink
01-26-07, 07:03 PM
JOptionPanes just make dialogs. I suppose you could insert line breaks and put everything in one big frickin string and output it all into a single window, but I don't think there's a way to make it scrollable. You'd need another control for that, which means you'd have to create a window for your application and learn how to use a little bit of swing or awt.

Marvel_us
01-26-07, 07:20 PM
Is that like the Jtextarea and jscrollpane?

rhink
01-26-07, 07:34 PM
Yup.

Marvel_us
01-26-07, 08:00 PM
Yup.

Ah cool, I somewhat know how to use those.

This is the method I created for the Bank database class which basically runs through the list and creates a display for every object. However this method creates a display for every object and requires the user to click through. Can't figure out how to just join everything up though.

public void DisplayList()
{
for(int i = 0; i < accounts.size(); i++)
{
Customer c = accounts.get( i ) ;

String s = c.toStringCustomer() ;

JTextArea text = new JTextArea(s, 40, 40);
JScrollPane pane = new JScrollPane(text);


JOptionPane.showMessageDialog(null, pane, "Account Information",
JOptionPane.INFORMATION_MESSAGE);
}

}

rhink
01-26-07, 08:17 PM
If you want to display everything at once, just concatenate the customer strings into one long one before creating the JOptionPane.

Marvel_us
01-26-07, 08:28 PM
So I'd need to create two strings, one that holds the toString for a specific object and assign the other string variable to the first string? Wouldn't the string get replaced after every loop or would it hold all of them?

Then use the joptionpane outside of the loop, right?

Something like:
public void DisplayList()
{
for(int i = 0; i < accounts.size(); i++)
{
Customer c = accounts.get( i ) ;

String s = c.toStringCustomer() ;

String r += s ;
}

JTextArea text = new JTextArea(r, 40, 40);
JScrollPane pane = new JScrollPane(text);


JOptionPane.showMessageDialog(null, pane, "Account Information",
JOptionPane.INFORMATION_MESSAGE);

}