Symbianize Forum

Most of our features and services are available only to members, so we encourage you to login or register a new account. Registration is free, fast and simple. You only need to provide a valid email. Being a member you'll gain access to all member forums and features, post a message to ask question or provide answer, and share or find resources related to mobile phones, tablets, computers, game consoles, and multimedia.

All that and more, so what are you waiting for, click the register button and join us now! Ito ang website na ginawa ng pinoy para sa pinoy!

Java LRT Station.

acjfcyber14

Recruit
Basic Member
Messages
1
Reaction score
0
Points
16
Hello po! Bago lang po ko dito so I'm asking for help of making LRT Station using Array on Java. Clearly I was assigned to do LRT1 Station so right now I'm having a problem of doing the fare pricing.

Example: Current Station ko is: Monumento
Destination is hanggang Baclaran.

Then the price will show up
then the ticket kung ilan gusto mong bilhin na ticket
then the user will input the money and the total change.

Here's the code:

import java.util.Arrays;
import java.util.*;

public class LRTTicket{
public static void main(String[] args){
String lrtArray[] = { "Roosevelt", "Balintawak", "Monumento", "Fifth Avenue", "R. Papa", "Abad Santos", "Blumentritt", "Tayuman", "Bambang", "Doroteo Jose", "Carriedo",
"Central Terminal", "United Nations", "Pedro Gil", "Quirino", "Vito Cruz", "Gil Puyat", "Libertad", "Edsa", "Baclaran" };
int payment;
int ticket;
int total;
Scanner station = new Scanner(System.in);

System.out.print("Enter a station: ");
lrtArray[0] = station.nextLine();

System.out.print("Enter destination: ");
lrtArray[1] = station.nextLine();

switch(lrtArray[1]){
case "Roosevelt":
System.out.println("P15");
System.out.print("Please enter your payment: ");
payment = station.nextInt();
if(payment >= 15){
System.out.println("Here you go.");
payment = payment - 15;
System.out.print("Your change is: " +payment);
} else {
System.out.print("Not enough money to buy this ticket.");
}
break;
case "Balintawak":
System.out.println("P15");
System.out.print("Please enter your payment: ");
payment = station.nextInt();
if(payment >= 15){
System.out.println("Here you go.");
payment = payment - 15;
System.out.print("Your change is: " +payment);
} else {
System.out.print("Not enough money to buy this ticket.");
}
break;
case "Monumento":
System.out.println("P15");
System.out.print("Please enter your payment: ");
payment = station.nextInt();
if(payment >= 15){
System.out.println("Here you go.");
payment = payment - 15;
System.out.print("Your change is: " +payment);
} else {
System.out.print("Not enough money to buy this ticket.");
}
break;
case "Fifth Avenue":
System.out.println("P20");
System.out.print("Please enter your payment: ");
payment = station.nextInt();
if(payment >= 20){
System.out.println("Here you go.");
payment = payment - 20;
System.out.print("Your change is: " +payment);
} else {
System.out.print("Not enough money to buy this ticket.");
}
break;
case "R. Papa":
System.out.println("P20");
System.out.print("Please enter your payment: ");
payment = station.nextInt();
if(payment >= 20){
System.out.println("Here you go.");
payment = payment - 20;
System.out.print("Your change is: " +payment);
} else {
System.out.print("Not enough money to buy this ticket.");
}
break;
case "Abad Santos":
System.out.println("P20");
System.out.print("Please enter your payment: ");
payment = station.nextInt();
if(payment >= 20){
System.out.println("Here you go.");
payment = payment - 20;
System.out.print("Your change is: " +payment);
} else {
System.out.print("Not enough money to buy this ticket.");
}
break;
case "Blumentritt":
System.out.println("P20");
System.out.print("Please enter your payment: ");
payment = station.nextInt();
if(payment >= 20){
System.out.println("Here you go.");
payment = payment - 20;
System.out.print("Your change is: " +payment);
} else {
System.out.print("Not enough money to buy this ticket.");
}
break;
case "Tayuman":
System.out.println("P20");
System.out.print("Please enter your payment: ");
payment = station.nextInt();
if(payment >= 20){
System.out.println("Here you go.");
payment = payment - 20;
System.out.print("Your change is: " +payment);
} else {
System.out.print("Not enough money to buy this ticket.");
}
break;
case "Bambang":
System.out.println("P20");
System.out.print("Please enter your payment: ");
payment = station.nextInt();
if(payment >= 20){
System.out.println("Here you go.");
payment = payment - 20;
System.out.print("Your change is: " +payment);
} else {
System.out.print("Not enough money to buy this ticket.");
}
break;
case "Doroteo Jose":
System.out.println("P30");
System.out.print("Please enter your payment: ");
payment = station.nextInt();
if(payment >= 30){
System.out.println("Here you go.");
payment = payment - 30;
System.out.print("Your change is: " +payment);
} else {
System.out.print("Not enough money to buy this ticket.");
}
break;
case "Carriedo":
System.out.println("P30");
System.out.print("Please enter your payment: ");
payment = station.nextInt();
if(payment >= 30){
System.out.println("Here you go.");
payment = payment - 30;
System.out.print("Your change is: " +payment);
} else {
System.out.print("Not enough money to buy this ticket.");
}
break;
case "Central Terminal":
System.out.println("P30");
System.out.print("Please enter your payment: ");
payment = station.nextInt();
if(payment >= 30){
System.out.println("Here you go.");
payment = payment - 30;
System.out.print("Your change is: " +payment);
} else {
System.out.print("Not enough money to buy this ticket.");
}
break;
case "United Nations":
System.out.println("P30");
System.out.print("Please enter your payment: ");
payment = station.nextInt();
if(payment >= 30){
System.out.println("Here you go.");
payment = payment - 30;
System.out.print("Your change is: " +payment);
} else {
System.out.print("Not enough money to buy this ticket.");
}
break;
case "Pedro Gil":
System.out.println("P30");
System.out.print("Please enter your payment: ");
payment = station.nextInt();
if(payment >= 30){
System.out.println("Here you go.");
payment = payment - 30;
System.out.print("Your change is: " +payment);
} else {
System.out.print("Not enough money to buy this ticket.");
}
break;
case "Quirino":
System.out.println("P30");
System.out.print("Please enter your payment: ");
payment = station.nextInt();
if(payment >= 30){
System.out.println("Here you go.");
payment = payment - 30;
System.out.print("Your change is: " +payment);
} else {
System.out.print("Not enough money to buy this ticket.");
}
break;
case "Vito Cruz":
System.out.println("P30");
System.out.print("Please enter your payment: ");
payment = station.nextInt();
if(payment >= 30){
System.out.println("Here you go.");
payment = payment - 30;
System.out.print("Your change is: " +payment);
} else {
System.out.print("Not enough money to buy this ticket.");
}
break;
case "Gil Puyat":
System.out.println("P30");
System.out.print("Please enter your payment: ");
payment = station.nextInt();
if(payment >= 30){
System.out.println("Here you go.");
payment = payment - 30;
System.out.print("Your change is: " +payment);
} else {
System.out.print("Not enough money to buy this ticket.");
}
break;
case "Libertad":
System.out.println("P30");
System.out.print("Please enter your payment: ");
payment = station.nextInt();
if(payment >= 30){
System.out.println("Here you go.");
payment = payment - 30;
System.out.print("Your change is: " +payment);
} else {
System.out.print("Not enough money to buy this ticket.");
}
break;
case "Edsa":
System.out.println("P30");
System.out.print("Please enter your payment: ");
payment = station.nextInt();
if(payment >= 30){
System.out.println("Here you go.");
payment = payment - 30;
System.out.print("Your change is: " +payment);
} else {
System.out.print("Not enough money to buy this ticket.");
}
break;
case "Baclaran":
System.out.println("P30");
System.out.print("Please enter your payment: ");
payment = station.nextInt();
if(payment >= 30){
System.out.println("Here you go.");
payment = payment - 30;
System.out.print("Your change is: " +payment);
} else {
System.out.print("Not enough money to buy this ticket.");
}
break;
}
}
}


P.S Pabago bago kasi ako ng code pero eto ung una kong ginawa.

Patulong nalang po. Salamat po
 
HELLO, ETO YUNG GINAWA KONG SOLUTIONS SA PROBLEM MO. MAY ILANG COMMENTS JAN FOR CLARIFICATION PERO TRY TO UNDERSTAND PARIN. BINIGAY KO NA SAYO SAMPLE CODE, IKAW NA BAHALA UMINTINDI NG LOGIC :). NAPAKAHABA KASI NUNG GINAWA MO, SAKIT SA MATA HAHAHAHA

============================================================
Scanner scan = new Scanner(System.in);
String[] stations = new String[] {"A","B","C","D","E"};
int[] fare = new int[] {10,20,30,40,50};
int payment; int total; int number_of_tickets;
int starting_index = 0; int destination_index = 0;
String starting_point; String destination_point;

System.out.println("Enter Starting");
starting_point = scan.nextLine();
System.out.println("Enter Destination");
destination_point = scan.nextLine();
System.out.println("How many tickets?:");
number_of_tickets = scan.nextInt();

//for getting the array index of starting and destination
for(int x = 0 ; x<stations.length ; x++){
if(starting_point.equalsIgnoreCase(stations[x])){
starting_index = x;
}
if(destination_point.equalsIgnoreCase(stations[x])){
destination_index = x;
}
}

//for getting the fare and for making payment
if(starting_index > destination_index){
total = fare[(starting_index - destination_index)-1] * number_of_tickets;
System.out.println("Total is: "+total);
System.out.println("Enter your payment: ");
payment = scan.nextInt();
if(payment >= total){
System.out.println("The change is: "+(payment-total));
}else{
System.out.println("Insufficient Funds");
}
}else{
total = fare[(destination_index - starting_index)-1] * number_of_tickets;
System.out.println("Total is: "+total);
System.out.println("Enter your payment: ");
payment = scan.nextInt();
if(payment >= total){
System.out.println("The change is: "+(payment-total));
}else{
System.out.println("Insufficient Funds");
}
}

================================================================
KUNG MAPAPANSIN MO, LIMA LANG YUNG STATIONS NA NILAGAY KO. YUNG ARRAY FOR FARE, YAN YUNG BAYAD DEPENDE SA DISTANCE. KUNWARI YUNG STARTING AY A AT ANG DESTINATION AY NEXT STATION LANG (B). THE VALUE OF THE TICKET IS FARE[0] SINCE MAGKASUNOD LANG. IKAW NA BAHALA UMINTINDI. HAPPY CODING BROTHER, STUDY HARD HAHA
 
Last edited:
Split into 3 classes. Main, LrtUserInput and LtrTransaction. This is just based on my assumptions.

Pwede mo ma try ang sample code dito. :lol:
https://repl.it/repls/AmbitiousImmenseUsername


Code:
import java.util.List;
import java.util.ArrayList;

class Main {
  public static void main(String[] args) {
  List<String> stations = new ArrayList<>();
        stations.add("Roosevelt");
        stations.add("Balintawak");
        stations.add("Monumento");

        stations.add("Fifth Avenue");
        stations.add("R. Papa");
        stations.add("Abad Santos");
        stations.add("Blumentritt");
        stations.add("Tayuman");
        stations.add("Bambang");
        stations.add("Doroteo Jose");

        stations.add("Carriedo");

        stations.add("Central Terminal");
        stations.add("United Nations");
        stations.add("Pedro Gil");
        stations.add("Quirino");
        stations.add("Vito Cruz");
        stations.add("Gil Puyat");
        stations.add("Libertad");
        stations.add("Edsa");
        stations.add("Baclaran");

        final LrtUserInput lrtUserInput = new LrtUserInput(stations);

        String selectedStation = lrtUserInput.getSelectedStation();
        String selectedDestination = lrtUserInput.getSelectedDestination();
        Integer numberOfTickets = lrtUserInput.getNumberOfTickets();

        LtrTransaction ltrTransaction = new LtrTransaction(stations, selectedStation, selectedDestination, numberOfTickets);
        ltrTransaction.displayTotalFare();

        boolean validPayment;
        do {
            Integer payment = lrtUserInput.getPayment();
            validPayment = ltrTransaction.computePayment(payment);
        } while (!validPayment);
      System.out.println("Done . . .");
    }
  }

Code:
import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern;

public class LrtUserInput {

    private Scanner scanner;
    private List<String> lrtStations;

    public LrtUserInput(List<String> lrtStations) {
        this.lrtStations = lrtStations;
        this.scanner = new Scanner(System.in);
        displayLrtStations();
    }

    private void displayLrtStations() {
        System.out.println("LRT Stations: ");
        this.lrtStations.forEach((station) -> {
            System.out.println(String.format("%s", station));
        });
        System.out.println("----------------------");
    }

    public Integer getNumberOfTickets() {
        Integer numberOfTickets;
        do {
            numberOfTickets = inputNumberOfTickets();
        } while (numberOfTickets == null);
        return numberOfTickets;
    }

    public String getSelectedDestination() {
        String selectedDestination;
        do {
            selectedDestination = inputDestination();
        } while (selectedDestination == null);
        return selectedDestination;
    }

    public String getSelectedStation() {
        String selectedStation;
        do {
            selectedStation = inputStation();
        } while (selectedStation == null);
        return selectedStation;
    }


    public Integer getPayment() {
        System.out.println("Enter your payment:");
        String input = scanner.nextLine();
        if (validateIntegerInput(input)) {
            System.out.println("Please enter valid number");
            return null;
        }
        System.out.println("----------");
        return Integer.valueOf(input);
    }

    private String inputStation() {
        System.out.println("Enter Station :");
        final String station = scanner.nextLine();
        if (checkValidLrtStationInput(station)) {
            return null;
        }
        System.out.println("----------");
        return station;
    }

    private String inputDestination() {
        System.out.println("Enter Destination:");
        final String destination = scanner.nextLine();
        if (checkValidLrtStationInput(destination)) {
            return null;
        }
        System.out.println("----------");
        return destination;
    }


    private Integer inputNumberOfTickets() {
        System.out.println("Enter number of tickets:");
        String input = scanner.nextLine();
        if (validateIntegerInput(input)) {
            System.out.println("Please enter valid number");
            return null;
        }
        System.out.println("----------");
        return Integer.valueOf(input);
    }

    private boolean validateIntegerInput(String input) {
        Pattern isInteger = Pattern.compile("\\d+");
        return !isInteger.matcher(input).matches();
    }

    private boolean checkValidLrtStationInput(String ltrStation) {
        if (!this.lrtStations.contains(ltrStation)) {
            System.out.println("Invalid LRT Station. Please enter correct name");
            System.out.println();
            return true;
        }
        return false;
    }

}


Code:
import java.util.List;

public class LtrTransaction {

    private List<String> lrtStations;
    private String station;
    private String destination;
    private int numberOfTicket;
    private int fare;

    public LtrTransaction(List<String> lrtStations, String station, String destination, int numberOfTicket) {
        this.lrtStations = lrtStations;
        this.station = station;
        this.destination = destination;
        this.numberOfTicket = numberOfTicket;
        this.determineFare();
    }

    public void displayTotalFare() {
        System.out.println(String.format("Total is %d: ", this.fare * numberOfTicket));
    }

    public boolean computePayment(Integer payment) {
        if (payment == null) {
            return false;
        }
        if (payment < getTotalPayable()) {
            System.out.println("Not enough money to buy ticket.");
            return false;
        }
        final Integer change = payment - getTotalPayable();
        System.out.println();
        System.out.println("----------------------------");
        System.out.println("Station : " + station);
        System.out.println("Destination : " + destination);
        System.out.println("Fare: " + fare);
        System.out.println("Number of tickets: " + numberOfTicket);
        System.out.println();
        System.out.println(String.format("Total is %d: ", this.fare));
        System.out.println(String.format("Payment is %d: ", payment));
        System.out.println(String.format("Your change is : %d", change));
        System.out.println("----------------------------");
        System.out.println();
        return true;
    }

    private int computeFareMatrix() {
        final int from = this.lrtStations.indexOf(station);
        final int to = this.lrtStations.indexOf(destination);
        int numberOfStations = Math.abs(from - to);
        if (numberOfStations >= 9) {
            return 30;
        } else if (numberOfStations >= 3) {
            return 20;
        } else {
            return 15;
        }
    }

    private void determineFare() {
        this.fare = computeFareMatrix();
    }

    private int getTotalPayable() {
        return this.fare * this.numberOfTicket;
    }

}
 
looking po ako tutor as soon as possible po. need ko lang magpropose sa prof ko. kaso hirap na hirap ako umintindi sa klase namin. bka meron dto sa inyo. pasig area po. willing mag bayad para matuto lang. salamat po
 
Last edited:
Back
Top Bottom