Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Call class method from another class

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Call class method from another class

    So I am creating a Netflix based application for a school assignment. So yes this is homework. I created a userinterface that looks like the following

    import java.io.IOException;
    import java.util.Queue;
    import java.util.Scanner;
     
     
    public class UserInterface extends Users {
     
    	public UserInterface(String userName, double accountNumber,
    			Queue<Movie> playList, Queue<Movie> lastMovies) {
    		super(userName, accountNumber, playList, lastMovies);
    	}
     
    	public static void main(String[] args){		
     
    		Scanner keyboard = new Scanner(System.in);
    		while(true){
     
    			System.out.println("X) Exit");
    			System.out.println("0) Create a new movie");
    			System.out.println("1) Add Movie to your playlist");
    			System.out.println("2) Upgrade your account");
     
    			char c = keyboard.next().charAt(0);
    			switch(c){
    				case 'X':
    					System.exit(0);
    				case '0':
    					createMovies;
    				case '1':
    					for(int i= 0; i < playList.size(); i++)
    					System.out.println(playList);
    					break;
    				case '2':
    					System.out.println("Small one time free of $10,000,000!");
    					break;
    			}	
    		}
    	}
    }

    so now what I am running into is the "case '0'" selection, where I am trying to just call "createMovies;" which is a method inside another class which looks like the following

    import java.util.Date;
    import java.util.Scanner;
     
    public class createMovie 
    {
    	public Scanner keyboard = new Scanner(System.in);
     
    	public static void main(String args[])
    	{
     
    	}
     
    public void createMovies()
    {
     
    	String password = "adminpassword";
    	boolean isAdmin = false;
    	double inputPrice = 0;
    	String inputMovie;
    	String inputGenre;
    	Date inputDate = new Date(); 
    	String inputPassword;
     
    	System.out.println("Enter Movie Title:");
    	inputMovie = keyboard.nextLine();
    	System.out.println("Enter Movie Genre:");
    	inputGenre = keyboard.nextLine();
    	System.out.println("Enter Admin Password:");
    	inputPassword = keyboard.nextLine();
     
    	passCheck(inputPassword, password, isAdmin);
    	adminPower(isAdmin, inputPrice);
    	Movie movies = new Movie(inputMovie, inputDate, inputGenre, inputPrice);
    }
     
        public void passCheck(String inputPassword, String password, boolean isAdmin)
        {
        if(inputPassword == password)
        {
        	isAdmin = true;
        }
        else System.out.println("Sorry wrong password");
        }
     
        public void adminPower(boolean isAdmin, double inputPrice)
        {
    	if (isAdmin == true)
    		System.out.println("Enter Movie Price:");
    	inputPrice = keyboard.nextDouble();
        }
     
    }

    and without adding more code to this post I will just say this creates a Movie object, that I will later have saved to a Movie selection linked list, and then allow the user to select movies out of that list.

    So the problem is I can't get the function to call properly inside my interface class. How would I go about doing this? I tried creating a "createMovie movies = null;" and then passing it through the function like so "movies.createMovies(); but apparently that doesn't actually do anything because it is null.


  2. #2
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    My Mood
    Sleepy
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Call class method from another class

    Ever tried "createMovie movies = new createMovie();" and "movies.createMovies()"? That would probably do you good.

    Just a heads up, where is the "break;" in case 'X' or case '0'?

Similar Threads

  1. Can't call paint() method from another class
    By Huw in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 18th, 2012, 03:15 AM
  2. update static field and call method within the class?
    By GeneralPihota in forum Object Oriented Programming
    Replies: 7
    Last Post: February 6th, 2012, 09:20 PM
  3. Call method(s) within the same class
    By mwr76 in forum Object Oriented Programming
    Replies: 8
    Last Post: September 26th, 2011, 01:58 AM
  4. How do I call a class in a main class
    By Leprechaun_hunter in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2011, 04:11 AM
  5. problem with data access when a class call another class
    By ea09530 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2010, 06:20 PM

Tags for this Thread