225.3.15(手机端APP地铁路线)
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
// 石家庄地铁站点映射(修正同名站点问题)
private static final Map<String, Integer> STATION_MAP = new HashMap<>();
static {
// 1号线站点
STATION_MAP.put("西王(1号线)", 101);
STATION_MAP.put("时光街(1号线)", 102);
STATION_MAP.put("长城桥(1号线)", 103);
STATION_MAP.put("和平医院(1号线)", 104);
STATION_MAP.put("烈士陵园(1号线)", 105);
STATION_MAP.put("新百广场(1号线)", 106);
STATION_MAP.put("西三庄(3号线)", 301);
STATION_MAP.put("高柱(3号线)", 302);
STATION_MAP.put("柏林庄(3号线)", 303);
STATION_MAP.put("市庄(3号线)", 304);
STATION_MAP.put("市二中(3号线)", 305);
STATION_MAP.put("新百广场(3号线)", 306);
STATION_MAP.put("柳辛庄(2号线)", 201);
STATION_MAP.put("庄窠-铁道大学(2号线)", 202);
}
private EditText etStartStation, etEndStation, etTicketCount;
private Button btnBuyTickets;
private TextView tvResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etStartStation = findViewById(R.id.et_start_station);
etEndStation = findViewById(R.id.et_end_station);
etTicketCount = findViewById(R.id.et_ticket_count);
btnBuyTickets = findViewById(R.id.btn_buy_tickets);
tvResult = findViewById(R.id.tv_result);
btnBuyTickets.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String startStation = etStartStation.getText().toString().trim();
String endStation = etEndStation.getText().toString().trim();
String ticketCountStr = etTicketCount.getText().toString().trim();
// 输入验证
if (startStation.isEmpty() || endStation.isEmpty() || ticketCountStr.isEmpty()) {
Toast.makeText(MainActivity.this, "请输入完整信息", Toast.LENGTH_SHORT).show();
return;
}
if (!STATION_MAP.containsKey(startStation) || !STATION_MAP.containsKey(endStation)) {
Toast.makeText(MainActivity.this, "输入的站点不存在", Toast.LENGTH_SHORT).show();
return;
}
// 获取站点编号
int startIndex = STATION_MAP.get(startStation);
int endIndex = STATION_MAP.get(endStation);
// 检查是否同一线路
int startLine = startIndex / 100;
int endLine = endIndex / 100;
if (startLine != endLine) {
Toast.makeText(MainActivity.this, "跨线路购票暂不支持,请选择同一线路站点", Toast.LENGTH_SHORT).show();
return;
}
// 计算站数(起点站不计入)
int stationCount = Math.abs(endIndex - startIndex) - 1;
if (stationCount < 0) {
Toast.makeText(MainActivity.this, "请选择正确的站点顺序", Toast.LENGTH_SHORT).show();
return;
}
int ticketCount = Integer.parseInt(ticketCountStr);
if (ticketCount <= 0) {
Toast.makeText(MainActivity.this, "购票数量必须大于0", Toast.LENGTH_SHORT).show();
return;
}
// 计算费用
int fee = (int) Math.ceil((double) stationCount / 3);
int totalFee = fee * ticketCount;
tvResult.setText("购票成功,总费用:" + totalFee + " 元\n站数:" + stationCount + "站");
}
});
}
}

浙公网安备 33010602011771号