Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I wrote a simple program its Car rental program

when i run the code its print word (null) many time

Java
<pre>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package carsrental;

import java.util.Scanner;
import java.util.ArrayList;

/**
 *
 * @author رغد
 */
public class CarsRental {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        int choice;
        int color;
        int pay;
        String year, first, last, phone;
        double duration;
        Scanner input = new Scanner(System.in);
        Car c1 = new Car();

        prsonalInformation p1 = new prsonalInformation();
        Money m1 = new Money();

        System.out.println("---------------------------------------");
        System.out.println("----------Welcome to ray cars----------");
        System.out.println("---------------------------------------");

        System.out.println("The cars:");
        System.out.println("1.Mercedes");
        System.out.println("2.Mazda");
        System.out.println("Enter your choice: ");
        choice = input.nextInt();

        if (choice == 1) {
            c1.setMake("Mercedes");
            System.out.println(">>The price of one day: " + c1.getPrice1());
        } else if (choice == 2) {
            c1.setMake("Mazda");
            System.out.println(">>The price of one day: " + c1.getPrice2());
        } else {
            while (choice > 2) {
                System.out.println("That number is invalid.");
                System.out.println("Please Enter again a choice 1 or 2");
                choice = input.nextInt();
            }
        }

        System.out.println("The color of car:");
        System.out.println("1.Black");
        System.out.println("2.blue");
        System.out.println("Enter your choice: ");
        color = input.nextInt();

        if (color == 1) {
            c1.setColor("Black");
        } else if (color == 2) {
            c1.setColor("blue");
        }
        input.nextLine();
        System.out.println("Enter the year modal of cars: ");
        year = input.nextLine();
        c1.setYearMOdal(year);

        System.out.println("Enter your first name: ");
        first = input.nextLine();
        p1.setFirstName(first);
        System.out.println("Enter your last name: ");
        last = input.nextLine();
        p1.setLastName(last);
        System.out.println("Enter your phone number: ");
        phone = input.nextLine();
        p1.setPhoneNumber(phone);

        System.out.println("The way of payment: ");
        System.out.println("1.Apple pay ");
        System.out.println("2.Mastercard");
        System.out.println("Enter your choice: ");
        pay = input.nextInt();

        if (pay == 1) {
            m1.setPayment("Apple pay ");

        } else if (pay == 2) {
            m1.setPayment("Mastercard");
        }

        if (choice == 1) {
            c1.getPrice1();

        } else if (choice == 2) {
            c1.getPrice2();
        }

        System.out.println("How many days you want?");
        duration = input.nextDouble();

        Renting r1 = new Renting(duration);
        Renting r2 = new Renting(p1);
        Renting r3 = new Renting(c1);
        Renting r4 = new Renting(m1);

        r1.setDuration(duration);
        if (choice == 1) {
            m1.setBalance(c1.getPrice1() * duration);

        } else if (choice == 2) {
            m1.setBalance(c1.getPrice2() * duration);

        }
        ArrayList<Renting> list = new ArrayList<Renting>();
        list.add(r1);
        list.add(r2);
        list.add(r3);
        list.add(r4);
        System.out.println("-----Your bill-----");
        for (Renting r : list) {

            System.out.println(r.getPerson());
           System.out.println(r.getcar());
            System.out.println(r.getmo());

        }
        

        System.out.println("-----Thank you!!-----");

    }

}



output :
run:
---------------------------------------
----------Welcome to ray cars----------
---------------------------------------
The cars:
1.Mercedes
2.Mazda
Enter your choice: 
2
>>The price of one day: 250
The color of car:
1.Black
2.blue
Enter your choice: 
1
Enter the year modal of cars: 
2020
Enter your first name: 
nawaf
Enter your last name: 
alkhadidi
Enter your phone number: 
05555555555
The way of payment: 
1.Apple pay 
2.Mastercard
Enter your choice: 
1
How many days you want?
3
-----Your bill-----
null
null
null
 >>The pesonal information The fisrt name is: nawaf
  The last name is: alkhadidi
  The phone number: 05555555555
null
null
null
 >>The car information 
  The company make is: Mazda
  The Year Modal is: 2020
  The color is: Black
null
null
null
 >>The payment information 
  The way of payment: Apple pay 
  You have to payment: 750.0
-----Thank you!!-----
BUILD SUCCESSFUL (total time: 29 seconds)





What I have tried:

i dont know what i do

its hard to me
Posted
Updated 8-Apr-21 10:43am

1 solution

Your create four Renting objects (r1, r2, r3, and r4) but you only set any detains for one of them: r1.

So when you print the results, you get what you input: mostly nulls.
 
Share this answer
 
Comments
Member 15142485 8-Apr-21 16:59pm    
how i can solve it?
Richard Deeming 9-Apr-21 4:41am    
Change your code to populate the details of the other three objects.

Or change your code to only print the details of the one object you have populated.
OriginalGriff 9-Apr-21 4:55am    
Where did my reply last night go?
Strange ...

I was kinda hoping he would work that out for himself, but I guess it's a bit complicated for him. :sigh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900