日报2025314

今日课堂测验,没想到是实现前端功能
实现石家庄市地铁票价计算
Line.java

package com.example.subway.pojo;

import java.util.List;

public class Line {
    private int id; // 线路编号
    private String name; // 线路名称
    private List<Station> stations; // 站点列表

    public Line(int id, String name, List<Station> stations) {
        this.id = id;
        this.name = name;
        this.stations = stations;
    }

    // Getter 方法
    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public List<Station> getStations() {
        return stations;
    }
}

Station

package com.example.subway.pojo;

import java.util.List;

public class Station {
    private String name; // 站点名称
    private int index; // 站点在所属线路中的索引
    private int lineId; // 所属线路编号
    private List<Integer> transferTo; // 可换乘的线路列表

    public Station(String name, int index, int lineId) {
        this.name = name;
        this.index = index;
        this.lineId = lineId;
    }

    public Station(String name, int index, int lineId, List<Integer> transferTo) {
        this.name = name;
        this.index = index;
        this.lineId = lineId;
        this.transferTo = transferTo;
    }

    // Getter 方法
    public String getName() {
        return name;
    }

    public int getIndex() {
        return index;
    }

    public int getLineId() {
        return lineId;
    }

    public List<Integer> getTransferTo() {
        return transferTo;
    }

    @Override
    public String toString() {
        return name + "(" + lineId + "号线)";
    }
}

MainActivity

package com.example.subway;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.subway.pojo.Line;
import com.example.subway.pojo.Station;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private Spinner spinnerStart;
    private Spinner spinnerEnd;
    private EditText editTextTickets;
    private TextView textViewAmount;
    private Button buttonCalculate;
    private Button buttonBuy;

    private List<Line> allLines;
    private List<Station> allStations;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);

        initViews();
        setupSpinners();
        setupListeners();
    }

    private void initViews() {
        spinnerStart = findViewById(R.id.spinnerStart);
        spinnerEnd = findViewById(R.id.spinnerEnd);
        editTextTickets = findViewById(R.id.editTextTickets);
        textViewAmount = findViewById(R.id.textViewAmount);
        buttonCalculate = findViewById(R.id.buttonCalculate);
        buttonBuy = findViewById(R.id.buttonBuy);

        // Initialize lines and stations
        allLines = new ArrayList<>();
        allLines.add(createLine1());
        allLines.add(createLine2());
        allLines.add(createLine3());

        allStations = new ArrayList<>();
        for (Line line : allLines) {
            if (line != null && line.getStations() != null) {
                allStations.addAll(line.getStations());
            }
        }
    }

    private Line createLine1() {
        List<Station> stations = new ArrayList<>();
        stations.add(new Station("西王", 0, 1));
        stations.add(new Station("时光街", 1, 1));
        stations.add(new Station("长城桥", 2, 1));
        stations.add(new Station("和平医院", 3, 1));
        stations.add(new Station("烈士陵园", 4, 1));
        stations.add(new Station("新百广场", 5, 1, new ArrayList<Integer>() {{ add(3); }}));
        stations.add(new Station("解放广场", 6, 1));
        stations.add(new Station("平安大街", 7, 1));
        stations.add(new Station("北国商城", 8, 1, new ArrayList<Integer>() {{ add(2); }}));
        stations.add(new Station("博物院", 9, 1));
        stations.add(new Station("体育场", 10, 1));
        stations.add(new Station("北宋", 11, 1));
        stations.add(new Station("谈固", 12, 1));
        stations.add(new Station("朝晖桥", 13, 1));
        stations.add(new Station("白佛", 14, 1));
        stations.add(new Station("留村", 15, 1));
        stations.add(new Station("火炬广场", 16, 1));
        stations.add(new Station("石家庄东站", 17, 1));
        stations.add(new Station("南村", 18, 1));
        stations.add(new Station("洨河大道", 19, 1));
        stations.add(new Station("西庄", 20, 1));
        stations.add(new Station("东庄", 21, 1));
        stations.add(new Station("会展中心", 22, 1));
        stations.add(new Station("商务中心", 23, 1));
        stations.add(new Station("园博园", 24, 1));
        stations.add(new Station("福泽", 25, 1));

        return new Line(1, "1号线", stations);
    }

    private Line createLine2() {
        List<Station> stations = new ArrayList<>();
        stations.add(new Station("柳辛庄", 0, 2));
        stations.add(new Station("庄窠·铁道大学", 1, 2));
        stations.add(new Station("义堂", 2, 2));
        stations.add(new Station("建和桥", 3, 2));
        stations.add(new Station("长安公园", 4, 2));
        stations.add(new Station("北国商城", 5, 2, new ArrayList<Integer>() {{ add(1); }}));
        stations.add(new Station("裕华路", 6, 2));
        stations.add(new Station("槐中路", 7, 2));
        stations.add(new Station("欧韵公园", 8, 2));
        stations.add(new Station("元村", 9, 2));
        stations.add(new Station("石家庄", 10, 2, new ArrayList<Integer>() {{ add(3); }}));
        stations.add(new Station("塔坛", 11, 2));
        stations.add(new Station("仓丰路留村", 12, 2));
        stations.add(new Station("南位", 13, 2));
        stations.add(new Station("嘉华路", 14, 2));

        return new Line(2, "2号线", stations);
    }

    private Line createLine3() {
        List<Station> stations = new ArrayList<>();
        stations.add(new Station("西三庄", 0, 3));
        stations.add(new Station("高柱", 1, 3));
        stations.add(new Station("柏林庄", 2, 3));
        stations.add(new Station("市庄", 3, 3));
        stations.add(new Station("市二中", 4, 3));
        stations.add(new Station("新百广场", 5, 3, new ArrayList<Integer>() {{ add(1); }}));
        stations.add(new Station("东里", 6, 3));
        stations.add(new Station("槐安桥", 7, 3));
        stations.add(new Station("西三教", 8, 3));
        stations.add(new Station("石家庄", 9, 3, new ArrayList<Integer>() {{ add(2); }}));
        stations.add(new Station("汇通路", 10, 3));
        stations.add(new Station("孙村", 11, 3));
        stations.add(new Station("塔冢", 12, 3));
        stations.add(new Station("东王", 13, 3));
        stations.add(new Station("南王", 14, 3));
        stations.add(new Station("位同", 15, 3));
        stations.add(new Station("东二环南路", 16, 3));
        stations.add(new Station("西仰陵", 17, 3));
        stations.add(new Station("中仰陵", 18, 3));
        stations.add(new Station("南豆", 19, 3));
        stations.add(new Station("太行南大街", 20, 3));
        stations.add(new Station("乐乡", 21, 3));

        return new Line(3, "3号线", stations);
    }

    private void calculateFare() {
        if (spinnerStart.getSelectedItemPosition() < 0 || spinnerEnd.getSelectedItemPosition() < 0) {
            Toast.makeText(this, "请选择起点站和终点站", Toast.LENGTH_SHORT).show();
            return;
        }

        Station startStation = allStations.get(spinnerStart.getSelectedItemPosition());
        Station endStation = allStations.get(spinnerEnd.getSelectedItemPosition());

        if (startStation == null || endStation == null) {
            Toast.makeText(this, "无效的站点选择", Toast.LENGTH_SHORT).show();
            return;
        }

        int ticketCount = 0;
        try {
            ticketCount = Integer.parseInt(editTextTickets.getText().toString());
        } catch (NumberFormatException e) {
            e.printStackTrace();
            Toast.makeText(this, "请输入有效的票数", Toast.LENGTH_SHORT).show();
            return;
        }

        if (startStation.getName().equals(endStation.getName())) {
            Toast.makeText(this, "起点站和终点站不能相同", Toast.LENGTH_SHORT).show();
            return;
        }

        int stationCount = calculateStationCount(startStation, endStation);
        int farePerTicket = (stationCount + 2) / 3;
        int totalFare = farePerTicket * ticketCount;

        textViewAmount.setText("总金额:" + totalFare + "元");
        buttonBuy.setEnabled(true);
    }

    private int calculateStationCount(Station start, Station end) {
        if (start == null || end == null) {
            return 0;
        }

        // 如果在同一条线路上
        if (start.getLineId() == end.getLineId()) {
            return Math.abs(start.getIndex() - end.getIndex());
        }

        // 需要换乘的情况
        int minStations = Integer.MAX_VALUE;

        // 遍历所有可能的换乘路径
        for (Station transfer1 : allStations) {
            if (transfer1 != null && transfer1.getTransferTo() != null && transfer1.getTransferTo().size() > 0 && transfer1.getLineId() == start.getLineId()) {
                // 直接换乘到终点线路
                if (transfer1.getTransferTo().contains(end.getLineId())) {
                    Station transferEnd = findStationByNameAndLine(transfer1.getName(), end.getLineId());
                    if (transferEnd != null) {
                        int count = Math.abs(start.getIndex() - transfer1.getIndex()) +
                                Math.abs(end.getIndex() - transferEnd.getIndex());
                        minStations = Math.min(minStations, count);
                    }
                }

                // 需要二次换乘
                for (Station transfer2 : allStations) {
                    if (transfer2 != null && transfer2.getTransferTo() != null && transfer2.getTransferTo().size() > 0 &&
                            transfer2.getLineId() == end.getLineId() &&
                            !transfer2.getName().equals(transfer1.getName())) {
                        // 找到中间线路的换乘站
                        Station midTransfer1 = findStationByNameAndLine(transfer1.getName(), transfer1.getTransferTo().get(0));
                        Station midTransfer2 = findStationByNameAndLine(transfer2.getName(), midTransfer1 != null ? midTransfer1.getLineId() : -1);

                        if (midTransfer1 != null && midTransfer2 != null) {
                            int count = Math.abs(start.getIndex() - transfer1.getIndex()) +
                                    Math.abs(midTransfer1.getIndex() - midTransfer2.getIndex()) +
                                    Math.abs(end.getIndex() - transfer2.getIndex());
                            minStations = Math.min(minStations, count);
                        }
                    }
                }
            }
        }

        return minStations == Integer.MAX_VALUE ? 0 : minStations;
    }

    private Station findStationByNameAndLine(String name, int lineId) {
        if (allStations == null) {
            return null;
        }
        for (Station station : allStations) {
            if (station != null && station.getName().equals(name) && station.getLineId() == lineId) {
                return station;
            }
        }
        return null;
    }

    private void setupSpinners() {
        if (allStations == null) {
            return;
        }

        List<String> stationNames = new ArrayList<>();
        for (Station station : allStations) {
            stationNames.add(station.getName() + "(" + allLines.get(station.getLineId() - 1).getName() + ")");
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<>(
                this,
                android.R.layout.simple_spinner_item,
                stationNames
        );
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spinnerStart.setAdapter(adapter);
        spinnerEnd.setAdapter(adapter);
    }

    private void setupListeners() {
        buttonCalculate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calculateFare();
            }
        });

        buttonBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "购票成功!", Toast.LENGTH_SHORT).show();
                resetForm();
            }
        });
    }

    private void resetForm() {
        spinnerStart.setSelection(0);
        spinnerEnd.setSelection(0);
        editTextTickets.setText("");
        textViewAmount.setText("");
        buttonBuy.setEnabled(false);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context=".MainActivity">

    <Spinner
        android:id="@+id/spinnerStart"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="选择起始站" />

    <Spinner
        android:id="@+id/spinnerEnd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="选择终点站" />

    <EditText
        android:id="@+id/editTextTickets"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="购买张数"
        android:inputType="number"/>

    <TextView
        android:id="@+id/textViewAmount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="总金额:0元"
        android:textSize="18sp"
        android:layout_marginTop="16dp"/>

    <Button
        android:id="@+id/buttonCalculate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="计算票价" />

    <Button
        android:id="@+id/buttonBuy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="立即购票"
        android:enabled="false" />

</androidx.appcompat.widget.LinearLayoutCompat>

posted @ 2025-03-14 21:42  花落水无痕  阅读(27)  评论(0)    收藏  举报