daily pastebin goal
35%
SHARE
TWEET

Untitled

a guest Mar 20th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Car {
  2.  
  3.     private String regNo, make, model, deliverName;
  4.     private int passengerCapacity;
  5.     private boolean avaiable;
  6.     private Booking currentBookings[] = new Booking[5];
  7.     private Booking pastBookings[] = new Booking[5];
  8.  
  9.     public Car(String regNo, String make, String model, String deliverName, int passengerCapacity) {
  10.  
  11.         this.regNo = regNo;
  12.         this.make = make;
  13.         this.model = model;
  14.         this.deliverName = deliverName;
  15.         this.passengerCapacity = passengerCapacity;
  16.  
  17.         // bussiness validations
  18.  
  19.         // 1 - reg no validation
  20.         if (this.regNo.length() > 6) {
  21.             // handle the error
  22.         } else {
  23.             // if its less than 6 digits check if first 3 are alph followed by numbers
  24.             // check if first letters are not Alphabetics
  25.             if (!Character.isAlphabetic(this.regNo.charAt(0)) && !Character.isAlphabetic(this.regNo.charAt(1))
  26.                     && !Character.isAlphabetic(this.regNo.charAt(2))) {
  27.                 // handle the error
  28.             }
  29.             // check if lest of the chars are not numbers
  30.             else if (!Character.isDigit(this.regNo.charAt(0)) && !Character.isDigit(this.regNo.charAt(1))
  31.                     && !Character.isDigit(this.regNo.charAt(2))) {
  32.                 // handle the error
  33.             }
  34.         }
  35.  
  36.         // 2 - passenger capacity should now be greater than 0 and less than 10 ;
  37.         if (this.passengerCapacity <= 0) {
  38.             this.passengerCapacity = 1;
  39.         } else if (this.passengerCapacity >= 10) {
  40.             this.passengerCapacity = 9;
  41.         }
  42.  
  43.     }
  44.  
  45.     public boolean book(String firstName, String lastName, DateTime required, int numPassengers) {
  46.         if (this.currentBookings.length > 5) {
  47.             // car is not avaiable for bookings
  48.             this.avaiable = false;
  49.             return false;
  50.         } else {
  51.             // car is avaiable
  52.             this.avaiable = true;
  53.         }
  54.  
  55.         // check if datetime greater than todays datetime
  56.         //
  57.  
  58.         // code
  59.  
  60.         //
  61.  
  62.         if (numPassengers > this.passengerCapacity) {
  63.             // car cannot have this amount of passengers
  64.             this.avaiable = false;
  65.             return false;
  66.         }
  67.  
  68.  
  69.         //after all validation make a booking object
  70.         Booking booking = new Booking(required, this, firstName, lastName, 1.50);
  71.         this.currentBookings[0] = booking;
  72.  
  73.     }
  74.  
  75.     public String getDetails(){
  76.         return " RegNo :" + this.regNo + "\n Make & Model :"+ this.make+" "+this.model+"\n Drivers Name:"+this.deliverName+"\n Capacity:"+this.passengerCapacity+"\n Avaiable:"+this.avaiable;
  77.     }
  78.  
  79.     public String toString() {
  80.         return this.regNo+":"+this.make+":"+this.model+":"+this.deliverName+":"+this.passengerCapacity+":"+this.avaiable;
  81.    
  82.     }
  83. }
  84.  
  85. public class Booking {
  86.  
  87.     private DateTime pickupDateTime;
  88.     private Car car;
  89.     private String  firstName, lastName;
  90.     private double bookingFee, kilometresTravelled, tripFee;
  91.     private int numPassengers;
  92.  
  93.     public Booking(DateTime pickupDateTime, Car car, String firstName, String lastName, double bookingFee) {
  94.         this.pickupDateTime = pickupDateTime;
  95.         this.car = car;
  96.         this.id = id;
  97.         this.firstName = firstName;
  98.         this.lastName = lastName;
  99.         this.bookingFee = bookingFee;
  100.        
  101.     }
  102.  
  103.     public setKilometersTravelled(double km){
  104.         this.kilometresTravelled = km;
  105.     }
  106.  
  107.     double calculateFee(){
  108.         return ((30/100)*this.bookingFee) * this.kilometresTravelled;
  109.     }
  110.  
  111. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top