软工随笔9

今天编程,挺顺利的。
写了几百行代码,最终写了个简单的app出来
`

<!-- 起点站输入框 -->
<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/startStationLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="起点站">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/startStation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>

<!-- 终点站输入框 -->
<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/endStationLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="终点站">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/endStation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>

<ImageView
    android:id="@+id/ditieImage"
    android:layout_width="wrap_content"
    android:layout_height="278dp"
    android:scaleType="centerCrop"
    android:src="@drawable/ditie" />

<!-- 总站数显示 -->
<TextView
    android:id="@+id/totalStations"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="总站数: 0"
    android:textSize="16sp"
    android:layout_marginTop="16dp" />

<!-- 购票价格显示 -->
<TextView
    android:id="@+id/ticketPrice"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="购票价格: 0元"
    android:textSize="16sp"
    android:layout_marginTop="8dp" />

<!-- 购票按钮 -->
<Button
    android:id="@+id/buyButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="购票"
    android:layout_marginTop="16dp"
    android:backgroundTint="@android:color/holo_blue_dark"
    android:textColor="@android:color/white" />

<!-- 结果消息 -->
<TextView
    android:id="@+id/resultMessage"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text=""
    android:textColor="@android:color/holo_green_dark"
    android:textSize="18sp"
    android:layout_marginTop="16dp" />

package com.example.a02;

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.*;

public class MainActivity extends AppCompatActivity {

private EditText startStationEdit;
private EditText endStationEdit;
private TextView totalStationsText;
private TextView ticketPriceText;
private Button buyButton;
private TextView resultMessage;

// 地铁线路数据
private Map<String, List<String>> subwayLines;
private Set<String> transferStations;

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

    // 初始化视图
    startStationEdit = findViewById(R.id.startStation);
    endStationEdit = findViewById(R.id.endStation);
    totalStationsText = findViewById(R.id.totalStations);
    ticketPriceText = findViewById(R.id.ticketPrice);
    buyButton = findViewById(R.id.buyButton);
    resultMessage = findViewById(R.id.resultMessage);

    // 初始化地铁线路数据
    initSubwayData();

    // 设置按钮点击事件
    buyButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            calculateAndDisplayTicketPrice();
        }
    });
}

// 初始化地铁线路数据
private void initSubwayData() {
    subwayLines = new HashMap<>();
    transferStations = new HashSet<>();

    // 一号线
    List<String> line1 = Arrays.asList(
            "福泽", "园博园", "商务中心", "会展中心", "东庄", "西庄", "洨河大道", "南村", "石家庄东站", "火炬广场", "留村", "白佛", "朝晖桥", "谈固", "北宋", "体育场", "博物院", "北国商城", "平安大街", "解放广场", "新百广场", "烈士陵园", "和平医院", "长城桥", "时光街", "西王"
    );
    subwayLines.put("1号线", line1);

    // 二号线
    List<String> line2 = Arrays.asList(
            "柳辛庄", "庄窠·铁道大学", "义堂", "建和桥", "长安公园", "北国商城", "裕华路", "槐中路", "欧韵公园", "元村", "石家庄站", "塔坛", "仓丰路留村", "南位", "嘉华路"
    );
    subwayLines.put("2号线", line2);

    // 三号线
    List<String> line3 = Arrays.asList(
            "乐乡", "太行南大街", "南豆", "中仰陵", "西仰陵", "东二环南路", "位同", "南王", "东王", "塔冢", "孙村", "汇通路", "石家庄站", "西三教", "槐安桥", "东里", "新百广场", "市二中", "市庄", "柏林庄", "高柱", "西三庄"
    );
    subwayLines.put("3号线", line3);

    // 中转站
    transferStations.add("北国商城");
    transferStations.add("新百广场");
    transferStations.add("石家庄站");
}

// 计算并显示票价
private void calculateAndDisplayTicketPrice() {
    String startStation = startStationEdit.getText().toString();
    String endStation = endStationEdit.getText().toString();

    if (startStation.isEmpty() || endStation.isEmpty()) {
        resultMessage.setText("请输入起点站和终点站");
        return;
    }

    // 计算总站数
    int totalStops = calculateTotalStops(startStation, endStation);
    if (totalStops == -1) {
        resultMessage.setText("站点输入错误,请检查");
        return;
    }

    // 计算票价
    int price = (totalStops + 2) / 3; // 每3站1元,不足3站按3站算

    // 显示结果
    totalStationsText.setText("总站数: " + totalStops);
    ticketPriceText.setText("购票价格: " + price + "元");
    resultMessage.setText("成功");
}

// 计算两个站点之间的总站数
private int calculateTotalStops(String start, String end) {
    // 找到起点站和终点站所在的线路
    String startLine = findLineByStation(start);
    String endLine = findLineByStation(end);

    if (startLine == null || endLine == null) {
        return -1; // 站点不存在
    }

    // 如果起点和终点在同一条线路上
    if (startLine.equals(endLine)) {
        return Math.abs(subwayLines.get(startLine).indexOf(start) - subwayLines.get(endLine).indexOf(end));
    }

    // 如果起点和终点在不同线路上,找到中转站
    for (String transfer : transferStations) {
        if (subwayLines.get(startLine).contains(transfer) && subwayLines.get(endLine).contains(transfer)) {
            int stops1 = Math.abs(subwayLines.get(startLine).indexOf(start) - subwayLines.get(startLine).indexOf(transfer));
            int stops2 = Math.abs(subwayLines.get(endLine).indexOf(end) - subwayLines.get(endLine).indexOf(transfer));
            return stops1 + stops2;
        }
    }

    return -1; // 没有找到中转站
}

// 根据站点找到所属线路
private String findLineByStation(String station) {
    for (Map.Entry<String, List<String>> entry : subwayLines.entrySet()) {
        if (entry.getValue().contains(station)) {
            return entry.getKey();
        }
    }
    return null; // 站点不存在
}

}`

posted @ 2025-03-14 18:46  Thanatos。syts  阅读(9)  评论(0)    收藏  举报