0% found this document useful (0 votes)
2 views

code

The TransportAuthority Java program manages passenger data for a transport system. It generates random passenger counts for 30 days of the week, calculates total passengers for specific days, identifies the day with the highest average passengers, and computes total sales between given days based on weekday and weekend fees. The program reads and writes passenger data to and from a file, facilitating data manipulation and analysis.

Uploaded by

gincreate12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

code

The TransportAuthority Java program manages passenger data for a transport system. It generates random passenger counts for 30 days of the week, calculates total passengers for specific days, identifies the day with the highest average passengers, and computes total sales between given days based on weekday and weekend fees. The program reads and writes passenger data to and from a file, facilitating data manipulation and analysis.

Uploaded by

gincreate12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

import java.io.

File;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Scanner;

public class TransportAuthority {

public static void main(String[] args) {

int[][] passengers = new int[30][7];

double[] fees = {2.5, 1.5}; // Fee for weekdays and weekends

// Step 1: Generate and write random passenger data to a file

generateRandomPassengers();

// Step 2: Read passenger data from the file into a 2D array

readPassengersIntoArray(passengers);

// Step 3: Print the total passengers for a specific day (e.g., column 2 =
Wednesday)

System.out.println("Total passengers on Wednesday: " +


total(passengers, 2));

// Step 4: Print the day with the highest average number of passengers

highestAverage(passengers);

// Step 5: Calculate total sales between two days (e.g., from week 1,
Monday to week 3, Wednesday)

System.out.println("Total sales from week 1, Monday to week 3,


Wednesday: " + salesCalculate(passengers, 0, 0, 2, 2, fees));
}

// Function to generate random passenger data and write to a file

public static void generateRandomPassengers() {

try (FileWriter writer = new FileWriter("passengers.txt")) {

for (int i = 0; i < 30; i++) {

for (int j = 0; j < 7; j++) {

int number = (int) (Math.random() * 101) + 200; // Random


number between 200 and 300

writer.write(number + (j == 6 ? "\n" : " ")); // Write 7 numbers


per line

System.out.println("Passenger data generated.");

} catch (IOException e) {

e.printStackTrace();

// Function to read passengers data from file into a 2D array

public static void readPassengersIntoArray(int[][] passengers) {

try (Scanner scanner = new Scanner(new File("passengers.txt"))) {

for (int i = 0; i < 30; i++) {

for (int j = 0; j < 7; j++) {

if (scanner.hasNextInt()) {

passengers[i][j] = scanner.nextInt();

}
}

System.out.println("Passenger data read into array.");

} catch (Exception e) {

e.printStackTrace();

// Function to calculate the total passengers for a specific day (column)

public static int total(int[][] passengers, int column) {

int sum = 0;

for (int i = 0; i < 30; i++) {

sum += passengers[i][column];

return sum;

// Function to find the day with the highest average number of passengers

public static void highestAverage(int[][] passengers) {

double maxAvg = 0;

int maxDay = -1;

for (int col = 0; col < 7; col++) {

int total = total(passengers, col);

double average = (double) total / 30;

if (average > maxAvg) {

maxAvg = average;
maxDay = col;

System.out.println("The day with the highest average is " +


convert(maxDay) + " with an average of " + maxAvg);

// Function to convert column number to day of the week

public static String convert(int day) {

String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday",


"Friday", "Saturday", "Sunday"};

return days[day];

// Function to calculate total sales between two specific days

public static double salesCalculate(int[][] passengers, int row1, int col1, int
row2, int col2, double[] fees) {

double totalSales = 0;

for (int i = row1; i <= row2; i++) {

for (int j = (i == row1 ? col1 : 0); j <= (i == row2 ? col2 : 6); j++) {

if (j >= 0 && j <= 4) {

totalSales += passengers[i][j] * fees[0]; // Weekday

} else {

totalSales += passengers[i][j] * fees[1]; // Weekend

}
}

return totalSales;

You might also like