Ben rents a trailer to move his furniture to his new house. The basic cost is R250 per day plus a specific amount that must be entered by him per kilometre travelled. He also has to enter the distance travelled in kilometres.
If Ben travels less than 60 kilometres, an additional surcharge of 3% is payable on the amount due for distance. However, if he uses the trailer for more than 350 kilometres, he receives a discount of 9% on the amount due for distance (Pretorius & Erasmus, 2012:65).
Write a program to calculate and display the amount due.
Hint: use object oriented programming concepts to develop your program.
package trailer;
import java.util.Scanner;
public class Trailer {
 private int basic_cost;
 private int distance;
 private double pricePerKm;
 public Trailer(int d, double km){
   basic_cost = 250;
   distance = d;
   pricePerKm = km;
 }
  void calculate_amount_due(){
    double amount=0;
    if(distance <60){
     Â
      amount = (pricePerKm * distance)+ basic_cost;
      amount = amount + 0.03;
    }
    else if(distance>350){
    amount = (pricePerKm * distance)+ basic_cost;Â
    amount = amount - (amount * 0.09);
    }
    System.out.println("Amount due is: "+ amount);
   Â
  }Â
  public static void main(String[] args) {
    System.out.println("Enter price per km: ");
    Scanner scan = new Scanner(System.in);
    double price = scan.nextDouble();
    Â
    System.out.println("Enter the distance travelled: ");
    int d= scan.nextInt();
    Trailer t= new Trailer(d, price);
    t.calculate_amount_due();
    Â
  }
  Â
}
Comments
Leave a comment