hi guys

I am practicing a java program and it goes like this.

I am trying to this as a practice JAVA Object-Oriented program for a rental car company to create an inventory file(.txt) as a random access file which can easily be updated by users for the following specifications:

The file record consists of a car name, id, existing mileage, gas cost, numofdays, rate, total charge, discount, tax, net charge and return mileage.

The program should be able to change the total charge and the existing mileage field of the records. Use the methods seek() and getFilePointer(). If the mileage is 30,000 or more; output a message "needs service". If the mileage is 65,000 or more display a message "marked for sale". Enter at least 5 records for different cars. Test your program with some data.
the problem that I am experiencing with the program is. everything that it prompt for is working just fine but it is not reading the if statement
the if statement goes like this:
if(mileage > 30,000)
system.out.println(car need service);
else if (mileage > 65,000)
system.out.println(car for sale) as it said in the question but the program only read up until the last condition. I am also to a for loop or is it possible a while loop can be done anyway here is the code

        int car_idnum, retmile, numdays, exmile;
        double costofgas, rate, mileage = 0, totalchar, discount, netchar, tax;
        String carname;

        for(int i =1; i<=2; i++)
        {
        System.out.println("please enter car id number");
        car_idnum = scan.nextInt();

        System.out.println("please enter the name of car");
        carname = scan.next();

        System.out.println("please enter the cost of gas");
        costofgas = scan.nextDouble();

        System.out.println("please enter the rate");
        rate = scan.nextDouble();

        System.out.println("please enter the net charge");
        netchar = scan.nextDouble();

        System.out.println("please enter existing mileage");
        exmile = scan.nextInt();

        System.out.println("please enter return mileage");
        retmile = scan.nextInt();

        if(mileage > 30000)

            System.out.println("car needs servicing");

        else if(mileage > 65000)

            System.out.println("car for sale");

        }
    }
 }  

tell me what I am missing here and also can a while loop can be use
the code stop up to the last condition which is line 21 it doesnt read the rest of the statement (as in the if statement)

Recommended Answers

All 5 Replies

You input two mileage figures (exmile, retmile), but your if tests use mileage, which you never set.

Typically you would have a do....while loop that keeps processing user input until the user enters a "stop" value (eg a car ID number of <= 0), or you ask if there's more input and he answers "no".

There's nothing Object Oriented about your code. An OO solution would have at least a Car class.

is this the way to set it

public exmile()
int exmile = 3000;

public Car rental()
int retmile = 65000;

also, a remark:

if(mileage > 30000)
            System.out.println("car needs servicing");
        else if(mileage > 65000)
            System.out.println("car for sale");
        }

you can't use this system. to explain: for which value of mileage do you think it will not be over 30.000, but will be over 65.000?

you'll need to add a second condition in your if.

if(mileage > 30000 && mileage <= 65000)
            System.out.println("car needs servicing");
        else if(mileage > 65000)
            System.out.println("car for sale");
        }

The problem isn't how you set exmile and retmile.
The problem is that you do the if tests with a third variable called mileage, but you don't set that variable anywhere. Just think: which mileage figure should you be using for the if tests?

thank guys will try dah one now get ah glimse to what james is saying

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.