package main; import java.util.ArrayList; /** * (10 points) * Do a simple bank and account demonstration program. * (1) Define an account class, including attributes such as username, password, balance, and methods such as depositing and withdrawing money. * (2) Define a bank class, including attributes such as bank name, account list, etc., and the functions of opening accounts and checking accounts. A list of accounts can use collection-related classes, * Such as ArrayList, it is best to use generics at the same time. * (3) Define a test class and main function, create a bank in it, open some accounts, simulate depositing money, withdrawing money, displaying balance, etc. */ public class Main { public static void main(String[] args){ Bank bank = new Bank("HDBank"); bank.openAccount("xiaoming","666",0); bank.openAccount("xiaoli","777",0); bank.openAccount("xiaohong","888",0); bank.saveMoney("xiaoli",100); // Deposit money to a specific account bank.drawMoney("xiaoli",50,"777"); //To withdraw money from a specific account, you need to verify the password bank.showAccount("xiaoli"); //Display xiaoli account information bank.drawMoney("xiaoli",100,"a wrong password"); //If the password is wrong, the withdrawal of money fails bank.drawMoney("xiaoli",100,"777"); //Insufficient balance } } class Account{ private String userName; private String password; private double balance; Account(String userName,String password,double balance){ this.userName = userName; this.password = password; this.balance = balance; } public void save(double money){ //save money balance += money; System.out.printf("%s has deposited %.2f yuan, and the account balance is %.2f yuan.\n",userName,money,balance); } public void draw(double money){ //Withdraw money, if the account balance is insufficient, the money cannot be withdrawn if(balance - money <= 0) { System.out.printf("%s withdraws money %.2f, and the account balance is %.2f yuan.",userName,money,balance); System.out.println("Insufficient balance, failed to withdraw money."); }else { balance -= money; System.out.printf("%s took %.2f yuan, and the account balance is %.2f yuan.\n", userName, money, balance); } } public void show(){ //display account information System.out.printf("userName:%s\n",userName); System.out.printf("balance:%.2f\n",balance); } public String getUserName(){ //get username return userName; } public String getPassword(){ // get the password return password; } public String getInfo(){ //return account information return "userName:"+userName+",banlance:"+balance; } } class Bank{ public String bankName; private ArrayList<Account> accounts; Bank(String bankName){ this.bankName=bankName; accounts=new ArrayList<Account>(); } Bank(String bankName,ArrayList<Account> accounts){ this.bankName=bankName; this.accounts=accounts; } public void openAccount(String userName,String password,double balance){ //open an account accounts.add(new Account(userName,password,balance)); } public Account seekAccount(String userName){ //Find the account by user name, if found, return the account, otherwise, return null for(Account account:accounts){ if(userName.equals(account.getUserName())) return account; } return null; } public void showAccount(String userName){ Account account=seekAccount(userName); if(account==null) System.out.println("The account does not exist."); else account.show(); } public void saveMoney(String userName,double money){ // Deposit money to a specific account Account account = seekAccount(userName); //查找 if(account==null) System.out.println("The account does not exist."); else account.save(money); } public void drawMoney(String userName,double money,String password){ //To withdraw money from a specific account, you need to verify the password Account account = seekAccount(userName); //查找 if(account==null) System.out.println("The account does not exist."); else { if(password.equals(account.getPassword())) { account.draw(money); }else { System.out.println("The password is incorrect. Failed to withdraw money."); } } } }
java practice questions - bank customers
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=325378336&siteId=291194637
Ranking