向对象中输入数据

Inputting data into an object

我正在做家庭作业,要求我们创建一个银行系统。第一个选项是创建客户并将他们的信息存储在客户对象中。我的 create_customer() 方法开始询问他们的名字并将其存储到新创建的 Customer 对象中,但是当我调用 getName() 方法时,没有任何返回。这是初始 class Atm,其中包含每个选项的所有操作。

import java.util.ArrayList;

public class Atm
 {
   private ArrayList<Customer> cust;
  private int starting_account_number;
  private int starting_customer_number;
  private String admin_pin;
  private int interest_rate;
  private int transaction_counter;
  ConsoleReader console = new ConsoleReader(System.in);

  public Atm()  // constructor
  {
    cust = new ArrayList<>(100);
    starting_account_number = 1001;
    starting_customer_number = 101;
    admin_pin = "abcd";
    interest_rate = 5;
    transaction_counter = 0;
  }

void create_customer()
{
    Customer customer = new Customer();
    System.out.println("Please input your name: ");
    customer.setName(console.readLine());
    customer.getName();
    while (customer.istrue)
        {
        System.out.println("Please input a 4 digit alphanumeric PIN: ");
        customer.setPin(console.readLine());
        if (customer.istrue == false) break;
        }
    System.out.println("A system generated ID was created for you, it is: ");
    String customer_id = String.valueOf(starting_customer_number);
    customer.setId(customer_id); // set customer ID
    starting_customer_number++; //incrememnt customer ID
    System.out.print(customer.getId());
    cust.add(customer); //puts the customer object into atm class arraylist
}

这是客户class

import java.util.ArrayList;

public class Customer
{
 public boolean istrue;
 private String name;
 private String id; // 3 digits string
 private String pin;    // 4 digits string
 private ArrayList<Account> acct;
 private double total_bal;  // for all accounts



 public Customer()  //constructor
 { 
     acct = new ArrayList<>(100);
     istrue = true;
     name = "NoName";
     id = "000";
     pin = "0000";
     total_bal = 0;

 }
//  public cal_total_bal()  { }

  public String getName() {
       return name;
  }

  public void setName(String name) {
      this.name = name;
  }

  public String getId() {
      return id;
  } 

  public void setId(String id) {
      this.id = id;
  }

  public String getPin() {
      return pin;
  }

  public void setPin(String pin) {
      this.pin = pin;
    if (pin.length() != 4)
    {
        System.out.println("That was not 4 digits, please input a 4 digit alphanumeric PIN: ");
}
else istrue = false;

  }

您正在调用一个方法

customer.getName();

但你实际上并没有用它做任何事情..你可能想打印它:

System.out.println(customer.getName());