-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinalExam.java
More file actions
195 lines (154 loc) · 4.46 KB
/
finalExam.java
File metadata and controls
195 lines (154 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Abraham Estrada
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class finalExam {
public static void main(String[] args){
int attendance[][]=readAttendantsAmtsFile("attendance.txt");
System.out.println();
double prices[]=readPricesFile("prices.txt");
System.out.println();
String movies[]=readMovieFile("movies.txt");
System.out.println();
System.out.println();
displayMoviePrices(attendance,prices,movies);
System.out.println();
avgAttend(attendance,movies);
}
public static int[][] readAttendantsAmtsFile(String fileName) {
int attendantsAmts[][];
Scanner fileScanner = null;
try {
fileScanner = new Scanner(new File(fileName));
}
catch (FileNotFoundException e) {
System.out.println("File not found! Program will now exit!");
System.exit(1);
}
int rowSize = fileScanner.nextInt();
int colSize = fileScanner.nextInt();
attendantsAmts = new int[rowSize][colSize];
for (int row = 0; row < rowSize; row++) {
for (int col = 0; col < colSize; col++) {
attendantsAmts[row][col] = fileScanner.nextInt();
}
}
printArr(attendantsAmts);
return attendantsAmts;
}
public static String[] readMovieFile(String fileName){
String movies[];
Scanner fileScanner = null;
try {
fileScanner = new Scanner(new File(fileName));
}
catch (FileNotFoundException e) {
System.out.println("File not found! Program will now exit!");
System.exit(1);
}
int rowSize = fileScanner.nextInt();
fileScanner.nextLine();
movies = new String[rowSize];
for (int row = 0; row < rowSize; row++) {
movies[row] = fileScanner.nextLine();
}
return movies;
}
public static double[] readPricesFile(String fileName){
double price[];
Scanner fileScanner = null;
try {
fileScanner = new Scanner(new File(fileName));
}
catch (FileNotFoundException e) {
System.out.println("File not found! Program will now exit!");
System.exit(1);
}
int rowSize = fileScanner.nextInt();
price = new double[rowSize];
for (int row = 0; row < rowSize; row++) {
price[row] = fileScanner.nextDouble();
}
printArr(price);
return price;
}
public static void displayMoviePrices(int[][] attendants, double[] prices, String[] movies) {
double totalPrice[] = new double[movies.length];
for(int row=0; row < attendants.length; row++){
double price = 0.0;
if(row==3){
System.out.print(movies[row]+ " Cost:\t\t\t");
}
else if (row==4){
System.out.print(movies[row]+ " Cost:\t\t");
}
else{
System.out.print(movies[row]+ " Cost: ");
}
for(int col=0; col < attendants[0].length; col++){
for(int pos=0; pos < attendants[row][col]; pos++){
price += prices[col];
}
}
totalPrice[row] = price;
System.out.print("$ " + Math.round(price*100.0)/100.0);
System.out.println();
}
System.out.println();
highestCost(totalPrice, movies);
lostCost(totalPrice, movies);
}
public static void highestCost(double[] totalPrice, String[] movies) {
double max = 0;
int maxIndex = 0;
for(int row=0; row < movies.length; row++){
if(max < totalPrice[row]){
max = totalPrice[row];
maxIndex = row;
}
}
System.out.println("Highest sales: " + movies[maxIndex]);
}
public static void lostCost(double[] allCosts, String[] movies) {
double min = 5000000000000.0;
int minIndex = 0;
for(int row=0; row < movies.length; row++){
if(min > allCosts[row]){
min = allCosts[row];
minIndex = row;
}
}
System.out.println("lowest Sales " + movies[minIndex]);
}
public static void avgAttend(int[][] attendance, String[] movies){
for(int row = 0; row<attendance.length; row++){
double sumOfRow = 0;
double totalAvg = 0;
for(int col = 0; col< attendance[0].length; col++){
sumOfRow+=attendance[row][col];
totalAvg= sumOfRow/attendance[row].length;
}
System.out.println(movies[row] + " avg is "+ Math.round(totalAvg));
}
}
public static void printArr(double[] arr) {
for (int i = 0 ; i < arr.length ; i++) {
System.out.print(arr[i] + " ");
}
}
public static void printArr(String[] arr) {
for (int i = 0 ; i < arr.length ; i++) {
System.out.print(arr[i] + " ");
}
}
public static void printArr(int [] [] table){
for(int row = 0; row<table.length;row++){
for(int col = 0; col<table[0].length;col++){
System.out.print(table[row][col]+ " ");
}
System.out.println("");
}
}
}