Untitled
unknown
plain_text
2 years ago
3.5 kB
1
Indexable
Never
package midtermlabexam; import java.util.ArrayList; public class Shuttle { private String driverName; private int shuttleNo,passengers; public Shuttle(String driverName,int shuttleNo,int passengers) { this.driverName = driverName; this.shuttleNo = shuttleNo; this.passengers = passengers; } public String getDriverName() { return driverName; } public int getShuttleNo() { return shuttleNo; } public int getPassengers() { return passengers; } } class Route { private String startingFrom,terminating; ArrayList<Shuttle>shuttleList = new ArrayList<Shuttle>(10); public Route(String startingFrom,String terminating) { this.startingFrom = startingFrom; this.terminating = terminating; } public String getStartingFrom() { return startingFrom; } public String getTerminating() { return terminating; } public void assignShuttle(Shuttle shuttle) { shuttleList.add(shuttle); } public int getTotalNoofPassengers() { int passengers = 0; for(Shuttle shuttle:shuttleList) { passengers += shuttle.getPassengers(); } return passengers; } public Shuttle searchShuttle(int shuttleNo) { for (Shuttle shuttle : shuttleList) { if (shuttle.getShuttleNo() == shuttleNo) { return shuttle; } } return null; } public void display() { System.out.println("================="); System.out.print("Starting From : "+ startingFrom); System.out.println(); System.out.print("Ending at : "+ terminating); System.out.println(); System.out.println("================="); for (Shuttle shuttle : shuttleList) { System.out.print("Driver Name : "+ shuttle.getDriverName()); System.out.println(); System.out.print("Number of Shuttles : "+ shuttle.getShuttleNo()); System.out.println(); System.out.print("Number of Passengers : "+ shuttle.getPassengers()); System.out.println(); } } } class Driver1 { public static void main(String[] args) { Shuttle shuttle1 = new Shuttle("Fawaz",2,46); Shuttle shuttle2 = new Shuttle("Saim",4,38); Route route1 = new Route("Pakistan","Germany"); Route route2 = new Route("Karachi","Lahore"); route1.assignShuttle(shuttle1); route2.assignShuttle(shuttle2); route1.display(); int totalPassengers = route1.getTotalNoofPassengers(); System.out.println("Total passengers from "+route1.getStartingFrom()+" to "+route1.getTerminating()+" are : "+totalPassengers); route2.display(); int totalPassengers2 = route2.getTotalNoofPassengers(); System.out.println("Total passengers from "+route2.getStartingFrom()+" to "+route2.getTerminating()+" are : "+totalPassengers2); Shuttle searchedShuttle = route1.searchShuttle(2); if (searchedShuttle != null) { System.out.println("Searched shuttle found: " + searchedShuttle.getDriverName()); } else { System.out.println("Searched shuttle not found."); } } }