Sie sollten auf Schnittstellen programmieren und die Sicherheit Ihrer Daten gewährleisten. Sie sollten die Änderungen einkapseln. Sie möchten nicht, dass Referenzen weitergegeben werden, was dazu führen kann, dass Benutzerinformationen geändert werden (dies ist unerwünscht). Dies kann zu Debugging-Stunden führen. Getter und Setter-Methoden für eine Mehrheit Ihrer Felder besiegt den Zweck von OO-Design, Beschreibung here. Nicht sagen, dass du es getan oder getan hast, sondern nur als Referenz für alle, die lesen und interessiert sind.
/*
* 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));
}
};
}
Also was genau ist die Frage? – Mureinik
Lernen Sie über Klassen ... Machen Sie eine "Kunden" -Klasse. Speichern Sie diese in einem 'Customer []' –
@ cricket_007 Genau! –