Continuation of this:
https://www.assignmentexpert.com/homework-answers/programming-and-computer-science/algorithms/question-246742
1.In solving the given problem above, implement the algorithm you have written in
Activity Sheet 01 using Java as the programming language.
2.In writing your program, create methods to implement the operations identified
in the previous activity. Also, add a constructor that will display a greeting to the
user of the system.
Passenger.java
public class Passenger {
private String name;
private String type;
private double fare;
public Passenger(){
name = "";
type = "";
fare = 0;
}
public Passenger(String n, String t){
name = n;
type = t;
fare = 0;
}
public void ComputeFare(){
if(type.equals("Senior")){
fare = 100 - (100 * 0.2);
}
else if(type.equals("Student")){
fare = 100 - (100 * 0.1);
}
else{
fare = 100;
}
}
public String get_name(){
return name;
}
public String get_type() {
return type;
}
public double get_fare(){
return fare;
}
}
________
Main.java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Passenger[] p = new Passenger[5];
Scanner inp = new Scanner(System.in);
String name, type;
for( int i = 0; i < 5; i++){
System.out.print("Enter the Name of Passenger no." + (i+1) + " : ");
name = inp.nextLine();
System.out.print("Enter the type of Passenger no." + (i+1) + " : ");
type = inp.nextLine();
p[i] = new Passenger(name, type);
p[i].ComputeFare(); // Compute fare
}
System.out.println("\n");
System.out.println(String.format("%-12s| %-10s| %-6s |", "Name", "Type", "Fare"));
System.out.println("**********************************");
for( int i = 0; i < 5; i++){
System.out.println(String.format("%-12s| %-10s| %-6.2f |", p[i].get_name(), p[i].get_type(), p[i].get_fare()));
}
}
}
Comments
Leave a comment