Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Car {
- private String regNo, make, model, deliverName;
- private int passengerCapacity;
- private boolean avaiable;
- private Booking currentBookings[] = new Booking[5];
- private Booking pastBookings[] = new Booking[5];
- public Car(String regNo, String make, String model, String deliverName, int passengerCapacity) {
- this.regNo = regNo;
- this.make = make;
- this.model = model;
- this.deliverName = deliverName;
- this.passengerCapacity = passengerCapacity;
- // bussiness validations
- // 1 - reg no validation
- if (this.regNo.length() > 6) {
- // handle the error
- } else {
- // if its less than 6 digits check if first 3 are alph followed by numbers
- // check if first letters are not Alphabetics
- if (!Character.isAlphabetic(this.regNo.charAt(0)) && !Character.isAlphabetic(this.regNo.charAt(1))
- && !Character.isAlphabetic(this.regNo.charAt(2))) {
- // handle the error
- }
- // check if lest of the chars are not numbers
- else if (!Character.isDigit(this.regNo.charAt(0)) && !Character.isDigit(this.regNo.charAt(1))
- && !Character.isDigit(this.regNo.charAt(2))) {
- // handle the error
- }
- }
- // 2 - passenger capacity should now be greater than 0 and less than 10 ;
- if (this.passengerCapacity <= 0) {
- this.passengerCapacity = 1;
- } else if (this.passengerCapacity >= 10) {
- this.passengerCapacity = 9;
- }
- }
- public boolean book(String firstName, String lastName, DateTime required, int numPassengers) {
- if (this.currentBookings.length > 5) {
- // car is not avaiable for bookings
- this.avaiable = false;
- return false;
- } else {
- // car is avaiable
- this.avaiable = true;
- }
- // check if datetime greater than todays datetime
- //
- // code
- //
- if (numPassengers > this.passengerCapacity) {
- // car cannot have this amount of passengers
- this.avaiable = false;
- return false;
- }
- //after all validation make a booking object
- Booking booking = new Booking(required, this, firstName, lastName, 1.50);
- this.currentBookings[0] = booking;
- }
- public String getDetails(){
- return " RegNo :" + this.regNo + "\n Make & Model :"+ this.make+" "+this.model+"\n Drivers Name:"+this.deliverName+"\n Capacity:"+this.passengerCapacity+"\n Avaiable:"+this.avaiable;
- }
- public String toString() {
- return this.regNo+":"+this.make+":"+this.model+":"+this.deliverName+":"+this.passengerCapacity+":"+this.avaiable;
- }
- }
- public class Booking {
- private DateTime pickupDateTime;
- private Car car;
- private String firstName, lastName;
- private double bookingFee, kilometresTravelled, tripFee;
- private int numPassengers;
- public Booking(DateTime pickupDateTime, Car car, String firstName, String lastName, double bookingFee) {
- this.pickupDateTime = pickupDateTime;
- this.car = car;
- this.id = id;
- this.firstName = firstName;
- this.lastName = lastName;
- this.bookingFee = bookingFee;
- }
- public setKilometersTravelled(double km){
- this.kilometresTravelled = km;
- }
- double calculateFee(){
- return ((30/100)*this.bookingFee) * this.kilometresTravelled;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement