マイcustomer.datファイル次のように次のようになります。最初の文字列が名前である一方、セッター/ゲッターとのgetName()とアプリケーションエラー
4
Jon 1000
Jane 2000
Jake 20000
Drake 100000
最初の数字は、そのファイルの顧客の合計数を表し後者は彼らが持っているバランスです。
次のコードは、私が書いたアプリケーションコードです。私はcustomer.datから読み込み、配列を作成します。アプリケーションの問題は、顧客IDを作成し、ユーザーが入力した名前(配列に存在する場合は顧客IDを設定する場合)を確認することですが、アプリケーションを実行するときは、私はドレイクを入力した場合でも - とポートフォリオを表示するには、「1」を入力し、それは常にジョンである最初の名前を示す終わる
Scanner infile2 = new Scanner(new File("customer.dat"));
customer[]mycustomers; // declaring an array to stock
int numofcustomers = infile2.nextInt();
mycustomers = new customer [numofcustomers];
for (int i = 0; i < numofcustomers; i++)
{
mycustomers[i] = new customer(infile2.next(),infile2.nextDouble(), numofcustomers);
System.out.println(mycustomers[i].toString());
}
Scanner input = new Scanner (System.in);
System.out.println("Please type in your name");
String Name = input.nextLine();
int cusID = 0;
for (int i = 0; i < numofcustomers; i++)
{
if (Name == mycustomers[i].getName())
{
cusID = i;
}
}
System.out.println("Please type in 1 for a customer to view their portfolio, 2 to trade stocks or 3 to exit the application");
int choice = Integer.parseInt(input.nextLine());
if (choice == 1)
{
System.out.println(mycustomers[cusID].toString());
}
次は私のcustomer.javaに位置しているものの抜粋です。
public customer(String n, double b, int Size)
{
Name = n;
balance = b;
stock[] A = new stock[Size];
Portfolio = A;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
私は私が正しく問題を記述することができたかどうかわからないんだけど、いない場合は、任意のより多くの情報が必要な場合は私が知っている:セッターとゲッターが含まれています。