Java 在一个数组中存储多个输入
大家好,我还在学习Java,我正在尝试创建一个小型数据库。我想检索一个姓名、电话号码和电子邮件地址,当我呼叫一个姓名时,我想在这里显示我目前得到的信息:Java 在一个数组中存储多个输入,java,arrays,arraylist,Java,Arrays,Arraylist,大家好,我还在学习Java,我正在尝试创建一个小型数据库。我想检索一个姓名、电话号码和电子邮件地址,当我呼叫一个姓名时,我想在这里显示我目前得到的信息: private static String [][] info; private static String name; private static ArrayList<String> nArray = new ArrayList<>(); private static ArrayList<St
private static String [][] info;
private static String name;
private static ArrayList<String> nArray = new ArrayList<>();
private static ArrayList<String> pArray = new ArrayList<>();
private static ArrayList<String> eArray = new ArrayList<>();
public static void addCustomer()
{
Scanner new_customer = new Scanner(System.in);
System.out.print("Name: ");
name = new_customer.nextLine();
nArray.add(name);
System.out.print("Phone Number: ");
String phone = new_customer.nextLine();
pArray.add(phone);
System.out.print("E-Mail Address: ");
String email = new_customer.nextLine();
eArray.add(email);
}
public static void customerInfo()
{
int totalCustomers = nArray.size();
System.out.print("Current Customers : "+totalCustomers+" Total\n");
StringBuilder arrayOutput = new StringBuilder();
for ( String name : nArray) {
arrayOutput.append(name+"\n");
}
String text = arrayOutput.toString();
System.out.print(text);
}
}
私有静态字符串[][]信息;
私有静态字符串名;
私有静态ArrayList nArray=新ArrayList();
私有静态ArrayList pArray=新ArrayList();
私有静态ArrayList array=新ArrayList();
公共静态void addCustomer()
{
扫描仪新客户=新扫描仪(System.in);
系统输出打印(“名称:”);
name=new_customer.nextLine();
添加(名称);
系统输出打印(“电话号码:”);
String phone=new_customer.nextLine();
pArray.add(电话);
系统输出打印(“电子邮件地址:”);
字符串email=new_customer.nextLine();
添加(电子邮件);
}
公共静态void customerInfo()
{
int totalCustomers=nArray.size();
系统输出打印(“当前客户:+totalCustomers+“Total\n”);
StringBuilder arrayOutput=新建StringBuilder();
for(字符串名称:nArray){
arrayOutput.append(name+“\n”);
}
String text=arrayOutput.toString();
系统输出打印(文本);
}
}
您可以使用这种方法,首先为客户创建一个bean类,如下所示:
public class Customer {
private String name;
private String phone;
private String email;
public Customer(String name, String phone, String email) {
this.name = name;
this.phone = phone;
this.email = email;
}
// getters and setters
}
现在你可以这样使用它:
// declare list for customers
List<Customer> customers = new ArrayList<>();
// get the information from the user input
....
// create Customer
Customer customer = new Customer(name, phone, email);
// add the Customer to the list
customers.add(customer);
//为客户声明列表
列出客户=新建ArrayList();
//从用户输入中获取信息
....
//创建客户
客户=新客户(姓名、电话、电子邮件);
//将客户添加到列表中
客户。添加(客户);
现在,您无需声明三个不同的列表
来保存数据如果您想做的是能够根据客户的姓名显示特定的客户信息,那么一种方法就是通过nArray
循环并比较这些值
public static int getCustomerIndex(String name){
for (int q = 0; q < nArray.size(); q++){
if (name.equals(nArray.get(q))){
return q;
}
}
return -1;
}
public static void displayCustomer(int index){
System.out.println("Name: " + nArray.get(index));
System.out.println("Phone #: " + pArray.get(index));
System.out.println("Email: " + eArray.get(index));
}
public static int getCustomerIndex(字符串名称){
对于(int q=0;q
但是实际上,您可能希望创建一个类Customer
,而不是拥有3个不同的ArrayList
,您应该对接口进行编程,并确保数据的安全性。
您应该封装所做的更改。您不希望传递引用,这可能会导致用户信息的更改(这是不需要的)。这可能会导致数小时的调试。在大多数领域中使用Getter和Setter方法会破坏OO设计、描述和应用的目的。不是说你做过或做过,只是作为所有阅读和感兴趣的人的参考
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package stackexchange;
import stackexchange.user.*;
import java.util.Set;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeSet;
import java.util.TreeMap;
/**
*
* @author Tony
*/
public class StackExchange {
private static Set<Customer> dataset;
private static Map<String, Customer> emailQuery = null;
/**
* @param args the command line arguments
*/
@SuppressWarnings("empty-statement")
public static void main(String[] args) {
Scanner istream = new Scanner(System.in);
String in;
dataset = new TreeSet<>(Customer.comparator);
while (fillDatabase());
printAll();
while (true){
System.out.print("Find User (y/n): ");
in = istream.nextLine();
// If enter null break
if ("n".equals(in.toLowerCase()))
break;
System.out.print("Enter user Email: ");
in = istream.nextLine();
printData(findByEmail(in));
}
}
/**
*
* @return
*/
private static boolean fillDatabase(){
Customer customer = new Customer();
UserInfo userInfo;
String add;
String first;
String last;
String phone;
String email;
Scanner new_customer = new Scanner(System.in);
System.out.print("add user (y/n): ");
add = new_customer.nextLine();
if ("y".equals(add.toLowerCase())){
System.out.print("First Name: ");
first = new_customer.nextLine();
System.out.print("Last Name: ");
last = new_customer.nextLine();
System.out.print("Phone Number: ");
phone = new_customer.nextLine();
System.out.print("E-Mail Address: ");
email = new_customer.nextLine();
userInfo = new UserInfo(first, last, email, phone);
customer.setUserInfo(userInfo);
if (dataset.add(customer) == false){
System.out.println("Customer : <" + first + " " + last + "> : already exists!");
}
return (true);
}
return (false);
}
/**
* Get customer by id
* @param _id [in] customer
* @return
*/
private static Customer findByEmail(String _email){
if (emailQuery == null){
UserInfo userInfo;
emailQuery = new TreeMap<>();
for (Customer c : dataset){
userInfo = c.getUserInfo();
emailQuery.put(userInfo.email, c);
}
}
return (emailQuery.get(_email));
}
/**
* Print all data
*/
private static void printAll(){
UserInfo userInfo;
for (Customer c : dataset){
printData(c);
}
}
/**
* Print data
* @param _c [in] customer
*/
private static void printData(Customer _c){
if (_c != null){
UserInfo userInfo = _c.getUserInfo();
System.out.println("+==================================================+");
System.out.println("Name: " + userInfo.firstName + " " + userInfo.lastName);
System.out.println("Email: " + userInfo.email);
System.out.println("Phone#: " + userInfo.phoneNumber);
System.out.println("+==================================================+");
}else{
System.out.println("Error : customer cannot be null!");
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package stackexchange.user;
/**
* Basic user information
* @author Tony
*/
public class UserInfo {
public final String firstName;
public final String lastName;
public final String email;
public final String phoneNumber;
public UserInfo(String _strFirstName, String _strLastName, String _strEmail,
String _strPhoneNumber){
firstName = _strFirstName;
lastName = _strLastName;
email = _strEmail;
phoneNumber = _strPhoneNumber;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package stackexchange.user;
/**
*
* @author Tony
*/
public interface User {
public static final int PRIVILEGE_ADMIN = 0x00000001;
public static final int PRIVILEGE_NORMAL = 0x00000002;
public static final int PRIVILEGE_GUEST = 0x00000004;
public static final int PRIVILEGE_UNKNOWN = 0x80000000;
/**
* Getter Methods
*/
/**
* User privileges
* @return
*/
int getPrivilege();
/**
* Object id
* @return id associated with this object
*/
String getObjectId();
/**
* User information (immutable)
* @return instance of UserInfo
*/
UserInfo getUserInfo();
/**
* Setter Methods
*/
/**
* Set user information
* @param _userInfo
*/
void setUserInfo(UserInfo _userInfo);
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package stackexchange.user;
import java.util.Comparator;
/**
*
* @author Tony
*/
public class Customer implements User{
private final String objectId;
private final int privilegeValue;
private String firstName;
private String lastName;
private String phoneNumber;
private String email;
public Customer(){
objectId = this.hashCode() + ""; // Create Unique Id for each object
privilegeValue = User.PRIVILEGE_NORMAL; // user privileges
}
@Override
public int getPrivilege() {
return (privilegeValue);
}
@Override
public String getObjectId() {
return (objectId);
}
@Override
public UserInfo getUserInfo() {
UserInfo userInfo = new UserInfo(firstName, lastName, email, phoneNumber);
return (userInfo);
}
@Override
public void setUserInfo(UserInfo _userInfo) {
if (_userInfo == null){
System.out.println("Error : argument passed cannot be null!");
}else{
firstName = _userInfo.firstName;
lastName = _userInfo.lastName;
email = _userInfo.email;
phoneNumber = _userInfo.phoneNumber;
}
}
/**
* Compare two users
* @param _anotherCustomer [in] user to compare with
* @return comparison result
*/
public int compareTo(Customer _anotherCustomer){
return (this.objectId.compareTo(_anotherCustomer.getObjectId()));
}
/**
* Used for sorting information in ADT's
*/
public static final Comparator<Customer> comparator = new Comparator<Customer>(){
@Override
public int compare(Customer _c1, Customer _c2){
return (_c1.compareTo(_c2));
}
};
}
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
包裹交换;
导入stackexchange.user.*;
导入java.util.Set;
导入java.util.Map;
导入java.util.Scanner;
导入java.util.TreeSet;
导入java.util.TreeMap;
/**
*
*@作者托尼
*/
公共类StackExchange{
私有静态集数据集;
私有静态映射emailQuery=null;
/**
*@param指定命令行参数
*/
@SuppressWarnings(“空语句”)
公共静态void main(字符串[]args){
扫描仪istream=新扫描仪(System.in);
串入;
数据集=新树集(Customer.comparator);
while(fillDatabase());
printAll();
while(true){
系统输出打印(“查找用户(y/n):”;
in=istream.nextLine();
//如果输入null break
if(“n”.equals(in.toLowerCase()))
打破
系统输出打印(“输入用户电子邮件:”);
in=istream.nextLine();
打印数据(findByEmail(in));
}
}
/**
*
*@返回
*/
私有静态布尔fillDatabase(){
客户=新客户();
用户信息用户信息;
字符串添加;
先串;
最后一串;
字符串电话;
字符串电子邮件;
扫描仪新客户=新扫描仪(System.in);
系统输出打印(“添加用户(y/n):”;
add=new_customer.nextLine();
if(“y”.equals(add.toLowerCase())){
系统输出打印(“名字:”);
first=新的_customer.nextLine();
系统输出打印(“姓氏:”);
last=新客户.nextLine();
系统输出打印(“电话号码:”);
phone=new_customer.nextLine();
系统输出打印(“电子邮件地址:”);
email=new_customer.nextLine();
userInfo=新的userInfo(第一个、最后一个、电子邮件、电话);
customer.setUserInfo(userInfo);
if(dataset.add(customer)==false){
System.out.println(“客户::已存在!”);
}
返回(真);
}
返回(假);
}
/**
*通过id获取客户
*@param_id[in]客户
*@返回
*/
私有静态客户findByEmail(字符串\u电子邮件){
if(emailQuery==null){
用户信息用户信息;
emailQuery=新建树映射();
用于(客户c:数据集){
userInfo=c.getUserInfo();
emailQuery.put(userInfo.email,c);
}
}
返回(emailQuery.get(_email));
}
/**
*打印所有数据
*/
私有静态void printAll(){
用户信息用户信息;
对于(客户c: