Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-04-2008, 08:27 PM
Member
 
Join Date: Apr 2008
Posts: 33
Bascotie is on a distinguished road
Using 2 Classes= Controller x Definition
Hey guys,

For school I am suppose to write a program with a controller class and a definition class.

I wonder if im on the right path, please assist me:

I am using Eclipse and what I have done was just make a package and within that package I have two classes, (Project2.java and Project2Class2.java)

My project wants me to use the JOptionPane to get some information from the "customer" and have them enter it in one line so this is what I have:

Code:
import javax.swing.JOptionPane; import java.util.StringTokenizer; public class Project2Class2 { public static void main(String[] args) { String userNameFirst = ""; String userNameLast = ""; String userPhone = ""; String userRolls = ""; String userPrints = ""; final int USER_EXPOSURES = 1; String userInput = JOptionPane.showInputDialog(null, "Please enter the following information with a space inbetween each:\n\n" + "First Name\n" + "Last Name\n" + "Phone Number \n" + "Number of Rolls \n" + "Number of Prints \n" + userNameFirst + " " + userNameLast + " " + userPhone + " " + userRolls + " " + userPrints); JOptionPane.showMessageDialog(null, "You input:\n" + userInput); } }
The problem is, I dont see why I need a controller class and another class to hold the definition. I know it must be done but I have no idea how it is to be done because all the examples I have looked at that have used a controller class and another class I do not understand how it could apply to this problem.

What the problem starts out asking for is for the "customer" to enter input such as user name, phone number, etc all in one line in the JOptionPane seperated by spaces and I have done that but how could I seperate it into two different classes?

Someone please help me get this one started, thank you!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-04-2008, 09:23 PM
Senior Member
 
Join Date: Jul 2007
Posts: 910
hardwired is on a distinguished road
Code:
import javax.swing.JOptionPane; public class AnExample { public static void main(String[] args) { DataAcquisition controller = new DataAcquisition(); // Create one PersonData: PersonData person = new PersonData(); person.name = controller.getName(); person.address = controller.getAddress(); person.age = controller.getAge(); person.interests = controller.getInterests(); System.out.println("person = " + person); } } /** controller class */ class DataAcquisition { public String getName() { return getData("name"); } public String getAddress() { return getData("address"); } public int getAge() { return Integer.parseInt(getData("age")); } public String getInterests() { return getData("interests"); } private String getData(String subject) { String s = "Enter " + subject; String retVal = JOptionPane.showInputDialog(s); if(retVal != null) return retVal; return "unknown"; } } /** definition (aka data store) class */ class PersonData { String name; String address; int age; String interests; public String toString() { return "PersonData[name:" + name + " address:" + address + " age:" + age + " interests:" + interests + "]"; } }

Last edited by hardwired : 05-05-2008 at 07:00 AM. Reason: fixed typo (bold)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-04-2008, 09:27 PM
Member
 
Join Date: Apr 2008
Posts: 33
Bascotie is on a distinguished road
Thanks, I'll experiment with similar code and come back with any questions!
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-04-2008, 09:37 PM
Member
 
Join Date: Apr 2008
Posts: 33
Bascotie is on a distinguished road
Going through this I think I still dont understand what exactly the seperate classes and whatnot are used for.

if someone could give me a short summary of why we use this type of coding in two seperate classes and a little explanation of the code, I would really appreciate it because I'd like to understand it, I feel thats why I still cant figure things out for myself because I dont know what I am trying to figure out.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-04-2008, 10:02 PM
Member
 
Join Date: Apr 2008
Posts: 33
Bascotie is on a distinguished road
Ok im not sure if im doing this correctly, but according to what I see your definition looks like, I've come up with this for the strings that I need:

Code:
import javax.swing.JOptionPane; import java.util.StringTokenizer; public class Project2Definition { //Declare variables class PersonData { String userFirstName; String userLastName; String userPhone; String userRolls; String userPrints; public String toString() { return "PersonData[name:]" + userFirstName + userLastName + "Phone:" + userPhone + "Number of Rolls:" + userRolls + " Number of Prints:" + userPrints; } } }
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-04-2008, 11:48 PM
Member
 
Join Date: Apr 2008
Posts: 33
Bascotie is on a distinguished road
When I input all this code, is it going to be together or is the definition part going to be in a class inside the package and then in a SEPARATE class in the SAME package I will have the other class for the rest of the code?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 05-04-2008, 11:58 PM
Senior Member
 
Join Date: Jul 2007
Posts: 910
hardwired is on a distinguished road
Second option is usually preferred.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 05-05-2008, 02:31 AM
Member
 
Join Date: Apr 2008
Posts: 33
Bascotie is on a distinguished road
Is the reason for separating classes purely for security issues or is it required/better how?
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 05-05-2008, 02:52 AM
Member
 
Join Date: Apr 2008
Posts: 33
Bascotie is on a distinguished road
What's really confusing me is, for example, to display the JOptionPane the way I wanted with the answers seperated by spaces I used this code:

Code:
public static void main(String[] args) { String userNameFirst = ""; String userNameLast = ""; String userPhone = ""; String userRolls = ""; String userPrints = ""; final int USER_EXPOSURES = 1; String userInput = JOptionPane.showInputDialog(null, "Please enter the following information with a space inbetween each:\n\n" + "First Name\n" + "Last Name\n" + "Phone Number \n" + "Number of Rolls \n" + "Number of Prints \n" + userNameFirst + " " + userNameLast + " " + userPhone + " " + userRolls + " " + userPrints); JOptionPane.showMessageDialog(null, "You input:\n" + userInput); }
But how would I do that in this case where I am using two different classes, the code is definitely different right?
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 05-05-2008, 07:25 AM
Senior Member
 
Join Date: Jul 2007
Posts: 910
hardwired is on a distinguished road
I fixed a typo in this line
Code:
// System.out.println("person = " + definition); System.out.println("person = " + person);
in post #2. It would have not compiled as posted. Sorry.
Code:
import javax.swing.JOptionPane; public class AnExample2 { public static void main(String[] args) { DataAcquisition controller = new DataAcquisition(); // Create one PersonData: String[] data = controller.getData(); PersonData person = new PersonData(data); System.out.println("person = " + person); } } class DataAcquisition { public String[] getData() { String userInput = JOptionPane.showInputDialog(null, "Please " + "enter the following information with a space inbetween " + "each:\n\n" + "Name\n" + "Address\n" + "Age\n" + "Interests"); String[] data = userInput.split("\\s"); return data; } } class PersonData { String name; String address; int age; String interests; public PersonData(String[] data) { this.name = data[0]; this.address = data[1]; age = Integer.parseInt(data[2]); interests = data[3]; } public String toString() { return "PersonData[name:" + name + " address:" + address + " age:" + age + " interests:" + interests + "]"; } }
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 05-06-2008, 12:40 AM
Member
 
Join Date: Apr 2008
Posts: 33
Bascotie is on a distinguished road
Thank you, I will try that out today God willing.

So I'll put the definition part in one class file and the other in another class file under the same project, but what I do not understand is how one class is able to read from the other?
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 05-06-2008, 02:33 AM
Member
 
Join Date: Apr 2008
Posts: 33
Bascotie is on a distinguished road
It wont run, I was able to finally get rid of all errors except for "PersonData cannot be resolved to a type"
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 05-06-2008, 02:44 AM
Member
 
Join Date: Apr 2008
Posts: 33
Bascotie is on a distinguished road
Sorry it does work I had to rename my class names ^^;; thanks, God willing I can start figuring out how everything works and build upon it, but please keep looking back for questions! =P
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 05-06-2008, 03:15 AM
Member
 
Join Date: Apr 2008
Posts: 33
Bascotie is on a distinguished road
Heres what I have so far, now what im trying to do is display the results of the input in the JOptionPane rather than the console and to stop and display an error if:

a. The name of the customer is not blank
b. The phone number of the customer is not blank and doesn’t exceed 10
characters and is an integer with positive number only
c. The number of rolls is between 1 and 100 and is an positive integer
d. The number of prints can only be (1,2,8,10, 25,30,50), positive integer

Code:
public class ProjectDefinition { String userNameFirst; String userNameLast; String userPhone; String userFilm; String userPrints; final int USER_EXPOSURES = 1; public ProjectDefinition(String[] data) { this.userNameFirst = data[0]; this.userNameLast = data[1]; userPhone = data[2]; userFilm = data[3]; userPrints = data[4]; } public String toString() { return "First Name:" + " " + userNameFirst + " " + "Last Name:" + " " + userNameLast + " " + "Phone Number:" + " "+ userPhone + " " + "Rolls of Film:" + " "+ userFilm + " " + "Number of Prints:"+ " " + userPrints; } }
Code:
public class ProjectController { public static void main(String[] args){ DataAcquisition controller = new DataAcquisition(); // Create one PersonData: String[] data = controller.getData(); ProjectDefinition person = new ProjectDefinition(data); System.out.println("person = " + person); } } class DataAcquisition { public String[] getData() { String userInput = JOptionPane.showInputDialog(null, "Please enter the following information with a space inbetween each:\n\n" + "First Name:\n" + "Last Name:\n" + "Phone Number:\n" + "Rolls of Film:\n" + "Number of Prints:"); String[] data = userInput.split("\\s"); return data; }}
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 05-06-2008, 03:21 AM
Member
 
Join Date: Apr 2008
Posts: 33
Bascotie is on a distinguished road
Scratch that, I got everything to display in the JOptionPane but I need the exceptions to be caught using the try/catch exceptions. Example on how to do this?
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 05-06-2008, 04:27 AM
Senior Member
 
Join Date: Jul 2007
Posts: 910
hardwired is on a distinguished road
Code:
import javax.swing.JOptionPane; public class PC { public static void main(String[] args){ DataAcquisition dataAcqn = new DataAcquisition(); // Create one PersonData: String[] data = dataAcqn.getData(); ProjectDefinition person = new ProjectDefinition(data); System.out.println("person = " + person); } } class DataAcquisition { public String[] getData() { String userInput = JOptionPane.showInputDialog(null, "Please enter the following information with " + "a space inbetween each:\n\n" + "First Name:\n" + "Last Name:\n" + "Phone Number:\n" + "Rolls of Film:\n" + "Number of Prints:"); String[] data = userInput.split("\\s"); validateData(data); return data; } /** * a. The name of the customer is not blank * b. The phone number of the customer is not blank and * doesn’t exceed 10 characters and is an integer * with positive number only * c. The number of rolls is between 1 and 100 and is an * positive integer * d. The number of prints can only be (1,2,8,10, 25,30,50), * positive integer */ private void validateData(String[] data) { String first = data[0]; String last = data[1]; String phone = data[2]; String film = data[3]; String prints = data[4]; try { if(first.length() == 0 || last.length() == 0) throw new IllegalArgumentException("names must not be blank"); // validate each of the four things listed above... } catch(IllegalArgumentException e) { System.out.println("Illegal Argument: " + e.getMessage()); System.exit(1); } } } class ProjectDefinition { String userNameFirst; String userNameLast; String userPhone; String userFilm; String userPrints; final int USER_EXPOSURES = 1; public ProjectDefinition(String[] data) { this.userNameFirst = data[0]; this.userNameLast = data[1]; userPhone = data[2]; userFilm = data[3]; userPrints = data[4]; } public String toString() { return "First Name:" + " " + userNameFirst + " " + "Last Name:" + " " + userNameLast + " " + "Phone Number:" + " " + userPhone + " " + "Rolls of Film:" + " " + userFilm + " " + "Number of Prints:" + " " + userPrints; } }
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 05-07-2008, 02:23 AM
Member
 
Join Date: Apr 2008
Posts: 33
Bascotie is on a distinguished road
Wow, yesterday I was working at this in a school lab and we ran into the try/catch exception problem using arrays. Turns out even though we have not gone over arrays in class my professor is fine as long as we are able to cover everything he asks for. I will first try a little code without the array and see if I am able to complete it or not. I really appreciate your help hardwired, no doubt you've had your cup of java.
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 05-07-2008, 02:26 AM
Member
 
Join Date: Apr 2008
Posts: 33
Bascotie is on a distinguished road
As an example, youve helped me cover two major points but using arrays could I still cover what the teacher is asking for such as:

2
6. Compute the cost for making pictures using the price of $5.19 for development of
a roll of film and the price of $0.15 per picture.

7. Use the following to perform the calculations:
a. Cost for development = number of rolls * development cost
b. Cost for printing = number of rolls * number of prints * number of
exposures * printing cost
c. Subtotal = Cost for development + Cost for printing
d. Tax = Subtotal * Tax rate
e. Total = Subtotal + Tax

8. Use a while loop to accept multiple orders from different customers and display
the order summary at the end of each order

9. Display the following summary using a JOptionPane window


Also we had modified the existing code to display everything in the JOptionPane, such as the results of the input and we got that far but using arrays they did not know how to cover these and also the try catch exceptions mentioned before.
Bookmark Post in Technorati
Reply With Quote
  #19 (permalink)  
Old 05-07-2008, 02:36 AM
Member
 
Join Date: Apr 2008
Posts: 33
Bascotie is on a distinguished road
This is what my definition code looks like now using a constructor:

Code:
public class ProjectDefinition { String userNameFirst; String userNameLast; String userPhone; int userFilm; int userPrints; final int USER_EXPOSURES = 1; public ProjectDefinition(String userNameFirst, String userNameLast, String userPhone, int userFilm, int userPrints) { super(); this.userNameFirst = userNameFirst; this.userNameLast = userNameLast; this.userPhone = userPhone; this.userFilm = userFilm; this.userPrints = userPrints; } public String toString() { return "Welcome to Photo World"+ "\n\n" + "First Name:" + " " + userNameFirst + "\n" + " " + "Last Name:" + " " + userNameLast + "\n" + " " + "Phone Number:" + " " + userPhone + "\n" + " " + "Rolls of Film:" + " " + userFilm + "\n" + " " + "Number of Prints:" + " " + userPrints; } }
My problem is changing the controller code to work with it:

Code:
public class ProjectDefinition { String userNameFirst; String userNameLast; String userPhone; int userFilm; int userPrints; final int USER_EXPOSURES = 1; public ProjectDefinition(String userNameFirst, String userNameLast, String userPhone, int userFilm, int userPrints) { super(); this.userNameFirst = userNameFirst; this.userNameLast = userNameLast; this.userPhone = userPhone; this.userFilm = userFilm; this.userPrints = userPrints; } public String toString() { return "Welcome to Photo World"+ "\n\n" + "First Name:" + " " + userNameFirst + "\n" + " " + "Last Name:" + " " + userNameLast + "\n" + " " + "Phone Number:" + " " + userPhone + "\n" + " " + "Rolls of Film:" + " " + userFilm + "\n" + " " + "Number of Prints:" + " " + userPrints; } }
Bookmark Post in Technorati
Reply With Quote
  #20 (permalink)  
Old 05-07-2008, 03:14 AM
Senior Member
 
Join Date: Jul 2007
Posts: 910
hardwired is on a distinguished road
Numbers 6 and 7 in reply #18 can be done in your ProjectDefinition class.
Number 8 is done in the PC class of #16 or in your ProjectController class of #14.
If you display the input data for each trip through the while loop you won't need to use arrays.
The need for an array comes when you want to store/keep_track_of each new ProjectDefinition instance created from/with the info entered during each trip through the while loop.

My problem is changing the controller code to work with it
Code:
public class PC { public static void main(String[] args){ DataAcquisition dataAcqn = new DataAcquisition(); while(user_wants_to_enter_more_data) { // Create another PersonData: String[] data = dataAcqn.getData(); ProjectDefinition person = new ProjectDefinition(data); System.out.println("person = " + person); // show data in JOptionPane(... // check with user for exit_loop/another_round_of_data } } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter