石家庄地铁app
今天完善mainactivity文件,里面包括页面的输入框和按钮的功能实现代码
package com.example.try2;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private EditText etStartStation, etEndStation, etTicketCount;
private Button btnBuyTicket;
private TextView tvResult;
// 地铁线路数据
private final Map<String, List<String>> metroLines = new HashMap<>();
private final Map<String, String> transferStations = new HashMap<>(); // 换乘站及其所属线路
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etStartStation = findViewById(R.id.etStartStation);
etEndStation = findViewById(R.id.etEndStation);
etTicketCount = findViewById(R.id.etTicketCount);
btnBuyTicket = findViewById(R.id.btnBuyTicket);
tvResult = findViewById(R.id.tvResult);
// 初始化地铁线路数据
initMetroData();
btnBuyTicket.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculateAndDisplayTicketPrice();
}
});
}
private void initMetroData() {
// 1号线站名
List<String> line1 = Arrays.asList(
"西王", "时光街", "长城桥", "和平医院", "烈士陵园", "新百广场",
"解放广场", "平安大街", "北国商城", "博物院", "体育场", "谈固",
"朝晖桥", "白佛", "留村", "火炬广场", "石家庄东站"
);
metroLines.put("1号线", line1);
// 2号线站名
List<String> line2 = Arrays.asList(
"柳辛庄", "庄窠·铁道大学", "义堂", "建和桥", "长安公园", "裕华路",
"北国商城", "大戏院", "新世隆", "石家庄站"
);
metroLines.put("2号线", line2);
// 3号线站名
List<String> line3 = Arrays.asList(
"西三庄", "高柱", "柏林庄", "市庄", "市二中", "新百广场",
"东里", "槐安桥", "西三教", "石家庄站"
);
metroLines.put("3号线", line3);
// 换乘站信息
transferStations.put("新百广场", "1号线,3号线");
transferStations.put("北国商城", "1号线,2号线");
transferStations.put("石家庄站", "2号线,3号线");
}
private void calculateAndDisplayTicketPrice() {
String startStation = etStartStation.getText().toString();
String endStation = etEndStation.getText().toString();
String ticketCountStr = etTicketCount.getText().toString();
if (startStation.isEmpty() || endStation.isEmpty() || ticketCountStr.isEmpty()) {
tvResult.setText("请输入完整信息");
return;
}
int ticketCount = Integer.parseInt(ticketCountStr);
// 获取起点站和终点站所在的线路
String startLine = findLineByStation(startStation);
String endLine = findLineByStation(endStation);
if (startLine == null || endLine == null) {
tvResult.setText("站点输入错误,请检查站名");
return;
}
// 如果起点站和终点站在同一线路
if (startLine.equals(endLine)) {
int stationCount = calculateStationCount(startStation, endStation, startLine);
int pricePerTicket = (stationCount + 2) / 3; // 每3站1元,不足3站按1元计算
int totalPrice = pricePerTicket * ticketCount;
tvResult.setText("购票成功!总金额:" + totalPrice + "元");
} else {
// 跨线路,需要找到换乘站
String transferStation = findTransferStation(startLine, endLine);
if (transferStation == null) {
tvResult.setText("无法找到换乘站,请检查线路");
return;
}
// 计算起点站到换乘站的站数
int stationCount1 = calculateStationCount(startStation, transferStation, startLine);
// 计算换乘站到终点站的站数
int stationCount2 = calculateStationCount(transferStation, endStation, endLine);
// 总站数
int totalStationCount = stationCount1 + stationCount2;
// 票价 = 总站数 / 3(向上取整) + 换乘费用(1元)
int pricePerTicket = (totalStationCount + 2) / 3 ;
int totalPrice = pricePerTicket * ticketCount;
tvResult.setText("购票成功!总金额:" + totalPrice + "元");
}
}
// 根据站名查找所属线路
private String findLineByStation(String station) {
for (Map.Entry<String, List<String>> entry : metroLines.entrySet()) {
if (entry.getValue().contains(station)) {
return entry.getKey();
}
}
return null;
}
// 计算两个站之间的站数
private int calculateStationCount(String startStation, String endStation, String line) {
List<String> stations = metroLines.get(line);
int startIndex = stations.indexOf(startStation);
int endIndex = stations.indexOf(endStation);
return Math.abs(endIndex - startIndex);
}
// 查找两个线路之间的换乘站
private String findTransferStation(String startLine, String endLine) {
for (Map.Entry<String, String> entry : transferStations.entrySet()) {
String lines = entry.getValue();
if (lines.contains(startLine) && lines.contains(endLine)) {
return entry.getKey();
}
}
return null;
}
}
在虚拟机中运行效果如下



浙公网安备 33010602011771号