String類StringBuileder用法及客戶資訊管理軟體專案
首先,我們來說說String類的用法:
String 是在 java.lang 包下面,我們在使用時,不需要導包就可以直接使用,在這個關卡五,我們首先需要知道:
代表字串例如"abc"都是被實作為此類的實體,也就是說,java程式中所有的雙引號字串,都是String類的物件
那么,String有啥特點呢?
特點:1:字串不可變,它們的值在創建后不能被更改2:雖然String的值時不可變的,但是它們可以被共享3:字串效果上相當于字符陣列(char[]),但是底層原理時位元組陣列(byte[])
String物件的特點:
String 物件的特點:1:通過New創建的字串物件,每一次new都會申請一個記憶體空間,雖然內容相同,但地址值不同2:以“”方式給出的字串,只要字符序列相同(順序和大小寫),無論在程式代碼中出現幾次,jvm都只會建立一個String物件,并在字串池中維護

談談字串的比較:
字串的比較:字串是物件,它比較內容是否相同,是通過一個方法來實作,這個方法叫:equals()

下面講講如何訪問字串中的字符呢?
首先,字串可以看成是字符陣列,那么我們訪問陣列的元素是通過陣列名[索引]這樣來訪問的,那么同理,我們也可以通過charAt()方法來實作,字串.charAt[i]來遍歷,遍歷字串:獲取字串中的每一個字符用 public char charAt(int index):回傳指定索引處的char值,字串的索引也是從0開始的,
那么說了String的相關方法,接下來說說StringBuilder:
概念:是一個可變的字串類,我們可以把它看成一個容器,這里的可變是指StringBuilder物件中的內容是可變的
String與StringBuilder以及StringBuffer三者的區別:

StringBuilder提供了append()方法進行添加,以及reverse()方法
二者的轉換:StringBuilder通過toString()方法來實作轉換為String.
String是通過構造方法轉換為StringBuilder
接下里是學習集合的相關知識:
集合類的特點:提供一種存盤空間可變的存盤模型,存盤的資料容量可以發生改變,
集合類有很多,目前我們先學習一個:ArrayList,
ArrayList 可調整大小的陣列實作 是一種特殊的資料型別,泛型,
ArrayList的相關方法:
public boolean add(E e) 將指定的元素追加到此集合末尾,
public void add(int index,E element) 在此集合中的指定位置插入指定的元素,
public boolean remove(Object o) 洗掉指定的元素,回傳洗掉是否成功,
public E remove(int index) 洗掉指定索引的元素,回傳被洗掉的元素,
public E get(int index) 回傳指定索引的元素,
public int size() 回傳集合中的元素的個數,
客戶資訊管理軟體專案實作以及代碼:
CustomerView代碼如下:
public class CustomerView {
public static Scanner sc = new Scanner(System.in);
private static CustomerList customerList = new CustomerList(10);
public static void main(String[] args) {
while (true) {
System.out.println("---------客戶資訊管理軟體---------");
System.out.println("1 添加客戶");
System.out.println("2 修改客戶");
System.out.println("3 洗掉客戶");
System.out.println("4 客戶串列");
System.out.println("5 退 出");
System.out.println("請選擇(1-5):");
char n = CMUtility.readMenuSelection();
switch (n) {
case '1':
addCustomerList();
break;
case '2':
modifyCustomer();
break;
case '3':
deleteCustomer();
break;
case '4':
listAllCustomers();
break;
case '5':
System.out.println("確認是否退出(Y/N):");
char next = CMUtility.readConfirmSelection();
if (next == 'N') {
break;
}
if (next == 'Y') {
System.out.println("退出成功");
System.exit(0);
}
}
}
}
//增加客戶到customerList中
public static void addCustomerList() {
System.out.println("--------------添加客戶-------------");
System.out.print("姓名:");
String name = CMUtility.readString(4);
System.out.print("性別:");
char sex = CMUtility.readChar();
System.out.print("年齡:");
int age = CMUtility.readInt();
System.out.print("電話:");
String phone = CMUtility.readString(15);
System.out.print("郵箱:");
String emile = CMUtility.readString(15);
//封裝一個物件給陣列CustomrerList
Customer c1 = new Customer(name, sex, age, phone, emile);
boolean flag = customerList.addCustomer(c1);
if (flag) {
System.out.println("添加成功");
} else {
System.out.println("陣列已滿");
}
}
//修改指定編號的客戶資訊
private static void modifyCustomer() {
System.out.println("---------------修改客戶----------");
int bh;
int index = -1;
while (true) {
System.out.print("請選擇待修改客戶編號(-1退出):");
bh = CMUtility.readInt();
if (bh == index) {
return;
}
if (bh > customerList.total || bh <= 0) {
System.out.println("編號不存在,請重新輸入:");
} else {
break;
}
}
bh--;
System.out.print("姓名(" + customerList.getCustomer(bh).getName() + "):");
String name = CMUtility.readString(4, customerList.getCustomer(bh).getName());
System.out.print("性別(" + customerList.getCustomer(bh).getGender() + "):");
char sex = sc.next().charAt(0);
System.out.print("年齡(" + customerList.getCustomer(bh).getAge() + "):");
int age = sc.nextInt();
System.out.print("電話(" + customerList.getCustomer(bh).getPhone() + "):");
String phone = CMUtility.readString(11, customerList.getCustomer(bh).getPhone());
System.out.print("郵箱(" + customerList.getCustomer(bh).getEmail() + "):");
String emile = CMUtility.readString(15, customerList.getCustomer(bh).getEmail());
Customer[] Cust = customerList.getAllCustomers();
for (int i = 0; i < customerList.total; i++) {
if (bh == i) {
Cust[i].setName(name);
Cust[i].setGender(sex);
Cust[i].setAge(age);
Cust[i].setPhone(phone);
Cust[i].setEmail(emile);
}
}
System.out.println("修改成功");
}
//洗掉指定編號的客戶資訊
private static void deleteCustomer() {
System.out.println("----------洗掉客戶------------");
int bh;
int index = -1;
while (true) {
System.out.print("請選擇待洗掉客戶編號(-1退出):");
bh = CMUtility.readInt();
if (bh == index) {
return;
}
if (bh > customerList.total || bh <= 0) {
System.out.println("編號不存在,請重新輸入:");
} else {
break;
}
}
System.out.println("確認是否洗掉(Y/N):");
char next = CMUtility.readConfirmSelection();
if (next == 'N') {
return;
}
Customer[] Cust = customerList.getAllCustomers();
Customer cust;
boolean a = false;
for (int i = 0; i < customerList.total; i++) {
if (bh == (i + 1)) {
a = customerList.deleteCustomer(bh - 1);
}
if (a) {
System.out.println("洗掉完成");
}
}
}
//查看customerList中的所有客戶并顯示
private static void listAllCustomers() {
System.out.println("--------------客戶串列------------");
Customer[] Cust = customerList.getAllCustomers();
if (customerList.total == 0) {
System.out.println("沒有資料");
} else {
System.out.println("編號\t" + "姓名\t" + "\t性別" + "\t年齡" + "\t電話\t\t\t" + "郵箱");
for (int i = 0; i < customerList.total; i++) {
System.out.println(i + 1 + "\t" + Cust[i].getName() + "\t\t" + Cust[i].getGender() + "\t" + Cust[i].getAge() + "\t" + Cust[i].getPhone() + "\t\t\t" + Cust[i].getEmail());
}
System.out.println("客戶串列完成");
}
}
}
下面展示代碼CustomerView中的方法代碼:
//增加客戶到customerList中
public static void addCustomerList() {
System.out.println("--------------添加客戶-------------");
System.out.print("姓名:");
String name = CMUtility.readString(4);
System.out.print("性別:");
char sex = CMUtility.readChar();
System.out.print("年齡:");
int age = CMUtility.readInt();
System.out.print("電話:");
String phone = CMUtility.readString(15);
System.out.print("郵箱:");
String emile = CMUtility.readString(15);
//封裝一個物件給陣列CustomrerList
Customer c1 = new Customer(name, sex, age, phone, emile);
boolean flag = customerList.addCustomer(c1);
if (flag) {
System.out.println("添加成功");
} else {
System.out.println("陣列已滿");
}
}
//修改指定編號的客戶資訊
private static void modifyCustomer() {
System.out.println("---------------修改客戶----------");
int bh;
int index = -1;
while (true) {
System.out.print("請選擇待修改客戶編號(-1退出):");
bh = CMUtility.readInt();
if (bh == index) {
return;
}
if (bh > customerList.total || bh <= 0) {
System.out.println("編號不存在,請重新輸入:");
} else {
break;
}
}
bh--;
System.out.print("姓名(" + customerList.getCustomer(bh).getName() + "):");
String name = CMUtility.readString(4, customerList.getCustomer(bh).getName());
System.out.print("性別(" + customerList.getCustomer(bh).getGender() + "):");
char sex = sc.next().charAt(0);
System.out.print("年齡(" + customerList.getCustomer(bh).getAge() + "):");
int age = sc.nextInt();
System.out.print("電話(" + customerList.getCustomer(bh).getPhone() + "):");
String phone = CMUtility.readString(11, customerList.getCustomer(bh).getPhone());
System.out.print("郵箱(" + customerList.getCustomer(bh).getEmail() + "):");
String emile = CMUtility.readString(15, customerList.getCustomer(bh).getEmail());
Customer[] Cust = customerList.getAllCustomers();
for (int i = 0; i < customerList.total; i++) {
if (bh == i) {
Cust[i].setName(name);
Cust[i].setGender(sex);
Cust[i].setAge(age);
Cust[i].setPhone(phone);
Cust[i].setEmail(emile);
}
}
System.out.println("修改成功");
}
//洗掉指定編號的客戶資訊
private static void deleteCustomer() {
System.out.println("----------洗掉客戶------------");
int bh;
int index = -1;
while (true) {
System.out.print("請選擇待洗掉客戶編號(-1退出):");
bh = CMUtility.readInt();
if (bh == index) {
return;
}
if (bh > customerList.total || bh <= 0) {
System.out.println("編號不存在,請重新輸入:");
} else {
break;
}
}
System.out.println("確認是否洗掉(Y/N):");
char next = CMUtility.readConfirmSelection();
if (next == 'N') {
return;
}
Customer[] Cust = customerList.getAllCustomers();
Customer cust;
boolean a = false;
for (int i = 0; i < customerList.total; i++) {
if (bh == (i + 1)) {
a = customerList.deleteCustomer(bh - 1);
}
if (a) {
System.out.println("洗掉完成");
}
}
}
//查看customerList中的所有客戶并顯示
private static void listAllCustomers() {
System.out.println("--------------客戶串列------------");
Customer[] Cust = customerList.getAllCustomers();
if (customerList.total == 0) {
System.out.println("沒有資料");
} else {
System.out.println("編號\t" + "姓名\t" + "\t性別" + "\t年齡" + "\t電話\t\t\t" + "郵箱");
for (int i = 0; i < customerList.total; i++) {
System.out.println(i + 1 + "\t" + Cust[i].getName() + "\t\t" + Cust[i].getGender() + "\t" + Cust[i].getAge() + "\t" + Cust[i].getPhone() + "\t\t\t" + Cust[i].getEmail());
}
System.out.println("客戶串列完成");
}
}
CustomerList中的代碼如下:
public class CustomerList {
private static Customer[] customers;//用來保存各戶物件的陣列
int total = 0;//紀錄已保存客戶物件的數量
/*
* 用途:構造器,用來初始化customers陣列
引數:totalCustomer:指定customers陣列的最大空間
* */
public CustomerList(int totalCustomer) {
customers = new Customer[totalCustomer];
}
//添加
public boolean addCustomer(Customer customer) {
if (total >= customers.length) {
return false;
}
customers[total++] = customer;
return true;
}
//修改
public boolean replaceCustomer(int index, Customer customer) {
index--;
if (index >= total || index < 0) {
return false;
}
customers[index] = customer;
return true;
}
//洗掉
public boolean deleteCustomer(int index) {
if (index >= total || index < 0) {
return false;
}
for (int i = index; i < total - 1; i++) {
customers[i] = customers[i + 1];
}
customers[--total] = null;
return true;
}
//查看
public Customer[] getAllCustomers() {
return customers;
}
//獲取指定物件
public Customer getCustomer(int index) {
return customers[index];
}
//獲取記錄條數
public int getTotal() {
return total;
}
}
Customer類中的代碼如下:
public class Customer {
private String name;
private char gender;//性別
private int age;
private String phone;
private String email;//郵箱
public Customer() {
}
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() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Cmutility工具類代碼如下:
/**
CMUtility工具類:
將不同的功能封裝為方法,就是可以直接通過呼叫方法使用它的功能,而無需考慮具體的功能實作細節,
*/
public class CMUtility {
private static Scanner scanner = new Scanner(System.in);
/**
用于界面選單的選擇,該方法讀取鍵盤,如果用戶鍵入’1’-’5’中的任意字符,則方法回傳,回傳值為用戶鍵入字符,
*/
public static char readMenuSelection() {
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("選擇錯誤,請重新輸入:");
} else break;
}
return c;
}
/**
從鍵盤讀取一個字符,并將其作為方法的回傳值,
*/
public static char readChar() {
String str = readKeyBoard(1, false);
return str.charAt(0);
}
/**
從鍵盤讀取一個字符,并將其作為方法的回傳值,
如果用戶不輸入字符而直接回車,方法將以defaultValue 作為回傳值,
*/
public static char readChar(char defaultValue) {
String str = readKeyBoard(1, true);
return (str.length() == 0) ? defaultValue : str.charAt(0);
}
/**
從鍵盤讀取一個長度不超過2位的整數,并將其作為方法的回傳值,
*/
public static int readInt() {
int n;
for (; ; ) {
String str = readKeyBoard(2, false);
try {
n = Integer.parseInt(str);
break;
} catch (NumberFormatException e) {
System.out.print("數字輸入錯誤,請重新輸入:");
}
}
return n;
}
/**
從鍵盤讀取一個長度不超過2位的整數,并將其作為方法的回傳值,
如果用戶不輸入字符而直接回車,方法將以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("數字輸入錯誤,請重新輸入:");
}
}
return n;
}
/**
從鍵盤讀取一個長度不超過limit的字串,并將其作為方法的回傳值,
*/
public static String readString(int limit) {
return readKeyBoard(limit, false);
}
/**
從鍵盤讀取一個長度不超過limit的字串,并將其作為方法的回傳值,
如果用戶不輸入字符而直接回車,方法將以defaultValue 作為回傳值,
*/
public static String readString(int limit, String defaultValue) {
String str = readKeyBoard(limit, true);
return str.equals("")? defaultValue : str;
}
/**
用于確認選擇的輸入,該方法從鍵盤讀取‘Y’或’N’,并將其作為方法的回傳值,
*/
public static char readConfirmSelection() {
char c;
for (; ; ) {
String str = readKeyBoard(1, false).toUpperCase();
c = str.charAt(0);
if (c == 'Y' || c == 'N') {
break;
} else {
System.out.print("選擇錯誤,請重新輸入:");
}
}
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("輸入長度(不大于" + limit + ")錯誤,請重新輸入:");
continue;
}
break;
}
return line;
}
}
代碼實作效果圖如下:

添加客戶,每一項都能做出相應的判斷如圖所示:

修改客戶

修改之后再查看是否修改成功

可以看到是修改成功的
我們再添加2個客戶,方便洗掉演示:
此時有3個客戶資訊,那么我們刪掉編號2的王五,胖虎就會充當編號2
我們看看是否成功

再看看客戶資訊:

從上圖可以看出洗掉是成功的
那么當我們輸入-1時:

便可以退出,修改也是一樣可以退出,在這里就不演示了,
再來看看退出操作:

輸入n時回傳上一界面,輸入y時則退出系統

上面洗掉功能也是如此,就不再演示此功能,
那么我們來總結一下,首先是要有一個Customer類,然后把這類實體化放在Customer陣列中,讓后把Customer陣列封裝給CustomerList,這里類似于集合,讓后我們在CustomerView中通過CustomerList來遍歷其中的customer物件,以及方法的呼叫達到其顯示的效果,
那么String類中的方法很多,我們可以通過百度搜索其常用的方法,便于大家進一步鞏固自己以及彌補自己的不足,
在此希望大家多多評論,讓我發現自己的不足,最后謝謝大家,
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/291685.html
標籤:其他
