Exercise A02

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

การโปรแกรมเชิงวัตถุเบือ้ งต้น รายงาน-แบบฝึกหัด

รายงาน-แบบฝึ กหัดที่ 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;
}

public void setId(String id) {


this.id = id;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public double getBalance() {


return balance;
}

// 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
การโปรแกรมเชิงวัตถุเบือ้ งต้น รายงาน-แบบฝึกหัด

Account other = (Account) obj;


return id.equals(other.getId());
}

// Deposit - amount > 0


public boolean deposit(double amount) {
boolean success = false;
if(amount > 0){
balance += amount;
success = true;
}
return success;
}

// Withdraw - amount < balance


public boolean withdraw(double amount) {
boolean success = false;
if(amount < balance){
balance = balance - amount;
success = true;
}
return success;
}

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;

public class BankAccount {

private String fileName;


private ArrayList<Account> accList;

// 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;

Account acc = search(id);


if (acc != null) {
success = acc.deposit(amount);
}

return success;
}

// Withdraw
public boolean withdraw(String id, double amount) {
boolean success = false;

Account acc = search(id);


if (acc != null) {
success = acc.withdraw(amount);
}

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;
}

public class SortAccountById implements Comparator<Account> {


public int compare(Account a1, Account a2) {
return a1.getId().compareTo(a2.getId());
}
}

//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
การโปรแกรมเชิงวัตถุเบือ้ งต้น รายงาน-แบบฝึกหัด

int ind = accList.indexOf(a);


if (ind != -1) {
a = accList.get(ind);
accList.remove(ind);
success = true;
}
return success;
}

// 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);
}

// Format Current Date Time


public static String formatDateNow() {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter df = DateTimeFormatter.ofPattern("dd/MMM/yyyy HH:mm")
.withChronology(ThaiBuddhistChronology.INSTANCE);
return df.format(now);
}

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());

for (int j = start; j < end; j++) {


Account acc = accList.get(j);
String id = acc.getId();
String name = acc.getName();
double balance = acc.getBalance();
System.out.printf("%-10s %-15s %-15s %-10s\n", count, id,
formatNumber(balance), name);
count++;
total += balance;
grd_total += balance;
}
//Footer
System.out.printf("--------------------------------------------------
-----\n");
System.out.printf("%16s %17s %15s %s/%s\n", "Total",
formatNumber(total), "Page:", i, total_page);
}

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy