Implementation using Thread
unknown
java
5 years ago
9.9 kB
14
Indexable
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Random;
import java.util.Scanner;
abstract class Travel {
int travelCode;
double price;
ArrayList<String> citys;
int travelYear;
public Travel(int travelCode, double price, ArrayList<String> citys, int travelYear) {
this.travelCode = travelCode;
this.price = price;
this.citys = citys;
this.travelYear = travelYear;
}
}
class InternationalTravel extends Travel {
String flightType;
String country;
String passportId;
public InternationalTravel(int travelCode, double price, ArrayList<String> citys, String flightType,
String country, String passportId, int travelYear) {
super(travelCode, price, citys, travelYear);
this.flightType = flightType;
this.country = country;
this.passportId = passportId;
}
@Override
public String toString() {
return "\nInternationalTravel: [" + "\n\ttravelCode = " + travelCode + ", " + "\n\tcities = " + citys + ", "
+ "\n\ttravelYear = " + travelYear + ", " + "\n\tprice = " + price + ", " + "\n\tcountry=" + country
+ ", " + "\n\tflightType=" + flightType + ", " + " \n\tpassportId=" + passportId + "]";
}
}
class NationalTravel extends Travel {
String busType;
String governmentId;
public NationalTravel(int travelCode, double price, ArrayList<String> citys, String busType,
String governmentId, int travelYear) {
super(travelCode, price, citys, travelYear);
this.busType = busType;
this.governmentId = governmentId;
}
@Override
public String toString() {
return "\nNationalTravel: [" + "\n\ttravelCode = " + travelCode + ", " + "\n\tcities = " + citys + ", "
+ "\n\ttravelYear = " + travelYear + ", " + "\n\tprice = " + price + ", " + "\n\tusType=" + busType
+ ", " + "\n\tgovernmentId = " + governmentId + "]";
}
}
public class TravelTest {
static Thread t1;
static Thread t2;
static Thread t3;
public static void main(String[] args) throws IOException{
t1 = new Thread() {
@Override
public void run() {
try {
TravelTest.scanNationalFIleDetail();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
t2 = new Thread() {
@Override
public void run() {
try {
TravelTest.scanInternationalFIleDeail();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
t3 = new Thread() {
@Override
public void run() {
try {
TravelTest.displayBothFIle();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
displaySystemMenu();
}
public static void scanNationalFIleDetail() throws IOException, InterruptedException {
Scanner sc = new Scanner(System.in);
int travelCode = TravelTest.generateTravelCode();
System.out.println("Enter year of travel");
int travelYear = sc.nextInt();
sc.nextLine();
System.out.println("Enter Cities name You want to visit");
String []city = sc.nextLine().split(",");
ArrayList<String> citys = new ArrayList<>();
for(String c : city) {
citys.add(c);
}
double price = TravelTest.generatePrice();
System.out.println("Enter bus type");
String busType = sc.nextLine();
System.out.println("Enter Goverment Id no");
String governmentId = sc.nextLine();
Travel nt = new NationalTravel(travelCode, price, citys, busType, governmentId, travelYear);
TravelTest.writeFile("file1.txt", nt);
}
public static void scanInternationalFIleDeail() throws IOException, InterruptedException {
Scanner sc = new Scanner(System.in);
int travelCode = TravelTest.generateTravelCode();
System.out.println("Enter year of travel");
int travelYear = sc.nextInt();
sc.nextLine();
System.out.println("Enter Cities name You want to visit");
String []city = sc.nextLine().split(",");
ArrayList<String> citys = new ArrayList<>();
for(String c : city) {
citys.add(c);
}
double price = generatePrice();
System.out.println("Enter country name");
String country = sc.nextLine();
System.out.println("Enter flight type");
String flightType = sc.nextLine();
System.out.println("Enter PassportId no");
String passportId = sc.nextLine();
Travel It = new InternationalTravel(travelCode, price, citys, flightType, country, passportId, travelYear);
writeFile("file2.txt", It);
}
public static void displayBothFIle() throws FileNotFoundException, InterruptedException {
ArrayList<Travel> nt= readFile("File1.txt");
ArrayList<Travel> it= readFile("File2.txt");
nt.forEach(System.out::println);
it.forEach(System.out::println);
}
public static void displaySystemMenu() throws IOException {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("....................Travel....................");
System.out.println("[0] to Exit");
System.out.println("[1] Get National Travel info");
System.out.println("[2] Get Internatiol Travel info");
System.out.println("[3] Display Travel info");
System.out.println("\nEnter your choice : ");
// taking choice input between 0 to 3
int choice = sc.nextInt();
if(choice == 0) {
System.out.println("Thank You for using Travel System.");
return;
}
sc.nextLine();
// handing invalid choice
if (choice < 0 || choice > 4) {
System.out.println("enter a valid number ");
continue;
}
switch (choice) {
case 1:
t1.run();
break;
case 2:
t2.run();
break;
case 3:
t3.run();
break;
}
}
}
public static int generateTravelCode() {
int low = 10000;
int high = 200000;
int travelcode = new Random().nextInt(high - low) + low;
return travelcode;
}
public static double generatePrice() {
int low = 1000;
int high = 2000;
double price = new Random().nextDouble() * (high-low) + low;
return price;
}
public static void writeFile(String fileName, Travel travel) throws IOException {
FileWriter fw = new FileWriter(new File(fileName),true);
if (travel instanceof InternationalTravel) {
InternationalTravel tr = (InternationalTravel) travel;
int travelCode = tr.travelCode;
double price = tr.price;
ArrayList<String> citys = tr.citys;
String flightType = tr.flightType;
String country = tr.country;
String passportId = tr.passportId;
int travelYear = tr.travelYear;
StringBuilder writeLine = new StringBuilder("");
StringBuilder cityString = new StringBuilder("[");
for(String city : citys) {
cityString.append(city+",");
}
cityString.append("]");
writeLine.append(travelCode+" "+price+" "+passportId+" "+cityString.toString()+" "+flightType+" "+country+" "+travelYear);
fw.write(writeLine.toString());
fw.write("\r\n");
}
if (travel instanceof NationalTravel) {
NationalTravel tr = (NationalTravel) travel;
int travelCode = tr.travelCode;
double price = tr.price;
ArrayList<String> citys = tr.citys;
String busType=tr.busType;
String governmentId=tr.governmentId;
int travelYear = tr.travelYear;
StringBuilder writeLine = new StringBuilder("");
StringBuilder cityString = new StringBuilder("[");
for(String city : citys) {
cityString.append(city+",");
}
cityString.append("]");
writeLine.append(travelCode+" "+price+" "+governmentId+" "+cityString.toString()+" "+busType+" "+travelYear);
fw.write(writeLine.toString());
fw.write("\r\n");
}
fw.close();
}
public static ArrayList<Travel> readFile(String fileName) throws FileNotFoundException {
Scanner scanFile = new Scanner(new File(fileName));
ArrayList<Travel> travels = new ArrayList<Travel>();
while(scanFile.hasNextLine()) {
String [] data = scanFile.nextLine().split(" ");
data[3]= data[3].replace("[","");
data[3] = data[3].replace("]","");
ArrayList<String> cities = new ArrayList<String>(Arrays.asList(data[3].split(",")));
if(data.length==6) {
travels.add(new NationalTravel(Integer.parseInt(data[0]), Double.parseDouble(data[1]), cities, data[4], data[2],Integer.parseInt(data[5])));
}else if(data.length==7) {
travels.add(new InternationalTravel(Integer.parseInt(data[0]), Double.parseDouble(data[1]), cities, data[4], data[5], data[2],Integer.parseInt(data[6])));
}
}
return travels;
}
}
Editor is loading...