CustomerData.java
package fciet.tools;
import feige.bean.Customer;
import java.util.Arrays;
import java.util.PropertyResourceBundle;
public class CustomerData {
private static int SIZE = 10; // 一共能存储的用户数
private static int COUNT = 0; // 已经存储的用户
private static Customer[] CUSTOMERDATA = new Customer[SIZE];
//把新增的用户保存在数组中
public static void sava(Customer customer ) {
if (COUNT == SIZE ) { // 数组满了,扩容为原来的2倍
CUSTOMERDATA = Arrays.copyOf(CUSTOMERDATA , SIZE*2);
SIZE *= 2;
}
for (int i = 0; i < CUSTOMERDATA.length; i++) {
if (null == CUSTOMERDATA[i]) {
CUSTOMERDATA[i] = customer;
COUNT++;
return;
}
}
}
//从数组中查询用户的信息
public static Customer get(String customerId , String pwd ) {
for (Customer c : CUSTOMERDATA ) {
if ( c.getCustomerId().equals(customerId) && c.getPwd().equals(pwd)) {
return c;
}
}
return null;
}
// 只根据id查询用户
public static Customer get(String customerId) {
for (Customer c :CUSTOMERDATA) {
if (customerId.equals(c.getCustomerId())) {
return c;
}
}
return null;
}
}
Courier.java
package feige.bean;
public class Courier extends Person{
private String courierId;
public Courier(){
}
public Courier(String courierId, String pwd){
super.setPwd(pwd);
this.courierId = courierId;
}
public Courier(String courierId, String name, String sex, int age , String pwd) {
super(name,age,pwd,sex);//super只能出现在方法的第一行
this.courierId = courierId;
}
public String getCourierId() {
return courierId;
}
public void setCourierId(String courierId) {
this.courierId = courierId;
}
@Override
public String toString() {
return "Courier{" +
"CourierId'" + courierId +'\''+
",name='" +getName() +'\'' +
",age='" +getAge() +'\'' +
",pwd='" +getPwd() +'\'' +
",sex='" +getSex() +'\'' +
'}';
}
}
Customer.java
package feige.bean;
public class Customer extends Person{
private String customerId;
private String phone;
public Customer(){
}
//构建器 用来构建一个对象
public static Customer builder(){
return new Customer();
}
public String getCustomerId() {
return customerId;
}
public Customer setCustomerId(String customerId) {
this.customerId = customerId;
return this;
}
public Customer Pwd(String pwd) {
super.setPwd(pwd);
//this.pwd = pwd ;
return this;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
Person.java
package feige.bean;
public class Person {
private String name;
private String sex;
private String pwd;
private Integer age;
public Person(){
}
public Person(String name, int age, String pwd,String sex){
this.name = name;
this.age = age;
this.pwd = pwd;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
LoginService.java
package feige.service;
import feige.bean.Customer;
import java.util.Scanner;
public class LoginService {
private Scanner sc = null;
public LoginService(Scanner scanner) {
this.sc = scanner;
}
//注册
public void register() {
System.out.println("请输入用户的编号:");
String clientId = sc.next();
System.out.println("请输入用户的密码:");
String pwd = sc.next();
System.out.println("请输入用户名:");
String name = sc.next();
System.out.println("请输入年龄:");
int age = sc.nextInt();
System.out.println("请输入性别:");
String sex = sc.next();
System.out.println("请输入手机号:");
String phone = sc.next();
Customer customer = Customer.builder().setCustomerId(clientId).Pwd(pwd);
customer.setName(name);
customer.setAge(age);
customer.Pwd(pwd);
customer.setSex(sex);
// 把上面的对象保存到数组
// CustomerData.save(customer);
}
}