-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab11Prob03.java
More file actions
149 lines (125 loc) · 3.33 KB
/
Copy pathLab11Prob03.java
File metadata and controls
149 lines (125 loc) · 3.33 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
/**
* File: Lab11Prob03.java
* Class: CSCI 1302
* Authors: Eli McHugh, Rendy Jenkins
* Last Modified: November 22, 2024
* Description: Read people.dat and print to a copy file as well as the console, create people objects accordingly.
*/
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.FileOutputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.ObjectInputStream;
import java.util.ArrayList;
public class Lab11Prob03 {
public static void main(String args[]) {
ArrayList<Person> personArrayList = new ArrayList<Person>();
try (
DataInputStream input = new DataInputStream(new FileInputStream("src/people.dat"));
DataOutputStream output = new DataOutputStream(new FileOutputStream("src/people-copy.dat"));
) {
while(true) {
int age = input.readInt();
output.writeInt(age);
String name = input.readUTF();
output.writeUTF(name);
String Address = input.readUTF();
output.writeUTF(Address);
int zipCode = input.readInt();
output.writeInt(zipCode);
double salary = input.readDouble();
output.writeDouble(salary);
//System.out.printf("%d %s %s %d %.2f%n", age, name, Address, zipCode, salary);
personArrayList.add(new Person(age, name, Address, zipCode, salary));
}
} catch (EOFException e) {
System.out.print("");
} catch (Exception e) {
e.printStackTrace();
}
try (ObjectOutputStream outdata = new ObjectOutputStream(new FileOutputStream("src/people-salary-sorted.dat"));
){
java.util.Collections.sort(personArrayList);
for (int i = 0; i < personArrayList.size(); i++) {
outdata.writeObject(personArrayList.get(i));
System.out.println(personArrayList.get(i).toString());
}
} catch (EOFException e) {
System.out.print("");
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Person implements Comparable<Person>, Serializable {
private static final long serialVersionUID = 1L;
private int age;
private String name;
private String address;
private int zipCode;
private double salary;
//default constructor
public Person() {
setAge(20);
setName("John Doe");
setAddress("123 Main St");
setZipCode(12345);
setSalary(63000.3);
}
//convenience constructor
public Person(int age, String name, String address, int zipCode, double salary) {
setAge(age);
setName(name);
setAddress(address);
setZipCode(zipCode);
setSalary(salary);
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setAddress(String address) {
this.address = address;
}
public void setZipCode(int zipCode) {
this.zipCode = zipCode;
}
public void setSalary(double salary) {
this.salary = salary;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public int getZipCode() {
return zipCode;
}
public double getSalary() {
return salary;
}
public String toString() {
return String.format("%d %s %s %d %.2f", age, name, address, zipCode, salary);
}
@Override
public int compareTo(Person o) {
if (this.salary == o.salary) {
return 0;
} else {
if(this.salary > o.salary) {
return -1;
} else {
return 1;
}
}
}
}