2012-03-27 4 views
0

파트 4 : String aName, int aPin, double aWithdraw 및 double aDeposit을 읽습니다. 그런 다음 anotherArray를 검색하여 이름이 aName이고 핀이 aPin 인 고객 레코드를 찾으십시오. 해당 레코드가 발견되면 당좌 대월 및 양수 금액을 사용하여 인출 및 예금 거래를 수행하고 레코드의 잔액을 업데이트하십시오. 그렇지 않으면 오류 메시지를 인쇄합니다. 이 프로세스는 다른 레코드의 다른 트랜잭션에 대해 반복 될 수 있습니다. 조금만했는데 'if 문'에 오류가 있습니다.배열의 레코드를 찾은 다음 저울을 업데이트하려면 어떻게합니까?

파트 5 : 고객 레코드를 업데이트 한 후 바이너리 파일 customer_records.dat에 anotherArray의 고객 레코드를 작성하십시오.

제발 도와주세요! 나는 정말로 그것을 바르게 평가할 것이다!

import java.io.Serializable; 
    import java.util.Scanner; 

/** 
Serialized class for data on endangered species. 
Includes a main method. 
*/ 
public class CustomerRecord implements Serializable 
{ 
private String name; 
private int pin; 
private int account; 
private static double balance; 

public CustomerRecord() 
{ 
    name = null; 
    pin = 0; 
    account = 0; 
    balance = 0.00; 
} 

public CustomerRecord(String initialName, int initialPin, int initialAccount, double initialBalance) 
{ 
    name = initialName; 

    if (initialPin >= 1111 && initialPin <= 9999) 
      pin = initialPin; 
    else 
    { 
      System.out.println("ERROR: Pin is not acceptable."); 
      System.exit(0); 
    } 

if (initialAccount >= 400111 && initialAccount <= 500111) 
     account = initialAccount; 
else 
{ 
    System.out.println("ERROR: Account number is not acceptable."); 
      System.exit(0); 
} 

if (initialBalance >= 0) 
     balance = initialBalance; 
else 
{ 
    System.out.println("ERROR: Initial Balance is not acceptable."); 
      System.exit(0); 
} 
} 

public String toString() 
{ 
    return ("Name = " + name 
      + " Account = " + account 
    + " Balance = " + "$" + balance + "\n"); 
} 

public void setCustomerRecord() 
{ 
    Scanner keyboard = new Scanner(System.in); 
    System.out.println("\nEnter new customer's name: "); 
    name = keyboard.nextLine(); 

    System.out.println("Enter new customer's pin: "); 
    pin = keyboard.nextInt(); 
    while (pin < 1111 || pin > 9999) 
    { 
     System.out.println("Pin should be in the range of 1111-9999."); 
     System.out.println("Reenter pin:"); 
     pin = keyboard.nextInt(); 
    } 

    System.out.println("Enter new customer's account no: "); 
account = keyboard.nextInt(); 
    while (account < 400111 || pin > 500111) 
    { 
     System.out.println("Account should be in the range of 400111-500111."); 
     System.out.println("Reenter account number:"); 
     account = keyboard.nextInt(); 
    } 

System.out.println("Enter new customer's initial balance: "); 
    balance = keyboard.nextDouble(); 
while (balance < 0) 
    { 
     System.out.println("Initial balance should be positive or zero."); 
     System.out.println("Reenter initial balance:"); 
     balance = keyboard.nextDouble(); 
    } 
} 

public void writeOutput() 
{ 
    System.out.print("Name = " + name + "\t"); 
    System.out.print("Account = " + account + "\t"); 
    System.out.print("Balance = " + "$" + balance + "\n"); 
} 

public String getName() 
{ 
    return name; 
} 

public int getPin() 
{ 
    return pin; 
} 

public int getAccount() 
{ 
    return account; 
} 



public void setBalance(double amount) 
{ 
    balance = amount; 
} 

public static void deposit(double aDeposit) 
{ 
balance = balance + aDeposit; 
} 
public static void withdraw(double aWithdraw) 
{ 
    if 
    (balance >= aWithdraw) 
     balance = balance - aWithdraw; 
    else if 
    (balance < aWithdraw) 
     System.out.println("Cannot withdarw amount."); 
    return; 
} 
    public double getBalance() 
    { 
    return balance; 
} 
public boolean equal(CustomerRecord otherObject) 
{ 
    return (name.equalsIgnoreCase(otherObject.name) && 
      (pin == otherObject.pin) && 
      (account == otherObject.account) && 
      (balance == otherObject.balance)); 
} 
} 

import java.io.FileInputStream; 
    import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.ObjectInputStream; 
    import java.io.ObjectOutputStream; 
    import java.util.Scanner; 

public class BankCustomers 
    { 
public static void main(String[] args) 
{ 
    //---------------------------------------------------------------- 

    //part1: Create array of customer records and write them 
    //in the file customer_record.dat 


    CustomerRecord[] oneArray = new CustomerRecord[100]; 
    oneArray[0] = new CustomerRecord("John Gray", 2222, 400222, 10000.00); 
    oneArray[1] = new CustomerRecord("Ann Black", 3333, 400333, 1500.00); 
    oneArray[2] = new CustomerRecord("Mary White", 4444, 400555, 16500.00); 
    oneArray[3] = new CustomerRecord("Jack Green", 7777, 400888, 100.00); 

    int index = 4; 

    String fileName = "customer_records.dat"; 
    System.out.println("---Open file: " + fileName); 
    System.out.println("---Customer records written to file: " + fileName); 
    System.out.println("---Close file: " + fileName); 
    try 
    { 
     ObjectOutputStream outputStream = 
       new ObjectOutputStream(
        new FileOutputStream(fileName)); 
     outputStream.writeObject(oneArray); 
     outputStream.close(); 
    } 
    catch(IOException e) 
    { 
     System.out.println("Error writing to file " + 
          fileName + "."); 
     System.exit(0); 
    } 
    System.out.println("\n"); 


    //------------------------------------------------------------------- 
    //Part2: Read from the file customer_record.dat and save 
    //the customer records in anotherArray 


    System.out.println("---Open file: " + fileName); 
    System.out.println("---Customer records read from file: " + fileName); 
    System.out.println("---Close file: " + fileName); 
    CustomerRecord[] anotherArray = null; 
    try 
    { 
     ObjectInputStream inputStream = 
        new ObjectInputStream(
          new FileInputStream(fileName)); 
     anotherArray = (CustomerRecord[])inputStream.readObject(); 
     inputStream.close(); 
    } 
    catch(Exception e) 
    { 
     System.out.println("Error reading file " + 
          fileName + "."); 
     System.exit(0); 
    } 
      //Print the records on screen 
    for (int i = 0; i < index; i++) 
     System.out.print(anotherArray[i]); 


    //------------------------------------------------ 
    //Part3: Add a new customer record to anotherArray 

    anotherArray[index] = new CustomerRecord(); 
    anotherArray[index].setCustomerRecord(); 
    index++; 
      //Print the records on screen 
    for (int i = 0; i < index; i++) 
     System.out.print(anotherArray[i]); 


    //------------------------------------------------ 
    //Part4: Find a customer record from anotherArray 
    //to do transaction(s) and update the record's balance 

    char repeat; // User control to repeat or quit 


    do{ 


     System.out.println("Enter the name"); 
     String aName; 
     Scanner keyboard = new Scanner(System.in); 
     aName = keyboard.nextLine(); 

     System.out.println("Enter the pin"); 
     int aPin; 
     aPin = keyboard.nextInt(); 


     System.out.println("Enter the amount you wish to withdraw"); 
     double aWithdraw; 
     aWithdraw = keyboard.nextDouble(); 
     CustomerRecord.withdraw(aWithdraw); 

     System.out.println("Enter the amount you wish to deposit"); 
     double aDeposit; 
     aDeposit = keyboard.nextDouble(); 
     CustomerRecord.deposit(aDeposit); 

      for 
      (int i = 0; i < anotherArray.length; i++) { 
       CustomerRecord record = anotherArray[i]; 
       if 
       ((record.getName().equalsIgnoreCase(aName)) && (record.getPin() == (aPin))) 
    { 


       System.out.println(record); 
       record.getBalance(); 

       } 

       } 


     System.out.println("\nAnother Transaction? (y for yes)"); 
     repeat = keyboard.next().charAt(0); 

    } 
    while 
     (

     repeat == 'y' || repeat == 'Y') ; 

     //Print the records on screen 

    { for (int i = 0; i < index; i++) 
     System.out.print(anotherArray[i]); 
    } 


    //------------------------------------------------ 
    //Part5: Write the customer records from anotherArray 
    //in the file customer_record.dat 

    String fileName1 = "customer_records.dat"; 
    System.out.println("---Open file: " + fileName1); 
    System.out.println("---Customer records written to file: " + fileName1); 
    System.out.println("---Close file: " + fileName1); 
    try 
    { 
     ObjectOutputStream outputStream = 
       new ObjectOutputStream(
        new FileOutputStream(fileName1)); 
     outputStream.writeObject(anotherArray); 
     outputStream.close(); 
    } 
    catch(IOException e) 
    { 
     System.out.println("Error writing to file " + 
          fileName1 + "."); 
     System.exit(0); 
    } 
    System.out.println("\n"); 

    //End of program 
    System.out.println("\n---End of program."); 
} 
}  

편집
메시지 오류 : 스레드
예외 BankCustomers.main에서 "주요"java.lang.NullPointerException이 (BankCustomers.java:115가)

+0

죄송합니다. – Jake

+1

42. (더 심각하게 문제, 오류 등을 말하십시오.) –

+2

* " 'if 문'에 오류가 있습니다."- 어떤 오류가 있습니까? (내 마음 읽기 장치가 오늘 아침에 작동하지 않습니다.) –

답변

0

이 여기에 놀라운 정말 없습니다 당신이 일부처럼 당신이 i < index에 루프 싶은 생각 - 처음이 일을하고 있습니다 :

CustomerRecord[] oneArray = new CustomerRecord[100]; 
oneArray[0] = new CustomerRecord("John Gray", 2222, 400222, 10000.00); 
oneArray[1] = new CustomerRecord("Ann Black", 3333, 400333, 1500.00); 
oneArray[2] = new CustomerRecord("Mary White", 4444, 400555, 16500.00); 
oneArray[3] = new CustomerRecord("Jack Green", 7777, 400888, 100.00); 

그런 다음 손으로 하나 (잘 좋은을 만들), 그런 다음 전체 고객 세트를 반복하려고 시도합니다!

oneArray[0] = 'John Gray'; 
oneArray[1] = 'Ann Black'; 
oneArray[2] = 'Mary White'; 
oneArray[3] = 'Jack Green'; 
oneArray[4] = 'WHowever you made up'; 
oneArray[5] to oneArray[99] = NULL; 

BAM의 경우 핀과 이름을 가져올 때 NullPointerException이 발생합니다.

+0

어떻게 해결할 수 있습니까? – Jake

+0

감사합니다. Buddy, @mellamokb이 나를 도왔습니다. 또 다른 질문 - 사용자가 인출 및 입금액을 입력하는 숫자는 CustomerRecord 클래스의 규칙에 따라 결정됩니다. – Jake

+0

@ 제이크 : 규칙은 무엇입니까? 나는 그것을 볼 때 충분한 자금을 확인하고, 그 밖에 무엇이 있습니까? –

0

하여 예외의 원인이다 당신은 완전히 가득 차지 않은 경우에도, 100 파일에 길이의 배열을 작성 :

그런 다음
CustomerRecord[] oneArray = new CustomerRecord[100]; 

당신이 다시로 읽기 배열 : (당신이 제 3 부에서 하나를 만들 수 있기 때문에)

anotherArray = (CustomerRecord[])inputStream.readObject(); 

지금이 배열은 5 개 실제 레코드를 포함하지만 배열의 크기는 100입니다 그래서 때 배열의 전체 길이를 통해 루프 :

for (int i = 0; i < anotherArray.length; i++) { 

record.getName() 또는 record.getPin()을 레코드 6, 7, 8, ... 100에 호출하면 해당 레코드가 없으므로 NullReferenceException이 발생합니다. 나는 3.

+0

환상적! 그것은 정말로 효과가있었습니다. 어떤 아이디어, 사용자가 철회 및 입금액을 입력하는 숫자는 CustomerRecord 클래스의 규칙에 따라 결정 되는가? – Jake

관련 문제