[英]How would I create a LinkedList, then create a value from input. Then if input matches value in LinkedList return that value?
Below is my code, I want to create a Linkedlist, with input from a user. 下面是我的代码,我想创建一个链接列表,其中包含用户的输入。 ONce the user has inputed values into the LinkedList(accounts). 一旦用户将值输入到LinkedList(帐户)中。 He can then search for those values in a LinkedList, if those values(account name) he inputed match those in the linkedList, then that value become the new account selected. 然后,他可以在LinkedList中搜索那些值,如果他输入的那些值(帐户名)与linkedList中的值匹配,则该值将成为新的选定帐户。
How can I finish my choose method. 如何完成选择方法。
import java.util.*;
public class Customer {
public static void main(String[] args) {
new Customer();
}
private LinkedList<Account> accounts = new LinkedList<Account>();
public Customer() {
setup();
}
private void setup() {
accounts.add(new Account("",0.0));
accounts.add(new Account("",0.0));
accounts.add(new Account("",0.0));
}
public void use() {
char choice;
while((choice = readChoice()) != 'x') {
switch(choice) {
case 'a': choose(); break;
case 'i': addInterest(); break;
case 's': show(); break;
default: help();
}
}
}
private char readChoice() {
return 'x';
}
private void choose() {
for (Account account: accounts) {
}
}
private String readName() {
System.out.print("??? account ???; ");
return In.nextLine();
}
private Account account(String name) {
return null;
}
private void addInterest() {
for (Account account: accounts) {
for (int i = 0; i < 30; i++)
account.addDailyInterest();
account.addMonthlyInterest();
}
}
private void help() {
String s = "The menu choices are";
s += "\n a: choose an account";
s += "\n i: add interest to all accounts";
s += "\n s show";
s += "\n x exit";
System.out.println(s);
}
private void show() {
System.out.println(this);
}
public String toString() {
String s = "";
return s;
}
}
As I understand your problem logic, you want to be able to choose an account by name and set it as current. 据我了解您的问题逻辑,您希望能够按名称选择一个帐户并将其设置为当前帐户。 To do that you need to add a member to your Customer
class that will store this selected Account
, eg 为此,您需要将一个成员添加到将存储此选定Account
Customer
类中,例如
Account selected; // null by default
Then the choose()
method should loop through all accounts in order to find a match: 然后, choose()
方法应遍历所有帐户以找到匹配项:
void choose(String name) {
Account found = null;
for (Account acc : accounts) {
if (acc.getName().equals(name)) {
found = acc;
}
}
if (found) {
selected = found;
}
else {
System.out.println(name + " not found in list.");
}
As a suggestion, you can have show()
that will display the account information from the selected (if it exists), and a showAll()
that can display all accounts. 作为建议,您可以使用show()
来显示所选帐户的信息(如果存在),以及可以显示所有帐户的showAll()
。
You need also to initialise properly the accounts
list and write the logic in the main()
. 您还需要正确初始化accounts
列表,并将逻辑写入main()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.