Hello guys well I have been working in this arraylist that is now driving me crazy!
Bascially I wrote a CAR class which has some methods in it, however now I created an arraylist so it could store cars from the other class and at the same time have access to some of the methods of that class such as rentTheCar or returnCar. I am having trouble with three things.
First showCar is just now working properly it shows me all the cars although I put the carNumber when I invoke the method, moreover I want the method to print the description of that particular car.
The rentTheCar method is renting the car, well Basically renting all the cars at the same time instead of renting the car with carNumber specified when invoking the method.
The return car method does exactly the exact same as the rentTheCar method but in this case it returns them all insetad of just returning the car specified when invoking the method.
Here I leave both classes:
public class Car
{
// This represents the car's features entered by the user.
private String description;
// This represents the name of the user or the person that will rent the car.
private String name;
// This represents the days that the car will be rented for.
private int days;
// This represents the down payment made bny the user to rent the car.
private int deposit;
// This represents the daily rate of rental for the car.
private int rate;
// This represents the start date of the rental.
private String dateOfHire;
// This represents the date that the car will be returned.
private String dateOfReturn;
// This represents the total amount of money that will be paid until the end of the rental.
private int totalRent;
// This represents the status of the car, that can be rented or not rented.
private boolean onLoan;
/**
* Stablish a car with first features such as description and rate.
*/
public Car(String carDescription, int dailyRate, int downPayment)
{
description = carDescription;
rate = dailyRate;
days = 0;
name = "";
dateOfHire = "";
dateOfReturn = "";
deposit = downPayment;
totalRent = 0;
onLoan = false;
}
/**
* This is a method that returns the car's description
*/
public String getDescription()
{
return description;
}
/**
* This is a method that displays on screen the rental review.
* The rental review consists of the description, customer's name, start date,
* return date, period of time, down payment, amount due, daily rate.
*/
public void displayDescription()
{
System.out.println(" Rental Review ");
System.out.println("The car will be a " + description);
System.out.println("The daily rate is £" + rate);
System.out.println("It will be rented to " + name);
System.out.println("From "+ dateOfHire);
System.out.println("To " + dateOfReturn);
System.out.println("Hired for " + days + " days");
System.out.println("The Down Payment is £ " + deposit);
System.out.println("The amount to be paid at the end of the rental is £ " + (rate*days));
System.out.println("The total payment is £ " + (deposit + (rate*days)));
}
/**
* This is a method that returns the daily rate of the specific car.
*/
public int getDailyRate()
{
return rate;
}
/**
* This is a method that sets a new daily rate for the car if required.
*/
public int newDailyRate(int newRate)
{
rate = newRate;
return rate;
}
/**
* This represents the simple action of renting a car and retrieving some information
* such us the customer's name, start date, return date, total days of rental and the downpayment.
*/
public void rentTheCar(String CustomerName, String hireDate, String returnDate, int daysOfHire)
{
if (onLoan == true) {
System.out.println("Error!! This car is already rented. It wil be available the " + dateOfReturn);
}
else {
name = CustomerName;
dateOfHire = hireDate;
dateOfReturn = returnDate;
days = daysOfHire;
onLoan = true;
totalRent = totalRent + ((rate * days) + deposit);
}
}
/**
* This is a method that returns the customers full name.
*/
public String getCustomerName()
{
return name;
}
/**
* This is a method that returns the start date of the rental.
*/
public String getHireDate()
{
return dateOfHire;
}
/**
* This is a method that returns the car's date of return.
*/
public String getReturnDate()
{
return dateOfReturn;
}
/**
* This is a method that returns the total days that the car will be rented for.
*/
public int getDaysOfHire()
{
return days;
}
/**
* This is a method that returns the down payment that the customer will need to
* pay, to rent the car.
*/
public int getDownPayment()
{
return deposit;
}
/**
* This is a method that sets a new down payment if required.
*/
public int newDownPayment(int newDeposit)
{
deposit = newDeposit;
return deposit;
}
/**
* This represents the simple action of returning the car and reseting some information and values
* such us the customer's name, start date, return date, total days of rental and the downpayment.
*/
public void returnTheCar()
{
if (onLoan == true) {
name = "";
dateOfHire = "";
dateOfReturn = "";
days = 0;
totalRent = 0;
onLoan = false;
}
else {
System.out.println("Error!! This car is not rented! ");
}
}
}
and now the Rental arraylist
import java.util.ArrayList;
public class rentalCompany
{
private ArrayList<Car>cars;
private int i;
/**
* Constructor for objects of class RentalCompany
*/
public rentalCompany ()
{
cars = new ArrayList<Car>();
}
/**
* Constructor for objects of class RentalCompany
*/
public void createCar(String carDescription, int downPayment, int dailyRate){
cars.add( new Car (carDescription, downPayment, dailyRate));
}
public void removeCar (int carNumber)
{
if (cars.size() < 0){
System.out.println ("Error!! It is impossible to remove a car from an empty list");
}
if (cars.size() == cars.size()) {
cars.remove(carNumber);
}
}
*/
public void returnCar (int carNumber)
{
cars.get(carNumber);
if (cars.size() < 0) {
System.out.println("Error!! It is impossible to return a car from an empty list");
}
else if (carNumber > cars.size()) {
System.out.println("Error!! Not a valis car!!");
}
else {
for (Car car : cars) {
car.returnTheCar();
System.out.println ("The car has been returned correctly");
}
}
}
*/
public void rentTheCar (int carNumber, String CustomerName, String hireDate, String returnDate, int daysOfHire)
{
if (cars.size() < 0) {
System.out.println("Empty list!! Add cars first!!");
}
else if (carNumber > cars.size()) {
System.out.println("This car does not exist!!");
}
else {
cars.get (carNumber);
for (Car car : cars) {
car.rentTheCar(CustomerName, hireDate, returnDate, daysOfHire);
}
}
}
/**
* Constructor for objects of class RentalCompany
*/
public int numberOfCars()
{
return cars.size();
}
/**
* Constructor for objects of class RentalCompany
*/
public void showAllCars ()
{
for ( Car car:cars) {
if (cars.size()>0) {
int i = 0;
System.out.println(car.getDescription());
i++;
}
else if ( cars.size() < 0 ){
System.out.println ("Add cars first");
}
}
}
/**
* Constructor for objects of class RentalCompany
*/
public void showCar (int carNumber)
{
}
public Car searchCar(String description, String parameter2)
{
for (Car car : cars) {
if (car.getDescription() == description) {
return car;
}
else {
System.out.println("This car is not listed. Retry!!");
}
}
return null;
}
public void clearScreen()
{
System.out.print('\u000C');
}
}
Thanks for all your help!!