Results 1 to 20 of 47
- 05-30-2008, 04:44 AM #1
Member
- Join Date
- Apr 2008
- Posts
- 88
- Rep Power
- 0
[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:
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.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;
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.. =)
- 05-30-2008, 04:48 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
I can't see you have set any values to owner and film in your code.
- 05-30-2008, 04:59 AM #3
Member
- Join Date
- Apr 2008
- Posts
- 88
- Rep Power
- 0
Youre right, to help understand, here are my customer and film classes:
And film..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; } }
Remember these are just two of 5 classes, the others being the PhotoCenter, the PhotoCenterController, and the Validator.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; } }
- 05-30-2008, 05:18 AM #4
Member
- Join Date
- Apr 2008
- Posts
- 88
- Rep Power
- 0
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:
- 05-30-2008, 05:26 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You have to make few changes on Coutomer and File classes. Som mistake you have done on constructor.
- 05-30-2008, 05:26 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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; } }
- 05-30-2008, 05:27 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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; } }
- 05-30-2008, 05:31 AM #8
Member
- Join Date
- Apr 2008
- Posts
- 88
- Rep Power
- 0
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?
- 05-30-2008, 05:39 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Do you know what have done with 'this' keyword. You have use it in wrong way.
- 05-30-2008, 05:41 AM #10
Member
- Join Date
- Apr 2008
- Posts
- 88
- Rep Power
- 0
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?
- 05-30-2008, 05:45 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 05-30-2008, 05:52 AM #12
Member
- Join Date
- Apr 2008
- Posts
- 88
- Rep Power
- 0
Thank you, I think I understand.
My next problem is concerning this array in my PhotoCenter class:
The format this is in 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"};
[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:
Is the for loop correct? I also need a tokenizer and an if statement but im not too sure..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++) { } }
I was thinking something like
breaking up the array into tokens, is that correct?
- 05-30-2008, 06:02 AM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Your array have only one element there. Is that your array contain right data in right way.
- 05-30-2008, 06:06 AM #14
Member
- Join Date
- Apr 2008
- Posts
- 88
- Rep Power
- 0
Well,
what we start off with is
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?Java Code:private static String[] pricingSheet ={"APS 24 5 0.10"};
Should it be like
So I just make 5 pricingSheet until i have them all?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?
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 06:12 AM.
- 05-30-2008, 06:13 AM #15
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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?
- 05-30-2008, 06:18 AM #16
Member
- Join Date
- Apr 2008
- Posts
- 88
- Rep Power
- 0
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; } }
- 05-30-2008, 06:52 AM #17
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
I'm not clear about your array. Your array is like this.
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"};
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"};
- 05-30-2008, 08:09 PM #18
Member
- Join Date
- Apr 2008
- Posts
- 88
- Rep Power
- 0
Thank you so much Eranga, youve been so much help.
So once I have the array like:
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, ....,?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"};
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-30-2008 at 11:58 PM.
- 05-31-2008, 06:06 AM #19
Member
- Join Date
- Apr 2008
- Posts
- 88
- Rep Power
- 0
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?
- 05-31-2008, 06:27 AM #20
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
Similar Threads
-
Stuck on Two Questions, Please Help
By sylo18 in forum New To JavaReplies: 5Last Post: 03-11-2008, 01:03 AM -
musically stuck cry for help 2
By geork in forum New To JavaReplies: 0Last Post: 02-07-2008, 02:09 PM -
musically stuck
By geork in forum New To JavaReplies: 1Last Post: 02-06-2008, 09:44 PM -
Stuck and Frustrated.
By jazzinspace in forum New To JavaReplies: 7Last Post: 01-12-2008, 02:38 PM -
I am completely stuck
By jpnym15 in forum New To JavaReplies: 2Last Post: 11-14-2007, 06:40 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks