我编写了一个 ATM 接口,用户可以在其中取款、存款或检查他/她的帐户余额。在此代码中,每次我再次运行代码时,余额都会重置为 15000(我的初始设置值)。
这是我的代码:
import java.util.Scanner;
abstract class ATMmachine {
abstract void depositAmount();
abstract void withdrawAmount();
}
class userAccount extends ATMmachine {
Scanner sc = new Scanner(System.in);
private int withdraw, deposit;
private int balance = 15000;
public void withdrawAmount() {
boolean positiveAmount = true;
while (positiveAmount) {
System.out.print("Please enter withdrawal amount:");
withdraw = sc.nextInt();
if (withdraw < 0) {
System.out.println("Please enter a positive number!");
}
else if (withdraw >= 0) {
positiveAmount = false;
}
}
if (withdraw <= balance) {
balance -= withdraw;
System.out.println("An amount of " + withdraw + " has been withdrawn. Remaining balance: " + balance);
positiveAmount = false;
}
else if (withdraw > balance) {
System.out.println("Error, insufficent funds!");
}
}
public void depositAmount(){
boolean positiveDeposit = true;
while (positiveDeposit) {
System.out.print("Please enter deposit amount:");
deposit = sc.nextInt();
if (deposit > 0) {
balance += deposit;
System.out.println("You made a deposit of " + deposit + " your balance now is : " + balance);
positiveDeposit = false;
}
else {
System.out.println("Error, balance cannot be negative!");
}
}
}
public int getBalance() {
return balance;
}
}
public class atmsystem {
public static void main(String[] args) {
String[] options = {"Withdraw amount","Deposit amount","Check your balance"};
Scanner scanner = new Scanner(System.in);
userAccount obj1 = new userAccount();
try {
System.out.print("Welcome to Platino bank, please select an option: \n");
for (int index = 0; index < options.length; index++) {
System.out.println(index + 1 + " => " + options[index]);
}
int select = scanner.nextInt();
if (select == 1){
obj1.withdrawAmount();
}
else if (select == 2) {
obj1.depositAmount();
}
else if (select == 3) {
obj1.getBalance();
System.out.println(obj1.getBalance());
}
else {
System.out.println("Error, no such option exists!");
}
scanner.close();
}
catch (Exception e) {
System.out.println("Please enter an integer value!");
}
}
}
这段代码出了什么问题,我可以进行哪些更改,以便每次 getter 方法都能反映更新后的余额?
你只需要在你的代码中添加两行,并进行一些小的修改,错误是在第一次操作后,你退出应用程序,你必须创建一个循环,让你继续操作:
class atmsystem {
public void main() {
String[] options = { "Withdraw amount", "Deposit amount", "Check your balance", "Exit" };
Scanner scanner = new Scanner( System.in );
userAccount obj1 = new userAccount();
try {
System.out.print( "Welcome to Platino bank, please select an option: \n" );
// insert this line, to create a loop
while( true ) {
for( int index = 0; index < options.length; index ++ ) {
System.out.println( index + 1 + " => " + options[ index ] );
}
int select = scanner.nextInt();
if( select == 1 ) {
obj1.withdrawAmount();
}...
// with this "else if", we give the user the option to exit
else if( select == 4 ) {
System.out.println( "bye" );
scanner.close();
break;
}
else {
System.out.println( "Error, no such option exists!" );
}
// move "scanner.close();" to "else if" with "select == 4"
}
}
catch( Exception e ) {
System.out.println( "Please enter an integer value!" );
}
}
}