By GokiSoft.com|
19:11 06/05/2024|
Java Basic
[Share Code] Java Basic - Quản lý ATM - Quản lý tài khoản ngân hàng - C2307L
Java Basic - Quản lý ATM - Quản lý tài khoản ngân hàng
#Transaction.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package lesson10;
import java.text.Format;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
* @author diepvan
*/
public class Transaction {
String receiverNo;
int money;
Date createdAt;
String content;
public Transaction() {
this.createdAt = new Date();
}
public Transaction(String receiverNo, int money, String content) {
this.receiverNo = receiverNo;
this.money = money;
this.content = content;
this.createdAt = new Date();
}
public String getReceiverNo() {
return receiverNo;
}
public void setReceiverNo(String receiverNo) {
this.receiverNo = receiverNo;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "receiverNo=" + receiverNo + ", money=" + getFormatMoney() + ", createdAt=" + getDateStr() + ", content=" + content;
}
String getFormatMoney() {
NumberFormat formatter = NumberFormat.getCurrencyInstance();
String moneyString = formatter.format(money);
return moneyString;
}
String getDateStr() {
Format formatter = new SimpleDateFormat("HH:mm dd/MM/yyyy");
String s = formatter.format(createdAt);
return s;
}
public void display() {
System.out.println(this);
}
}
#History.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package lesson10;
import java.text.Format;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
* @author diepvan
*/
public class History {
int money;
Date createdAt;
String content;
public History() {
this.createdAt = new Date();
}
public History(int money, String content) {
this.money = money;
this.content = content;
this.createdAt = new Date();
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "money=" + getFormatMoney() + ", createdAt=" + getDateStr() + ", content=" + content;
}
String getFormatMoney() {
NumberFormat formatter = NumberFormat.getCurrencyInstance();
String moneyString = formatter.format(money);
return moneyString;
}
String getDateStr() {
Format formatter = new SimpleDateFormat("HH:mm dd/MM/yyyy");
String s = formatter.format(createdAt);
return s;
}
public void display() {
System.out.println(this);
}
}
#Account.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package lesson10;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
*
* @author diepvan
*/
public class Account {
String fullname;
String accountNo;
int amount;
String phoneNumber;
String address;
List<History> historys;
List<Transaction> transactions;
public Account() {
historys = new ArrayList<>();
transactions = new ArrayList<>();
}
public Account(String fullname, String accountNo, int amount, String phoneNumber, String address) {
this.fullname = fullname;
this.accountNo = accountNo;
this.amount = amount;
this.phoneNumber = phoneNumber;
this.address = address;
historys = new ArrayList<>();
transactions = new ArrayList<>();
}
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
public String getAccountNo() {
return accountNo;
}
public void setAccountNo(String accountNo) {
this.accountNo = accountNo;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap ten: ");
fullname = scan.nextLine();
System.out.println("Nhap STK: ");
accountNo = scan.nextLine();
System.out.println("Nhap so tien: ");
amount = Integer.parseInt(scan.nextLine());
System.out.println("Nhap SDT: ");
phoneNumber = scan.nextLine();
System.out.println("Nhap dia chi: ");
address = scan.nextLine();
}
public void inputMoney() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap so tien can nap: ");
int amountInput = Integer.parseInt(scan.nextLine());
System.out.println("Noi dung nap tien: ");
String content = scan.nextLine();
this.amount += amountInput;
History his = new History();
his.money = amountInput;
his.content = content;
historys.add(his);
}
public void transaction() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap so tien can gui: ");
int amountInput = Integer.parseInt(scan.nextLine());
if(amountInput > this.amount) {
System.out.println("So tien gui vuot qua so tien trong tai khoan");
return;
}
System.out.println("Nhap STK nhan: ");
String receiverNo = scan.nextLine();
System.out.println("Nhap noi dung: ");
String content = scan.nextLine();
Transaction trans = new Transaction(receiverNo, amountInput, content);
transactions.add(trans);
}
public void displayHistory() {
System.out.println("Lich su nap tien");
for (History history : historys) {
history.display();
}
}
public void displayTransaction() {
System.out.println("Lich su gui tien");
for (Transaction trans : transactions) {
trans.display();
}
}
@Override
public String toString() {
return "fullname=" + fullname + ", accountNo=" + accountNo + ", amount=" + getFormatMoney() + ", phoneNumber=" + phoneNumber + ", address=" + address;
}
String getFormatMoney() {
NumberFormat formatter = NumberFormat.getCurrencyInstance();
String moneyString = formatter.format(amount);
return moneyString;
}
public void display() {
System.out.println(this);
}
}
#Main.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package lesson10;
import java.util.Scanner;
/**
*
* @author diepvan
*/
public class Main {
static final String USER_NAME = "dieptv";
static final String PWD = "123456";
static Account account;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int choose;
System.out.println("=== WELCOME ===");
do {
System.out.println("Nhap tai khoan: ");
String uname = scan.nextLine();
System.out.println("Nhap mat khau: ");
String pwd = scan.nextLine();
if(uname.equalsIgnoreCase(USER_NAME) && pwd.equals(PWD)) {
//Login thanh cong
System.out.println("Dang nhap thanh cong");
break;
} else {
System.out.println("Yeu cau dang nhap lai!!!");
}
} while(true);
account = new Account();
do {
showMenu();
choose = Integer.parseInt(scan.nextLine());
switch (choose) {
case 1 -> account.input();
case 2 -> account.inputMoney();
case 3 -> account.transaction();
case 4 -> account.displayHistory();
case 5 -> account.displayTransaction();
case 6 -> System.out.println("Thoat!!!");
default -> System.out.println("Nhap sai!!!");
}
} while(choose != 6);
}
static void showMenu() {
System.out.println("1. Khoi tao tai khoan");
System.out.println("2. Nap tien");
System.out.println("3. Gui tien");
System.out.println("4. Xem lich su nap tien");
System.out.println("5. Xem lich su gui tien");
System.out.println("6. Thoat");
System.out.println("Chon: ");
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)