Skip to content

Instantly share code, notes, and snippets.

@Viacheslav77
Last active October 15, 2016 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Viacheslav77/a223b34136db559780f0b9939a8d34b6 to your computer and use it in GitHub Desktop.
Save Viacheslav77/a223b34136db559780f0b9939a8d34b6 to your computer and use it in GitHub Desktop.
Есть список поездов, представленный с виде XML. 1.Вывести на экран информацию о тех поездах, которые отправляются сегодня с 15:00 до 19:00. 2.Написать код для добавления новых поездов в существующий XML.
Прочтённый список поездов из файла
------------------------------------------------------------------------
[Скоростной 96 Одесса Львов 13.04.2016 14:11, Скоростной 156 Николаев Киев 12.10.2016 14:12]
Добавленны новые поезда к списку
------------------------------------------------------------------------
[Скоростной 96 Одесса Львов 13.04.2016 14:11, Скоростной 156 Николаев Киев 12.10.2016 14:12, Пассажирский 846 Харьков Киев 14.11.2016 14:27, Скорый 824 Львов Харьков 13.06.2016 12:03]
Поезда, которые отправляются : 13.04.2016 с : 14:00 по 15:00
------------------------------------------------------------------------
Скоростной 96 Одесса Львов 13.04.2016 14:11
Найдено: 1
package com.comp.Trains.JAXB;
import java.util.Random;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="train")
class Train {
private String type;
private String id;
private String from;
private String to;
private String date;
private String departure;
public Train (){
String [] listType = {"Скорый","Пассажирский","Скоростной","Фирменный"};
String [] listCities = {"Киев","Львов", "Одесса", "Харьков", "Николаев", "Херсон"};
Random r = new Random ();
type=listType[r.nextInt(3)+r.nextInt(1)];
id= String.valueOf(r.nextInt(99)*10+r.nextInt(9));
from=listCities[r.nextInt(6)];
String toTMP;
do {
toTMP = listCities[r.nextInt(6)];
}while ((from.equalsIgnoreCase(toTMP)));
to = toTMP;
String mTMP = ""+r.nextInt(12);
if(Integer.valueOf(mTMP)<10 || Integer.valueOf(mTMP)==0)
mTMP = "0"+ mTMP;
if(Integer.valueOf(mTMP)==0)
mTMP = "01";
date=""+r.nextInt(3)+(r.nextInt(8)+1) +"." + mTMP + "."+"2016";
departure=""+r.nextInt(2)+ (r.nextInt(5)+1) + ":" + r.nextInt(5) + r.nextInt(9);
}
public Train (String type1,String id1, String from1, String to1, String date1, String departure1){
type=type1;
id=id1;
from=from1;
to=to1;
date=date1;
departure=departure1;
}
public String getType() {
return type;
}
public String getTo() {
return to;
}
public String getFrom() {
return from;
}
public String getDeparture() {
return departure;
}
public String getDate() {
return date;
}
public String getId() {
return id;
}
@Override
public String toString() {
return ( type +" " + id +" " + from +" " +to +" " + date +" " + departure);
}
@XmlElement
public void setType(String type) {
this.type = type;
}
@XmlElement
public void setId(String id) {
this.id = id;
}
@XmlElement
public void setFrom(String from) {
this.from = from;
}
@XmlElement
public void setTo(String to) {
this.to = to;
}
@XmlElement
public void setDate(String date) {
this.date = date;
}
@XmlElement
public void setDeparture(String departure) {
this.departure = departure;
}
}
package com.comp.Trains.JAXB;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "trainlist")
class TrainList {
@XmlElement(name="train")
private static List <Train> trainList = new ArrayList <Train>();
private String path;
public TrainList (){ }
public TrainList (String path){
this.path=path;
}
public void add(Train train){
trainList.add(train);
}
@Override
public String toString(){
return Arrays.deepToString(trainList.toArray());
}
public void createList (int j){
for(int i = 0; i<j; i++)
trainList.add(new Train());
System.out.println("\n" +"Добавленны новые поезда к списку" + "\n" + "------------------------------------------------------------------------");
System.out.println(trainList);
}
public void saveList (){
TrainList trainList = this;
try {
File file = new File(path);
JAXBContext jaxbContext = JAXBContext.newInstance(TrainList.class);
Marshaller marshaller = jaxbContext.createMarshaller();
// write in file
marshaller.marshal(trainList, file);
} catch (JAXBException e) {
e.printStackTrace();
}
}
public void loadList (){
TrainList trainList = this;
try {
File file = new File(path);
JAXBContext jaxbContext = JAXBContext.newInstance(TrainList.class);
//read from file
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
trainList = (TrainList) unmarshaller.unmarshal(file);
System.out.println("Прочтённый список поездов из файла" + "\n" + "------------------------------------------------------------------------");
System.out.println(trainList);
} catch (JAXBException e) {
e.printStackTrace();
}
}
public static void findTrain (String date, String fromTime, String toTime){
int count=0;
System.out.println("\nПоезда, которые отправляются : "+date+" с : "+fromTime+" по "+toTime + "\n" + "------------------------------------------------------------------------");
for (Train train: trainList){
if (train.getDate().equals(date) &
train.getDeparture().compareTo(fromTime)>=0 &
train.getDeparture().compareTo(toTime)<=0) {
System.out.println(" "+train);
count++;
}
}
System.out.println("Найдено: "+count);
}
}
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
-<trainlist>
-<train>
<date>13.04.2016</date>
<departure>14:11</departure>
<from>Одесса</from>
<id>96</id>
<to>Львов</to>
<type>Скоростной</type>
</train>
-<train>
<date>12.10.2016</date>
<departure>14:12</departure>
<from>Николаев</from>
<id>156</id>
<to>Киев</to>
<type>Скоростной</type>
</train>
-<train>
<date>14.11.2016</date>
<departure>14:27</departure>
<from>Харьков</from>
<id>846</id>
<to>Киев</to>
<type>Пассажирский</type>
</train>
-<train>
<date>13.06.2016</date>
<departure>12:03</departure>
<from>Львов</from>
<id>824</id>
<to>Харьков</to>
<type>Скорый</type>
</train>
<path>D://1//trains.xml</path>
</trainlist>
package com.comp.Trains.JAXB;
/* Есть список поездов, представленный с виде XML.
* 1.Вывести на экран информацию о тех поездах, которые отправляются сегодня с 15:00 до 19:00.
* 2.Написать код для добавления новых поездов в существующий XML.
*/
public class Main {
public static void main (String[] arg) {
TrainList trains = new TrainList("D://1//trains.xml");
//Читаем из файла список поездов
trains.loadList();
//Создаём и добовляем в список ещё 300 поездов
trains.createList(2);
//Сохраняем в файл список поездов
trains.saveList();
//Ищем информацию о тех поездах.
TrainList.findTrain("01.09.2016", "26.09.2016", "01:00", "13:00");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment