Java Program Write a program for a car rental business that
Java Program
Write a program for a car rental business that accepts as input a renter name and number of miles driven and calculates the car rental bill. Everyone is charged a rental fee of $65 and a mileage charge after the first 25 miles driven of 55 cents per mile. Use a posttest loop that asks the user if they have more data to enter. Print each renter\'s miles and bill in table format, then after all data is entered, print the average miles and bill. Make sure your output table does not have input lines between the detail lines. Test your program with the following data:
Name Miles Driven
Pam Smith 103
Bob Jones 21
Jane Fuller 67
Tom Albert 15
It runs but it keeps giving me only the last name that I put in and I can not figure out why?
import java.util.*;
public class carRental
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
final double RENTAL_FEE = 65.00;
final double MILES_OVER = 55; //this is for miles over 25
//initialize variables
double avgMilesDriven = 0;
double avgRentalFee = 0;
int count = 0;
int miles;
double rentalBill = 0;
String name, trash;
char choice;
do
{
System.out.print(\"Enter the Name of the renter : \");
name = console.nextLine();
System.out.println();
System.out.print(\"Enter number of miles travelled: \");
miles = console.nextInt();
System.out.println();
if (miles < 25)
rentalBill = 65.00;
else if (miles >= 25)
rentalBill = (RENTAL_FEE + (miles * MILES_OVER));
count++;
System.out.print(\"Do you want to continue (y for yes or n for no)?: \");
choice = console.next().charAt(0);
System.out.println();
trash = console.nextLine();
}while(choice ==\'y\'||choice ==\'Y\');
//to display output in table format
System.out.println(\"Name \" + \" Miles Driven \" + \" Rental Fee \");
System.out.println(\"___________________________________________________\");
for(int i = 0;i < count; i++)
{
System.out.printf(\"%-10s %10d %10.2f\", name, miles, rentalBill);
System.out.println();
avgMilesDriven = avgMilesDriven + (miles / count);
avgRentalFee = avgRentalFee + (rentalBill / count);
}
System.out.println(\"The average mile driven is: \" + avgMilesDriven);
System.out.println();
System.out.println(\"The average rental fee is: \" + avgRentalFee);
System.out.println();
}
}
Output
Enter the Name of the renter : Pam Smith
Enter number of miles travelled: 103
Do you want to continue (y for yes or n for no)?: y
Enter the Name of the renter : Bob Jones
Enter number of miles travelled: 21
Do you want to continue (y for yes or n for no)?: y
Enter the Name of the renter : Jane Fuller
Enter number of miles travelled: 67
Do you want to continue (y for yes or n for no)?: n
Name Miles Driven Rental Fee
___________________________________________________
Jane Fuller 67 3750.00
Jane Fuller 67 3750.00
Jane Fuller 67 3750.00
The average mile driven is: 66.0
The average rental fee is: 3750.0
Solution
You were not taking into account all the entries. On each iteration, you were just overwriting name, miles driven and rental fee.
You need to store these variables for all entries. Since, the size of the array is not known in advanced, it is a good idea to create an arraylist to store these values.
Hence, I created arraylists for each of these variables and stored corresponding value for each entry in them.
Also as a side note, it is a convention to write class name starting with capital letter.
Code
import java.util.*;
public class CarRental
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
final double RENTAL_FEE = 65.00;
final double MILES_OVER = 55; //this is for miles over 25
//initialize variables
double avgMilesDriven = 0;
double avgRentalFee = 0;
int count = 0;
int mile;
double rentalBill = 0;
ArrayList<Integer> miles = new ArrayList<Integer>();
ArrayList<Double> rentalBills = new ArrayList<Double>();
ArrayList<String> names = new ArrayList<String>();
String name, trash;
char choice;
do{
System.out.print(\"Enter the Name of the renter : \");
name = console.nextLine();
names.add(name);
System.out.println();
System.out.print(\"Enter number of miles travelled: \");
mile = console.nextInt();
miles.add(mile);
System.out.println();
if (mile<25){
rentalBill = 65.00;
}
else if (mile >= 25){
rentalBill = (RENTAL_FEE + (mile * MILES_OVER));
}
count++;
rentalBills.add(rentalBill);
System.out.print(\"Do you want to continue (y for yes or n for no)?: \");
choice = console.next().charAt(0);
System.out.println();
trash = console.nextLine();
}while(choice ==\'y\'||choice ==\'Y\');
//to display output in table format
System.out.println(\"Name \" + \" Miles Driven \" + \" Rental Fee \");
System.out.println(\"___________________________________________________\");
for(int i = 0;i < count; i++)
{
System.out.printf(\"%-10s %10d %10.2f\", names.get(i), miles.get(i), rentalBills.get(i));
System.out.println();
avgMilesDriven = avgMilesDriven + (miles.get(i)*1.0 / count);
avgRentalFee = avgRentalFee + (rentalBills.get(i)*1.0/ count);
}
System.out.println(\"The average mile driven is: \" + avgMilesDriven);
System.out.println();
System.out.println(\"The average rental fee is: \" + avgRentalFee);
System.out.println();
}
}



