Results 1 to 4 of 4
  1. #1
    d2idan is offline Member
    Join Date
    Oct 2014
    Posts
    9
    Rep Power
    0

    Default help with program that calculates parking fee

    i succeed but with limit. the code is below.

    I cant use advanced material (really just the basics even without loops)
    if statements are allowed, and i must use Final veriables for key value.
    i must use one class and one method (the main )
    I need to prompt the user for a entrance time and exit time
    it must be with a space , like 18 00 and not 18:00
    also, if the entrance time is greater or equal to 18 00 the charge is always 20 dollars.
    if the user enters wrong data or out of range data an output of "ERROR WRONG DATA"
    and then the programs terminates.
    same for wrong time . like if the enter hour is bigger than the exit hour
    an output of "ERROR entrance time must preccede exit"
    for the first hour the output is "no charge",
    for the second hour the charge is 10 dollars.
    from the 3 hour for every 15 minutes an additional fee of 3 dollars.
    for example if the user entered 10 00 and 13 20 , it should be 52 dollars
    ** it works for me but if the difference between the entered and exit hours is more then 3 it doesnt work.
    i could've added another if statements for the bigger hours but it will be very long.

    i managed everything. except the last part.
    that calculates adiitional 3 dollar for each 15 minutes. what is did is.
    converted everything to minutes.
    subtract enter time from exit time.
    and devide by 15.
    thats give me the number of 15 minute periods.
    if it last or equal to 4. no charge
    else last or equal to 8 . 10 dollars
    else...... here is the hard part.
    if the totall minutes wont divide even by 15 it wont work.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: help with program that calculates parking fee

    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    9

    Default Re: help with program that calculates parking fee

    Hello d2idan, welcome.

    For completeness, here is the code posted on the other site.

    Java Code:
    import java.util.Scanner;
     
    public class Parking
    {
      public static void main(String[] args)
      {  
       final int MAX_HOUR = 24;   //no more than 24 hours to enter 
       final int MAX_MIN = 59;   //no more than 59 minutes to enter
       final int HOUR_MIN = 01;  //no less then 1 hour to enter
       final int QUARTER = 15;  //4 quarters of a clock
       final int HALF = 30;
       final int QUARTER_THREE = 45;
       final int QUARTER_FOUR = 59;
       final int SIX_PM = 18;      //for the 20 NIS charge start from 18 00
       final int SIX_CHARGE = 20;
       final int QUARTER_CHARGE = 3;   //charge for each additional 15 minutes
       final int FIRST_CHARGE = 10;    //charge for the second hour
       int hourEnter, hourExit, minEnter, minExit; //user input veriables
       int amount = 0;
       Scanner input = new Scanner(System.in);
       System.out.print("Enter entrance time: ");
       hourEnter = input.nextInt();
       minEnter = input.nextInt();
       System.out.print("Enter exit time: ");
       hourExit = input.nextInt();
       minExit = input.nextInt();
       if((hourEnter > MAX_HOUR||hourExit > MAX_HOUR)||(minEnter > MAX_MIN||minExit > MAX_MIN)
        ||(hourEnter< HOUR_MIN ||hourExit < HOUR_MIN)) {
         System.out.println("Error! Illegal data.");
       }else if(hourEnter>hourExit){
         System.out.println("Error! Entrance time must preccede exit time.");
       }else if(hourEnter >= SIX_PM){
         System.out.println("Totall charge is " + SIX_CHARGE + " NIS.");
       }else if(hourExit-hourEnter == 1 && minExit < minEnter){
         System.out.println("No charge.");
       }else if(hourExit == hourEnter && minExit<=MAX_MIN){
         System.out.println("No charge.");
       }else if(hourExit-hourEnter==1 && minExit >= minEnter){
         System.out.println("Charge is " + FIRST_CHARGE + " NIS.");
       }else if(hourExit-hourEnter == 2 && minExit < minEnter){
         System.out.println("Charge is " + FIRST_CHARGE + " NIS.");
       }else if((hourExit-hourEnter >= 2) && (minExit-minEnter >=0 && minExit-minEnter<=QUARTER)){
         System.out.println("Charge is " + (FIRST_CHARGE + QUARTER_CHARGE) +" NIS.");
       }else if((hourExit-hourEnter >= 2) && (minExit-minEnter >QUARTER && minExit-minEnter<=HALF)){
         System.out.println("Charge is " + (FIRST_CHARGE + (QUARTER_CHARGE * 2)) +" NIS.");
       }else if((hourExit-hourEnter >= 2) && (minExit-minEnter > HALF && minExit-minEnter<= QUARTER_THREE)){
         System.out.println("Charge is " + (FIRST_CHARGE + (QUARTER_CHARGE * 3)) +" NIS.");
       }else if((hourExit-hourEnter >= 2) && (minExit-minEnter > QUARTER_THREE && minExit-minEnter<= QUARTER_FOUR)){
         System.out.println("Charge is " + (FIRST_CHARGE + (QUARTER_CHARGE * 4)) +" NIS.");
       }
     
      }
     
    }
    I started to read the code but my head started to hurt. There are only so many times I could read the line
    Java Code:
    else if((hourExit-hourEnter >= 2)
    Seriously, if the continuous check for this condition is necessary then its much simpler to nest the if statements. On the other hand if all other possible conditions have already been checked (i.e. Difference is equal to or less than 2) then this check become mute and an unnecessary use of processing time.

    Splitting up the quarters is also completely unnecessary. This is something which could be quickly calculated by using the modus of the minutes.

    Not to disrespect your attempt at coding (We all started somewhere), but I would recommend going back to the drawing board on this. You will appreciate the chance to do this again and hopefully gain an understanding of how to generalize the problem in such a way you do not micromanage every conceivable condition.

    We would be happy to provide pointers for any further attempt.

    Regards.
    Last edited by Ronin; 10-23-2014 at 05:49 PM. Reason: Typo

  4. #4
    d2idan is offline Member
    Join Date
    Oct 2014
    Posts
    9
    Rep Power
    0

    Default Re: help with program that calculates parking fee

    Quote Originally Posted by Ronin View Post
    Hello d2idan, welcome.

    For completeness, here is the code posted on the other site.

    Java Code:
    import java.util.Scanner;
     
    public class Parking
    {
      public static void main(String[] args)
      {  
       final int MAX_HOUR = 24;   //no more than 24 hours to enter 
       final int MAX_MIN = 59;   //no more than 59 minutes to enter
       final int HOUR_MIN = 01;  //no less then 1 hour to enter
       final int QUARTER = 15;  //4 quarters of a clock
       final int HALF = 30;
       final int QUARTER_THREE = 45;
       final int QUARTER_FOUR = 59;
       final int SIX_PM = 18;      //for the 20 NIS charge start from 18 00
       final int SIX_CHARGE = 20;
       final int QUARTER_CHARGE = 3;   //charge for each additional 15 minutes
       final int FIRST_CHARGE = 10;    //charge for the second hour
       int hourEnter, hourExit, minEnter, minExit; //user input veriables
       int amount = 0;
       Scanner input = new Scanner(System.in);
       System.out.print("Enter entrance time: ");
       hourEnter = input.nextInt();
       minEnter = input.nextInt();
       System.out.print("Enter exit time: ");
       hourExit = input.nextInt();
       minExit = input.nextInt();
       if((hourEnter > MAX_HOUR||hourExit > MAX_HOUR)||(minEnter > MAX_MIN||minExit > MAX_MIN)
        ||(hourEnter< HOUR_MIN ||hourExit < HOUR_MIN)) {
         System.out.println("Error! Illegal data.");
       }else if(hourEnter>hourExit){
         System.out.println("Error! Entrance time must preccede exit time.");
       }else if(hourEnter >= SIX_PM){
         System.out.println("Totall charge is " + SIX_CHARGE + " NIS.");
       }else if(hourExit-hourEnter == 1 && minExit < minEnter){
         System.out.println("No charge.");
       }else if(hourExit == hourEnter && minExit<=MAX_MIN){
         System.out.println("No charge.");
       }else if(hourExit-hourEnter==1 && minExit >= minEnter){
         System.out.println("Charge is " + FIRST_CHARGE + " NIS.");
       }else if(hourExit-hourEnter == 2 && minExit < minEnter){
         System.out.println("Charge is " + FIRST_CHARGE + " NIS.");
       }else if((hourExit-hourEnter >= 2) && (minExit-minEnter >=0 && minExit-minEnter<=QUARTER)){
         System.out.println("Charge is " + (FIRST_CHARGE + QUARTER_CHARGE) +" NIS.");
       }else if((hourExit-hourEnter >= 2) && (minExit-minEnter >QUARTER && minExit-minEnter<=HALF)){
         System.out.println("Charge is " + (FIRST_CHARGE + (QUARTER_CHARGE * 2)) +" NIS.");
       }else if((hourExit-hourEnter >= 2) && (minExit-minEnter > HALF && minExit-minEnter<= QUARTER_THREE)){
         System.out.println("Charge is " + (FIRST_CHARGE + (QUARTER_CHARGE * 3)) +" NIS.");
       }else if((hourExit-hourEnter >= 2) && (minExit-minEnter > QUARTER_THREE && minExit-minEnter<= QUARTER_FOUR)){
         System.out.println("Charge is " + (FIRST_CHARGE + (QUARTER_CHARGE * 4)) +" NIS.");
       }
     
      }
     
    }
    I started to read the code but my head started to hurt. There are only so many times I could read the line
    Java Code:
    else if((hourExit-hourEnter >= 2)
    Seriously, if the continuous check for this condition is necessary then its much simpler to next the if statements. On the other hand if all other possible conditions have already been checked (i.e. Difference is equal to or less than 2) then this check become mute and an unnecessary use of processing time.

    Splitting up the quarters is also completely unnecessary. This is something which could be quickly calculated by using the modus of the minutes.

    Not to disrespect your attempt at coding (We all started somewhere), but I would recommend going back to the drawing board on this. You will appreciate the chance to do this again and hopefully gain an understanding of how to generalize the problem in such a way you do not micromanage every conceivable condition.

    We would be happy to provide pointers for any further attempt.

    Regards.
    yep. youre right.
    ok.
    thanks

Similar Threads

  1. parking code
    By disel in forum New To Java
    Replies: 26
    Last Post: 05-17-2014, 09:47 AM
  2. Replies: 5
    Last Post: 09-05-2013, 01:13 AM
  3. Replies: 0
    Last Post: 05-16-2013, 07:25 PM
  4. Replies: 14
    Last Post: 11-06-2012, 09:56 AM
  5. a parking garage program
    By jihad in forum New To Java
    Replies: 3
    Last Post: 12-24-2011, 02:06 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
  •