Results 1 to 4 of 4
  1. #1
    mcdanielnc89 is offline Member
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Default Problem in my code

    Hey guys. I'm having some problems with my code. I don't get any errors when coding it and running it. However it is giving me the wrong output and i have a feeling i know it lies in the totals method in the part of the code, but i am not certain. Could I get a little help please? It it tells me the total of all costs it calculates it wrong, but all i did was add it together.

    Java Code:
    // Cell Phone Billing  Coding
    
    import javax.swing.JOptionPane;
    
    import java.text.DecimalFormat;
    
    
    public class CodingMethods 
    {
    	public static void main(String [] args)
    	{ // open main
    
    		// declare variables
    
    		String cellPlanStr, minutesStr, dataStr, name, output, planType;
    		double discount, price, minutes, data, minuteCost, dataCost, totalCost;
    		int cellPlan, planNumber;
    
    		// get input
    
    		name = JOptionPane.showInputDialog("Please enter your First and Last Name: ");
    		cellPlanStr = JOptionPane.showInputDialog("Please enter the Cell Phone Plan: \n1 - Ultimate \n2 - Gold \n3 - Silver");
    		minutesStr = JOptionPane.showInputDialog("Please enter the estimated amount of minutes used: ");
    		dataStr = JOptionPane.showInputDialog("Please enter the estimated amount of data used in Gigabytes: ");
    
    		// cast the data
    
    		cellPlan = Integer.parseInt(cellPlanStr);
    		minutes = Double.parseDouble(minutesStr);
    		data = Double.parseDouble(dataStr);
    
    		// Check for invalid cell plan entry
    
    		while (cellPlan < 1 || cellPlan > 3)
    		{ // open while loop
    			
    			JOptionPane.showMessageDialog(null, "Please enter a value between 1 and 3.");
    			cellPlanStr = JOptionPane.showInputDialog("Please enter the Cell Phone Plan: \n1 - Ultimate \n2 - Gold \n3 - Silver");
    			cellPlan = Integer.parseInt(cellPlanStr);
    			
    		} // close while loop
    
    			// call method named calcMinuteCharge
    
    			minuteCost = calcMinuteCharge(cellPlan, minutes);
    
    			// call method named calcDataCharge
    
    			dataCost = calcDataCharge(cellPlan, data);
    
    			// call calcTotal for total charge
    
    			totalCost = calcTotal(cellPlan, minutes, data);
    
    			// call method to determine Plan Name
    
    			planType = getPlanType(cellPlan);
    
    			// create decimal format
    
    			DecimalFormat dollars = new DecimalFormat("$0.00");
    
    			// display the output
    
    			output =  name + ", thank you for using the " + planType + " Package." +
    					"\n\nYour minutes used estimated cost is " + dollars.format(minuteCost) +
    					"\nYour data used estimated cost is " + dollars.format(dataCost) +
    					"\nYour total package estimated cost is " + dollars.format(totalCost);
    
    			JOptionPane.showMessageDialog(null, output);
    
    	}// close main
    	
    	public static double calcMinuteCharge(int thePlanType, double theMinutes) 
    	{
    		double charge;
    
    		if (thePlanType == 1)
    		{
    			charge = theMinutes * .05;
    		}
    		else if ( thePlanType == 2)
    		{
    			charge = theMinutes * .07;
    		}
    		else
    		{
    			charge = theMinutes * .10;
    		}
    
    		return charge;
    
    	}
    
    	public static double calcDataCharge(int thePlanType, double theData) 
    	{
    		double dataCharge;
    
    		if (thePlanType == 1)
    		{
    			dataCharge = theData * 5.00;
    		}
    		else if (thePlanType == 2)
    		{
    			dataCharge = theData * 10.00;
    		}
    		else
    		{
    			dataCharge = theData * 15.00;
    		}
    
    		return dataCharge;
    
    	}
    
    	private static double calcTotal(int thePlanType, double theMinCost, double theDataCost) 
    	{
    		final double ULTIMATE = 40;
    		final double GOLD = 30;
    		final double SILVER = 20;
    		double price;
    
    		if (thePlanType == 1)
    		{
    			price = ULTIMATE + theMinCost + theDataCost;
    		}
    		else if (thePlanType == 2)
    		{
    			price = GOLD + theMinCost + theDataCost;
    		}
    		else
    		{
    			price = SILVER + theMinCost + theDataCost;
    		}
    
    		return price;
    	}
    
    	private static String getPlanType(int thePlanType) 
    	{
    		String planName;
    
    		if (thePlanType == 1)
    		{
    			planName = "Ultimate";
    		}
    		else if (thePlanType == 2)
    		{
    			planName = "Gold";
    		}
    		else
    		{
    			planName = "Silver";
    		}
    
    		return planName;
    
    	}	
    }
    Last edited by mcdanielnc89; 10-31-2013 at 02:44 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,418
    Rep Power
    5

    Default Re: Problem in my code

    Your passing the wrong information to your total cost method. You should be passing minuteCost and dataCost.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default Re: Problem in my code

    Output error

    Duplicate post!

  4. #4
    mcdanielnc89 is offline Member
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Default Re: Problem in my code

    Oh goodnesss. That simple.... I knew I wasn't doing something right. thanks guys

Similar Threads

  1. Having a problem with my code
    By jman8910 in forum New To Java
    Replies: 1
    Last Post: 01-15-2013, 09:33 PM
  2. I'm not sure where there is a problem with the code...
    By Blondedude092 in forum New To Java
    Replies: 5
    Last Post: 10-01-2012, 12:24 AM
  3. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  4. Replies: 9
    Last Post: 09-21-2010, 04:15 PM
  5. Problem with my first code
    By paul in forum New To Java
    Replies: 2
    Last Post: 07-26-2007, 04:09 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
  •