Reputation: 43
This code is to print these objects to file named customer.txt. It is working correctly but I am trying to put condition like if balance>3000 print that object.
How can I declare balance? Should I read the bytes for balance and print it ?
File a = new File("customer.txt");
FileWriter v = new FileWriter(a);
BufferedWriter b = new BufferedWriter(v);
PrintWriter p = new PrintWriter(b);
human Iman = new human("Iman", 5000);
human Nour = new human("Nour", 3500);
human Redah = new human("Redah", 0);
human iman = new human("iman", 200);
human MohamedREDA = new human("MohamedREDA", 3000);
human Mohamed_Redah = new human("Mohamed Redah", 2000);
human[] h = new human[6];
h[0] = Iman;
h[1] = Nour;``
h[2] = Redah;
h[3] = iman;
h[4] = MohamedREDA;
h[5] = Mohamed_Redah;
p.println(Iman);
p.println(Nour);
p.println(Redah);
p.println(iman);
p.println(MohamedREDA);
p.println(Mohamed_Redah);
p.flush();
}
}
class human {
public String name;
public double balance;
public human(String n, double b) {
this.balance = b;
this.name = n;
}@
Override
public String toString() {
return name + " " + balance;
}
Upvotes: 0
Views: 88
Reputation: 122008
Change your logic as below:
Loop through over the array
for (int i = 0; i < h.length; i++){
if(h[i].balance>3000){
p.println(h[i]);
}
}
Upvotes: 2