Page 1 of 2 12 LastLast
Results 1 to 20 of 36
  1. #1
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default 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:

    Java 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!

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java 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 = " + [b]person[/b]);
        }
    }
    
    /** 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 06:00 AM. Reason: fixed typo (bold)

  3. #3
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default

    Thanks, I'll experiment with similar code and come back with any questions!

  4. #4
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default

    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.

  5. #5
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default

    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:

    Java 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;
    	    }
    	}
      
     
      
      
      
    }

  6. #6
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default

    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?

  7. #7
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Second option is usually preferred.

  8. #8
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default

    Is the reason for separating classes purely for security issues or is it required/better how?

  9. #9
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default

    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:

    Java 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?

  10. #10
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    I fixed a typo in this line
    Java Code:
    //        System.out.println("person = " + definition);
            System.out.println("person = " + person);
    in post #2. It would have not compiled as posted. Sorry.
    Java 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 + "]";
        }
    }

  11. #11
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default

    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?

  12. #12
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default

    It wont run, I was able to finally get rid of all errors except for "PersonData cannot be resolved to a type"

  13. #13
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default

    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

  14. #14
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default

    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

    Java 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;
    	    }
    }
    Java 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;
    	    }}

  15. #15
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default

    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?

  16. #16
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java 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;
        }
    }

  17. #17
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default

    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.

  18. #18
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default

    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.

  19. #19
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default

    This is what my definition code looks like now using a constructor:

    Java 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:

    Java 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;
    	    }
    }

  20. #20
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    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
    Java 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
            }
        }
    }

Page 1 of 2 12 LastLast

Similar Threads

  1. Error: no class definition found
    By toby in forum New To Java
    Replies: 6
    Last Post: 08-28-2011, 10:32 PM
  2. Subclass definition
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:03 PM
  3. Eclipse- jump to method definition
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-07-2007, 03:34 PM
  4. Eclipse - jumping to method definition
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-07-2007, 02:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •