第一次结对开发作业——手机端

  本博客为第一次结对开发作业——手机端的代码展示。

以下为代码:

1.changeline

package Bean;


public class changeline {
private String ID1;
private String ID2;
private String ChangeStopName;

public changeline(){

}
public String getID1() {
return ID1;
}

public void setID1(String ID1) {
this.ID1 = ID1;
}

public String getID2() {
return ID2;
}

public void setID2(String ID2) {
this.ID2 = ID2;
}

public String getChangeStopName() {
return ChangeStopName;
}

public void setChangeStopName(String changeStopName) {
ChangeStopName = changeStopName;
}
}

2.firstline

package Bean;

public class firstline {
private String StopID;
private String StopName;
private String Line;

public firstline() {
}
public firstline(String StopID, String StopName,String Line) {
this.StopID=StopID;
this.StopName=StopName;
this.Line=Line;
}


public String getStopID() {
return StopID;
}

public void setStopID(String stopID) {
StopID = stopID;
}

public String getStopName() {
return StopName;
}

public void setStopName(String stopName) {
StopName = stopName;
}

public String getLine() {
return Line;
}

public void setLine(String line) {
Line = line;
}
}

3.Line

package Bean;
import java.util.ArrayList;

/*
*
*/
public class Line
{
private String startstopname;//起始站站名
private String endstopname;//结束站站名
private int startstopID;// 起始站站编号
private int endstopID;//结束站站编号
private String startline1=null;//起始点的第一条线
private String startline2=null;//起始点第二条线
private String endline1=null;//结束点的第一条线
private String endline2=null;//结束点的第二条线
private ArrayList<String> array = new ArrayList<String>();//存放过程站点
private int originline;//起始站所在线路
private int finishline;//结束点所在线路
private String middlestop;//中转站站名
private String temporarystartstopname;//临时起始站点名称
private String temporaryendstopname;//临时终点站点名称
private int temporaryline=0;//临时线路
public Line() {

}
public int getTemporaryline() {
return temporaryline;
}
public void setTemporaryline(int temporaryline) {
this.temporaryline = temporaryline;
}
public String getTemporarystartstopname() {
return temporarystartstopname;
}
public void setTemporarystartstopname(String temporarystartstopname) {
this.temporarystartstopname = temporarystartstopname;
}
public String getTemporaryendstopname() {
return temporaryendstopname;
}
public void setTemporaryendstopname(String temporaryendstopname) {
this.temporaryendstopname = temporaryendstopname;
}
public String getMiddlestop() {
return middlestop;
}
public void setMiddlestop(String middlestop) {
this.middlestop = middlestop;
}
public int getOriginline() {
return originline;
}
public void setOriginline(int originline) {
this.originline = originline;
}
public int getFinishline() {
return finishline;
}
public void setFinishline(int finishline) {
this.finishline = finishline;
}
public ArrayList<String> getArray() {
return array;
}
public void setArray(ArrayList<String> array) {
this.array = array;
}
public String getStartline1() {
return startline1;
}
public void setStartline1(String startline1) {
this.startline1 = startline1;
}
public String getStartline2() {
return startline2;
}
public void setStartline2(String startline2) {
this.startline2 = startline2;
}
public String getEndline1() {
return endline1;
}
public void setEndline1(String endline1) {
this.endline1 = endline1;
}
public String getEndline2() {
return endline2;
}
public void setEndline2(String endline2) {
this.endline2 = endline2;
}
public String getStartstopname() {
return startstopname;
}
public void setStartstopname(String startstopname) {
this.startstopname = startstopname;
}
public String getEndstopname() {
return endstopname;
}
public void setEndstopname(String endstopname) {
this.endstopname = endstopname;
}
public int getStartstopID() {
return startstopID;
}
public void setStartstopID(int startstopID) {
this.startstopID = startstopID;
}
public int getEndstopID() {
return endstopID;
}
public void setEndstopID(int endstopID) {
this.endstopID = endstopID;
}


}

4.DianQueryActivity

package com.example.subway;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.sql.SQLException;
import java.util.ArrayList;

import Bean.Line;
import Bean.firstline;
import Dao.UserDao;

public class DianQueryActivity extends AppCompatActivity implements View.OnClickListener {
private EditText startName,EndName;

String desc;
Button btn_submit;
private TextView tv;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dian_query);
startName=findViewById(R.id.startName);
EndName=findViewById(R.id.EndName);
findViewById(R.id.btn_submit).setOnClickListener(this);
tv = findViewById(R.id.find_id_tv);

}
final Handler hand1 = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
tv.setText(desc);
}
};

@Override
public void onClick(View v) {
new Thread() {
@Override
public void run() {
Line line = new Line();
ArrayList<String> array = new ArrayList<String>();
String start=startName.getText().toString();
String End=EndName.getText().toString();
line.setStartstopname(start);
line.setEndstopname(End);
UserDao dao=new UserDao();
try {
int x=dao.CheckAll(line);
if (x == 1) {
array = dao.Connectonlyoneline(line);
} else if (x == 2) {
array = dao.Connecttwoline(line);
} else if (x == 3) {
array = dao.Connectthreeline(line);
}
} catch (SQLException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
desc="";
for (int i = 0; i < array.size(); i++) {
if(i==0) {
desc = desc + "该路线为:" + array.get(i)
+ "\n";
}
else{
desc = desc + "-->" +array.get(i)
+ "\n";
}
}
int msg=1;
hand1.sendEmptyMessage(msg);

}
}.start();
}
}

5.LineQueryActivity

package com.example.subway;



import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import Bean.firstline;
import Dao.UserDao;

import java.util.ArrayList;

public class LineQueryActivity extends AppCompatActivity implements View.OnClickListener {
private EditText et;
private String desc;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_line_query);
findViewById(R.id.find_id_But).setOnClickListener(this);
et = findViewById(R.id.find_id_ET);
tv = findViewById(R.id.find_id_tv);
}
final Handler hand1 = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
tv.setText(desc);
}
};
@Override
public void onClick(View view) {
new Thread() {
@Override
public void run() {
String Line=et.getText().toString();

UserDao ab=new UserDao();
ArrayList<firstline> p=ab.findLine(Line);
desc="";
for (int i = 0; i < p.size(); i++) {
desc = desc + "该路线为:" + p.get(i).getStopName()
+ "\n";
}
int msg=1;
hand1.sendEmptyMessage(msg);

}
}.start();
}
}

6.MainActivity

package com.example.subway;

import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

;import androidx.appcompat.app.AppCompatActivity;

import util.Commonutil;



public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private Button btn_teacher;
private String mDatabaseName;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_enter).setOnClickListener(this);
mDatabaseName = getFilesDir() + "/test.db";

btn_teacher=findViewById(R.id.btn_teacher);
btn_teacher.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i5 = new Intent(MainActivity.this,DianQueryActivity.class);
startActivity(i5);
}
});
}


@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_enter:
//创建或者打开数据库,如果数据库不存在就创建,如果存在的话就打开数据库
SQLiteDatabase db = openOrCreateDatabase(mDatabaseName, Context.MODE_PRIVATE,null);
Commonutil.showLongMsg(this,"欢迎!");
Intent intent = new Intent(this, LineQueryActivity.class);
startActivity(intent);
break;
}
}
}

7.UserDao

package Dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import Bean.Line;
import Bean.firstline;
import util.MysqlHelper;

public class UserDao extends MysqlHelper {
ArrayList<String> array=new ArrayList<String>();
public ArrayList<firstline> findLine(String Line) {
ArrayList<firstline> item = new ArrayList();
try{
int i = 0;
getConnection();
String sql = "select * from firstline where Line=?";
pStmt = conn.prepareStatement(sql);
pStmt.setString(1, String.valueOf(Line));
rs = pStmt.executeQuery();
while(rs.next()){
firstline f= new firstline(
rs.getString("StopID"),
rs.getString("StopName"),
rs.getString("Line")
);
item.add(f);
}
return item;
}catch (Exception e){
e.printStackTrace();
}finally {
closeAll();
}
return null;
}
public ArrayList<String> Connectonlyoneline(Line line) throws ClassNotFoundException
{
ArrayList<String> array1=new ArrayList<String>();
int c=0;
try {
//连接数据库
getConnection();
String sql="SELECT * FROM firstline f1 JOIN firstline f2 on f1.Line=f2.Line WHERE f1.StopName='"+line.getStartstopname()+"' AND f2.StopName='"+line.getEndstopname()+"'";

pStmt = conn.prepareStatement(sql);
//保证起始站(同时有可能是中转站)与终点站(同时有可能是中转站)在同一条线路上
rs=pStmt.executeQuery(sql);
while(rs.next()) {
line.setTemporaryline(rs.getInt("Line"));
}
//准备数据库查询:将起始站和终点站的中间站
System.out.println(line.getTemporaryline());
array.add("乘坐"+line.getTemporaryline()+"号线");
c=select(line); //得到起始站与终点站的顺序
System.out.println(c);
if(c==1)
{//起始站编号小于终点站,即起始站在前,终点站在后
getConnection();
String sql1="select * from firstline where StopID>=(select StopID from firstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from firstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 找到这样的一些站:它们的ID号大于起始站的ID号,
* 它们的ID号小于结束站的ID号,并且它们都是同一条线上的站点
*/
pStmt = conn.prepareStatement(sql1);
ResultSet rs1 = pStmt.executeQuery(sql1);
while(rs1.next())
{//遍历结果集
array.add(rs1.getString("StopName"));
}
rs1.close();
}
else if(c==2)
{//起始站编号大于终点站,即起始站在后,终点站在前
getConnection();
String sql2="select * from firstline where StopID>=(select StopID from firstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from firstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"')";
pStmt = conn.prepareStatement(sql2);
ResultSet rs2 = pStmt.executeQuery(sql2);
while(rs2.next())
{
array1.add(rs2.getString("StopName"));
}
for(int i=0;i<array1.size();i++)
{//测试是否有值
System.out.print(array1.get(i)+" ");
}
for(int j=array1.size()-1;j>=0;j--)
{//将站名倒序传入
array.add(array1.get(j));
}

rs2.close();
}
closeAll();
}catch(SQLException e) {
e.printStackTrace();
}finally {
line.setArray(array);//似乎无意义,在下面已经传回array
for(int i=0;i<array.size();i++) {
System.out.println(array.get(i)+" ");
}
}
return array;
}

/*
* 先不着急,起始站/终点站是换乘站的情况最后考虑
* 首先判断起始站或结束站是否为中转站
*/
public void JudgeChangeStop(Line line) throws ClassNotFoundException {
try {
getConnection();
String sql="select * from changeline";
pStmt = conn.prepareStatement(sql);
ResultSet rs = pStmt.executeQuery(sql);
while(rs.next()) {
if(line.getStartstopname().equals(rs.getString("ChangeStopName")))
{
line.setStartline1(rs.getString("ID1"));
line.setStartline2(rs.getString("ID2"));
}
if(line.getEndstopname().equals(rs.getString("ChangeStopName")))
{
line.setEndline1(rs.getString("ID1"));
line.setEndline2(rs.getString("ID2"));
}
}
closeAll();
}catch(SQLException e) {
throw new RuntimeException(e);
}finally {
if(line.getStartline1()==null)
{

}
}
}
/*
* 看站点顺序
*/
public int select(Line line) throws ClassNotFoundException {
int i=0;//返回值,1为顺序(数据库中),2为逆序(数据库中)
try {
getConnection();

String sql="select * from firstline where StopName='"+line.getStartstopname()+"'and Line='"+line.getTemporaryline()+"' or StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"'";
/*
* 找到与起始站名称相同,结束站名称相同,且同时属于同一条线的两个站
*/
pStmt = conn.prepareStatement(sql);
ResultSet rs = pStmt.executeQuery(sql);
while(rs.next()) {
if(line.getStartstopname().equals(rs.getString("StopName")))
{
line.setStartstopID(rs.getInt("StopID"));
}
if(line.getEndstopname().equals(rs.getString("StopName"))) {
line.setEndstopID(rs.getInt("StopID"));
}
}
closeAll();
}catch(SQLException e) {
throw new RuntimeException(e);
}
System.out.println(line.getStartstopID()+" "+line.getEndstopID());
if(line.getStartstopID()<line.getEndstopID()) {
i=1;
return i;
}
else {
i=2;
return i;
}
}
/*
* 判断是否为同一条线
*/
public int same(Line line) throws ClassNotFoundException {
int checksameline=0;//同一条线
try {
getConnection();

String sql="select * from firstline where StopName='"+line.getStartstopname()+"' or StopName='"+line.getEndstopname()+"'";
/*
* 同上,但用不着判断是否属于同一线路(本判断只存在于起始点与结束点是普通点)
*/
pStmt = conn.prepareStatement(sql);
ResultSet rs = pStmt.executeQuery(sql);
while(rs.next()) {
if(line.getStartstopname().equals(rs.getString("StopName")))
{
line.setOriginline(rs.getInt("Line"));
}
if(line.getEndstopname().equals(rs.getString("StopName"))) {
line.setFinishline(rs.getInt("Line"));
}
}
}catch(SQLException e) {
throw new RuntimeException(e);
}
if(line.getFinishline()==line.getOriginline()) {//是同一条线
checksameline=1;
return checksameline;
}
else {//不是同一条线
checksameline=2;
return checksameline;
}
}
/*
* 两条不同的、有交点的线路
*/
public ArrayList<String> Connecttwoline(Line line) throws SQLException, ClassNotFoundException{
int c=0;

getConnection();
String sql="select * from changeline where ID1='"+line.getOriginline()+"' and ID2='"+line.getFinishline()+"'";
pStmt = conn.prepareStatement(sql);
same(line);
//根据交点线路查询中转站
ResultSet rs = pStmt.executeQuery(sql);
while(rs.next()) {
line.setMiddlestop(rs.getString("ChangeStopName"));
}
rs.close();
line.setTemporarystartstopname(line.getStartstopname());
//记录当前起始点与结束点
line.setTemporaryendstopname(line.getEndstopname());
//同上
getConnection();
String sql1="select * from firstline where Line='"+line.getOriginline()+"' and StopName='"+line.getMiddlestop()+"'";
//根据起始线路和中转站名称标出中转站位置
pStmt = conn.prepareStatement(sql1);
ResultSet rs1 = pStmt.executeQuery(sql1);
while(rs1.next()) {
line.setEndstopname(rs1.getString("StopName"));
//起始点不变,结束点设置为中转点
}
rs1.close();
line.setStartstopname(line.getTemporarystartstopname());
line.setTemporaryline(line.getOriginline());
//将起始点和中转点所在线路一致的放入临时站线路
/*
*
*
*
*
*/
array.add("乘坐"+line.getOriginline()+"号线");
c=select(line); //得到起始站与终点站的顺序
if(c==1)
{//起始站编号小于终点站,即起始站在前,终点站在后
getConnection();
String sql2="select * from firstline where StopID>=(select StopID from firstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from firstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 找到这样的一些站:它们的ID号大于起始站的ID号,
* 它们的ID号小于结束站的ID号,并且它们都是同一条线上的站点
*/
pStmt = conn.prepareStatement(sql2);
ResultSet rs2 = pStmt.executeQuery(sql2);
while(rs2.next())
{//遍历结果集
array.add(rs2.getString("StopName"));
}
rs2.close();
}
else if(c==2)
{//起始站编号大于终点站,即起始站在后,终点站在前
getConnection();
ArrayList<String> array1=new ArrayList<String>();
String sql2="select * from firstline where StopID>=(select StopID from firstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from firstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 同上
*/
pStmt = conn.prepareStatement(sql2);
ResultSet rs2 = pStmt.executeQuery(sql2);
while(rs2.next())
{
array1.add(rs2.getString("StopName"));
}
for(int i=0;i<array1.size();i++)
{//测试是否有值
System.out.print(array1.get(i)+" ");
}
for(int j=array1.size()-1;j>=0;j--)
{//将站名倒序传入
array.add(array1.get(j));
}

rs2.close();
}
array.add("转乘"+line.getFinishline()+"号线");
/*
*
*
*
*
*/
line.setStartstopname(line.getMiddlestop());
//将起始点设为中转点
line.setEndstopname(line.getTemporaryendstopname());
//将结束点回归
line.setTemporaryline(line.getFinishline());
//将中转点和结束点所在线路一致的放入临时站线路
/*
*
*
*
*/
c=select(line); //得到起始站与终点站的顺序
if(c==1)
{//起始站编号小于终点站,即起始站在前,终点站在后
getConnection();
String sql2="select * from firstline where StopID>=(select StopID from firstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from firstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 找到这样的一些站:它们的ID号大于起始站的ID号,
* 它们的ID号小于结束站的ID号,并且它们都是同一条线上的站点
*/
pStmt = conn.prepareStatement(sql2);
ResultSet rs2 = pStmt.executeQuery(sql2);
while(rs2.next())
{//遍历结果集
array.add(rs2.getString("StopName"));
}
rs2.close();
}
else if(c==2)
{//起始站编号大于终点站,即起始站在后,终点站在前
getConnection();
ArrayList<String> array1=new ArrayList<String>();
String sql2="select * from firstline where StopID>=(select StopID from firstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from firstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 同上
*/
pStmt = conn.prepareStatement(sql2);
ResultSet rs2 = pStmt.executeQuery(sql2);
while(rs2.next())
{
array1.add(rs2.getString("StopName"));
}
for(int i=0;i<array1.size();i++)
{//测试是否有值
System.out.print(array1.get(i)+" ");
}
for(int j=array1.size()-1;j>=0;j--)
{//将站名倒序传入
array.add(array1.get(j));
}

rs2.close();
}
/*
*
*
*
*
*/
//由于array是全局变量,所以继续直接接受数据即可
return array;

}
/*
* 判断有无交点
*/
public int checklink(Line line) throws ClassNotFoundException {
int c=0;
try {
same(line);
getConnection();

String sql="select * from changeline where ID1='"+line.getOriginline()+"' and ID2='"+line.getFinishline()+"'";
pStmt = conn.prepareStatement(sql);
ResultSet rs=pStmt.executeQuery(sql);
while(rs.next()) {
if(rs.getString("ChangeStopName").equals("")) {

}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


return c;
}
/*
* 解决两条线之间没有换乘站,只能通过第三条线换乘
* 即A(普通站)->B(中转站)->C(中转站)->D(普通站)
*
*/
public ArrayList<String> Connectthreeline(Line line) throws ClassNotFoundException {
ArrayList<Integer> array1=new ArrayList<Integer>();
int c=0;
int min=0;
try
{
same(line);
getConnection();
String sql="SELECT * FROM changeline c1 JOIN changeline c2 ON c1.ID2 = c2.ID1 WHERE trim(c1.ChangeStopName) != '' AND trim(c2.ChangeStopName) != '' AND c1.ID1 = '"+line.getOriginline()+"' AND c2.ID2 = '"+line.getFinishline()+"'";
pStmt = conn.prepareStatement(sql);
ResultSet rs = pStmt.executeQuery(sql);
while(rs.next())
{
if(line.getOriginline()==rs.getInt("ID1"))
{
array1.add(rs.getInt("ID2"));
}
}
rs.close();
line.setTemporarystartstopname(line.getStartstopname());
line.setTemporaryendstopname(line.getEndstopname());
/*
*
* 第一次转乘
* A(普通站)->B(中转站)
*
*/
for(int j=0;j<array1.size();j++)
{
ArrayList<String> array3=new ArrayList<String>();
getConnection();
System.out.println("也许这是我想要的起始站"+line.getTemporarystartstopname());
String sql1="select * from changeline where ID1='"+line.getOriginline()+"' and ID2='"+array1.get(j)+"'";
pStmt = conn.prepareStatement(sql1);
ResultSet rs1 = pStmt.executeQuery(sql1);
while(rs1.next())
{
line.setEndstopname(rs1.getString("ChangeStopName"));
}
rs1.close();
array3.add("乘坐"+line.getOriginline()+"号线");
array3.add(line.getTemporarystartstopname());
getConnection();
String sql4="SELECT * FROM firstline f1 JOIN firstline f2 on f1.Line=f2.Line WHERE f1.StopName='"+line.getStartstopname()+"' AND f2.StopName='"+line.getEndstopname()+"'";
//保证两个中转站在同一条线路上
pStmt = conn.prepareStatement(sql4);
ResultSet rs4=pStmt.executeQuery(sql4);
while(rs4.next()) {
line.setTemporaryline(rs4.getInt("Line"));
}
rs.close();
c=select(line);
if(c==1)
{//起始站编号小于终点站,即起始站在前,终点站在后
getConnection();
String sql2="select * from firstline where StopID>=(select StopID from firstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from firstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 找到这样的一些站:它们的ID号大于起始站的ID号,
* 它们的ID号小于结束站的ID号,并且它们都是同一条线上的站点
*/
pStmt = conn.prepareStatement(sql2);
ResultSet rs2 = pStmt.executeQuery(sql2);
while(rs2.next())
{//遍历结果集
array3.add(rs2.getString("StopName"));
}
rs2.close();
}
else if(c==2)
{//起始站编号大于终点站,即起始站在后,终点站在前
getConnection();
ArrayList<String> array2=new ArrayList<String>();
String sql2="select * from firstline where StopID>=(select StopID from firstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from firstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 同上
*/
pStmt = conn.prepareStatement(sql2);
ResultSet rs2 = pStmt.executeQuery(sql2);
while(rs2.next())
{
array2.add(rs2.getString("StopName"));
}
for(int i=0;i<array2.size();i++)
{//测试是否有值
System.out.print(array2.get(i)+" ");
}
for(int i=array2.size()-1;i>=0;i--)
{//将站名倒序传入
array3.add(array2.get(i));
}
rs2.close();
}
/*
* 第二次换乘
* B(中转站)->C(中转站)
*
*/
line.setStartstopname(line.getEndstopname());
getConnection();
String sql3="select * from changeline where ID1='"+array1.get(j)+"' and ID2='"+line.getFinishline()+"'";
pStmt = conn.prepareStatement(sql3);
ResultSet rs3 = pStmt.executeQuery(sql3);
while(rs3.next())
{
line.setEndstopname(rs3.getString("ChangeStopName"));
}
getConnection();
String sql5="SELECT * FROM firstline f1 JOIN firstline f2 on f1.Line=f2.Line WHERE f1.StopName='"+line.getStartstopname()+"' AND f2.StopName='"+line.getEndstopname()+"'";
//保证两个中转站在同一条线路上
pStmt = conn.prepareStatement(sql5);
ResultSet rs5=pStmt.executeQuery(sql5);
while(rs5.next()) {
line.setTemporaryline(rs5.getInt("Line"));
}
rs5.close();
array3.add("转乘"+line.getTemporaryline()+"号线");
c=select(line);
if(c==1)
{//起始站编号小于终点站,即起始站在前,终点站在后
getConnection();
String sql2="select * from firstline where StopID>=(select StopID from firstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from firstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 找到这样的一些站:它们的ID号大于起始站的ID号,
* 它们的ID号小于结束站的ID号,并且它们都是同一条线上的站点
*/
pStmt = conn.prepareStatement(sql2);
ResultSet rs2 = pStmt.executeQuery(sql2);
while(rs2.next())
{//遍历结果集
array3.add(rs2.getString("StopName"));
}
rs2.close();
}
else if(c==2)
{//起始站编号大于终点站,即起始站在后,终点站在前
getConnection();
ArrayList<String> array2=new ArrayList<String>();
String sql2="select * from firstline where StopID>=(select StopID from firstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from firstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 同上
*/
pStmt = conn.prepareStatement(sql2);
ResultSet rs2 = pStmt.executeQuery(sql2);
while(rs2.next())
{
array2.add(rs2.getString("StopName"));
}
for(int i=0;i<array2.size();i++)
{//测试是否有值
System.out.print(array2.get(i)+" ");
}
for(int i=array2.size()-1;i>=0;i--)
{//将站名倒序传入
array3.add(array2.get(i));
}
rs2.close();
}
/*
*
* 最后一次换乘
* 即C(中转站)->D(普通站)
*/
line.setStartstopname(line.getEndstopname());
line.setEndstopname(line.getTemporaryendstopname());
getConnection();
String sql6="SELECT * FROM firstline f1 JOIN firstline f2 on f1.Line=f2.Line WHERE f1.StopName='"+line.getStartstopname()+"' AND f2.StopName='"+line.getEndstopname()+"'";
//保证两个中转站在同一条线路上
pStmt = conn.prepareStatement(sql6);
ResultSet rs6=pStmt.executeQuery(sql6);
while(rs6.next()) {
line.setTemporaryline(rs6.getInt("Line"));
}
rs.close();
array3.add("转乘"+line.getTemporaryline()+"号线");
c=select(line);
if(c==1)
{//起始站编号小于终点站,即起始站在前,终点站在后
String sql2="select * from firstline where StopID>=(select StopID from firstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from firstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 找到这样的一些站:它们的ID号大于起始站的ID号,
* 它们的ID号小于结束站的ID号,并且它们都是同一条线上的站点
*/
getConnection();
pStmt = conn.prepareStatement(sql2);
ResultSet rs2 = pStmt.executeQuery(sql2);
while(rs2.next())
{//遍历结果集
array3.add(rs2.getString("StopName"));
}
rs2.close();
}
else if(c==2)
{//起始站编号大于终点站,即起始站在后,终点站在前
ArrayList<String> array2=new ArrayList<String>();
String sql2="select * from firstline where StopID>=(select StopID from firstline where StopName='"+line.getEndstopname()+"' and Line='"+line.getTemporaryline()+"') and StopID<=(select StopID from firstline where StopName='"+line.getStartstopname()+"' and Line='"+line.getTemporaryline()+"')";
/*
* 同上
*/
getConnection();
pStmt = conn.prepareStatement(sql2);
ResultSet rs2 = pStmt.executeQuery(sql2);
while(rs2.next())
{
array2.add(rs2.getString("StopName"));
}
for(int i=0;i<array2.size();i++)
{//测试是否有值
System.out.print(array2.get(i)+" ");
}
for(int i=array2.size()-1;i>=0;i--)
{//将站名倒序传入
array3.add(array2.get(i));
}
rs2.close();
}
if(j==0) {
min=array3.size();
}
/*
* 第一次:5 min=5 array.size=5
* 第二次:3 array.size=3 min=5
* 第三次:7 array.size=7 min=3
*/
//输出array3测试
System.out.println();
System.out.println();
for(int x=0;x<array3.size();x++) {
System.out.print(array3.get(x)+" ");

}
System.out.println();
System.out.println(min);
if(min>=array3.size()) {
min=array3.size();
for(int x=0;x<array3.size();x++) {
System.out.print(array3.get(x)+" ");

}
array=array3;
}
}
closeAll();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return array;

}
public int CheckAll(Line line) throws SQLException {
int checkall=0;
getConnection();

String sql="SELECT * FROM firstline f1 JOIN firstline f2 on f1.Line=f2.Line WHERE f1.StopName='"+line.getStartstopname()+"' AND f2.StopName='"+line.getEndstopname()+"'";
pStmt = conn.prepareStatement(sql);
ResultSet rs = pStmt.executeQuery(sql);
while(rs.next()){
line.setTemporaryline(rs.getInt("Line"));
}
rs.close();
if(line.getTemporaryline()!=0) {
checkall=1;
return checkall;
}
else if(line.getTemporaryline()==0){
getConnection();
String sql1="select * from firstline where StopName='"+line.getStartstopname()+"' or StopName='"+line.getEndstopname()+"'";
/*
* 同上,但用不着判断是否属于同一线路(本判断只存在于起始点与结束点是普通点)
*/
pStmt = conn.prepareStatement(sql1);
ResultSet rs1 = pStmt.executeQuery(sql1);
while(rs1.next()) {
if(line.getStartstopname().equals(rs1.getString("StopName")))
{
line.setOriginline(rs1.getInt("Line"));
}
if(line.getEndstopname().equals(rs1.getString("StopName"))) {
line.setFinishline(rs1.getInt("Line"));
}
}
rs1.close();
if(line.getOriginline()==0||line.getFinishline()==0) {
checkall=4;
return checkall;
}
System.out.println("这不合理!所以线一为"+line.getOriginline()+",线二为"+line.getFinishline());
getConnection();
String sql2="select * from changeline where ID1='"+line.getOriginline()+"' and ID2='"+line.getFinishline()+"'";
ResultSet rs2 = pStmt.executeQuery(sql2);
while(rs2.next()) {
line.setMiddlestop(rs2.getString("ChangeStopName"));
}
System.out.println(line.getMiddlestop()+"也许不是我想要的");
if(line.getMiddlestop().equals("")) {
checkall=3;
return checkall;
}
else {
checkall=2;
return checkall;
}
}
return checkall;

}
}

8.Commonutil

package util;

import android.app.AlertDialog;
import android.content.Context;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Commonutil {
/*
*显示短消息
*/
public static void showShortMsg(Context context, String msg){
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}

/*
*显示长消息
*/
public static void showLongMsg(Context context, String msg){
Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
}

/*
*显示消息对话框
*/
public static void showDigMsg(Context context, String msg){
new AlertDialog.Builder(context)
.setTitle("提示信息")
.setMessage(msg)
.setPositiveButton("确定",null)
.setNegativeButton("取消",null)
.create().show();
}

public static String getNowDate(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(new Date());

}
}

9.MysqlHelper

package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class MysqlHelper {
private static final String user = "root";
private static final String password = "1003392478";
private static final String dbName = "subway";
private static final String ip = "172.18.45.41";
private static final int port = 3306;
private static final String url = "jdbc:mysql://" + ip + ":" + port
+ "/" + dbName;

public static Connection conn; //连接对象
public static Statement stmt; //命令集
public static PreparedStatement pStmt; //预编译命令集
public static ResultSet rs; //结果集

/*
*获取连接
*/
public static void getConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url,user,password);
System.out.print(111);
}
catch (Exception e){
e.printStackTrace();
System.out.print(996);
}
}

/*
*关闭连接
*/
public static void closeAll(){
try {
if(rs!=null){
rs.close();
rs = null;
}
if(stmt!=null){
stmt.close();
stmt = null;
}
if(pStmt!=null){
pStmt.close();
pStmt = null;
}
if(conn!=null){
conn.close();
conn = null;
}
} catch (Exception e){
e.printStackTrace();
}
}

}

10.activity_dian_query

<LinearLayout 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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="请输入您需要查询的站点信息"
android:textSize="31dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="40dp">
<TextView
android:text="起点:"
android:textSize="25dp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="5dp" />
<EditText
android:id="@+id/startName"
android:layout_margin="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="40dp">
<TextView
android:text="终点:"
android:textSize="25dp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="5dp" />
<EditText
android:id="@+id/EndName"
android:layout_margin="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

<Button
android:id="@+id/btn_submit"
android:layout_width="183dp"
android:layout_height="wrap_content"
android:text="进行查询"
android:textSize="25dp"
android:layout_gravity="center"/>

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/find_id_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>



</LinearLayout>

11.activity_line_query

<LinearLayout 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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="请输入您需要查询的路线"
android:textSize="40dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="路线号:"
android:textSize="25dp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="5dp"/>
<EditText
android:id="@+id/find_id_ET"
android:layout_margin="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

<Button
android:id="@+id/find_id_But"
android:layout_width="183dp"
android:layout_height="wrap_content"
android:text="进行查询"
android:layout_gravity="center"/>

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/find_id_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>


</LinearLayout>

12.activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:padding="30dp"
>

<TextView

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:gravity="center"
android:text="欢迎来到北京地铁查询系统"

android:textSize="30dp"
android:textStyle="bold" />


<Button
android:id="@+id/btn_enter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击进入路线查询"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginTop="5dp"/>
<Button
android:id="@+id/btn_teacher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击查询你的目的路线"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginTop="5dp"/>



</LinearLayout>

 

posted @   Joranger  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示