public class Ex5 {
public static void main(String[] args) {
Expense[] e = {
new Expense("lodging"),
new Expense("meals"),
new Expense("incidentals")
};
ReportGenerator.create(e);
}
}
|
import java.util.*;
public class Expense {
private String descrip;
private double amount;
public Expense(String descrip) {
this.descrip = descrip;
amount = 0.0;
}
public void setAmount(double a) {
amount = a;
}
public void ask(Scanner sc) {
System.out.print("Enter dollar amount for "
+ getDescrip()
+ " (0 for none): ");
amount = sc.nextDouble();
}
public String getDescrip() {
return descrip;
}
public double getAmount() {
return amount;
}
}
|
import java.util.*;
public class ReportGenerator {
// Query users for amounts of the
// expense categories listed in
// array e, and output expense report.
public static void create(Expense[] e) {
Scanner sc = new Scanner(System.in);
for (int i = 0; i < e.length; i++) {
e[i].ask(sc);
}
System.out.println();
double total = 0.0;
for (int i = 0; i < e.length; i++) {
double x = e[i].getAmount();
double y = Math.floor(x * 100 + 0.5) / 100.0;
total += y;
System.out.printf(" $%07.2f\t%s\n", y, e[i].getDescrip());
}
System.out.printf("---------\n $%07.2f\ttotal\n", total);
}
}
|