建立Android软件前的准备:安装Android studio(JDK已安装)
安装Android studio的教程在网络很多,很容易查到。
安装后,添加新项目,语言选择java。
在java文件夹里创建:
Station.java:
public class Station {
private String name;
private List
public Station(String name) {this.name = name;this.lines = new ArrayList<>();
}public String getName() {return name;
}public void addLine(String line) {if (!lines.contains(line)) {lines.add(line);}
}@Override
public boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Station station = (Station) o;return name.equals(station.name);
}@Override
public int hashCode() {return name.hashCode();
}
}
Line.java:
public class Line {
private String name;
private List
public Line(String name) {this.name = name;this.stations = new ArrayList<>();
}public void addStation(Station station) {stations.add(station);station.addLine(this.name);
}public List<Station> getStations() {return stations;
}public String getName() {return name;
}
}
MetroSystem.java:
import java.util.*;
public class MetroSystem {
private Map<String, Station> stations = new HashMap<>();
private List
private Map<Station, List
public void addLine(String lineName, List<String> stationNames) {Line line = new Line(lineName);List<Station> lineStations = new ArrayList<>();for (String name : stationNames) {Station station = stations.get(name);if (station == null) {station = new Station(name);stations.put(name, station);}station.addLine(lineName);line.addStation(station);lineStations.add(station);}lines.add(line);buildAdjacency(lineStations);
}private void buildAdjacency(List<Station> stations) {for (int i = 0; i < stations.size(); i++) {Station current = stations.get(i);adjacencyList.putIfAbsent(current, new ArrayList<>());if (i > 0) {Station prev = stations.get(i - 1);adjacencyList.get(current).add(prev);adjacencyList.get(prev).add(current); // 双向连接}}
}public int calculateFare(String start, String end) {Station startStation = stations.get(start);Station endStation = stations.get(end);if (startStation == null || endStation == null) return -1;int stops = bfs(startStation, endStation);if (stops == -1) return -1;return (int) Math.ceil(stops / 3.0);
}private int bfs(Station start, Station end) {Queue<Station> queue = new LinkedList<>();Map<Station, Integer> distances = new HashMap<>();queue.add(start);distances.put(start, 0);while (!queue.isEmpty()) {Station current = queue.poll();if (current.equals(end)) return distances.get(current);for (Station neighbor : adjacencyList.getOrDefault(current, new ArrayList<>())) {if (!distances.containsKey(neighbor)) {distances.put(neighbor, distances.get(current) + 1);queue.add(neighbor);}}}return -1;
}public List<String> getStationNames() {return new ArrayList<>(stations.keySet());
}
}
在activity_main.xml文件里:
<AutoCompleteTextViewandroid:id="@+id/startStation"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="起始站"/><AutoCompleteTextViewandroid:id="@+id/endStation"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="终点站"/><EditTextandroid:id="@+id/ticketCount"android:layout_width="match_parent"android:layout_height="wrap_content"android:inputType="number"android:hint="票数"/><Buttonandroid:id="@+id/calculateButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="计算费用"/><TextViewandroid:id="@+id/result"android:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="18sp"/>
MainActivity.java:
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
private MetroSystem metroSystem = new MetroSystem();
@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 初始化地铁线路(示例数据)metroSystem.addLine("1号线", Arrays.asList("A站", "B站", "换乘站", "D站"));metroSystem.addLine("2号线", Arrays.asList("E站", "换乘站", "G站"));setupUI();
}private void setupUI() {// 自动补全适配器ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line,metroSystem.getStationNames());AutoCompleteTextView startStation = findViewById(R.id.startStation);AutoCompleteTextView endStation = findViewById(R.id.endStation);startStation.setAdapter(adapter);endStation.setAdapter(adapter);Button calculateBtn = findViewById(R.id.calculateButton);calculateBtn.setOnClickListener(v -> calculate());
}private void calculate() {String start = ((AutoCompleteTextView) findViewById(R.id.startStation)).getText().toString();String end = ((AutoCompleteTextView) findViewById(R.id.endStation)).getText().toString();String countStr = ((EditText) findViewById(R.id.ticketCount)).getText().toString();if (start.isEmpty() || end.isEmpty() || countStr.isEmpty()) {showResult("请填写所有字段");return;}try {int count = Integer.parseInt(countStr);int fare = metroSystem.calculateFare(start, end);if (fare == -1) showResult("路线不存在");else showResult("总金额:" + (fare * count) + "元");} catch (NumberFormatException e) {showResult("请输入有效票数");}
}private void showResult(String msg) {((TextView) findViewById(R.id.result)).setText(msg);
}
}
通过这个可以建立起一个非常简单的应用(仍有些内容未完成)。
然后可以通过Android studio自带的虚拟机测试,也可以连接直接WiFi到手机上,在手机上操作。