Exercise A02
Exercise A02
Exercise A02
รายงาน-แบบฝึ กหัดที่ 2
Task 1 - Account.java
public class Account {
private String id;
private String name;
private double balance;
// Default constructor
public Account() {
super();
}
// Constructor
public Account(String id, String name, double balance) {
super();
this.id = id;
this.name = name;
this.balance = balance;
}
// Mutator / Accessor
public String getId() {
return id;
}
// Special case
public void setBalance(double balance) {
this.balance = balance;
}
// toString
@Override
public String toString() {
return "Account [id=" + id + ", name=" + name + ", balance=" + balance +
"]";
}
// equals
@Override
public boolean equals(Object obj) {
1/6
การโปรแกรมเชิงวัตถุเบือ้ งต้น รายงาน-แบบฝึกหัด
2/6
การโปรแกรมเชิงวัตถุเบือ้ งต้น รายงาน-แบบฝึกหัด
Task 2 - BankAccount.java
import java.util.ArrayList;
import java.util.Collections;
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
import java.time.chrono.ThaiBuddhistChronology;
import java.text.DecimalFormat;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.util.Comparator;
// Default constructor
public BankAccount() {
super();
}
// Constructor
public BankAccount(String fileName) {
super();
this.fileName = fileName;
accList = new ArrayList<Account>();
}
// toString
@Override
public String toString() {
return "BankAccount [accList=" + accList + "]";
}
// Deposit
public boolean deposit(String id, double amount) {
boolean success = false;
return success;
}
// Withdraw
public boolean withdraw(String id, double amount) {
boolean success = false;
3/6
การโปรแกรมเชิงวัตถุเบือ้ งต้น รายงาน-แบบฝึกหัด
return success;
}
// Empty
public void clear() {
accList.clear();
}
// Search
public Account search(String id) {
Account found = null;
Account s = new Account(id, "", 0.0);
int ind = accList.indexOf(s);
if (ind != -1) {
found = accList.get(ind);
}
return found;
}
//sort by-id
public void sortAccountById() {
Collections.sort(accList, new SortAccountById());
}
// Add
public boolean add(String id, String name, double balance) {
boolean success = false;
Account a = new Account(id,name,balance);
if (!accList.contains(a)) {
accList.add(a);
sortAccountById();
success = true;
}
return success;
}
// Update
public boolean update(String id, String name, double balance) {
boolean success = false;
Account a = new Account(id,"",0.0);
int ind = accList.indexOf(a);
if( ind != -1) {
a = accList.get(ind);
a.setName(name);
a.setBalance(balance);
success = true;
}
return success;
}
// Delete
public boolean delete(String id) {
boolean success = false;
Account a = new Account(id,"",0.0);
4/6
การโปรแกรมเชิงวัตถุเบือ้ งต้น รายงาน-แบบฝึกหัด
// Open
public boolean open() {
boolean success = true;
String fileName = this.fileName + ".txt";
try (BufferedReader reader = new BufferedReader(new
FileReader(fileName))) {
String line;
while ((line = reader.readLine()) != null) {
String[] arrs = line.split(",");
String id = arrs[0];
String name = arrs[1];
double balance = Double.parseDouble(arrs[2]);
add(id, name, balance);
}
} catch (IOException e) {
success = false;
}
return success;
}
// Save
public boolean save() {
boolean success = true;
String fileName = this.fileName + ".txt";
try (BufferedWriter writer = new BufferedWriter(new
FileWriter(fileName))) {
for (Account acc : accList) {
writer.write(acc.getId() + "," + acc.getName() + "," +
acc.getBalance());
writer.newLine();
}
} catch (IOException e) {
success = false;
}
return success;
}
// Format Number
public static String formatNumber(double num) {
DecimalFormat nf = new DecimalFormat("#,###,###,##0.00");
return nf.format(num);
}
5/6
การโปรแกรมเชิงวัตถุเบือ้ งต้น รายงาน-แบบฝึกหัด
// Report
public void report() {
int per_page = 20;
int total_page = (int) Math.ceil(1.0 * accList.size() / per_page);
double grd_total = 0.0;
System.out.printf("Account Report\n");
System.out.printf("=======================================================\n");
for (int i = 1; i <= total_page; i++) {
//Head
System.out.printf("%-10s %-15s %-15s %-10s\n", "No", "Id", "Balance",
"Name");
System.out.printf("--------------------------------------------------
-----\n");
//Body
double total = 0.0;
int count = (i - 1) * per_page + 1;
int start = (i - 1) * per_page;
int end = Math.min(start + per_page, accList.size());
System.out.printf("=======================================================\n");
System.out.printf("%16s %17s %15s %s\n", "Grand Total",
formatNumber(grd_total), "Total Page:", total_page);
System.out.printf("%55s",formatDateNow());
}
}
6/6