Project introduction
To simulate the realization of “customer information management software” based on text interface.
The software can insert, modify, and delete customer objects (using arrays) and print customer lists.
Knowledge points involved
- Use of class structures: properties, methods, and constructors
- Object creation and use
- Class encapsulation
- Declare and use arrays
- Insert, delete, and replace arrays
- Use of the keyword: this
Project requirements:
- The information for each Customer is stored in the Customer object.
- Record all current customers in an array of type Customer.
- After each “Add Customer” (Menu 1), the Customer object is added to the array.
- After each “Modify Customer” (Menu 2), the modified Customer object replaces the original object in the array.
- After each Delete Customer (menu 3), the Customer object is purged from the array.
- When the Customer List (Menu 4) is executed, the information for all the customers in the array is listed.
Project menu Display
Main menu interface
----------------- Customer information management software -----------------1Add the customer2Modify the customer3Delete the customer4Client list5To exit please select (1-5) : _Copy the code
Adding a Client Interface
Please select (1-5) :1-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- add customers -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- name: TongGang sex: male age:35Telephone:010-56253825E-mail: tongtong@atguigu.com -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- add complete -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -Copy the code
Modifying the Client Interface
Please select (1-5) :2-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- to modify customers -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - please select to modify the customer number (-1Exit) :1Name (Tong Gang) : < Enter to indicate no modification > Gender (Male) : Age (35) : Telephone (010-56253825) : Email (tongtong@163.Com) : tongg @123.Com -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- modified complete -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -Copy the code
Deleting the Client Interface
Please select (1-5) :3-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - removed clients -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - please choose to delete the customer number (-1Exit) :1Confirm whether delete (Y/N) : Y -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- delete completed -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -Copy the code
Customer list interface
Please select (1-5) :4-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- client list -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- no. Name Age and gender phone mailbox1TongGang male45 010-56253825 tong@abc.com
2Seal home female36 010-56253825 fengjie@ibm.com
3LeiFengYang male32 010-56253825 leify@163.Com -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- client list complete -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --Copy the code
Project Design structure
The software consists of the following modules
- CustomerView is the main module that displays menus and handles user actions
- CustomerList is the Customer object management module. It internally manages a set of Customer objects with an array and provides the corresponding add, modify, delete, and traversal methods for CustomerView to call
- Customer is an entity object that encapsulates Customer information
Software memory call diagram
Call diagram for the enterMainMenu method
Project implementation
The project adopts MVC design structure to manage the package structure of the project
Project package structure:
-
The Bean package holds the Customer class
Store the customer object, set the customer properties to achieve get set method, provide a constructor
-
The Service package holds the CustomerList class
It is used to add, delete, change, check and other functions
-
The util package stores the generic extraction class CMUtility
Used to implement keyboard read operations
-
View package stores the interface display class CustomerView
Used for interactive operation on the console display interface
Project design
Universal keyboard access class CMUtility design
A universal keyboard reading method is designed to encapsulate all keyboard reading operations
package com.bruce.project.project02.util;
import java.util.Scanner;
/** * CMUtility utility classes: Encapsulate different features as methods that you can use directly by calling methods, regardless of the implementation details. * /
public class CMUtility {
private static Scanner scanner = new Scanner(System.in);
/** * for interface menu selection. This method reads the keyboard and returns if the user types any character in '1' - '5'. The return value is a character typed by the user. * /
public static char readMenuSelection(a) {
char c;
for (;;) {
String str = readKeyBoard(1.false);
c = str.charAt(0);
if(c ! ='1'&& c ! ='2'&& c ! ='3'&& c ! ='4'&& c ! ='5') {
System.out.print("Wrong selection, please re-enter:");
} else
break;
}
return c;
}
/** * reads a character from the keyboard as the return value of the method. * /
public static char readChar(a) {
String str = readKeyBoard(1.false);
return str.charAt(0);
}
/** * reads a character from the keyboard as the return value of the method. If the user does not enter a character and enters directly, the method returns defaultValue. * /
public static char readChar(char defaultValue) {
String str = readKeyBoard(1.true);
return (str.length() == 0)? defaultValue : str.charAt(0);
}
/** * Reads an integer no longer than 2 bits from the keyboard as the return value of the method. * /
public static int readInt(a) {
int n;
for (;;) {
String str = readKeyBoard(2.false);
try {
n = Integer.parseInt(str);
break;
} catch (NumberFormatException e) {
System.out.print("Wrong number, please re-enter:"); }}return n;
}
/** * Reads an integer no longer than 2 bits from the keyboard as the return value of the method. If the user does not enter a character and enters directly, the method returns defaultValue. * /
public static int readInt(int defaultValue) {
int n;
for (;;) {
String str = readKeyBoard(2.true);
if (str.equals("")) {
return defaultValue;
}
try {
n = Integer.parseInt(str);
break;
} catch (NumberFormatException e) {
System.out.print("Wrong number, please re-enter:"); }}return n;
}
/** * reads a string of up to limit length from the keyboard as the return value of the method. * /
public static String readString(int limit) {
return readKeyBoard(limit, false);
}
/** * reads a string of up to limit length from the keyboard as the return value of the method. If the user does not enter a character and enters directly, the method returns defaultValue. * /
public static String readString(int limit, String defaultValue) {
String str = readKeyBoard(limit, true);
return str.equals("")? defaultValue : str; }/** * used to confirm selected input. The method reads' Y 'or' N 'from the keyboard as the return value of the method. * /
public static char readConfirmSelection(a) {
char c;
for (;;) {
String str = readKeyBoard(1.false).toUpperCase();
c = str.charAt(0);
if (c == 'Y' || c == 'N') {
break;
} else {
System.out.print("Wrong selection, please re-enter:"); }}return c;
}
private static String readKeyBoard(int limit, boolean blankReturn) {
String line = "";
while (scanner.hasNextLine()) {
line = scanner.nextLine();
if (line.length() == 0) {
if (blankReturn)
return line;
else
continue;
}
if (line.length() < 1 || line.length() > limit) {
System.out.print("Input length (not greater than" + limit + ") error, please re-enter:");
continue;
}
break;
}
returnline; }}Copy the code
Customer Class Customer class design
Encapsulates the user’s attributes, providing an empty parameter constructor and a constructor with all parameters
package com.bruce.project.project02.bean;
public class Customer {
private String name;
private char gender;
private int age;
private String phone;
private String email;
public Customer(a) {}public Customer(String name, char gender, int age, String phone, String email) {
this.name = name;
this.gender = gender;
this.age = age;
this.phone = phone;
this.email = email;
}
public String getName(a) {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getGender(a) {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public int getAge(a) {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhone(a) {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail(a) {
return email;
}
public void setEmail(String email) {
this.email = email;
}
/ * * * *@DescriptionMethods for displaying user information *@author Bruce
* @return String
*/
public String getDetails(a) {
return name + "\t" + gender + "\t" + age + "\t\t" + phone + "\t"+ email; }}Copy the code
Customer action class CustomerList class design
Design the function of adding, deleting, modifying and displaying customers in the project
package com.bruce.project.project02.service;
import com.bruce.project.project02.bean.Customer;
public class CustomerList {
private Customer[] customers;
private int total = 0;
/** * constructor, used to initialize the Customers array **@param totalCustomer
*/
public CustomerList(int totalCustomer) {
customers = new Customer[totalCustomer];
}
/ * * * *@DescriptionMethods to add customers *@author Bruce
* @param customer
* @returnBoolean returns true on success; False indicates that the array is full and the */ cannot be added
public boolean addCustomer(Customer customer) {
if (total >= customers.length) {
return false;
}
customers[total++] = customer;
return true;
}
/ * * * *@DescriptionMethods to modify customer information *@author Bruce
* @paramIndex * specifies the position of the replaced object in the array, starting at 0 *@param cust
* @returnBoolean returns true on success; False indicates that the index is invalid and cannot replace * */
public boolean repalceCustomer(int index, Customer cust) {
if (index < 0 || index >= total) {
return false;
}
customers[index] = cust;
return true;
}
/ * * * *@DescriptionMethods to delete customer information *@author Bruce
* @paramIndex specifies the index position of the deleted object in the array, starting from 0 *@returnBoolean Returns true on success; False indicates that the index is invalid and the */ cannot be deleted
public boolean deleteCustomer(int index) {
if (index < 0 || index >= total) {
return false;
}
for (int i = 0; i < total - 1; i++) {
customers[i] = customers[i + 1];
}
customers[--total] = null;
return true;
}
/ * * * *@DescriptionGet all customer information *@author Bruce
* @returnThe Customer[] array contains all the current Customer objects and is the same length as the number of objects. * /
public Customer[] getAllCustomers() {
Customer[] custs = new Customer[total];
for (int i = 0; i < total; i++) {
custs[i] = customers[i];
}
return custs;
}
/ * * * *@Description TODO
* @author Bruce
* @param index
* @returnThe Customer object */ that encapsulates Customer information
public Customer getCustomer(int index) {
if (index < 0 || index >= total) {
return null;
}
return customers[index];
}
public int getTotal(a) {
returntotal; }}Copy the code
Customer display interface class CustomerView class design
The main interface displayed in the console
package com.bruce.project.project02.view;
import com.bruce.project.project02.bean.Customer;
import com.bruce.project.project02.service.CustomerList;
import com.bruce.project.project02.util.CMUtility;
public class CustomerView {
private CustomerList customers = new CustomerList(10);
// Create a user initially
public CustomerView(a) {
Customer cust = new Customer("Zhang".'male'.30."1151511215"."[email protected]");
customers.addCustomer(cust);
}
/ * * * *@DescriptionSoftware main interface, display function *@author Bruce void
*/
public void enterMainMenu(a) {
boolean loopFlag = true;
do {
System.out.println("\n----------------- Customer Information Management software -----------------\n");
System.out.println("1 Add customer account");
System.out.println("2 Repair and change guest House");
System.out.println("3 Delete Guest Accounts");
System.out.println("4 List of Customers");
System.out.println("5 out \n");
System.out.print(Please select (1-5) :);
char key = CMUtility.readMenuSelection();
System.out.println();
switch (key) {
case '1':
addNewCustomer();
break;
case '2':
modifyCustomer();
break;
case '3':
deleteCustomer();
break;
case '4':
listAllCustomer();
break;
case '5':
System.out.println("Exit (Y/N):");
char yn = CMUtility.readConfirmSelection();
if (yn == 'Y') {
loopFlag = false;
}
break;
default:
break; }}while (loopFlag);
}
/ * * * *@DescriptionThe Add user interface is displayed@author Bruce void
*/
private void addNewCustomer(a) {
System.out.println("--------------------- Add customer ---------------------");
System.out.print("Name:");
String name = CMUtility.readString(4);
System.out.println("Gender");
char gender = CMUtility.readChar();
System.out.println("Age");
int age = CMUtility.readInt();
System.out.println("Telephone");
String phone = CMUtility.readString(15);
System.out.println("Email");
String email = CMUtility.readString(15);
Customer cust = new Customer(name, gender, age, phone, email);
boolean flag = customers.addCustomer(cust);
if (flag) {
System.out.println("--------------------- added completed ---------------------");
} else {
System.out.println("---------------- record is full, cannot add -----------------"); }}/ * * * *@DescriptionThe page for modifying user information is displayed@author Bruce void
*/
private void modifyCustomer(a) {
System.out.println("--------------------- Modify customer ---------------------");
int index = 0;
Customer cust = null;
for (;;) {
System.out.print(Please select the customer number to be changed (-1 exit) :);
index = CMUtility.readInt();
if (index == -1) {
return;
}
cust = customers.getCustomer(index - 1);
if (cust == null) {
System.out.println("The user could not be found");
} else
break;
}
System.out.println("Name (" + cust.getName() + "):");
String name = CMUtility.readString(4, cust.getName());
System.out.println("Gender" + cust.getGender() + "):");
char gender = CMUtility.readChar(cust.getGender());
System.out.println((" age" + cust.getAge() + "):");
int age = CMUtility.readInt(cust.getAge());
System.out.println((" phone" + cust.getPhone() + "):");
String phone = CMUtility.readString(15, cust.getPhone());
System.out.println("Email (" + cust.getEmail() + "):");
String email = CMUtility.readString(15, cust.getEmail());
cust = new Customer(name, gender, age, phone, email);
boolean flag = customers.repalceCustomer(index - 1, cust);
if (flag) {
System.out.println("--------------------- Modification completed ---------------------");
} else {
System.out.println("---------- could not find the specified customer, failed to modify --------------"); }}/ * * * *@DescriptionDelete the user interface *@author Bruce void
*/
private void deleteCustomer(a) {
System.out.println("--------------------- Delete client ---------------------");
int index = 0;
Customer cust = null;
for (;;) {
System.out.println("Select the customer number to delete (-1 exit)");
index = CMUtility.readInt();
if (index == -1) {
return;
}
cust = customers.getCustomer(index - 1);
if (cust == null) {
System.out.println("Unable to find designated customer");
} else
break;
}
// Out of the loop whether to delete
System.out.println("Delete customer (Y/N)");
char yn = CMUtility.readChar();
if (yn == 'N')
return;
boolean flag = customers.deleteCustomer(index - 1);
if (flag) {
System.out.println("--------------------- delete completed ---------------------");
} else {
System.out.println("---------- cannot find specified customer, delete failed --------------"); }}/ * * * *@DescriptionMake a list of customers *@author Bruce void
*/
private void listAllCustomer(a) {
System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the customer list -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
Customer[] custs = customers.getAllCustomers();
if (custs.length == 0) {
System.out.println("No customer number!");
} else {
System.out.println("Serial number, name, sex, age, telephone, email address");
for (int i = 0; i < custs.length; i++) {
System.out.println((i + 1) + "\t" + custs[i].getDetails());
}
}
System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the customer list complete -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --"); }}Copy the code
Software call class CustomerTest class design
package com.bruce.project.project02;
import com.bruce.project.project02.view.CustomerView;
public class CustomerTest {
public static void main(String[] args) {
CustomerView cView = newCustomerView(); cView.enterMainMenu(); }}Copy the code