其实这很明显是某位同学的作业, 不过充其量是个平时作业, 完全不是大作业, 写出来只是觉得此楼的某些争吵触到我的痛点...
关于版权, 这些代码不想保留任何版权
关于诈骗, 对此我保留意见
关于Java程序员的水准, 其实真正让我动手写的原因就是这个, 虽然写了还是可能被BS(包括任何新的理由)
关于30楼某位的类似于"语言不太重要"的说法, 我对这种流行的说法完全无法认同啊
如果这种粘贴大段代码的行为污染了v2ex的话, 管理员请删了, 绝对没意见..
import java.util.Arrays;
import java.util.Scanner;
public class BankSimulation {
public static Scanner scanner = new Scanner(
System.in);
private Bank bank = new Bank();
public void openAccount(){
System.out.print("Enter new account number: ");
String name = scanner.nextLine();
System.out.print("Enter initial balance: ");
double initial = scanner.nextDouble();
scanner.nextLine();
bank.openAccount(name, initial);
System.out.println();
}
public void closeAccount(){
bank.closeAccount();
System.out.println();
}
public void selectAccount(){
System.out.print("Enter account number: ");
String name = scanner.nextLine();
bank.selectAccount(name);
System.out.println();
}
public void deposit(){
System.out.print("Enter amount: ");
double amount = scanner.nextDouble();
scanner.nextLine();
bank.deposit(amount);
System.out.println();
}
public void withdraw(){
System.out.println("Enter amount: ");
double amount = scanner.nextDouble();
scanner.nextLine();
bank.withdraw(amount);
System.out.println();
}
public void quit(){
System.exit(0);
}
public void displayPrompt(){
System.out.println("-------------------------------------------------");
System.out.println("| Commands: o - Open account c - Close account|");
System.out.println("| d - Deposit w - Withdraw |");
System.out.println("| s - Select account q - Quit |");
System.out.println("-------------------------------------------------");
System.out.print("Current Account: ");
BankAccount current = bank.getCurrent();
if(current == null){
System.out.println("None Selected");
}else{
System.out.println(current);
}
System.out.println();
System.out.println();
System.out.print("Enter Command: ");
}
public static void main(String[] args) {
BankSimulation simu = new BankSimulation();
while(true){
simu.displayPrompt();
String line = scanner.nextLine();
if(line.length() == 0) continue;
char ans = Character.toUpperCase(line.charAt(0));
switch(ans){
case 'O' : simu.openAccount(); break;
case 'C' : simu.closeAccount(); break;
case 'S' : simu.selectAccount(); break;
case 'D' : simu.deposit(); break;
case 'W' : simu.withdraw(); break;
case 'Q' : simu.quit();
default: break;
}
}
}
}
class Bank {
private BankAccount[] accounts;
private int numAccounts = 0;
BankAccount currentAccount = null;
public Bank(){
accounts = new BankAccount[10];
}
public void openAccount(String name, double initial){
if(numAccounts == accounts.length){
accounts = Arrays.copyOf(accounts, accounts.length * 2);
}
accounts[numAccounts++] = new BankAccount(name, initial);
currentAccount = accounts[numAccounts - 1];
}
/**
*
* @
param name
* @
return true if the specified account is found
*/
public boolean selectAccount(String name){
for(int i = 0; i < numAccounts; i++){
if(accounts[i].getName().equals(name)){
currentAccount = accounts[i];
return true;
}
}
return false;
}
public void closeAccount(){
currentAccount = null;
}
public void deposit(double amount){
if(currentAccount != null){
currentAccount.deposit(amount);
}
}
public boolean withdraw(double amount){
if(currentAccount != null){
return currentAccount.withdraw(amount);
}
return false;
}
public BankAccount getCurrent(){
return currentAccount;
}
}
class BankAccount {
private String accountName;
private double balance;
public BankAccount(String name, double initial){
accountName = name;
balance = initial;
}
public void deposit(double amount){
balance += amount;
}
public boolean withdraw(double amount){
if(balance >= amount){
balance -= amount;
return true;
}else{
return false;
}
}
public String getName() {
return accountName;
}
public String toString(){
return accountName + " Balance: " + balance;
}
}
// 给楼主的ps: 之所以bank里的accounts用了基本的数组, 是根据题目的要求, 否则肯定会用ArrayList