在java和输出中创建帐户类

时间:2014-12-12 03:43:10

标签: java arrays object

我正在尝试在java中创建一个帐户类。我的主要问题不是类本身,而是输出。简而言之,我使用一个数组来存储银行账户信息,然后在存入/取出一定金额后检查一个ID。我的问题是,当我尝试输入id的无效值(这是用于检查/搜索部分)时,我应该得到“未找到帐户”的输出但是,由于某种原因,我最终获得了相同的五个输出行“未找到帐户”(可能必须对数组大小做一些事情,但我无法弄明白)。有没有办法让它只打印出一次线?

tl; dr:在输入无效ID时,我应该只获得一行“未找到帐户”,而是获得多个

import java.util.Scanner;

public class AccountDriver
{
   public static void main(String[] args)
   {
     Scanner kb = new Scanner(System.in);
     int id =0;
     double balance = 0;
     double interest = 0;
     Account[] accounts = new Account[5];
     //this part fills in the array by asking the user information about each account
     System.out.println("Enter the annual interest rate ");
     interest = kb.nextDouble(); 
     for (int i = 0; i < 5; i++)
     {
        System.out.println("Enter the account number: ");
        id = kb.nextInt();
        System.out.println("Enter the initial amount to deposit: ");
        balance = kb.nextDouble();
        Account a = new Account(id,balance,interest);
        accounts[i] = a;
    }
    boolean repeat = true;
    boolean found = false;
    while(repeat)
    {
        System.out.println("\n\n*******************************");
        System.out.println("Welcome to the BANK OF AMERICA");
        System.out.println("\n\n*******************************");
        System.out.println("Enter the account number to deposit, withdraw money :");
        id = kb.nextInt();
        int i = 0;
        while (!found && i < 5) 
        {

          if(id == accounts[i].getId())
              {

              System.out.println("Here is the account information currently:");
              System.out.println(accounts[i]);
              System.out.println("*******************");
              System.out.println("Enter the amount of deposit: ");
              double depositAmount = kb.nextDouble();
              accounts[i].deposit(depositAmount);
              System.out.println("Enter the amount to withdraw: ");
              double withdrawAmount = kb.nextDouble();
              if(!accounts[i].withdraw(withdrawAmount))
              {
                 System.out.println("*******Not enough money in your account*********");
              }
              else
              {   
                 System.out.println("Here is the account information after your transaction:");
                 System.out.println(accounts[i]);
              }   
          }
          **//confused about this part** 
          else
          {
              System.out.println("Account not found");
          }
          i++;   
  } 
     //this asks the user if they have any other accounts to continue running the program
     System.out.println("\nDo you have any other account?");
     String answer = kb.next();
     if(answer.equalsIgnoreCase("no"))
     {
        repeat = false;       
     }
  }  
}                    

}

如果有人需要,这是课程

public class Account{

//These are instance variables
private static double annualInterestRate;
private int id;
private double balance;
//This creates an object from the date class in java  
private java.util.Date dateCreated;

//This no argument constructor creates a default account
public Account() 
{

   dateCreated = new java.util.Date();
   id = 0;
   balance = 0;
   annualInterestRate = 0;
}
//This constructor creates an account with
public Account(int newId, double newBalance, double newAnnualInterestRate) 
{
  //initialize the instance variables to the given values
  dateCreated = new java.util.Date();
  id = newId;
  balance = newBalance;
  annualInterestRate = newAnnualInterestRate;

}
//accessors (get)fill in the following methods
public int getId() 
{
  return id;
}
public double getBalance() 
{
    return balance;
}
public static double getAnnualInterestRate() 
{
   return annualInterestRate;
}
//mutators (get)
public void setId(int newId) 
{
     if (newId > 0)
     {
        id = newId;
     }

}
public void setBalance(double newBalance) 
{
     if(newBalance >0)
     {
        newBalance = balance;
     }

}
public static void setAnnualInterestRate(double newAnnualInterestRate)
{
   if(newAnnualInterestRate > 0)
   {
     newAnnualInterestRate = annualInterestRate;
   }

}
public double getMonthlyInterest() 
{
   double monthlyInterest = (balance * annualInterestRate/100 / 12);
   return monthlyInterest;
}
public java.util.Date getDateCreated() 
{
   return dateCreated;
}
//this boolean method checks to see if the user has enough money to withdraw
public boolean withdraw(double amount) 
{

  if(amount > balance)
  {
     return false;
  }
  else
  {
     balance = balance - amount;
     return true;
  }
}
//this method returns the balance after the user enters an amount to deposit
public double deposit(double amount) 
{
   balance = balance + amount;
   return balance;   
} 
//this method compares two accounts
public boolean equals(Account a)
{
   return this.id  == a.id;
}
//this method outputs the account information for each account
public String toString()
{
   String s = "";
   s = s + "ID: " + id;
   s = s + String.format("\nBalance:  %.2f", balance);
   s = s + "\nAnnual interest rate: " + annualInterestRate;
   double value = getMonthlyInterest();
   s = s + String.format( "\nmonthly interest: %.2f", value);
   s = s + "\nDate account created: " + dateCreated;
   return s;
}     

}

1 个答案:

答案 0 :(得分:2)

在System.out.println();

之后放置一个休息符
      else{
          System.out.println("Account not found");

          //this will break your loop;
          break;
      }

或将found设置为true

      else{
          System.out.println("Account not found");

          //this will stop your inner while loop;
          found = true;
      }

更新

我发现你的代码有很多失误。

修复#1 :将boolean found = false;放在外部循环while(repeat)中,因为您将在内部while循环内稍后更改该布尔变量的值。在此声明boolean found = false;

之后移动您的int i = 0;

修复#2 :在您的交易结束后,在found = true;

之后加System.out.println(accounts[i]);

原因:这是为了阻止你的内在循环。

修复#3 :在您的其他情况下设置if条件,以验证是否没有其他帐户。在终止内循环之前。

      else{
        // i == 4 will return true after the loop reached the last account.
        if(i == 4){
            System.out.println("Account not found");
            // this is to stop the inner loop if no account found .
            found = true;
        }
      }