我要做的是使用JGraphT
创建航线路径的行程。我面临的问题是将我设置的字符串时间转换为实际时间,然后我可以在多天内进行计算,即如果航班在16.00离开并且到达18.30但是然后连接航班在14.00离开并到达最终目的地在16.00它将是24小时(即一天)。我迷失了,因为我试图将字符串解析为Flight类中的日期,并且还使用了导致错误的简单日期格式。
我的代码如下;
Flight3.java
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;
import org.jgrapht.alg.DijkstraShortestPath;
import org.jgrapht.graph.DefaultWeightedEdge;
import org.jgrapht.graph.SimpleDirectedWeightedGraph;
public class Flight3
{
private static ArrayList<String[]> myEdges;
private static ArrayList<Flight> flight;
public Flight3()
{
}
public static void main(String [] args)
{
myEdges = new ArrayList<String[]>();
flight = new ArrayList<Flight>();
SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> Graph = createGraph();
System.out.println("Airlines!");
Scanner sc = new Scanner(System.in);
System.out.println("Enter the airport you wish to fly from");
String startVertex = sc.nextLine();
while(!Graph.containsVertex(startVertex))
{
System.out.println("Sorry, that airport does not exist. Please select another;");
startVertex = sc.nextLine();
}
System.out.println("Enter destination airport");
String endVertex = sc.nextLine();
while(!Graph.containsVertex(endVertex))
{
System.out.println("Sorry, that airport does not exist. Please select another;");
endVertex = sc.nextLine();
}
calculatePath(Graph, startVertex, endVertex);
}
private static SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> createGraph()
{
SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> g =
(SimpleDirectedWeightedGraph<String, DefaultWeightedEdge>) new SimpleDirectedWeightedGraph<String, DefaultWeightedEdge>(DefaultWeightedEdge.class);
{
g.addVertex("London");
g.addVertex("France");
g.addVertex("Spain");
createTwoWayWeightedEdge(g, "London", "France", 80);
generateFlight("1600", "1830", "EH445", "0000", 80);
generateFlight("0400", "0600", "HE452", "0000", 80);
createTwoWayWeightedEdge(g, "France", "Spain", 130);
generateFlight("1400", "1600", "HD123", "0400", 130);
generateFlight("0400", "0600", "DH712", "0000", 130);
}
return g;
}
private static void createTwoWayWeightedEdge(SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> g, String vertex1, String vertex2, double weight)
{
g.addEdge(vertex1, vertex2);
g.addEdge(vertex2, vertex1);
g.setEdgeWeight(g.getEdge(vertex1, vertex2), weight);
g.setEdgeWeight(g.getEdge(vertex2, vertex1), weight);
String[] tmp1 = {vertex1, vertex2};
myEdges.add(tmp1);
String[] tmp2 = {vertex2, vertex1};
myEdges.add(tmp2);
}
private static void generateFlight(String depTime, String arrTime, String flightNo, String locTime, int duration)
{
Flight f = new Flight(depTime, arrTime, flightNo, locTime, duration);
flight.add(f);
}
private static String textToPrint(String[] format)
{
String text = " ";
for(int i = 0; i < format.length; i++)
{
switch(i)
{
case 0:
text = text + format[i];
for(int j = format[i].length(); j < 6 ; j++)
text = text + " ";
break;
case 1:
text = text + format[i];
for(int j = format[i].length(); j < 15 ; j++)
text = text + " ";
break;
case 2:
text = text + format[i];
for(int j = format[i].length(); j < 10 ; j++)
text = text + " ";
break;
case 3:
text = text + format[i];
for(int j = format[i].length(); j < 10 ; j++)
text = text + " ";
break;
case 4:
text = text + format[i];
for(int j = format[i].length(); j < 15 ; j++)
text = text + " ";
break;
case 5:
text = text + format[i];
for(int j = format[i].length(); j < 10 ; j++)
text = text + " ";
break;
case 6:
text = text + format[i];
for(int j = format[i].length(); j < 10 ; j++)
text = text + " ";
break;
}
}
return text;
}
private static void calculatePath(SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> g, String startVertex, String endVertex)
{
DijkstraShortestPath path = new DijkstraShortestPath(g, startVertex, endVertex);
path.getPath();
List<Object> edges = path.getPathEdgeList();
String item;
int count = 1;
double totalDuration = 0;
if(edges!=null)
{
System.out.println("\n The lowest cost route is:");
String[] labels = {"Flight.", "Leave from", "At", "On", "Arrive", "At", "Local Time"};
System.out.println(textToPrint(labels));
for(Object edge : edges)
{
item = edge.toString();
StringTokenizer st = new StringTokenizer(item, ":");
String firstAirport = st.nextToken().trim().substring(1);
String secondAirport = st.nextToken().trim();
secondAirport = secondAirport.substring(0, secondAirport.length()-1);
String depTime = null;
String arrTime = null;
String flightNo = null, locTime = null;
double price, flightDuration;
for(int i=0;i<flight.size();i++)
{
if(firstAirport.equals(myEdges.get(i)[0]) && secondAirport.equals(myEdges.get(i)[1]))
{
Flight details = flight.get(i);
flightNo = details.flightNo;
depTime = details.depTime;
arrTime = details.arrTime;
price = details.price;
flightDuration = details.duration;
totalDuration = totalDuration + details.getDuration();
locTime = details.getLocTime();
String[] flightInfo = {count+".", firstAirport, depTime, flightNo, secondAirport, arrTime, locTime};
System.out.println(textToPrint(flightInfo));
}
}
count++;
}
System.out.println("Cost of route = £"+path.getPathLength());
System.out.println("Total time in the air = "+totalDuration +"hrs");
}
else
System.out.println("Sorry you can't fly there from " + startVertex);
}
Flight.java
import java.text.SimpleDateFormat;
public class Flight {
String depTime;
String arrTime;
String flightNo;
String locTime;
double duration;
int price;
public Flight(String depTime, String arrTime, String flightNo, String locTime, int duration){
this.depTime = depTime;
this.arrTime = arrTime;
this.flightNo = flightNo;
this.locTime = locTime;
this.duration = duration;
}
public double getDuration(){
double duration = Integer.parseInt(arrTime) - Integer.parseInt(depTime);
return duration / 100;
}
public String getLocTime(){
int value = Integer.parseInt(locTime) + Integer.parseInt(arrTime);
locTime = ""+value;
return locTime;
}
public String getFlightNo(){
return flightNo;
}
public double getPrice(){
return price;
}
}
答案 0 :(得分:1)
你的问题并不完全清楚。但这里有一些一般的提示。
不要专注于抵达。到达时间是[到达+持续时间]的结果。换句话说,输出不输入。这可以解决您在午夜过夜的问题。
使用对象,而不是字符串。 Java在java.time类中具有出色的业界领先的日期时间框架。使用它们。但不使用臭名昭着的旧遗留日期时间类,java.time
包之外的类。仅在用户界面中根据需要使用字符串以及序列化数据。
特别是,您应该查看LocalTime
和Duration
类。请参阅Oracle Tutorial。
对于序列化,坚持标准的ISO 8601格式,例如HH:MM,仅限时间值,冒号对于“基本”版本是可选的,但我建议将冒号保留为“扩展”版。在解析和生成表示日期时间值的字符串时,java.time类默认使用ISO 8601的扩展版本。
Flight
无知图表,反之亦然从图表绘图中分离出您的数据模型。将Flight
定义为航班信息和功能,而不考虑图表。
class Flight {
LocalTime departure;
Duration duration;
LocalTime getArrival() {
LocalTime arrival = departure.plus( duration );
// If called *many* times, and you account for changing-data and thread-safety, you could cache this result for performance.
return arrival;
}
Flight( LocalTime departureArg , Duration durationArg ) {
this.departure = departureArg;
this.duration = durationArg;
}
}
要生成图表所需的确切数据,请使用Flight上的getter方法或Flight和图表之间的中介类。保持课程尽可能完全分开。你的汽车无线电立体声系统不需要了解有关空调的任何信息,而空调反过来又不需要知道发动机的燃油 - 氧气混合比。
房间里的大象是时区。你还没有明确表达自己的意图。
一般最好在UTC工作。仅在需要时转换为本地时区以呈现给用户。
在澄清业务背景的意图之前,不要多说这个。
搜索Java类LocalDateTime
,OffsetDateTime
,ZonedDateTime
,ZoneOffset
和ZoneId
的堆栈溢出以了解更多信息。
答案 1 :(得分:0)
在进行持续时间计算之前,我们可能必须附加/前缀某些今天日期的格式(例如:https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html)。
所以当我们有&#34; 0430&#34;的开始时间我们也会为今天的日期格式添加前缀。例如:
startDate = "0430"
变
startDateModified = new SimpleDateFormat("yyyy/MM/dd").format(new Date()) + startDate;
startDateObject = new SimpleDateFormat("yyyy/MM/dd HH:mm").parse(statDateModified)
类似地,我们在结束日期这样做(我们可能必须使用新的Date()的其他值,因为到达时间是第二天等)
然后我们进行计算。
现在关于你的问题,我们将不得不弄清楚连接航班起飞日期值是不是
new Date(); //today
但是
new Date(System.currentTimeMillis() + 24*3600); //tomorrow
事实上,我们可能需要另一种速记符号来表示航班今天离开并明天到达(例如&#34; +1 0430&#34;表示+1天等)