Page 1 of 3 123 LastLast
Results 1 to 20 of 47
  1. #1
    Bascotie is offline Member
    Join Date
    Apr 2008
    Posts
    88
    Rep Power
    0

    Default [SOLVED] Using Code Throughout 5 Classes-Stuck

    Hey guys,

    im remaking this thread to be more specific and organized about what I'd like to know:

    So far in my photo center program I can ask for input and have the input broken up into tokens using the string tokenizer, the input is seperated by spaces and assigned correctly to where it should go (almost);

    For example,

    Homer Simpson 5555555555 8 8

    should return

    firstName: Homer
    lastName: Simpson

    etc etc but the problem is when the summary page shows up it is showing the null values rather than what the user inputs.

    Heres my "receipt" code:

    Java Code:
    public String getReceipt(){		
    		NumberFormat cf = NumberFormat.getCurrencyInstance();
    		NumberFormat nf = NumberFormat.getNumberInstance();
    		nf.setMaximumFractionDigits(2);
    		
    		String summary = "";
    		
    		summary = "Welcome to PhotoCenter\n\n";
    		summary += "Your Order ID:";
    		summary += ++orderId;
    		summary += "\n";
    		summary += "-------------------------------";
    		summary += "\n";
    		summary += owner.toString();
    		summary += "\n";
    		summary += "-------------------------------";
    		summary += "\n";
    		summary += "Film Type:";
    		summary += film.toString();
    		summary += "\n";
    		summary += "------------------------------";
    		summary += "\n";
    		summary += "Development Cost ($5.00 per roll):";
    		summary += "\n";
    		summary += "Printing Cost ($0.10 per print):";
    		summary += "\n";
    		summary += "Subtotal:";
    		summary += "\n";
    		summary += "Tax (8%):";
    		summary += "\n";	
    		summary += "Total:";
    		
    			//==todo== you need to add to this summary
    		
    		return summary;
    owner.toString() and film.toString() are going into my film and customer classes (owner and film are just objects for those classes within the PhotoCenter class) and grabbing the return result.

    Problem is my summary is returning

    Customer First Name: null
    etc for the rest also

    If theres any other code I need to show please tell me, I dont want to post everything, people tend to not like that.. =)

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

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

    Default

    Youre right, to help understand, here are my customer and film classes:

    Java Code:
    public class Customer {
    	//Create local properties
    	//to hold the customer information
    	private String firstName;
    	private String lastName;
    	private String phone;
    	private int nbrRolls;
    	private int nbrPrints;
    	
    	public Customer(String aFirstName, String aLastName, String aPhone, int aNbrRolls, int aNbrPrints){
    		aFirstName = firstName;
    		aLastName = lastName;
    		aPhone = phone;
    		aNbrRolls = nbrRolls;
    		aNbrPrints = nbrPrints;
    		
    	
    	}
    	
    	public int getNbrRolls(){
    		return nbrRolls;
    
    	}
    	
    	public int getNbrPrints(){
            return nbrPrints;
    	}
    	
    	public String toString(){
    		String result = "";
    		result += "Customer First Name: " + this.firstName + "\n";
    		result += "Customer Last Name: " + this.lastName + "\n";
    		result += "Customer Phone: " + this.phone;
    		
    		return result;
    	}
    
    }
    And film..

    Java Code:
    public class Film {
    	//Create local properties
    	//to hold the Film information
    	private String filmType;
    	private int nbrExposures;
    	
    	//constructor
    	public Film(String aFilmType, int aNbrExposures){
    		aFilmType = filmType;
    		aNbrExposures = nbrExposures;
    		
    	}
    	
    	//getters
    	public String getFilmType(){
    		
    		return filmType;
    
    	}
    	
    	public int getNbrExposures(){
    		
    		return nbrExposures;
    
    	}
    	
    	//return object info as a string
    	public String toString(){
    		String result = "";
    		result += "Film Type: " + this.filmType + "\n";
    		result += "Film Exposures: " + this.nbrExposures;
    		
    		return result;
    	}
    	
    
    
    }
    Remember these are just two of 5 classes, the others being the PhotoCenter, the PhotoCenterController, and the Validator.

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

    Default

    Sorry, I see what you mean now, if I may its probably easier to understand by looking at my .jar, if i post all of my classes it might seem overwhelming. Here it is in .zip:
    Attached Files Attached Files

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    You have to make few changes on Coutomer and File classes. Som mistake you have done on constructor.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Java Code:
    public class Customer {
    	//Create local properties
    	//to hold the customer information
    	private String firstName;
    	private String lastName;
    	private String phone;
    	private int nbrRolls;
    	private int nbrPrints;
    	
    	public Customer(String aFirstName, String aLastName, String aPhone, int aNbrRolls, int aNbrPrints){
                    this.firstName = aFirstName;
                    this.lastName = aLastName;
                    this.phone = aPhone;
                    this.nbrPrints = aNbrPrints;
                    this.nbrRolls = aNbrRolls;
    	}
    	
    	public int getNbrRolls(){
    		return nbrRolls;
    
    	}
    	
    	public int getNbrPrints(){
            return nbrPrints;
    	}
    	
    	public String toString(){
    		String result = "";
    		result += "Customer First Name: " + firstName + "\n";
    		result += "Customer Last Name: " + lastName + "\n";
    		result += "Customer Phone: " + phone;
    		
    		return result;
    	}
    
    }

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Java Code:
    public class Film {
    	//Create local properties
    	//to hold the Film information
    	private String filmType;
    	private int nbrExposures;
    	
    	//constructor
    	public Film(String aFilmType, int aNbrExposures){
                    this.filmType = aFilmType;
                    this.nbrExposures = aNbrExposures;
    	}
    	
    	//getters
    	public String getFilmType(){
    		
    		return filmType;
    
    	}
    	
    	public int getNbrExposures(){
    		
    		return nbrExposures;
    
    	}
    	
    	//return object info as a string
    	public String toString(){
    		String result = "";
    		result += "Film Type: " + filmType + "\n";
    		result += "Film Exposures: " + nbrExposures;
    		
    		return result;
    	}
    	
    
    
    }

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

    Default

    Thank you, but if you could please explain to me why setting it to this.firstName works. I never quite understood how that works, does that mean it is only equal to aFirstName for that constructor, not for the outside code in other classes?

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

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

    Default

    Thats what I was wondering, I know its because I did not use the 'this' keyword when I should have, does the 'this' keyword only set the variable for that instance?

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Within a method or a constructor, this is reference to the current object. You can call any member of the current object using the keyword this.

    In the constructor, you call another class members. You can't do it. Values set to null.

    Hope it's clear.

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

    Default

    Thank you, I think I understand.

    My next problem is concerning this array in my PhotoCenter class:

    Java Code:
    private static String[] pricingSheet ={"APS 24 5 0.10 APS 28 10 0.13 35mm 12 3 0.08 35mm 26 6 0.12 35mm 30 14 0.18"};
    The format this is in is like this:

    [0] = Film Type
    [1] = Exposures
    [2] = Development Cost Per roll
    [3] = Printing Cost Per Roll

    and then it repeats.

    To help understand the first 4 would be APS film type, 24 exposures, $5 per roll, $.10 per print and then it repeats until the end of the array, film type, exposures, per roll, per print again

    So it holds 5 different film types with costs (24,28,35mm,35mm,35mm)

    Now I am trying to finish this code:

    Java Code:
    //this method finds the correct
    	//pricing for the item selected
    	//by the user. We loop through the 
    	//pricingSheet array and compare the value
    	//stored in the array with the one provided
    	//by the customer.
    	private void findPricing(){
    		//==todo==
    		//you need a for loop
    		//a tokenizer
    		//an if statement
    		//then assign the costs to each variable
    		for (double i = 0; i < pricingSheet.length; i++)
    		{
    			
    		}
    		
    		
    		
    	}
    Is the for loop correct? I also need a tokenizer and an if statement but im not too sure..

    I was thinking something like

    breaking up the array into tokens, is that correct?

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

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

    Default

    Well,

    what we start off with is

    Java Code:
    private static String[] pricingSheet ={"APS 24 5 0.10"};
    and the teacher tells us to fill out the rest of the elements, from what I understand elements are the numbers or words inside the array, the {... } part. Is that wrong?

    Should it be like

    Java Code:
    private static String[] pricingSheet ={"APS 24 5 0.10"};
    private static String[] pricingSheet2={"APS 28 10 0.13"};
    private static String[] pricingSheet3={"35mm 12 3 .08"};
    private static String...
    up until pricingSheet5?
    So I just make 5 pricingSheet until i have them all?

    The reason we need to use this is because each type of FILM has a different price and once the user inputs the type of film we have to grab the price from the array and use in our equation to determine price.
    Last edited by Bascotie; 05-30-2008 at 07:12 AM.

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    In this way you have five String arrays. Each of them has only one element.

    You want to deal with an element and get some informations?

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

    Default

    Yes, the teacher gives us the first array with one element and says the form of the array is
    Film Type
    Exposures
    Development Cost Per Roll
    Printing Cost Per Exposure

    and his instructions are:

    "put the rest of the pricing sheet elements"

    Here is the whole class:

    Java Code:
    import java.text.NumberFormat;
    import java.util.StringTokenizer;
    
    
    public class PhotoCenter {
    	
    	//Create our object variables (attributes)
    	//variables to hold the user input
    	//these are instance variables
    	private Customer owner;
    	private Film film;
    	
    	//totals these are static variables
    	//in other words CLASS variables
    	private static int totalRollsProcessed = 0;
    	private static int totalPrintsProcessed = 0;
    	private static double totalSales = 0;
    	private static int orderId = 0;
    	
    	//This follows the following format
    	//Film Type 
    	//Exposures
    	//Development Cost per Roll
    	//Printing Cost per exposure
    	//this is a CLASS variable
    	//==todo== put the rest of the pricing sheet elements
    	private static String[] pricingSheet ={"APS 24 5 0.10 APS 28 10 0.13 35mm 12 3 0.08 35mm 26 6 0.12 35mm 30 14 0.18"};	
    	
    	
    	//variables to hold the cost of
    	//each process
    	//these are instance variables
    	private double developCost;
    	private double printCost;
    	
    	//variables to hold the results
    	//of the calculations
    	//these are instance variables
    	private double subtotal;
    	private double tax;
    	private double total;
    	
    	//variables to hold the development rate
    	//and the print rate we are getting from
    	//the price sheet
    	//these used to be Constants in project1
    	//now they are instance variables
    	private double devRate;
    	private double printRate;
    	
    	//Create constants for
    	//items that are fixed and are not
    	//going to change
    	//this is a constant CLASS variable
    	private final static double TAX_RATE = 0.08;
    	
    	//This is the overloaded constructor
    	//it's similar to the default
    	//constructor but holds variables
    	//that are passed from the controller class
    	public PhotoCenter(Customer anOwner, Film aFilm){
    		
    		//initialize our local object variables
    		//with the variables passed by the user
    		//==todo==initialize the owner
    		//==todo===initialize the film
    		
    		this.owner = anOwner;
    		this.film = aFilm;
    		
    		
    		//These variables are initialized
    		//because their values are not set
    		//until the calculations are executed
    		developCost = 0;
    		printCost = 0;
    		
    		subtotal = 0;
    		tax = 0;
    		total =0;	
    	
    		//increment our order counter
    	
    	}
    	
    	//Create methods to do
    	//each calculation separately
    	//this way we can follow the 
    	//flow of execution
    	//these methods are private and are
    	//only accessible to the object itself
    	//the controller class cannot execute them
    	private void calcDevCost(){
    		
    		//==todo==increment number of rolls processed
    		++totalRollsProcessed;
    		//==todo==calculate development cost here
    		developCost = devRate * owner.getNbrRolls();
    
    	}
    	
    	private void calcPrintCost(){
    		//==todo==increment number of prints processed
    		++totalPrintsProcessed;
    		//==todo==calculate printing cost here
    		printCost = owner.getNbrPrints() * owner.getNbrRolls() * printRate * film.getNbrExposures();
    	}
    	
    	private void calcSubtotal(){
    		//==todo==calculate subtotal here
    		subtotal = developCost + printCost;
    	}
    	
    	private void calcTax(){
    		//==todo==calculate tax cost here
    		tax = subtotal * TAX_RATE;
    	}
    	
    	private void calcTotal(){
    		//==todo==calculate total cost here
    		total = subtotal + tax;
    		//==todo==increment total sales here
    		++totalSales;
    	}
    	
    		//this method finds the correct
    		//pricing for the item selected
    		//by the user. We loop through the 
    		//pricingSheet array and compare the value
    		//stored in the array with the one provided
    		//by the customer.
    		private void findPricing(){
    			//==todo==
    			//you need a for loop
    			//a tokenizer
    			//an if statement
    			//then assign the costs to each variable
    			for (double i = 0; i < pricingSheet.length; i++)
    			{
    				
    			}
    			
    			
    			
    		}	
    	
    	//create a public method 
    	//to have the object process the order
    	//this method is accessible 
    	//from the controller class
    	//The order is processed by 
    	//executing the calculations
    	//one at a time
    	public void processOrder(){
    		findPricing();
    		calcDevCost();		//calculate the dev. cost
    		calcPrintCost();	//calculate the print cost
    		calcSubtotal();		//calculate the subtotal
    		calcTax();			//calculate the tax
    		calcTotal();		//calculate the total
    	}
    	
    	
    	//this public method is used to return 
    	//a summary of this order this method 
    	//is accessible from the controller class
    	//we use the currency format to format
    	//the results and display them as currency
    	public String getReceipt(){		
    		NumberFormat cf = NumberFormat.getCurrencyInstance();
    		NumberFormat nf = NumberFormat.getNumberInstance();
    		nf.setMaximumFractionDigits(2);
    		
    		String summary = "";
    		
    		summary = "Welcome to PhotoCenter\n\n";
    		summary += "Your Order ID:";
    		summary += ++orderId;
    		summary += "\n";
    		summary += "-------------------------------";
    		summary += "\n";
    		summary += owner.toString();
    		summary += "\n";
    		summary += "-------------------------------";
    		summary += "\n";
    		summary += film.toString();
    		summary += "\n";
    		summary += "------------------------------";
    		summary += "\n";
    		summary += "Development Cost ($5.00 per roll):";
    		summary += cf.format(developCost);
    		summary += "\n";
    		summary += "Printing Cost ($0.10 per print):";
    		summary += cf.format(printCost);
    		summary += "\n";
    		summary += "Subtotal:";
    		summary += cf.format(subtotal);
    		summary += "\n";
    		summary += "Tax (8%):";
    		summary += cf.format(tax);
    		summary += "\n";	
    		summary += "Total:";
    		summary += cf.format(total);
    		
    		++totalPrintsProcessed;
    		++totalRollsProcessed;
    		++totalSales;
    		
    			//==todo== you need to add to this summary
    		
    		return summary;
    		
    	}
        
    		
    	//this public method is used to return
    	//a summary of all the sales made into the store
    	//it is a static CLASS method
    	public static String salesSummary(){
    		
    			//our number formatting
    			NumberFormat nf = NumberFormat.getCurrencyInstance();
    			String result = "";
    			result += "+++=================================+++\n";
    			result += "+++         Sales Summary           +++\n";
    			result += "+++=================================+++\n\n";
    			result += "\n\n";
    			result += "Total Prints Processed:";
    			result += totalPrintsProcessed;
    			result += "\n";
    			result += "Total Rolls Processed:";
    			result += totalRollsProcessed;
    			result += "\n\n";
    			result += "Total Sales:";
    			result += totalSales;
    			
    
    			//==todo==
    			//you need to add to this summary
    			
    			
    			return result;
    			
    		}
    	}

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    I'm not clear about your array. Your array is like this.

    Java Code:
    private static String[] pricingSheet ={"APS 24 5 0.10 APS 28 10 
    0.13 35mm 12 3 0.08 35mm 26 6 0.12 35mm 30 14 0.18"};
    But it's much better to make it like this. According to your requirement on the above array you have to do a grate work get an output.

    Java Code:
        private static String[] pricingSheet = {"APS 24 5 0.10" , 
                "APS 28 10 0.13", "35mm 12 3 0.08", "35mm 26 6 0.12", "35mm 30 14 0.18"};

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

    Default

    Thank you so much Eranga, youve been so much help.

    So once I have the array like:

    Java Code:
     private static String[] pricingSheet = {"APS 24 5 0.10" , 
                "APS 28 10 0.13", "35mm 12 3 0.08", "35mm 26 6 0.12", "35mm 30 14 0.18"};
    I will have 5 elements, then how do I scroll through the 5 elements to find pricing according to whether they click on APS 24, APS 28, ....,?

    Is it the binarySearch function?

    What I mean is if you look in the PhotoCenter class, down a little youll see instructions asking to use
    1) a for loop
    2) string Tokenizer
    3) an if statement

    So will it be something like what I showed up there saying

    for (i = 0, i < 4, i++){
    Then split it up into token show/
    Last edited by Bascotie; 05-31-2008 at 12:58 AM.

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

    Default

    Will it work if I use something like

    for (pricingSheet : pricingSheet)
    {
    StringTokenizer st = new StringTokenizer(pricingSheet)


    What I am trying to do is set each part of the 5 elements into tokens so I can cycle through them to find the appropriate price? Also what would the if statement look like please?

  20. #20
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    In a for loop, find each element of the array. Them make tokens from each element and assign them to a variable. Once you have all such details on variables you can return them to anywhere you want. That's the easiest way to do this.

Page 1 of 3 123 LastLast

Similar Threads

  1. Stuck on Two Questions, Please Help
    By sylo18 in forum New To Java
    Replies: 5
    Last Post: 03-11-2008, 02:03 AM
  2. musically stuck cry for help 2
    By geork in forum New To Java
    Replies: 0
    Last Post: 02-07-2008, 03:09 PM
  3. musically stuck
    By geork in forum New To Java
    Replies: 1
    Last Post: 02-06-2008, 10:44 PM
  4. Stuck and Frustrated.
    By jazzinspace in forum New To Java
    Replies: 7
    Last Post: 01-12-2008, 03:38 PM
  5. I am completely stuck
    By jpnym15 in forum New To Java
    Replies: 2
    Last Post: 11-14-2007, 07:40 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
  •