Advertisement
Louis331

Car

Oct 12th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 KB | None | 0 0
  1. import java.util.Scanner;
  2. //Program that will work out the cost of running a car with given distances
  3.  
  4. public class car{
  5.    
  6.     public static double Calculation(double[] carOne,double distance){
  7.        
  8.         double timesForDistance=Math.floor(distance/carOne[2]);
  9.         double timesToFill=distance/carOne[3];
  10.         double total=carOne[0]+(timesForDistance*carOne[1])+(timesToFill*carOne[4]);
  11.         return total;
  12.        
  13.     }
  14.  
  15.     public static double[] newCar(){
  16.         Scanner in= new Scanner(System.in);
  17.        
  18.         double[] carNew= new double[5];
  19.        
  20.         //asks for price of car
  21.         System.out.print("\nPlease enter the price of the car: ");
  22.         carNew[0]=in.nextDouble();
  23.  
  24.         //ask for price to service
  25.         System.out.print("\nPlease enter the price to service the car: ");
  26.         carNew[1]=in.nextDouble();
  27.  
  28.         //asks for the service interval
  29.         System.out.print("\nPlease enter the interval between services: ");
  30.         carNew[2]=in.nextDouble();
  31.  
  32.         //asks for km/l
  33.         System.out.print("\nPlease enter the km/l: ");
  34.         carNew[3]=in.nextDouble();
  35.  
  36.         //ask for fuel cost per litre
  37.         System.out.print("\nPlease enter the fuel cost per litre: ");
  38.         carNew[4]=in.nextDouble();
  39.         return carNew;
  40.     }
  41.    
  42.     public static double[] valuesForDistances(){
  43.         Scanner in=new Scanner(System.in);
  44.        
  45.         double[] valForDis=new double[3];
  46.        
  47.         System.out.print("\nEnter the min distance: ");
  48.             valForDis[0]=in.nextDouble();
  49.            
  50.             System.out.print("\nEnter the max distance: ");
  51.             valForDis[1]=in.nextDouble();
  52.            
  53.             System.out.print("\nEnter the interval: ");
  54.             valForDis[2]=in.nextDouble();
  55.            
  56.             return valForDis;
  57.     }
  58.    
  59.     public static void main(String[] args){
  60.        
  61.         Scanner in = new Scanner(System.in);       
  62.         int choice=0;      
  63.        
  64.         //menu system to work out what the user wants to do
  65.         while (choice<1||choice>3){
  66.            
  67.             System.out.print("\n Menu:\n1)For one distance\n2)For range of distance\n3)For 2 cars over range\n");
  68.             choice=in.nextInt();
  69.            
  70.         }
  71.        
  72.         //will work out the price to run the car for a set distance
  73.         if (choice==1){
  74.            
  75.             double[] carOne;       
  76.             carOne=car.newCar();
  77.            
  78.             System.out.print("\nEnter the distance the car is going to do: ");
  79.             double distance=in.nextDouble();           
  80.             System.out.printf("The cost of the car: \u00A3%1.2f",car.Calculation(carOne, distance));
  81.         }
  82.        
  83.         //will work out the price to run on car over range of distance
  84.         if (choice==2){
  85.            
  86.             double[] carOne;
  87.             double[] val;
  88.            
  89.            
  90.             val=car.valuesForDistances();
  91.             carOne=car.newCar();
  92.  
  93.             for (double counter=val[0]; counter<val[1]; counter+=val[2])
  94.             {
  95.                 System.out.printf("\nThe cost of the car for distance %.2f is: \u00A3%1.2f" ,counter ,car.Calculation(carOne, counter));
  96.             }
  97.            
  98.            
  99.         }
  100.        
  101.         //will make a table comparing distances and price for two cars
  102.         if (choice==3){
  103.            
  104.             double[] carOne;
  105.             double[] carTwo;
  106.             double[] val;
  107.            
  108.            
  109.             val=car.valuesForDistances();
  110.             System.out.print("\nCAR ONE\n");
  111.             carOne=car.newCar();
  112.             System.out.print("\nCAR TWO\n");
  113.             carTwo=car.newCar();
  114.            
  115.             for (double counter=val[0]; counter<val[1]; counter+=val[2])
  116.             {
  117.                 System.out.printf("\nMiles: %f1.2: Car one cost: \u00A3%1.2f Car two cost: \u00A3%1.2f",counter,car.Calculation(carOne,counter),car.Calculation(carTwo,counter));
  118.             }
  119.         }
  120.  
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement