-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem14.java
More file actions
118 lines (115 loc) ยท 3.02 KB
/
Problem14.java
File metadata and controls
118 lines (115 loc) ยท 3.02 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
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
class Fruit{
private String name;
private double cost;
public Fruit(String n, double c) {
this.name = n;
this.cost = c;
}
String getName() { return name;}
double getCost() { return cost;}
}
class FruitBox<T extends Fruit> {
private ArrayList<T> list = new ArrayList<T>();
void add(T item) { list.add(item); System.out.println(item.getName() + " " + item.getCost()); }
int getNumItems() { return list.size(); }
String getMaxItem() {
double max = 0;
int index = 0;
for(int i = 0; i < list.size(); i++) {
if(max < list.get(i).getCost()){
max = list.get(i).getCost();
index = i;
}
}
return list.get(index).getName();
}
double getMaxPrice() {
double max = 0;
int index = 0;
for(int i = 0; i < list.size(); i++) {
if(max < list.get(i).getCost()){
max = list.get(i).getCost();
index = i;
}
}
return list.get(index).getCost();
}
String getMinItem() {
double min = (getNumItems()>0)?list.get(0).getCost():0;
int index = 0;
for(int i = 1; i < list.size(); i++) {
if(min > list.get(i).getCost()){
min = list.get(i).getCost();
index = i;
}
}
return list.get(index).getName();
}
double getMinPrice() {
double min = (getNumItems()>0)?list.get(0).getCost():0;
int index = 0;
for(int i = 1; i < list.size(); i++) {
if(min > list.get(i).getCost()){
min = list.get(i).getCost();
index = i;
}
}
return list.get(index).getCost();
}
double getAvgPrice() {
double s = 0;
for(int i = 0; i < list.size(); i++) {
s += list.get(i).getCost();
}
return (s/getNumItems());
}
}
public class Problem14 {
public static class ItemReader{
public static boolean fileToBox(String f, FruitBox<Fruit> box) {
BufferedReader br;
try {
br = new BufferedReader(new FileReader(f));
}catch (FileNotFoundException e) {
System.out.println("Input file not found");
return false;
}
while(true) {
try {
String line = br.readLine();
if(line == null) break;
String word[] = line.split(" ",2);
double e = Double.parseDouble(word[1]);
box.add(new Fruit(word[0], e));
} catch(IOException e) {
e.printStackTrace();
}
}
try {
br.close();
}catch(IOException e) {
e.printStackTrace();
}
return true;
}
}
public static void main(String[] args) {
FruitBox<Fruit> box = new FruitBox<>();
boolean rv = ItemReader.fileToBox("input_prob14.txt", box);
if(rv == false) return;
box.add(new Fruit("orange", 9.99));
System.out.println("----------------");
System.out.println(" Summary");
System.out.println("----------------");
System.out.println("number of items: " + box.getNumItems());
System.out.println("most expensive item: " + box.getMaxItem() + " (" +
box.getMaxPrice() + ")");
System.out.println("cheapest item: " + box.getMinItem() + " (" +
box.getMinPrice() + ")");
System.out.printf("average price of items: %.2f", box.getAvgPrice());
}}