Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions MovieRental/Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import java.util.ArrayList;
import java.util.List;

public class Customer {
private String name;
private String email;
private String phone_number;
private String address;
private int ID;
private List<Rental> rentals;


public Customer(String name, String email, String phone_number, String address, int ID) {
this.name= name;
this.email = email;
this.phone_number = phone_number;
this.address = address;
this.ID = ID;
this.rentals = new ArrayList<>();
}

public int getID() {
return ID;
}

public String getName() {
return name;
}

public String getEmail() {
return email;
}

public String getPhone_number() {
return phone_number;
}

public String getAddress() {
return address;
}

public List<Rental> getRentals() {
return rentals;
}

public void setID(int ID) {
this.ID = ID;
}

public void setAddress(String address) {
this.address = address;
}

public void setEmail(String email) {
this.email = email;
}

public void setName(String name) {
this.name = name;
}

public void setPhone_number(String phone_number) {
this.phone_number = phone_number;
}

public void setRentals(List<Rental> rentals) {
this.rentals = rentals;
}
}
87 changes: 87 additions & 0 deletions MovieRental/Movie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Movie {
private String title;
private String genre;
private Date releaseDate;
private int ID;
private boolean isAvailable;

public Movie(String title, String genre, Date releaseDate, int ID) {
this.ID = ID;
this.genre = genre;
this.releaseDate = releaseDate;
this.title = title;
}

public int getID() {
return ID;
}

public Date getReleaseDate() {
return releaseDate;
}

public String getGenre() {
return genre;
}

public String getTitle() {
return title;
}

public boolean isAvailable() {
return isAvailable;
}

public void setAvailable(boolean available) {
isAvailable = available;
}

public void setGenre(String genre) {
this.genre = genre;
}

public void setID(int ID) {
this.ID = ID;
}

public void setReleaseDate(Date releaseDate) {
this.releaseDate = releaseDate;
}

public void setTitle(String title) {
this.title = title;
}

public void rentMovie(Customer customer) {
this.setAvailable(false);

Rental rental = new Rental(customer,this, Integer.parseInt(this.getID() + String.valueOf(customer.getID())));

try {
customer.getRentals().add(rental);
} catch (NullPointerException e) {
List<Rental> rentals = new ArrayList<>();
rentals.add(rental);
customer.setRentals(rentals);
}

System.out.println("Item named " + this.getTitle() + " rented");
}

public void returnMovie(Rental rental) {
this.setAvailable(true);

try {
rental.getCustomer().getRentals().remove(rental);
System.out.println("Item named " + this.getTitle() + " returned");
System.out.println("Your lateFee: " + rental.calculateLateFee() + 'T');
} catch (NullPointerException e) {
System.out.println("No rentals");
}

}
}
57 changes: 57 additions & 0 deletions MovieRental/Rental.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class Rental {
private final Customer customer;
private final Movie movie;
private final Date rentalDate;
private Date returnDate;
private final int ID;

public Rental(Customer customer, Movie movie, int ID) {
this.customer = customer;
this.movie = movie;
this.ID = ID;
this.rentalDate = new Date();
// Adding 7 days to rentalDate to create return date
this.returnDate = new Date(rentalDate.getTime() + TimeUnit.DAYS.toMillis(7));
}

public int getID() {
return ID;
}

public Movie getMovie() {
return movie;
}

public Customer getCustomer() {
return customer;
}

public Date getRentalDate() {
return rentalDate;
}

public void setReturnDate(Date returnDate) {
this.returnDate = returnDate;
}

public Date getReturnDate() {
return returnDate;
}

public double calculateLateFee() {
int lateFee = 10000;
Date date = new Date();

long diffInMillie = date.getTime() - this.returnDate.getTime();
long diff = TimeUnit.DAYS.convert(diffInMillie, TimeUnit.MILLISECONDS);

if(diff<=0) {
return 0;
}

return lateFee * diff;
}
}
165 changes: 165 additions & 0 deletions MovieRental/RentalStore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import java.util.ArrayList;
import java.util.List;

public class RentalStore {
private List<Customer> customers;
private List<Movie> movies;
private List<Movie> availableMovies;

public RentalStore() {
this.customers = new ArrayList<>();
this.movies = new ArrayList<>();
this.availableMovies = new ArrayList<>();
}

public void register(Customer customer) {
this.customers.add(customer);
System.out.println("Registered successfully");
}
public void addMovie(Movie movie) {
this.movies.add(movie);
this.availableMovies.add(movie);
System.out.println("Added successfully");
}
public void removeMovie(Movie movie) {
boolean check = false;
for (Movie movie_temp:movies) {
if (movie_temp == movie) {
check = true;
if (!this.availableMovies.contains(movie)) {
System.out.println("This movie can't be removed because it's rented");
}
else {
this.movies.remove(movie);
this.availableMovies.remove(movie);
System.out.println("Removed successfully");
}
break;
}
}
if (!check) {
System.out.println("Movie Not found!");
}
}

public List<Movie> getAvailableMovies() {
return availableMovies;
}

public List<Customer> getCustomers() {
return customers;
}

public List<Movie> getMovies() {
return movies;
}

public void setCustomers(List<Customer> customers) {
this.customers = customers;
}

public void setAvailableMovies(List<Movie> availableMovies) {
this.availableMovies = availableMovies;
}

public void setMovies(List<Movie> movies) {
this.movies = movies;
}

public void rentMovie(Movie movie, Customer customer) {
boolean customerChecker = false;
for (Customer customer_temp:customers) {
if (customer_temp == customer) {
customerChecker = true;
boolean itemChecker = false;
for (Movie movie_temp:movies) {
if (movie_temp == movie) {
itemChecker = true;
if(this.availableMovies.contains(movie)) {
this.availableMovies.remove(movie);
movie.rentMovie(customer);
}
else {
System.out.println("This movie can't be rented because it's rented already");
}
break;
}
}
if(!itemChecker) {
System.out.println("movie not found!");
}
break;
}
}
if(!customerChecker) {
System.out.println("Customer not found!");
}
}

public void returnMovie(Rental rental) {
boolean customerChecker = false;
for (Customer customer:customers) {
if (rental.getCustomer() == customer) {
customerChecker = true;
boolean itemChecker = false;
for (Movie movie:movies) {
if (rental.getMovie() == movie) {
itemChecker = true;
boolean rentalChecker = false;
try {
for (Rental rental_temp:customer.getRentals()) {
if (rental_temp == rental) {
rentalChecker = true;
availableMovies.add(movie);
movie.returnMovie(rental);
break;
}
}
if (!rentalChecker) {
System.out.println("Not borrowed");
}
} catch (NullPointerException e) {
System.out.println("No rentals");
}

break;
}
}
if (!itemChecker) {
System.out.println("Item not found!");
}
break;
}
}
if (!customerChecker) {
System.out.println("Customer not found!");
}

}

public Customer getCustomerByID(int ID) {
boolean IDChecker = false;
for (Customer customer:customers) {
if (customer.getID() == ID) {
return customer;
}
}
if (!IDChecker){
System.out.println("Not found!!!");
}
return null;
}

public Movie getMovieByID(int ID) {
boolean IDChecker = false;
for (Movie movie:movies) {
if (movie.getID() == ID) {
return movie;
}
}
if (!IDChecker){
System.out.println("Not found!!!");
}
return null;
}
}
Empty file.