Answer to Question #249886 in Java | JSP | JSF for Rob

Question #249886

What is the java program for this?


Passenger fare is classified according to the type of passenger. Senior Citizen enjoys 20%discount from the regular fare, Students get 10% discount. Regular passengers who do not qualified as neither Senior Citizen nor a Student should pay the regular fare of 150.

Write an algorithm that will accept details of five (5) passengers, including its name and

passenger type and will compute for the corresponding fare of each passenger. Display

the name and type of passenger, as well as the computer fare.


1
Expert's answer
2021-10-12T00:37:14-0400


package passengers;


import java.util.Scanner;




public class Passengers {


    private String name;
    private String type;
    Passengers(){
        
    }
    Passengers(String n, String t){
        name = n;
        type  = t;
    }
    void compute(){
        if("regular".equals(type)){
            float fare  =150;
            System.out.printf("Name:  %s\nType: Regular\n Fare: %f \n", name, fare);
        }
       else if("senior".equals(type)){
            double fare  =150 - (0.2 * 150);
            System.out.printf("Name:  %s\nType: Senior\n Fare: %f \n", name, fare);
        }
        
        else if("student".equals(type)){
            double fare  =150 - (0.1 * 150);
            System.out.printf("Name:  %s\nType: Student\n Fare: %f \n", name, fare);
        }
    }
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        
        
        Passengers pa[] = new Passengers[5];
        System.out.println("Enter the details of 5 Passengers");
        for(int i= 0; i<5; i++){
            System.out.println("Passenger "+(i+1));
              System.out.println("Enter the passenger name ");
           String name = scan.nextLine();
           System.out.println("Enter the passenger type ");
        String type = scan.nextLine();
         Passengers p = new Passengers(name, type);
         pa[i] = p;
        
        }
      for(int i=0; i<5; i++){
           pa[i].compute();
      }
      
       
        
    }
    
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS