软工随笔(软件工程日报)
今天完成了一个地铁售票计算票价的android app;
代码如下:
package com.example.subwayapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
private Spinner startStationSpinner, endStationSpinner;
private EditText ticketCountEditText;
private Button buyTicketButton;
private TextView resultTextView;
// 石家庄地铁线路信息
private static final List<String> LINE_1 = List.of("西王", "时光街", "长城桥", "和平医院", "烈士陵园", "新百广场", "解放广场", "平安大街", "北国商城", "博物院", "体育场", "北宋", "谈固", "朝晖桥", "白佛", "留村", "火炬广场", "石家庄东站", "南村", "洨河大道", "西庄", "东庄", "会展中心", "商务中心", "园博园", "福泽");
private static final List<String> LINE_2 = List.of("嘉华", "南位", "塔坛", "塔谈南", "石家庄站", "元村", "欧韵公园", "槐中路", "裕华路", "北国商城", "长安公园", "建和桥", "义堂", "庄窠·铁道大学", "柳辛庄");
private static final List<String> LINE_3 = List.of("西三庄", "高柱", "柏林庄", "市庄", "市二中", "新百广场", "东里", "槐安桥", "西三教", "石家庄站", "汇通路", "孙村", "塔冢", "东王", "南王", "位同", "东二环南路", "西仰陵", "中仰陵", "南豆", "太行南大街", "东乡");
private static final Map<String, List<String>> STATION_LINES = new HashMap<>();
private static final Map<String, List<String>> GRAPH = new HashMap<>();
static {
// 初始化站点和线路的映射
for (String station : LINE_1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
STATION_LINES.computeIfAbsent(station, k -> new ArrayList<>()).add("1号线");
}
}
for (String station : LINE_2) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
STATION_LINES.computeIfAbsent(station, k -> new ArrayList<>()).add("2号线");
}
}
for (String station : LINE_3) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
STATION_LINES.computeIfAbsent(station, k -> new ArrayList<>()).add("3号线");
}
}
// 构建图
buildGraph(LINE_1);
buildGraph(LINE_2);
buildGraph(LINE_3);
}
private static void buildGraph(List<String> line) {
for (int i = 0; i < line.size(); i++) {
String station = line.get(i);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
GRAPH.computeIfAbsent(station, k -> new ArrayList<>());
}
if (i > 0) {
GRAPH.get(station).add(line.get(i - 1));
}
if (i < line.size() - 1) {
GRAPH.get(station).add(line.get(i + 1));
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startStationSpinner = findViewById(R.id.startStationSpinner);
endStationSpinner = findViewById(R.id.endStationSpinner);
ticketCountEditText = findViewById(R.id.ticketCountEditText);
buyTicketButton = findViewById(R.id.buyTicketButton);
resultTextView = findViewById(R.id.resultTextView);
// 获取所有站点
List<String> allStations = new ArrayList<>();
allStations.addAll(LINE_1);
allStations.addAll(LINE_2);
allStations.addAll(LINE_3);
// 去除重复站点
Set<String> uniqueStations = new HashSet<>(allStations);
List<String> stationList = new ArrayList<>(uniqueStations);
// 创建适配器
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, stationList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// 设置适配器
startStationSpinner.setAdapter(adapter);
endStationSpinner.setAdapter(adapter);
buyTicketButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String startStation = startStationSpinner.getSelectedItem().toString();
String endStation = endStationSpinner.getSelectedItem().toString();
String ticketCountStr = ticketCountEditText.getText().toString();
if (ticketCountStr.isEmpty()) {
Toast.makeText(MainActivity.this, "请输入购票数量", Toast.LENGTH_SHORT).show();
return;
}
int ticketCount = Integer.parseInt(ticketCountStr);
int stationCount = calculateStationCount(startStation, endStation);
int price = calculatePrice(stationCount);
int totalPrice = price * ticketCount;
resultTextView.setText("购票成功,总金额:" + totalPrice + " 元");
}
});
}
private int calculateStationCount(String startStation, String endStation) {
// 使用广度优先搜索(BFS)计算最短路径
Queue<String> queue = new LinkedList<>();
Set<String> visited = new HashSet<>();
Map<String, Integer> distance = new HashMap<>();
queue.add(startStation);
visited.add(startStation);
distance.put(startStation, 0);
while (!queue.isEmpty()) {
String current = queue.poll();
if (current.equals(endStation)) {
return distance.get(current);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
for (String neighbor : GRAPH.getOrDefault(current, new ArrayList<>())) {
if (!visited.contains(neighbor)) {
queue.add(neighbor);
visited.add(neighbor);
distance.put(neighbor, distance.get(current) + 1);
}
}
}
}
return 0;
}
private int calculatePrice(int stationCount) {
return (int) Math.ceil((double) stationCount / 3);
}
}
下面是layout部分:
<Spinner
android:id="@+id/startStationSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18dp"/>
<Spinner
android:id="@+id/endStationSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="购票数量:"
android:textSize="18sp" />
<EditText
android:id="@+id/ticketCountEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
<Button
android:id="@+id/buyTicketButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="购票" />
<TextView
android:id="@+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />

浙公网安备 33010602011771号