《数据结构》课程设计报告
全国交通咨询模拟
学院(系): 计算机科学与工程学院
班
级:
学生姓名:
学号
指导教师:
时间: 从 2011 年 12 月 19 日 到 2011 年 12 月 23 日
1
一、课程设计概述:
使用语言: java
编译环境:java 虚拟机
二、课程设计题目一
[实验内容]
[问题描述]
全国交通咨询模拟
1、 管理员可以添加、修改、删除各交通路线信息。
2、 用户查询从某一地点到另一地点的信息,提供查询转车次数最少、花费最少、所用
时间最少的相关信息。
[需求分析]
1、 管理 员和用 户拥有 不同 的操作 界面。 管理员 从键盘 输入个 交通路 线并 保存在
trainInformation.txt 中。
2、 对文件 trainInformation.txt 中的数据进行处理,要求具有如下功能:
1 、城市信息进行编辑。
2 、对列车时刻表进行编辑(增设或删除)的功能。
3 、提供三种最优决策:最省钱到达,转车最少。
4 、提供各列车详细信息。
3、 界面美观
[概要设计]
class MoneyLeast{}
//求出两站点花费最少的路径
class TrainContral{}
//从文件中读出列车的所有信息或者将列车的信息写入到文件
class TrainGraph{}
//列车航线交通图
class AdmFrame{}
//管理员登陆界面
class SearchInFrame{}
//用户查询信息和显示信息界面
[存储结构]
class City{//城市存储
private String cityName;
2
}
class 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 Serializable {
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;
3
}
@Override
public String toString() {
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 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 {
4
n = t.getNumOfVer();
s = new int[n];
for (int i = 0; i < n; i++) {
money[i] = t.getMoney(c, t.city.get(i));
s[i] = 0;
if (money[i] < maxMoney) {
path[i] = c;
} else {
path[i] = null;
}
}
s[t.city.indexOf(c)] = 1;
for (int i = 0; i < n; i++) {
minMoney = maxMoney;
for (int j = 0; j < n; j++) {
if (s[j] == 0 && money[j] < minMoney) {
u = j;
minMoney = money[j];
}
}
if (minMoney == maxMoney) {
return;
}
s[u] = 1;
City city = t.city.get(u);
for (int j = 0; j < n; j++) {
if (s[j] == 0 && t.getMoney(city, t.city.get(j)) < maxMoney &&
money[u] + t.getMoney(city, t.city.get(j)) < money[j]) {
money[j] = money[u] + t.getMoney(city, t.city.get(j));
path[j] = city;
}
}
}
}
}
5
---- MoneyTest-----
package com.consel;
import java.util.Iterator;
import java.util.Stack;
public class MoneyTest {
MoneyLeast ml = new MoneyLeast();
TrainGraph tg = new TrainGraph();
int[] money;
City[] path;
public Stack st = new Stack();
public MoneyTest() {
int n = tg.city.size();
money = new int[n];
path = new City[n];
for (int i = 0; i < n; i++) {
money[i] = tg.maxMoney;
path[i] = null;
}
}
public Stack findMoneyLeast(City c1,City c2) throws Exception{
ml.leastMoney(tg, c1,money, path);
Stack s = new Stack();
s.add(c2);
while (!c2.equals(c1)) {
City c3 = path[tg.city.indexOf(c2)];
s.add(c3);
TrainContral ct = new TrainContral();
ct.trainRead();
Iterator i = ct.train.iterator();
while(i.hasNext()){
Train temp = i.next();
6
if(temp.getStartCity().equals(c3) && 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;
private 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 = startCity;
this.endCity = endCity;
this.startDate = startDate;
this.endDate = endDate;
7
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 startCity;
}
/**
* @return the endCity
*/
public City getEndCity() {
return endCity;
}
/**
* @return the startDate
*/
public Date getStartDate() {
return startDate;
}
/**
8