คลาสจำลองบัญชีธนาคารและการทำธุรกรรม
โจทย์
เขียนคลาส Java ชื่อ Account คลาสควรมีคุณสมบัติดังนี้:
- แอตทริบิวต์ (หรือฟิลด์ข้อมูล)
- แอตทริบิวต์ private ชนิด int ชื่อ “id” สำหรับบัญชี
- แอตทริบิวต์ private ชนิด double ชื่อ “balance” สำหรับยอดเงินในบัญชี
- แอตทริบิวต์ private ชนิด double ชื่อ “annualInterestRate” ที่เก็บอัตราดอกเบี้ยปัจจุบัน
- เมธอด
- เมธอด getter และ setter สำหรับ id, balance, และ annualInterestRate
- เมธอดชื่อ getMonthlyInterestRate() ที่ส่งคืนอัตราดอกเบี้ยรายเดือน
- เมธอดชื่อ getMonthlyInterest() ที่ส่งคืนดอกเบี้ยรายเดือน
- เมธอดชื่อ withdraw ที่ถอนเงินจำนวนที่ระบุจากบัญชี
- เมธอดชื่อ deposit ที่ฝากเงินจำนวนที่ระบุเข้าบัญชี
| Input | Output |
|---|---|
| 1122 20000 4.5 3000 0 | New Balance after deposit: 23000.0 New Balance after withdrawal: 23000.0 Account Statement ------------------------ Account ID: 1122 Balance: 23000.00 Monthly interest: 86.25 |
โค้ดเริ่มต้น
public class AccountDriver {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int id = sc.nextInt();
double balance = sc.nextDouble();
double annualInterestRate = sc.nextDouble();
// Create an Account object with the specified details
Account acc = new Account(id, balance, annualInterestRate);
// Deposit funds
double depositAmount = sc.nextDouble();
acc.deposit(depositAmount);
System.out.println("New Balance after deposit: " + acc.getBalance());
// Withdraw funds
double withdrawAmount = sc.nextDouble();
acc.withdraw(withdrawAmount);
System.out.println("New Balance after withdrawal: " + acc.getBalance());
// Display the balance, the monthly interest,
// and the date when this account was created
System.out.println("\nAccount Statement");
System.out.println("------------------------");
System.out.println("Account ID: " + acc.getId());
System.out.printf("Balance: %.2f\n", acc.getBalance());
System.out.printf("Monthly interest: %.2f\n",
acc.getMonthlyInterest());
}
}
class Account {
// Write your code here
// Constructor - DO NOT CHANGE HERE
Account() {
id = 0;
balance = 0.0;
annualInterestRate = 0.0;
}
Account(int newId, double newBalance, double newAnnualInterestRate) {
id = newId;
balance = newBalance;
annualInterestRate = newAnnualInterestRate;
}
// Write your code here
}โค้ด
import java.util.Scanner;
public class AccountDriver {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int id = sc.nextInt();
double balance = sc.nextDouble();
double annualInterestRate = sc.nextDouble();
// Create an Account object with the specified details
Account acc = new Account(id, balance, annualInterestRate);
// Deposit funds
double depositAmount = sc.nextDouble();
acc.deposit(depositAmount);
System.out.println("New Balance after deposit: " + acc.getBalance());
// Withdraw funds
double withdrawAmount = sc.nextDouble();
acc.withdraw(withdrawAmount);
System.out.println("New Balance after withdrawal: " + acc.getBalance());
// Display the balance, the monthly interest,
// and the date when this account was created
System.out.println("\nAccount Statement");
System.out.println("------------------------");
System.out.println("Account ID: " + acc.getId());
System.out.printf("Balance: %.2f\n", acc.getBalance());
System.out.printf("Monthly interest: %.2f\n",
acc.getMonthlyInterest());
}
}
class Account {
// Write your code here
private int id;
private double balance;
private double annualInterestRate;
// Constructor - DO NOT CHANGE HERE
Account() {
id = 0;
balance = 0.0;
annualInterestRate = 0.0;
}
Account(int newId, double newBalance, double newAnnualInterestRate) {
id = newId;
balance = newBalance;
annualInterestRate = newAnnualInterestRate;
}
public int getId() {
return id;
}
public double getBalance() {
return balance;
}
public double getMonthlyInterestRate() {
return annualInterestRate / 12.0;
}
public double getMonthlyInterest() {
return getMonthlyInterestRate() * (balance / 100.0);
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
double newBalance = balance - amount;
if (newBalance >= 0)
balance -= amount;
}
}ส่วนประกอบของคลาส
1. ตัวแปร (Attributes หรือ Data Fields)
private int id;
private double balance;
private double annualInterestRate;id: เลขที่บัญชี (เป็นตัวเลขจำนวนเต็ม)balance: ยอดเงินในบัญชี (เป็นตัวเลขทศนิยม)annualInterestRate: อัตราดอกเบี้ยต่อปี (เป็นตัวเลขทศนิยม)
ทั้งหมดนี้ถูกประกาศเป็น private ซึ่งหมายความว่าจะเข้าถึงได้เฉพาะภายในคลาส Account เท่านั้น เพื่อความปลอดภัยของข้อมูล
2. คอนสตรักเตอร์ (Constructors)
Account() {
id = 0;
balance = 0.0;
annualInterestRate = 0.0;
}
Account(int newId, double newBalance, double newAnnualInterestRate) {
id = newId;
balance = newBalance;
annualInterestRate = newAnnualInterestRate;
}คอนสตรักเตอร์เป็นวิธีการสร้างวัตถุ (object) ของคลาส Account:
- คอนสตรักเตอร์แรกสร้างบัญชีว่างๆ โดยกำหนดค่าเริ่มต้นทั้งหมดเป็น 0
- คอนสตรักเตอร์ที่สองสร้างบัญชีโดยรับค่าเริ่มต้นจากภายนอก
3. เมธอดดึงข้อมูล (Accessor Methods หรือ Getters)
public int getId() {
return id;
}
public double getBalance() {
return balance;
}เมธอดเหล่านี้ใช้สำหรับดึงข้อมูลจากตัวแปรภายในที่เป็น private ออกมาใช้ภายนอกคลาส
4. เมธอดคำนวณดอกเบี้ย
public double getMonthlyInterestRate() {
return annualInterestRate / 12.0;
}
public double getMonthlyInterest() {
return getMonthlyInterestRate() * (balance / 100.0);
}getMonthlyInterestRate(): คำนวณอัตราดอกเบี้ยต่อเดือนโดยหารอัตราดอกเบี้ยต่อปีด้วย 12getMonthlyInterest(): คำนวณดอกเบี้ยที่จะได้รับในเดือนนั้นๆ
5. เมธอดฝากและถอนเงิน
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
double newBalance = balance - amount;
if (newBalance >= 0)
balance -= amount;
}deposit(): เพิ่มจำนวนเงินที่ฝากเข้าไปในยอดเงินปัจจุบันwithdraw(): ลดจำนวนเงินที่ถอนออกจากยอดเงินปัจจุบัน แต่จะทำการถอนเฉพาะเมื่อยอดเงินคงเหลือไม่ติดลบเท่านั้น
ปรับปรุงล่าสุด