《[IT计算机]全国交通咨询模拟数据结构Jaa课程设计.doc》由会员分享,可在线阅读,更多相关《[IT计算机]全国交通咨询模拟数据结构Jaa课程设计.doc(46页珍藏版)》请在三一办公上搜索。
1、数据结构课程设计报告全国交通咨询模拟 学院(系): 计算机科学与工程学院 班 级: 学生姓名: 学号 指导教师: 时间: 从2011年12月19日 到2011年12月23日一、课程设计概述:使用语言: java编译环境:java虚拟机二、课程设计题目一实验内容全国交通咨询模拟问题描述1、 管理员可以添加、修改、删除各交通路线信息。2、 用户查询从某一地点到另一地点的信息,提供查询转车次数最少、花费最少、所用时间最少的相关信息。需求分析1、 管理员和用户拥有不同的操作界面。管理员从键盘输入个交通路线并保存在trainInformation.txt中。2、 对文件trainInformation.
2、txt中的数据进行处理,要求具有如下功能:、 城市信息进行编辑。、 对列车时刻表进行编辑(增设或删除)的功能。、 提供三种最优决策:最省钱到达,转车最少。、 提供各列车详细信息。3、 界面美观概要设计class MoneyLeast/求出两站点花费最少的路径class TrainContral/从文件中读出列车的所有信息或者将列车的信息写入到文件class TrainGraph/列车航线交通图class AdmFrame/管理员登陆界面class SearchInFrame/用户查询信息和显示信息界面 存储结构class City/城市存储private String cityName;cla
3、ss Train /列车存储 private String trainID; private City startCity; private City endCity; private Date startDate; private Date endDate;private int money; 详细设计- City-/* * 城市的对象 * 城市有城市名 * 实现序列化 * 重写equals方法和toString方法 */package com.consel;import java.io.Serializable;/实现序列化的接口public class City implements S
4、erializable private static final long serialVersionUID = 5612649006026227701L; private String cityName; public City(String cityName) this.cityName = cityName; public String getCityName() return cityName; public void setCityName(String cityName) this.cityName = cityName; Override public String toStri
5、ng() return cityName; Override public boolean equals(Object obj) if (obj instanceof City) City temp = (City) obj; if (this.cityName.equals(temp.cityName) return true; return false; - MoneyLeast-/* * MoneyLeast 查找从花费最少的路径,及乘车路径 * 使用的是普里姆算法 */package com.consel;public class MoneyLeast final static int
6、 maxMoney = 10000; int n; int s;/s用来存储n个节点的标记 int minMoney, u = 0; /t为列车信息图,c为起始城市,数组money是用来存储最少花费的路径,path是用来存储节点的上一节点 public void leastMoney(TrainGraph t, City c, int money, City path) throws Exception n = t.getNumOfVer(); s = new intn; for (int i = 0; i n; i+) moneyi = t.getMoney(c, t.city.get(i)
7、; si = 0; if (moneyi maxMoney) pathi = c; else pathi = null; st.city.indexOf(c) = 1; for (int i = 0; i n; i+) minMoney = maxMoney; for (int j = 0; j n; j+) if (sj = 0 & moneyj minMoney) u = j; minMoney = moneyj; if (minMoney = maxMoney) return; su = 1; City city = t.city.get(u); for (int j = 0; j n;
8、 j+) if (sj = 0 & t.getMoney(city, t.city.get(j) maxMoney & moneyu + t.getMoney(city, t.city.get(j) moneyj) moneyj = moneyu + t.getMoney(city, t.city.get(j); pathj = city; - MoneyTest-package com.consel;import java.util.Iterator;import java.util.Stack;public class MoneyTest MoneyLeast ml = new Money
9、Least(); TrainGraph tg = new TrainGraph(); int money; City path; public Stack st = new Stack(); public MoneyTest() int n = tg.city.size(); money = new intn; path = new Cityn; for (int i = 0; i n; i+) moneyi = tg.maxMoney; pathi = null; public Stack findMoneyLeast(City c1,City c2) throws Exception ml
10、.leastMoney(tg, c1,money, path); Stack s = new Stack(); s.add(c2); while (!c2.equals(c1) City c3 = pathtg.city.indexOf(c2); s.add(c3); TrainContral ct = new TrainContral(); ct.trainRead(); Iterator i = ct.train.iterator(); while(i.hasNext() Train temp = i.next(); if(temp.getStartCity().equals(c3) &
11、temp.getEndCity().equals(c2) st.add(temp); c2 = c3; return s; - Train-/* * Train 序列化,存储列车的信息,包括ID,起始站,终点站,发车时间,到站时间,价格 */package com.consel;import java.util.*;import java.io.Serializable;public class Train implements Serializable private static final long serialVersionUID = 5612649006026227700L; pri
12、vate String trainID;/列车号 private City startCity;/起始站 private City endCity;/终点站 private Date startDate;/发车时间 private Date endDate;/到站时间 private int money;/价格 public Train(String trainID, City startCity, City endCity, Date startDate, Date endDate, int money) this.trainID = trainID; this.startCity = st
13、artCity; this.endCity = endCity; this.startDate = startDate; this.endDate = endDate; this.money = money; /* * return the trainID */ public String getTrainID() return trainID; /* * return the money */ public int getMoney() return money; /* * return the startCity */ public City getStartCity() return s
14、tartCity; /* * return the endCity */ public City getEndCity() return endCity; /* * return the startDate */ public Date getStartDate() return startDate; /* * return the endDate */ public Date getEndDate() return endDate; - TrainContral-/* * TrainContral 从文件trainInformation.txt中读出各列车信息或将各列车信息写入到文件trai
15、nInformation.txt中 */package com.consel;import java.io.*;import java.util.*;public class TrainContral File filename = new File(config/trainInformation.txt); ObjectInputStream ois; ObjectOutputStream oos; public List train = new ArrayList(); /* * 从trainInformation.txt读出所有列车的信息 * 并存储于链表train中 */ public
16、 void trainRead() try ois = new ObjectInputStream(new FileInputStream(filename);/文件输入流 train = (List) ois.readObject(); catch (FileNotFoundException ex) ex.printStackTrace(); catch (IOException ex) ex.printStackTrace(); catch (ClassNotFoundException ex) ex.printStackTrace(); finally try ois.close();
17、/ 关闭文件输入流 catch (Exception exx) exx.printStackTrace(); /* * 将链表t中的所有火车信息写入到trainInformation.txt中 * param t */ public void trainWrite(List t) try oos = new ObjectOutputStream(new FileOutputStream(filename);/ 文件输出流 oos.writeObject(t); catch (FileNotFoundException ex) ex.printStackTrace(); catch (IOExc
18、eption ex) ex.printStackTrace(); finally try oos.flush();/关闭文件缓冲流 oos.close();/关闭文件流 catch (Exception exx) exx.printStackTrace(); - TrainGraph-/* * TrainGraph 表示所有列车的信息 * 查找以一个城市作为起点站,可以到达的另一个城市 */package com.consel;import java.util.*;public class TrainGraph final static int maxMoney = 10000; List c
19、ity = new ArrayList(); TrainContral tc = new TrainContral(); int edges; public List train; Stack stack1 = new Stack(); public Stack stack = new Stack();/*临时保存路径节点的栈*/ public ArrayList sers = new ArrayList();/*存储路径的集合*/ public int getNumOfVer() return city.size(); public TrainGraph() /构造方法 tc.trainRe
20、ad();/从文件中读出所有列车的信息 int n = tc.train.size(); train = tc.train; Iterator it = train.iterator(); /* * 存储交通路线中所有涉及到的城市 */ while (it.hasNext() Train t = (Train) it.next(); City c1 = t.getStartCity(); City c2 = t.getEndCity(); int x = city.indexOf(c1); int y = city.indexOf(c2); if (x 0) city.add(c1); if
21、(y 0) city.add(c2); edges = new intcity.size()city.size(); for (int i = 0; i city.size(); i+) for (int j = 0; j = 0) return getFirstNeighbor(n); return - 1; /查找邻接的城市 private int getFirstNeighbor(int v) throws Exception if (v city.size() throw new Exception(参数错误!); for (int col = 0; col 0) return col
22、; return -1; /查找下一个邻接城市 public int getNextNeighbor(City c1, City c2) throws Exception int m = city.indexOf(c1); int n = city.indexOf(c2); if (m = 0 & n = 0) return getNextNeighbor(m, n); return -1; /查找下一个邻接城市 private int getNextNeighbor(int v1, int v2) throws Exception if (v1 city.size() | v2 city.s
23、ize() throw new Exception(参数错误!); for (int col = v2 + 1; col 0) return col; return -1; /返回从c1到c2直达所花费的钱 public int getMoney(City c1, City c2) throws Exception int m = city.indexOf(c1); int n = city.indexOf(c2); if (m 0 | n 0) throw new Exception(城市C1或C2错误!); else Iterator i = train.iterator(); Train
24、 temp = null; boolean flag = false; while (i.hasNext() temp = i.next(); if (temp.getStartCity().equals(c1) & temp.getEndCity().equals(c2) flag = true; break; if (flag) return temp.getMoney(); else return maxMoney; private boolean isCityInStack(City city) /判断节点是否在栈中 Iterator it = stack.iterator(); wh
25、ile (it.hasNext() City node1 = it.next(); if (city.equals(node1) return true; return false; / 存储一条通路 private void savePath() Stack temp = new Stack(); Iterator i = stack.iterator(); while (i.hasNext() City c = i.next(); temp.add(c); sers.add(temp); /*转储*/ / 查找从startCity到endCity的所有路径 public void getP
26、aths(City startCity, City endCity) throws Exception stack.push(startCity); int n = getFirstNeighbor(startCity); if (n = 0) City c = city.get(n); getPaths(startCity, endCity, c, startCity); while (c != null) n = getNextNeighbor(startCity, c); if (n 0) c = city.get(n); getPaths(startCity, endCity, c,
27、startCity); else c = null; / 用递归的方法求startCity到endCity的路径currentCity是当前的城市,perCity是currentCity的上一个城市 private boolean getPaths(City startCity, City endCity, City currentCity, City perCity) throws Exception City nCity = null; if (currentCity != null & perCity != null & currentCity.equals(perCity) retur
28、n false; if (currentCity != null) stack.push(currentCity); if (currentCity.equals(endCity) savePath(); stack.pop(); return true; else int n = getFirstNeighbor(currentCity); if (n = 0) nCity = city.get(n); else nCity = null; while (nCity != null) if (perCity != null & (nCity.equals(startCity) | perCi
29、ty.equals(nCity) | isCityInStack(nCity) n = getNextNeighbor(currentCity, nCity); if (n = 0) nCity = city.get(n); else nCity = null; continue; if (getPaths(startCity, endCity, nCity, currentCity) if (!stack.isEmpty() stack.pop(); n = getNextNeighbor(currentCity, nCity); if (n = 0) nCity = city.get(n); else nCity = null; if (!stack.isEmpty() stack.pop(); return false; else return false; - AdmFrame-/* * 管理员登陆界面 * 用户名为:Adm * 口令为:123456 */package com.de