学习进度条

今日所花时间:一小时
今日代码量:100行
博客量:一篇 了解到的知识点:
深入学习androidStudio 完成课堂测试,优化地铁购票APP
本次这个测试没有连接数据库。
MainActivity代码展示

package com.example.subway;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class MainActivity extends AppCompatActivity {

    private Spinner startStation, endStation;
    private EditText ticketQuantity;
    private Button buyButton;
    private TextView resultText;

    // 存储线路和对应站点的映射
    private Map<String, List<String>> lineStationsMap = new HashMap<>();
    // 存储换乘站点和其所在线路的映射
    private Map<String, Set<String>> transferStations = new HashMap<>();
    // 存储站点到其所在线路的映射
    private Map<String, Set<String>> stationToLinesMap = new HashMap<>();

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

        startStation = findViewById(R.id.startStation);
        endStation = findViewById(R.id.endStation);
        ticketQuantity = findViewById(R.id.ticketQuantity);
        buyButton = findViewById(R.id.buyButton);
        resultText = findViewById(R.id.resultText);

        // 初始化线路和站点信息
        initializeLineStations();
        // 初始化换乘站点信息
        initializeTransferStations();
        // 构建站点到线路的映射
        buildStationToLinesMap();

        // 获取所有有效的站点
        List<String> allStations = getAllStations();
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, allStations);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        startStation.setAdapter(adapter);
        endStation.setAdapter(adapter);

        buyButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calculateAndDisplayTicketPrice();
            }
        });
    }

    private void initializeLineStations() {
        lineStationsMap.put("1号线", List.of("西王", "时光街", "长城桥", "和平医院", "烈士陵园", "新百广场", "解放广场", "平安大街", "北国商城", "博物馆", "体育场", "北宋", "谈固", "朝晖桥", "白佛", "留村", "火炬广场", "石家庄东站", "南村", "交河大道", "西庄", "东庄", "会展中心", "商务中心", "园博园", "福泽"));
        lineStationsMap.put("2号线", List.of("柳辛庄", "庄窠-铁道大学", "义堂", "建和桥", "长安公园", "北国商城", "裕华路", "槐中路", "欧韵公园", "元村", "石家庄站", "塔谈", "仓丰路留村", "南位", "嘉华路"));
        lineStationsMap.put("3号线", List.of("西三庄", "高柱", "柏林庄", "市庄", "市二中", "新百广场", "东里", "槐安路", "西三教", "石家庄站", "汇通路", "孙村", "塔冢", "东王", "南王", "位同", "东二环南路", "西仰陵", "中仰陵", "南豆", "太行南大街", "乐乡"));
    }

    private void initializeTransferStations() {
        // 添加换乘站点及其所在线路
        addTransferStation("新百广场", "1号线", "2号线");
        addTransferStation("北国商城", "1号线", "2号线");
        addTransferStation("石家庄站", "2号线", "3号线");
    }

    private void addTransferStation(String station, String... lines) {
        Set<String> lineSet = new HashSet<>();
        for (String line : lines) {
            lineSet.add(line);
        }
        transferStations.put(station, lineSet);
    }

    private void buildStationToLinesMap() {
        for (Map.Entry<String, List<String>> entry : lineStationsMap.entrySet()) {
            String line = entry.getKey();
            List<String> stations = entry.getValue();
            for (String station : stations) {
                stationToLinesMap.computeIfAbsent(station, k -> new HashSet<>()).add(line);
            }
        }
    }

    private List<String> getAllStations() {
        Set<String> allStationsSet = new HashSet<>();
        for (List<String> stations : lineStationsMap.values()) {
            allStationsSet.addAll(stations);
        }
        return new ArrayList<>(allStationsSet);
    }

    private void calculateAndDisplayTicketPrice() {
        String start = startStation.getSelectedItem().toString();
        String end = endStation.getSelectedItem().toString();
        String quantityStr = ticketQuantity.getText().toString();

        if (start.isEmpty() || end.isEmpty() || quantityStr.isEmpty()) {
            resultText.setText("请输入完整信息");
            return;
        }

        int quantity;
        try {
            quantity = Integer.parseInt(quantityStr);
            if (quantity <= 0) {
                resultText.setText("购票数量必须为正整数");
                return;
            }
        } catch (NumberFormatException e) {
            resultText.setText("购票数量必须为正整数");
            return;
        }

        int price = calculatePrice(start, end) * quantity;
        resultText.setText("购票成功,金额: " + price + "元");
    }

    private int calculatePrice(String start, String end) {
        Set<String> startLines = stationToLinesMap.get(start);
        Set<String> endLines = stationToLinesMap.get(end);

        if (startLines == null || endLines == null) {
            return 0; // 站点不存在
        }

        // 检查是否在同一条线路上
        for (String startLine : startLines) {
            for (String endLine : endLines) {
                if (startLine.equals(endLine)) {
                    List<String> stationsOnLine = lineStationsMap.get(startLine);
                    int startIndex = stationsOnLine.indexOf(start);
                    int endIndex = stationsOnLine.indexOf(end);
                    int stationCount = Math.abs(endIndex - startIndex);
                    return (int) Math.ceil(stationCount / 3.0);
                }
            }
        }

        // 处理换乘情况
        for (Map.Entry<String, Set<String>> entry : transferStations.entrySet()) {
            String transferStation = entry.getKey();
            Set<String> transferLines = entry.getValue();
            for (String startLine : startLines) {
                for (String endLine : endLines) {
                    if (transferLines.contains(startLine) && transferLines.contains(endLine)) {
                        int startToTransfer = calculatePrice(start, transferStation);
                        int transferToEnd = calculatePrice(transferStation, end);
                        return startToTransfer + transferToEnd;
                    }
                }
            }
        }

        return 0; // 无法换乘
    }
}

activity_main.xml代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <!-- 新增的标题TextView -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="石家庄地铁购票"
        android:textSize="24sp"
        android:textStyle="bold"
        android:gravity="center"
        android:paddingBottom="16dp" />

    <Spinner
        android:id="@+id/startStation"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:minHeight="48dp"
        android:padding="12dp" />

    <Spinner
        android:id="@+id/endStation"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:minHeight="48dp"
        android:padding="12dp" />

    <EditText
        android:id="@+id/ticketQuantity"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:hint="购票数量"
        android:inputType="number"
        android:minHeight="48dp"
        android:padding="12dp" />

    <Button
        android:id="@+id/buyButton"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:text="立即购票"
        android:minHeight="48dp"
        android:padding="12dp" />

    <TextView
        android:id="@+id/resultText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="18sp"
        android:gravity="center"
        android:padding="12dp" />
</LinearLayout>

通过上述代码基本完成课堂测试实现。

posted @ 2025-03-14 19:33  haoyinuo  阅读(17)  评论(0)    收藏  举报