-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccounts.java
81 lines (66 loc) · 3.02 KB
/
Accounts.java
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
import java.io.*;
import java.util.*;
class Accounts {
public static void main(String args[]) throws Exception {
File f1 = new File ("master.txt");
File f2 = new File("transaction.txt");
Scanner input = new Scanner(f1);
// Read the master file
while (input.hasNext()) {
String lineRead = input.nextLine();
String [] parts = lineRead.split(" ");
int customerNumber = Integer.parseInt(parts[0]);
String customerName = parts[1];
double balanceDue = Double.parseDouble(parts[2]);
// Print the records for the current customer
printRecords(customerName, customerNumber, balanceDue);
// View the transaction records for the master record and get balance due
balanceDue = transaction(customerNumber, customerName, balanceDue, f2);
System.out.printf("%30s Balance Due $%.2f\n", "", balanceDue);
}
}
// Handle the transactions of the customer account from the transaction file
public static double transaction(int customerNumber, String customerName, double balanceDue, File f2) throws Exception{
Scanner input = new Scanner(f2);
int transNb = 1;
while (input.hasNext()) {
String lineRead = input.nextLine();
String [] parts = lineRead.split(" ");
char records = parts[0].charAt(0);
int number = Integer.parseInt(parts[1]);
// Check if the invididual placed an order
if (records == 'O') {
int quantity = Integer.parseInt(parts[2]);
String itemName = parts[3];
double cost = Double.parseDouble(parts[4]);
// Check if the record has a discount
if (parts.length > 5) {
int discount = Integer.parseInt(parts[5]);
cost -= (cost * discount) / 100.0;
}
double amounts = quantity * cost;
balanceDue += amounts;
System.out.printf("%-30d %-20s %f\n", transNb, itemName, amounts);
}
// Check if the individual placed an payment
else if (records == 'P') {
double payments = Double.parseDouble(parts[2]);
// Check if the record has a discount
if (parts.length > 3) {
int discount = Integer.parseInt(parts[3]);
payments -= (payments * discount) / 100.0;
}
balanceDue -= payments;
System.out.printf("%-30d %-20s %f\n", transNb, "payment", payments);
}
transNb++;
}
return balanceDue;
}
// Print the customer records
public static void printRecords(String customerName, int customerNumber, double balanceDue) {
System.out.printf("%-30s %d\n", customerName, customerNumber);
System.out.printf("%30s Previous balance $%.2f\n", "", balanceDue);
System.out.println();
}
}