Search code examples
javaloopsif-statementsummarysentinel

Write a Basic Car rental program on java / problems


So I'm super confused and just looking for help :L. This is what the instructions from my instructor were.

Instructions : Use a sentinel value loop.

Ask each user for:

  • Type of vehicle (May use something other than strings, such as: 1 for economy, 2 for sedan, etc.)
  • Days rented

Calculate the (For each customer):

  • Rental cost,
  • Taxes,
  • Total Due.

There are three different rental options with seprate rates: Economy @ 31.76, sedan @ 40.32, SUV @ 47.56. [Note: only whole day units to be considered (no hourly rates)].

Sales tax is = to 6% on the TOTAL.

Create summary data with:

  • Number of customers
  • Total money collected.

Also, Include IPO, algorithm, and desk check values (design documents).

{WHAT I HAVE GOING AND MY QUESTION}

package yipe;

public class Umm {

    import java.util.*;

    int count = 0;
    static int CarType, days;
    static double DailyFee, Total;


    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);

        System.out.print("What vehical would you like to rent?\n");
        System.out.println("Enter 1 for an economy car\n");
        System.out.println("Enter 2 for a sedan car\n");
        System.out.println("Enter 3 for an SUV");
        CarType = keyboard.nextInt();
        if (CarType == '1')
              DailyFee=(int)31.76;
            else if(CarType == '2')
              DailyFee=(int)40.32;
            else if(CarType == '3')
              DailyFee=(int)43.50;

        System.out.print("Please enter the number of days rented. (Example; 3) : ");
        days = keyboard.nextInt();

        Total = (DailyFee * days * 6/100);

        System.out.printf("The total amount due is $" + Total);

    }


}
  1. How can I fix my IF statement(s) to get proper math results?
  2. how would I make it loop for to put in multiple information??
  3. how to make summary data?
  4. how can I round the Total to only two decimals?

Solution

  • Note that '1' is actually the character 1, not the integer 1. They're actually very different.

    In Java (as well as C#), int and char types are convertible to each other.

    To illustrate, the following actually prints 49:

    public class HelloWorld
    {
      public static void main(String[] args)
      {
        System.out.print((int)'1');
      }
    }
    

    Similarly, the following prints true:

    System.out.println('1' == 49);
    

    As you can see, the character is implicitly being converted to an equivalent int value.

    To understand why '1' is equal to 49 in particular, look at how characters are represented. In particular, look at the ASCII chart (which is a common convention for character encoding) - it turns out that the character '1' is ASCII 49. Indeed, we can do the same thing we did above "in reverse" to "convert" ASCII 49 to its equivalent character, and the following line prints 1:

    System.out.println((char)49);
    

    To understand how this kind of conversion works, you may want to read this rather excellent article linked to in the comments. If you're curious about how this works in C#, you may also want to read this question.

    One more point: when you do DailyFee=(int)31.76, converting this to an int will actually "drop" everything after the decimal point, so this is no different than writing DailyFee = 31. This is because 31 is an integer whereas 31.76 is not (it's a rational number).

    One minor stylistic point: you might consider using a switch statement here.