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 1 of 1

Thread: i can run my program but the math doesnt come otu

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default i can run my program but the math doesnt come otu

    um i dont know how to upload my file so im gona post my project, and i would post the pdf prompt but dono how to post that etiehr XD
    so basically i have a few classes and a main
    here is the main
    /**
     * Title: Car Wash with class
     *
     * Description: This program calculates the cost of a car wash based on level
     * of service, type of vehicle, and number of previous washes
     * This uses methods for calculation and validation
     *
     * @author Michael Newby
     * @version 1.0
     */
     
    import javax.swing.*;
    import java.text.DecimalFormat;
     
    public class CarWashWithClass
    {
        public static void main(String[] args)
        {
            long invoiceNumber;
            String name;
            char serviceType,
                 responseSUV;
            	// responseDetailing;
            int numberOfPrevious;
     
            int numberOfVehicles = 0;
     
            double totalDiscount = 0.0,
                   totalAmountDue = 0.0;
     
            DecimalFormat twodigits = new DecimalFormat("0.00");
     
            CarWash service = null;
     
            invoiceNumber = getInvoiceNumber();
     
            while (invoiceNumber > Long.MIN_VALUE)
            {
                name = getCustomerName();
            	serviceType = getValidType();
                responseSUV = getSUVResponse();
                numberOfPrevious = getPreviousService();
                if (numberOfPrevious == 0) // check for new customer
                {
                	service = new CarWash();
                	service.serviceForNewCustomer(invoiceNumber, name);
                }
                else
                {
                	service = new CarWash(invoiceNumber,name);
                	service.serviceForExistingCustomer(numberOfVehicles);
                }
                service.setSUV(responseSUV); //set whether SUV this has an error i dont know why
     
                JOptionPane.showMessageDialog(null, "Invoice number: " + service.getInvoiceNumber()
                		                       + "\nCustomer name: " + service.getName()
                                               + "\nType of service: " + service.typeOfServiceAsString()
                                               + "\nVehicle type: " + service.vehicleAsString()
                                               + "\nNumber of service: " + service.getnumberOfService()
                                               + "\nBasic cost: " +
                                                       twodigits.format(service.basicCost())
                                               + "\nSUV extra cost: " +
                                                       twodigits.format(service.extraCostForSUV())
                                               + "\nCost of service: " +
                                                       twodigits.format(service.costOfService())
                                               + "\nDiscount: " + twodigits.format(service.discountAmount())
                                               + "\nAmount due: " +
                                                      twodigits.format(service.amountDue())
                                               + "\nServices to next free: " + service.numberTillNextFree());
                numberOfVehicles++;
                totalDiscount += service.discountAmount();
                totalAmountDue += service.amountDue();
     
                invoiceNumber = getInvoiceNumber();
            }//end while
            JOptionPane.showMessageDialog(null, "Number of cars washed: " + numberOfVehicles
                                             + "\nTotal discount: " + twodigits.format(totalDiscount)
                                             + "\nTotal amount: " + twodigits.format(totalAmountDue));
           System.exit(0);
       } //end main
     
       public static long getInvoiceNumber()
       {
           long customerNumber;
           String inNumber;
     
           do
           {
               inNumber = JOptionPane.showInputDialog("Enter invoice number:");
               if (inNumber.equals(""))
                 customerNumber = Long.MIN_VALUE;
              else
              {
                 customerNumber = Long.parseLong(inNumber);
                 if (!isValidNumber(customerNumber))
                     JOptionPane.showMessageDialog(null, "Invoice number must be positive",
                                                   "Invalid Number", JOptionPane.ERROR_MESSAGE);
              } // end if
         } while (!isValidNumber(customerNumber));
         return customerNumber;
       } // end getCustomerNumber
     
       public static boolean isValidNumber(long number)
       {
         return (number > 0 || number == Long.MIN_VALUE);
       } //end isValidNumber
     
       public static String getCustomerName()
       {
    	   String name;
    	   name = JOptionPane.showInputDialog("Customer name");
    	   while (name.isEmpty())
    	   {
    		   name = JOptionPane.showInputDialog("Name cannot be blank..re-enter");
    	   }
    	   return name;	   
       }
     
       public static char getValidType()
       {
         char serviceType;
         String inType;
         do
         {
             inType = JOptionPane.showInputDialog("Type of wash:");
             if (inType.equals(""))
             {
                 serviceType = '\0';
             }
             else
             {
                serviceType = inType.charAt(0);
                serviceType = Character.toUpperCase(serviceType);
                if (!isValidService(serviceType))
                {
                    JOptionPane.showMessageDialog(null, "No such service",
                                                  "Invalid Service", JOptionPane.ERROR_MESSAGE);
                }
            } //end if
         } while (!isValidService(serviceType));
         return serviceType;
       } //end getValidType
     
     
       public static boolean isValidService(char serviceType)
       {
           boolean valid;
           switch (serviceType)
           {
           case 'T' : case 'W': case 'S':
               valid = true;
               break;
           default:
               valid = false;
               break;
           }// end switch
           return valid;
       } //end isValidService
     
       public static int getPreviousService()
       {
           int previousService;
           String inPrevious;
           inPrevious = JOptionPane.showInputDialog("Number of previous services:");
           previousService = Integer.parseInt(inPrevious);
           while (!isValidNumber(previousService))
           {
               JOptionPane.showMessageDialog(null, "Service must be between 0 and " + (CarWash.FREE_WASH - 1),
                                                  "Invalid Number", JOptionPane.ERROR_MESSAGE);
               inPrevious = JOptionPane.showInputDialog("Number of previous services:");
               previousService = Integer.parseInt(inPrevious);
            } //end while
            return previousService;
        }// end getPreviousService
     
        public static boolean isValidNumber(int numberOfService)
        {
            return (numberOfService >= 0 && numberOfService <= CarWash.FREE_WASH - 1);
        } // end isValidNumber
     
        public static char getSUVResponse()
        {
            char response;
            String inSUV;
            inSUV = JOptionPane.showInputDialog("Is vehicle an SUV?");
            response = inSUV.charAt(0);
            response = Character.toUpperCase(response);
            while (!isValidResponse(response))
            {
               JOptionPane.showMessageDialog(null, "Please respons Y or N",
                                                  "Invalid Number", JOptionPane.ERROR_MESSAGE);
               inSUV = JOptionPane.showInputDialog("Is vehicle an SUV?");
               response = inSUV.charAt(0);
               response = Character.toUpperCase(response);
           } //end while
           return response;
       }// end getSUVResponse
     
       public static boolean isValidResponse(char response)
       {
           boolean valid;
           switch (response)
           {
           case 'Y' : case 'N':
               valid = true;
               break;
           default:
               valid = false;
               break;
           }// end switch
           return valid;
       } //end isValidResponse
    } // end class CarWashWithClass
    ----
    here is CarWash the superclass
    public class CarWash
    {
    	final static double THE_WORKS_COST = 11.95;
       	final static double WHEEL_WORKS_COST = 14.95;
        final static double SUPER_WORKS_COST = 17.95;
      	final static double SUV_COST = 3.0;
       	final static int FULL_COST_LIMIT = 5;
       	final static int FREE_WASH = 10;
        final static double DISCOUNT_RATE = 0.05;	
     
    	private long invoiceNumber;
    	private String customerName;
    	protected double basicRate;
    	protected int numberOfService;
    	protected boolean whetherSUV;
     
    	public CarWash()
    	{
    	}
    	//end constructor
     
    	public CarWash(long number, String name)
    	{
    		invoiceNumber = number;
    		customerName = name;
    		basicRate = 0;
    		numberOfService = 0;
    		whetherSUV = false;
    	}
     
    	public void serviceForNewCustomer(long number, String name)
    	{
    		invoiceNumber = number;
    		customerName = name;
    		basicRate = 0;
    		numberOfService = 1;
    		whetherSUV = false;
    	}
     
    	public void serviceForExistingCustomer(int previousServices)
    	{
    		if (previousServices == 10)
    			numberOfService = 1;
    		else
    			numberOfService = previousServices + 1;
    		basicRate = 0;
    		whetherSUV = false;
    	}
     
    	public void setSUV()
    	{
    		whetherSUV = true;
    	}
     
    	public double basicCost()
    	{
    		 return basicRate;
        } // end basicCost
     
     
     
    	public double extraCostForSUV()
    	{
    		 double extraCost;
         		   if (whetherSUV)
                {
                	extraCost = SUV_COST;
                 }
           		else
                {
            		extraCost = 0.0;
                 } //end if
          		return extraCost;
                 }// end extraCostForSUV
     
    	public double costOfService()
    	{
    		return basicCost() + extraCostForSUV();
    	}
     
    	public double discountAmount()
    	{
    		double discount = 0.0;
            return discount;
        } //end discountAmount
     
    	  public double amountDue()
     	   {
         	   return costOfService() - discountAmount();
     	   } //end amountDue
     
    	public int numberTillNextFree()
        {
            int toNextService;
            if (numberOfService == FREE_WASH)
                toNextService = FREE_WASH;
            else
                toNextService = FREE_WASH - (numberOfService + 1);
            return toNextService;
        } //end numberToNextFree
     
        public String typeOfServiceAsString()
        {
        	return " ";
        } // end typeOfService
     
        public String vehicleAsString()
        {
            String outSUV;
            if (whetherSUV)
            {
                outSUV = "SUV";
            }
            else
            {
                outSUV = "Not SUV";
            } //end if
            return outSUV;
        }// end isSUVString
     
    	public long getInvoiceNumber()
        {
            return invoiceNumber;
        } //end getInvoiceNumber
     
    	public String getName()
        {
        	return customerName;
        }
        public double getRate()
        {
            return basicRate;
        } //end getServiceType
     
        public boolean isSUV()
        {
            return whetherSUV;
        } // end isVehicleSUV
     
        public int getnumberOfService()
        {
            return numberOfService;
        } // end getPreviousServices
    } // end class CarWash

    ---

    here is WheelWorks subclass
    public class WheelWorks extends CarWash
    {
    	public WheelWorks()
    	{
     
    	}//end constructor
     
    	public WheelWorks(long number, String name)
    	{
    		super(number, name);
    		basicRate = WHEEL_WORKS_COST;
    		numberOfService = 0;
    		whetherSUV = false;
    	}
     
    	public void serviceForNewCustomer(long number, String name)
    	{
    		super.serviceForNewCustomer(number, name);
    		basicRate = WHEEL_WORKS_COST;
    		numberOfService = 1;
    		whetherSUV = false;
    	}
     
    	public void serviceForExistingCustomer(long number, String name)
    	{
    		int previousServices = 0;
    		if (previousServices >= 0 && previousServices<=9)
    			basicRate = WHEEL_WORKS_COST;
     
    		else if (previousServices == 10)
    			numberOfService = previousServices + 1;
    			whetherSUV = false;
    	}
     
    	public double discountAmount()
    	{
    		double discount;
    	    if (numberOfService < FREE_WASH)
    		  discount = DISCOUNT_RATE * costOfService(); //applies discount if number of serivce < 10
    	    else if (numberOfService <= FULL_COST_LIMIT)
            {
                discount = 0.0;
            }//discount is 0 if number of service < 5
    	    else
            { 
                discount = costOfService();
            } //end if
            return discount;
        } //end discountAmount
     
    	public String typeOfVehicleAsString()
    	{
    		return "Wheel Works";
    	}
     
     
    }
    -----
    superworks
    public class SuperWorks extends CarWash
    {
    	protected boolean detailing;
     
    	public SuperWorks()
    	{
     
    	}
     
    	public SuperWorks(long number, String name)
    	{
    		super(number, name);
    		basicRate = SUPER_WORKS_COST;
    		numberOfService = 0;
    		whetherSUV = false;
     
    	}
     
    	public void serviceForNewCustomer(long number, String name)
    	{
    		super.serviceForNewCustomer(number, name);
    		basicRate = SUPER_WORKS_COST;
    		numberOfService = 0;
    		whetherSUV = false;
    		detailing = false;
    	}
     
    	public void serviceForExistingCustomer(long number, String name)
    	{
    		int previousServices = 0;
    		if (previousServices >= 0 && previousServices<=9)
    			basicRate = SUPER_WORKS_COST;
     
    		else if (previousServices == 10)
    			numberOfService = 0;
    			whetherSUV = false;
    			detailing = false;
    				}
     
    	public double discountAmount()
    	{
    		double discount;
            discount = 0.0;
            return discount;
        } //end discountAmount
     
    	public String typeOfVehicleAsString()
    	{
    		return "Super Works";
    	}
     
    	public int numberTillNextFree()
    	{
    		 return 9999;
    	     //end numberToNextFree
    	}
     
    	public void setDetailing()
    	{
    			detailing = true;
    	}
     
    	public double detailingCost()
    	{
    		double cost;
    		if (detailing = true)
    			cost = 10.00;
    		else
    			cost = 0.0;
    		return cost;
    	}
     
    	public double costOfService()
    	{
    		return basicCost() + extraCostForSUV() + detailingCost();
    	}
    }
    -----
    Theworks
    public class TheWorks extends CarWash
    {
    	public TheWorks()
    	{
     
    	}//end constructor
     
    	public TheWorks(long number,String name)
    	{
    		super(number, name);
    		basicRate = THE_WORKS_COST;
    		numberOfService = 0;
    		whetherSUV = false;
    	}
     
    	public void serviceForNewCustomer(long number, String name)
    	{
    		super.serviceForNewCustomer(number, name);
    		basicRate = THE_WORKS_COST;
    		numberOfService = 1;
    		whetherSUV = false;
    	}
     
    	public void serviceForExistingCustomer(long number, String name)
    	{
    			int previousServices = 0;
    		if (previousServices >= 0 && previousServices<=9)
    			basicRate = THE_WORKS_COST;
     
    		else if (previousServices == 10)
    			numberOfService = previousServices + 1;
    			whetherSUV = false;
    	}
    	public double discountAmount()
    	{
    		double discount;
    		  if (numberOfService < FREE_WASH)
    			  discount = DISCOUNT_RATE * costOfService(); //applies discount if number of serivce < 10
    		  else if (numberOfService <= FULL_COST_LIMIT)
    	        {
    	            discount = 0.0;
    	        }//discount is 0 if number of service < 5
    		  else
    	        { 
    	            discount = costOfService();
    	        } //end if
    	        return discount;
    	    } //end discountAmount
     
    	public String typeOfVehicleAsString()
    	{
    		return "The Works";
        } // end typeOfService
     
     
     
     
    }
    --

    this project involves using inheritance and polymorphism and i know i did alot of things wrong

    for one this is our 3rd project, but uses the same question from project 2 and project 1 but by doing it in different ways, the project 2 was using classes and objects i think, this one i have to use polymorphism

    anyways i copied and pasted his MAIN of PROJECT 2 into PROJECT 3, i checked the prompt and it is pretty correct i blieve

    however in the line of the main service.setsuv(responsesuv) it says i have an error because its changing it to a CHAR


    also the discount and basic rate come out as 0, i can run the project when i add the parameter Char responsesuv to the superclass but i dont think that is the correct way
    Last edited by helloworld922; May 7th, 2010 at 09:57 PM.


Similar Threads

  1. my menu doesnt work can u tell me whats wrong
    By claymore in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 8th, 2010, 04:16 AM
  2. JFrame, frame's title doesnt appear.
    By chronoz13 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 26th, 2009, 03:38 PM
  3. Question on my math
    By SwEeTAcTioN in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 25th, 2009, 05:42 PM
  4. math quiz program
    By hope.knykcah in forum Collections and Generics
    Replies: 1
    Last Post: October 23rd, 2009, 09:53 AM
  5. Math in Java?
    By [Kyle] in forum Java Theory & Questions
    Replies: 3
    Last Post: September 23rd, 2009, 12:21 PM