遇到java代码的问题

时间:2016-06-29 17:39:29

标签: java methods

以下代码使用main方法在每次交易后打印帐号和更新后的余额,或直到输入Q.

在methodUpdate余额中,给定账户余额,交易类型(D =存款,W =撤回,Q =退出)和交易金额,计算机并在存款后返还新账户余额或撤回给定的金额。

但是现在我遇到了问题并且不确定如何修复我生成的代码

import java.util.Scanner ;
public class CustomerAccount
{
public static void main( String[] args ) 
{
    Scanner in = new Scanner(System.in);
    System.out.print("Please enter the account number: ") ;
    String accountNumber = in.nextLine() ; 
    System.out.print("Please enter the initial balance: ") ;
    int startingBal = in.nextInt() ;
    int updatedBal = startingBal ;
    System.out.print("Please enter the transaction type /(D to deposit, W to withdraw, Q to quit: ") ;
    String type = in.nextLine() ;
    while(!"Q".equals(type)) 
    {
        System.out.print("Please enter the amount to be deposited or withdrawn: ") ;
        int adjustment = in.nextInt();
        if(type.equals("D"))
        {
            updatedBal = updatedBalance(depositAmt);
        }
        else 
            updatedBal = updatedBalance(withdrawAmt);
        }
        System.out.println("Account Number: " + accountNumber + "    UpdatedBalance: $" + updatedBal) ;
    }
}
public static int updatedBalance(int amount)
{
    amount = amount + adjustment ;
    return amount;
}
}

给出了以下输出:

[File: /CustomerAccount.java Line: 28, Column: 19] class, interface, or enum expected
[File: /CustomerAccount.java Line: 31, Column: 9] class, interface, or enum expected
[File: /CustomerAccount.java Line: 32, Column: 5] class, interface, or enum expected

1 个答案:

答案 0 :(得分:2)

这是一个小小的改进'您的代码版本,但仍有很多事情要做。看来你是初学者,我所做的主要修改是抽象。我添加了一个名为" CustomerAccount"和哪些商店;

  • 有关帐户的数据
  • 存款和取款功能等方法

因此,现在使用CustomerAccount类封装了帐户数据和功能。

程序的入口点成为CustomerAccountHandler类。

以下是代码和输出,希望这些都有助于您的学习过程;

A - CustomerAccount Class

public class CustomerAccount {

    private final String accountId; // account number is final, it should never be updated
    private int amount;

    // Constructor with two paremeters
    public CustomerAccount(String accountId, int initialAmount) {
        this.accountId = accountId;
        this.amount = initialAmount;
    }

    // Funcionality : deposits to the amount
    // Returns      : latest amount
    public int deposit(int adjustment) {
        this.amount = amount + adjustment;

        return this.amount;
    }

    // Funcionality : witdraws from the amount
    // Returns      : latest amount
    public int withdraw(int adjustment) {
        this.amount = amount - adjustment;

        return this.amount;
    }

    // getters

    public String getAccountId() {
        return this.accountId;
    }

    public int getAmount() {
        return this.amount;
    }

}

B - 演示代码

import java.util.Scanner;

public class CustomerAccountHandler {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        System.out.print("Please enter the account number: ");
        String accountId = in.nextLine();
        System.out.print("Please enter the initial balance: ");
        int startingBal = Integer.parseInt(in.nextLine());

        // Create the account here
        CustomerAccount account = new CustomerAccount(accountId, startingBal);

        int updatedBal, adjustment; // create here, we will use it a lot
        String type;

        while (true) {
            System.out.println("Please enter the transaction type /(D to deposit, W to withdraw, Q to quit: ");
            type = in.nextLine();

            if(type.equals("Q")) {
                System.out.println("Quitting transactions...");
                break;
            } else if(type.equals("W") || type.equals("D")) {
                System.out.println("Please enter the amount to be deposited or withdrawn: ");
                adjustment = Integer.parseInt(in.nextLine());

                if (type.equals("D")) {
                    updatedBal = account.deposit(adjustment);
                    System.out.println("Updated Balanced : " + updatedBal);
                } else if(type.equals("W")) {
                    updatedBal = account.withdraw(adjustment);
                    System.out.println("Updated Balanced : " + updatedBal);
                }
            } else {
                System.out.println("Please enter a valid Command: D,W,Q");
            }
        }

        System.out.println("\nAccount Number: " + account.getAccountId() + "    UpdatedBalance: $" + account.getAmount());
    }
}

C - 样本输出

Please enter the account number: Levent1299
Please enter the initial balance: 150
Please enter the transaction type /(D to deposit, W to withdraw, Q to quit: 
W
Please enter the amount to be deposited or withdrawn: 
30
Updated Balanced : 120
Please enter the transaction type /(D to deposit, W to withdraw, Q to quit: 
D
Please enter the amount to be deposited or withdrawn: 
80
Updated Balanced : 200
Please enter the transaction type /(D to deposit, W to withdraw, Q to quit: 
Q
Quitting transactions...

Account Number: Levent1299    UpdatedBalance: $200

始终使用面向对象编程的封装支柱。对于不同的域,应该有一个不同的类,在这种情况下,CustomerAccount类存储客户数据和客户方法并封装它们。