Applied
10.1 Practice Exercise
You have been provided with a text file in the workspace window called "tours.txt".
This file contains data in the following format:
origin, destination, days, cost
You are required to write the program to read this information from the file, store it in an appropriate collection, and display it on the screen.
Your program must report exceptions only.
You may include any additional classes based on responsibility-driven design and if included each class must include all the required aspects such as constructors, accessors, mutators, and a display method.
Tourr.java
public class Tour
{
private String origin;
private String destination;
private int days;
private int cost;
public Tour()
{
this.origin = "";
this.destination = "";
this.days = 0;
this.cost = 0;
}
public Tour(String origin, String destination, int days, int cost)
{
this.origin = origin;
this.destination = destination;
this.days = days;
this.cost = cost;
}
public int getCost()
{
return cost;
}
public int getDays()
{
return days;
}
public String getDestination()
{
return destination;
}
public String getOrigin()
{
return origin;
}
public void setCost(int cost)
{
this.cost = cost;
}
public void setDays(int days)
{
this.days = days;
}
public void setDestination()
{
this.destination = destination;
}
public void setOrigin()
{
this.origin = origin;
}
public String toString()
{
return String.format("Origin: %s, Destination: %s, Days: %d, Cost: $%d",
origin, destination, days, cost);
}
}TouManager.java
import java.util.List;
import java.util.ArrayList;
import java.io.*;
public class TourManager
{
private List<Tour> tours;
public TourManager()
{
this.tours = new ArrayList<>();
}
public TourManager(ArrayList<Tour> tours)
{
this.tours = tours;
}
public void display()
{
if (tours.isEmpty())
{
System.out.println("No tours to display");
}
System.out.println("Tour Information:");
System.out.println("---------------------");
for (Tour tour : tours)
{
System.out.println(tour.toString());
}
}
public static void main(String[] args)
{
TourManager manager = new TourManager();
manager.readFileFrom("tours.txt");
manager.display();
}
public void readFileFrom(String fileName)
{
try (BufferedReader reader = new BufferedReader(new FileReader(fileName)))
{
String line;
while ((line = reader.readLine()) != null)
{
String[] parts = line.split(",");
if (parts.length == 4)
{
String origin = parts[0].trim();
String destination = parts[1].trim();
int days = Integer.parseInt(parts[2].trim());
int cost = Integer.parseInt(parts[3].trim());
Tour tour = new Tour(origin, destination, days, cost);
tours.add(tour);
}
else
{
System.out.println("Skipping invalid line: " + line);
}
}
}
catch (FileNotFoundException e)
{
System.out.println("File not found: " + fileName);
}
catch (IOException e)
{
System.out.println("Error reading from file: " + e.getMessage());
}
catch (NumberFormatException e)
{
System.out.println("Invalid number format in file: " + e.getMessage());
}
}
}tours.txt
Venice,Rome,2,500
Rome,Milan,3,400
Milan,Geneva,4,700
Geneva,Hamburg,4,650