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.
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();
}
}
}
Comments
Leave a comment