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

    Default

    I dont have my program in front of me but I kind of have an idea, would it look like this..
    "Enhanced For Loop"
    Java Code:
    for (String pricingSheets : pricingSheet)
    {
    pricingSheets[0] = firstType
    pricingSheets[1] = secondType
    pricingSheets[2] = thirdType
    pricingSheets[3] = fourthType
    pricingSheets[4] = fifthType
    StringTokenizer st = new StringTokenizer(firstType);
    String firstTypeType = st.nextToken;
    String firstTypeExposures = st.nextToken;
    String firstTypeDevCost = st.nextToken;
    String firstTypePrintCost = st.nextToken;
    
    if (userInput.equals(APS))
    then (exposureCost.equals(firstTypeExposures);
    }
    Please tell me its somewhere along those lines lol?

  2. #22
    JT4NK3D's Avatar
    JT4NK3D is offline Member
    Join Date
    Nov 2007
    Posts
    50
    Rep Power
    0

    Default

    is pricingSheets the array and pricingSheet what you're calling it in the loop? if so, you have it mixed up its,
    (example)
    Java Code:
    String[] letters = { "a", "b", "c", "d", "e", "f", "g" };
    for(String element : letters) {
           System.out.print(element + ", ");
    }
    'element' is just a name to use in it, like an argument. the argument's value, the array is on the other side of
    the ":". the argument refers to the current element or the array the loop is iterating on.Hope this helps.
    You just got T4NK3D!
    Adonrcicg to rcesraeh the haumn mnid is clbapae of rdanieg tihs. -Jvaa Sun Moricyssemts-

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

    Default

    pricingSheet is my original array, im just making up the String pricingSheets just to satisfy the requirement, is that correct?

    Then I have to use the String tokenizer and an if statement to search through the 5 elements and depending on the "film type" assign a price which is also part of the element.

  4. #24
    JT4NK3D's Avatar
    JT4NK3D is offline Member
    Join Date
    Nov 2007
    Posts
    50
    Rep Power
    0

    Default

    I'm pretty sure you cant do pricingSheets[index number] like that. if you need to refer to an element of the array like that, do pricingSheet[index number], the original array. the variable pricingSheets just refers to the current element it's looping on. suggestion: to clear it up make the names more diffierent.
    You just got T4NK3D!
    Adonrcicg to rcesraeh the haumn mnid is clbapae of rdanieg tihs. -Jvaa Sun Moricyssemts-

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

    Default

    I see what you mean, so i'll use pricingSheet[index] instead, but as far as my code looks, should that work for getting the price (which is the last two parts of each element) of the exposures and prints?

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

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

    Default

    I tried that with no luck. To test it out I did

    Java Code:
    for (pricingSheets : pricingSheet)
    {
    
    System.out.println[0];}
    I did this to see what it would return and what it did was take element 0 and display it in the console 5 times like this

    Java Code:
    APS 24 5 0.10
    APS 24 5 0.10
    APS 24 5 0.10
    APS 24 5 0.10
    APS 24 5 0.10
    So im not sure how to do this? I have 5 classes in this project, maybe I should explain that first to clear up any misunderstandings.

    1 Class is to hold Customer information
    1 Class is to hold Film information
    1 Class is PhotoCenter which stores calculations like tax = .08 x subtotal, this is the same class in which the code we are currently working on is, it is inside of

    Java Code:
    public void findPricing()
    Then I have the for loop, string tokenizer, etc.

    1 Class is the PhotoCenterController which collects information
    1 Class is the Validator (which I have not worked on yet) which makes sure customer name is a certain length, phone number is not negative, etc, etc.

    So what I want to do is break up elements of this array into tokens, and then I would need to input some code to see what the user clicked on as far as what type of film they wanted, and then look inside the elements of the array for the price for that film type and put it into my calculations. So far everything ive tried no luck, would it help to post my photocenter and photocentercontroller classes here so you see an example?

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

    Default

    So you want to find the last part of each array element. So why can't you use such a simple thing.

    Java Code:
    public class GetData {
    
        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"};
        
        public static void main(String[] args) {
            for(int i = 0; i < pricingSheet.length; i++){
                System.out.println("Value: " + 
                        pricingSheet[i].substring(pricingSheet[i].lastIndexOf(" "), 
                        pricingSheet[i].length()));
            }
        }
        
    }

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

    Default

    Actually I have this code so far and it seems to be correct but I cant figure out for the life of me how to get the result into my equations. For example if the user clicks "APS 24" then I want the prices for that Element to be put into the appropriate rates

    Heres the 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 static void findPricing() {
            //==todo==
            //you need a for loop
            //a tokenizer
            //an if statement
            //then assign the costs to each variable
            for (String element : pricingSheet) {
                StringTokenizer st = new StringTokenizer(element);
                String filmType = st.nextToken();
                int exposures = Integer.parseInt(st.nextToken());
                double developCost = Double.parseDouble(st.nextToken());
                double printCost = Double.parseDouble(st.nextToken());
                System.out.printf("fileType = %s%nexposures = %d%n" +
                                  "developCost = %.2f%nprintCost = %.2f%n",
                                   filmType, exposures, developCost, printCost);
                System.out.println("---------------");
    So once the user chooses somthing I want it to go into the calculations and perform them.

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

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

    Default

    Ok since you might need to know the rest of the program ill post that class plus the others and ill even upload my jar.

    This is the PhotoCenter class which contains the code im having problems with:
    Java Code:
    import java.text.NumberFormat;
    import java.util.StringTokenizer;
    
    
    public class PhotoCenter {
    	
    	
    	private Customer owner;
    	private Film film;
    	
    	private static int totalRollsProcessed = 0;
    	private static int totalPrintsProcessed = 0;
    	private static double totalSales = 0;
    	private static int orderId = 0;
    	
     		
    	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"};	
    	
    	private double developCost;
    	private double printCost;
    	
    	private double subtotal;
    	private double tax;
    	private double total;
    	
    	private double devRate;
    	private double printRate;
    	
    	private final static double TAX_RATE = 0.08;
    	
    	public PhotoCenter(Customer anOwner, Film aFilm){
    		
    		
    		this.owner = anOwner;
    		this.film = aFilm;
    		
    		
    		developCost = 0;
    		printCost = 0;
    		
    		subtotal = 0;
    		tax = 0;
    		total =0;	
    	
    		
    	    ++orderId;
    	}
    	
    	
    	private void calcDevCost(){
    		
    		
    		totalRollsProcessed = totalRollsProcessed + owner.getNbrRolls();
    		
    		developCost = devRate * owner.getNbrRolls();
    
    	}
    	
    	private void calcPrintCost(){
    		
    		totalPrintsProcessed = totalPrintsProcessed + owner.getNbrPrints();
    		
    		printCost = owner.getNbrPrints() * owner.getNbrRolls() * printRate * film.getNbrExposures();
    	}
    	
    	private void calcSubtotal(){
    		
    		subtotal = developCost + printCost;
    	}
    	
    	private void calcTax(){
    		
    		tax = subtotal * TAX_RATE;
    	}
    	
    	private void calcTotal(){
    		
    		total = subtotal + tax;
    	    totalSales = totalSales + total;
    	}
    	
    		
    		private void findPricing(){
    		
    			 for (String element : pricingSheet) {
    		            StringTokenizer st = new StringTokenizer(element);
    		            String filmType = st.nextToken();
    		            int exposures = Integer.parseInt(st.nextToken());
    		            double developCost = Double.parseDouble(st.nextToken());
    		            double printCost = Double.parseDouble(st.nextToken());
    		            System.out.printf("filmType = %s%nexposures = %d%n" +
    		                              "developCost = %.2f%nprintCost = %.2f%n",
    		                               filmType, exposures, developCost, printCost);
    		            System.out.println("---------------");
    			 }	
    		}	
    	
    	
    	public void processOrder(){
    		findPricing();
    		calcDevCost();		
    		calcPrintCost();	
    		calcSubtotal();		
    		calcTax();			
    		calcTotal();		
    	}
    	
    	
    	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);
    
    		
    		return summary;
    		
    	}
        
    		
    	public static String salesSummary(){
    		
    		
    			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;
    				
    			return result;
    			
    		}
    	}
    Now my Customer, Film, and Validator 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){
    		this.firstName = aFirstName;
    		this.lastName = aLastName;
    		this.phone = aPhone;
    		this.nbrRolls = aNbrRolls;
    		this.nbrPrints = aNbrPrints;
    		
    	
    	}
    	
    	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;
    	}
    
    }
    Java Code:
    public class Film {
    	
    	private String filmType;
    	private int nbrExposures;
    	
    	public Film(String aFilmType, int aNbrExposures){
    		this.filmType = aFilmType;
    		this.nbrExposures = aNbrExposures;
    		
    }
    	
    	
    	public String getFilmType(){
    		
    		return filmType;
    
    	}
    	
    	public int getNbrExposures(){
    		
    		return nbrExposures;
    
    	}
    	
    	
    	public String toString(){
    		String result = "";
    		result += "Film Type: " + this.filmType + "\n";
    		result += "Film Exposures: " + this.nbrExposures;
    		
    		return result;
    	}
    	
    }
    Java Code:
    import javax.swing.JOptionPane;
    
    
    public class Validator {
    	private String firstName;
    	private String lastName;
    	private String phone;
    	private int nbrRolls;
    	private int nbrPrints;
    	
    	public Validator(String aFirstName, String aLastName, String aPhone, int aNbrRolls, int aNbrPrints){
    		firstName = aFirstName;
    		lastName = aLastName;
    		phone = aPhone;
    		nbrRolls = aNbrRolls;
    		nbrPrints = aNbrPrints;
    	}
    		
    		public String getfirstName(){
    			if (firstName.equals(""))
    			{JOptionPane.showMessageDialog(null, "First Name Cannot Be Blank! Exiting Program!"); 
    			System.exit(0);}
    			return firstName;
    		}
    		
    		public String getlastName(){
    			if (lastName.equals(""))
    			{JOptionPane.showMessageDialog(null, "Last Name Cannot Be Blank! Exiting Program!"); 
    			System.exit(0);}
    			return lastName;
    				
    		}
    		
    		public String getphone(){
    			if (phone.equals("")) 
    			{JOptionPane.showMessageDialog(null, ""); 
    			System.exit(0);}
    		if(phone.length() > 10) 
    		{JOptionPane.showMessageDialog(null, "Exiting the program!"); 
    		System.exit(0);}
    		
    		return phone;
    		}
    		
    		public int getNbrRolls(){
    			 if (nbrRolls > 100) 
    			 {JOptionPane.showMessageDialog(null, "Exiting the program!"); 
    				System.exit(0);}
    			 if (nbrRolls < 1) 
    			 {JOptionPane.showMessageDialog(null, "Exiting the program!"); 
    				System.exit(0);}
    			 return nbrRolls;
    		}
    		
    		public int getNbrPrints(){
    			if (nbrPrints != 1 & nbrPrints != 2 & nbrPrints !=8 & nbrPrints !=10 & nbrPrints != 25 & nbrPrints
    					!= 30 & nbrPrints !=50) 
    			{JOptionPane.showMessageDialog(null, "Exiting the program!"); 
    			System.exit(0);}
    			return nbrPrints;
    		}
    
    }
    And Finally, my PhotoCenterController Class:

    Java Code:
    import java.util.NoSuchElementException;
    import java.util.StringTokenizer;
    import javax.swing.JOptionPane;
    
    /***********************************************************
    *	Class Name: PhotoCenterController							   	   		 
    *	Description: This class controls the PhotoCenter Object
    *				
    *				Ask the user to enter some info
    *				 
    *				Create a new instance of the PhotoCenter
    *				Object
    *				Pass the info that the user entered
    *				through the constructor method
    *
    *				process the order by executing the 
    *				public processOrder method of the object
    *
    *				get the result by executing  
    *				the getReceipt public method of the object
    									   	
    ************************************************************/
    import javax.swing.JOptionPane;
    import java.util.StringTokenizer;
    public class PhotoCenterController {
    	
    	
    
    	public static void main(String[] args) {
    
    		do{
    			
    			String input = JOptionPane.showInputDialog("Welcolme to Photo Express" + "\n\n"
    		             +  "Please enter the following seperated by spaces:\n\n"
    		             + "First Name:" + "\n" 
    		             + "Last Name:" + "\n"
    		             + "Phone Number:" + "\n"
    		             + "Number of Rolls" + "\n"
    		             + "Number of Prints" + "\n" + "\n"
    		             + "Example:MonkeyD Luffy 6265554321 5 8");
    			
    		
    	        StringTokenizer st = new StringTokenizer(input);
    
    	       
    	        String firstName = st.nextToken();
    	        String lastName = st.nextToken();
    	        String phone = st.nextToken();
    	        String nbrRolls = st.nextToken();
    	        String nbrPrints = st.nextToken();
    		
    	        int aNbrRolls = Integer.parseInt(nbrRolls);
    		    int aNbrPrints = Integer.parseInt(nbrPrints); 
    	        
    		Validator validation = new Validator(firstName, lastName, phone, aNbrRolls, aNbrPrints);
    			validation.getfirstName();
    			validation.getlastName();
    			validation.getphone();
    			validation.getNbrPrints();
    			validation.getNbrRolls();
    			
    		
    	    Customer person = new Customer(firstName, lastName, phone, aNbrRolls, aNbrPrints);    
    	    
    	
    		String[]  choices = {"APS 24", "APS 28", "35mm 12", "35mm 26", "35mm 30"};
            
    		int response = JOptionPane.showOptionDialog(
                 null                       					// center over parent
               , "Select a Film type and Number of Exposures"   // message
               , "Camera Film Selection"       					// title in titlebar
               , JOptionPane.YES_NO_OPTION  					// Option type
               , JOptionPane.PLAIN_MESSAGE  					// messageType
               , null                       					// icon
               , choices                    					// Options
               , "APS 24"    									// initial value
            );
           
    		input = choices[response];
    		StringTokenizer om= new StringTokenizer(input);
    		String filmType = om.nextToken();
    		String nbrExposures = om.nextToken();
    		int nmbrExposures =Integer.parseInt(nbrExposures);
    		
    		Film filmObject = new Film(filmType, nmbrExposures);
    				
    	
    		PhotoCenter photoObject = new PhotoCenter(person,filmObject);
    		
    		
            photoObject.processOrder();
    		
    		
    		JOptionPane.showMessageDialog(null,photoObject.getReceipt());
    		
    		}while (JOptionPane.showConfirmDialog(null, "Enter More Customers?") == JOptionPane.YES_OPTION);
    		JOptionPane.showMessageDialog(null,PhotoCenter.salesSummary());
    		
    		
    	}
    
    }
    Attached Files Attached Files

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

    Default

    Scratch that, heres my new PhotoCenter class, its working so far but I only for 1 element:

    Java Code:
     private void findPricing(){
    	    	for (int i=0; i<pricingSheet.length; i++){
    	    		StringTokenizer st = new StringTokenizer(pricingSheet[0]);
    	            String filmy = st.nextToken();
    	            String test2 = st.nextToken();
    	            String test3 = st.nextToken();
    	            String test4 = st.nextToken();
    	    		if (film.getFilmType().equals(pricingSheet[0]) & film.getNbrExposures() == 24);
    	    		devRate = 5;
    	    		printRate = 0.10;
    	    		
    	    		
    	    	}

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

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

    Default

    We actually got it fixed at school and it FINALLY works (we've been working on it all day)

    The LAST thing for this program is to put in a try catch statement but i tried putting it in the validator class or the controller class and neither work.

    Where can I put the try catch and how should it look? I just get a lot of endless errors if i put it before the input in the controller class and if i put it in validator i can keep it from getting errors but it just doesnt work.

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

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

    Default

    For example,

    When I put in a blank name I get an ElementException

    so I put a try catch around the part where i validate firstname and it did not work.

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

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

    Default

    I tried this for example, I get no errors but it will still throw the exception instead of the message:

    Java Code:
    public String getfirstName(){
    			try{
    			if (firstName.equals(""))
    			{JOptionPane.showMessageDialog(null, "First Name Cannot Be Blank! Exiting Program!"); 
    			System.exit(0);}}
    			
    			 catch(NoSuchElementException e)
    				{
    					JOptionPane.showMessageDialog(null, "You must fill in all information");
    				}
    		
    	        return firstName;
    		}

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

    Default

    Did you import required package for this. If so this is fine.

    But actually it's useless. Reason is, in the if statement you have do some validation that a specific field is empty or not. So why did you worry about elements again.

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

    Default

    Because it is not working for the original exception, itll just throw an exception instead of catching it using my original statement.

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