API
概念:应用程序接口。
javaAPI:指的是JDK中提供的各种功能的java类。
String
概念:
String类在java.long包下,使用时不需要导包!
特点:
- 字符串不可变,他们的值在创建后不能被更改
- 虽然String的值不可变,但是他们能共享
- 字符串效果上相当于字符组( char [ ]),但是底层原理是字节数组( byte [] )
构造方法:
无参构造方法:
有参构造方法:
直接赋值:String s = "acdsd’ (推荐使用)
String对象的特点:
1.通过new创建的字符对象,每一次new都会申请一个内存康健,虽然内容相同,但是地址值不同。
2.以' '方式给出的字符串,只要字符序列相同(顺序和大小写),无法在程序代码中出现几次,JVM 都只会建立一个String对象,并在字符串中维护。
Stringbuilder
概述:是一个可变的字符串,可以看成是一个容器。
与String的区别:
- String:内容是不可变的
- Stringbuilder:内容是可变的
构造方法:
StringBuilder sb = new StringBuilder0;
StringBuilder的添加和反转方法
添加:addpeng
反转:reverse
与String的互转
String转换为StringBuilder用StringBuilder的构造方法就行了
StringBuilder转换位String用一个toString方法
字符串的比较:
使用==做比较时,基本类型比较的是数值,二引用类型比较的是地址,想要比较数值,则要使用equals方法实现
集合
概述:集合在编程的时候存储了多个数据,能满足我们对变化的需求
特点:提供一个存储空间可变的存储模型,存储的数据容量可以发生改变
ArrayList
添加方法:add方法
构造方法:Array List <String> array= new ArrayList<>();
常用方法:
remove方法:删除指定元素,返回删除是否元素
set方法: 修改指定元素,返回修改的元素
get 方法:返回指定索引的元素值
size方法:返回集合中的元素个数
客户信息管理系统
根据教学所给的ppt来写
代码表示:
Customer类的代码块
// Customer类的代码
package Customer_Information_management;
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;
}
}
CustomerList代码块:
package Customer_Information_management;
public class CustomerList {
//声明Customer类型数组customers和记录存储的客户的个数的变量
public Customer[] customers;
public int total = 0;
//定义无参和有参构造器
public CustomerList() {
}
public CustomerList(int totalCustomer) {
this.customers = new Customer[totalCustomer];
}
// 修改
public boolean addCustomer(Customer customer) {
if (customers.length == total) {
return false;
} else {
customers[total] = customer;
total++;
return true;
}
}
// 修改
public boolean replaceCustomer(int index, Customer cust) {
if (index < 0 || index > this.total) {
return false;
} else {
customers[index] = cust;
return true;
}
}
//删除
public boolean deleteCustomer(int index) {
if (index < 0 || index > this.total) {
return false;
}
//index++;
for (int i = 0; i < this.total; i++) {
if (i >= index) {
customers[i] = customers[i + 1]; //往前移
}
}
this.total--;
return true;
}
//返回数组对象
public Customer[] getAllCustomers() {
return customers;
}
//查询
public Customer getCustomer(int index) {
if (customers[index] == null) {
return null;
} else {
return customers[index];
}
}
//Customer对象的个数
public int getTotal() {
return total;
}
}
CustomerView代码块:
package Customer_Information_management;
import java.util.Scanner;
public class CustomerView {
private Scanner sc = new Scanner (System.in);
private CustomerList customerList = new CustomerList (10);
public static void main(String[] args) {
CustomerView c = new CustomerView ();
c.enterMainMenu ();
}
// 主界面
private void enterMainMenu() {
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 a =CMUtility.readMenuSelection ();
switch (a) {
case '1': // 添加
addNewCustomer ();
break;
case '2': // 修改
modifyCustomer ();
break;
case '3': // 删除
deleteCustomer ();
break;
case '4': // 客户列表
listAllCustomers ();
break;
case '5': // 退出
quit ();
break;
default:
System.out.println ("输入错误!!");
}
}
}
// 添加
private void addNewCustomer() {
System.out.println ("---------------------添加客户---------------------");
System.out.print ("请输入客户姓名:");
String name = CMUtility.readString (5);
System.out.println ();
System.out.print ("请输入性别:");
char gender = CMUtility.readChar ();
System.out.println ();
System.out.print ("请输入年龄:");
int age = CMUtility.readInt ();
System.out.println ();
System.out.print ("请输入电话号码:");
String phone = CMUtility.readString (11);
System.out.println ();
System.out.print ("请输入电子邮箱:");
String email = CMUtility.readString (20);
System.out.println ();
Customer c = new Customer (name, gender, age, phone, email);
boolean flag = customerList.addCustomer (c);
if (flag) {
System.out.println ("---------------------添加完成---------------------");
} else {
System.out.println ("添加失败!!");
}
}
// 修改
private void modifyCustomer() {
System.out.println ("---------------------修改客户---------------------");
System.out.println ("请选择待修改客户编号(-1退出):");
int index = sc.nextInt ();
if (index == -1) {
System.out.println ("退出修改操作");
} else {
index--;
while (true) {
if (index < customerList.getTotal () && index >= 0) {
System.out.print ("姓名(" + customerList.customers[index].getName ()+"):");
String xname = CMUtility.readString (5);
System.out.println ();
System.out.print ("性别(" + customerList.customers[index].getGender ()+"):");
char xgendenr =CMUtility.readChar ();
System.out.println ();
System.out.print ("年龄(" + customerList.customers[index].getAge ()+"):");
int xage = CMUtility.readInt ();
System.out.println ();
System.out.print ("电话(" + customerList.customers[index].getPhone ()+"):");
String xphone =CMUtility.readString (11);
System.out.println ();
System.out.print ("邮箱(" + customerList.customers[index].getEmail ()+"):");
String xemail =CMUtility.readString (20);
System.out.println ();
Customer c = new Customer (xname, xgendenr, xage, xphone, xemail);
boolean flag = customerList.replaceCustomer (index, c);
if(flag){
System.out.println("-----------------修改完成-----------------");
}else {
System.out.println("修改失败!");
}
break;
}else{
System.out.println ("输入的编号错误,请重新输入:");
int index1 = sc.nextInt();
index = index1-1;
}
}
}
}
// 删除
private void deleteCustomer() {
if(customerList.getTotal()==0){
System.out.println("没有客户消息!!!");
}else{
System.out.println("-----------------删除客户-----------------");
System.out.println("请选择待删除客户编号(-1退出):");
int index = sc.nextInt ();
if(index==-1){
System.out.println("已退出删除操作");
}else{
index--;
while (true){
if(index<customerList.getTotal() && index>=0){
System.out.println("确认是否删除(Y/N):");
char c =CMUtility.readConfirmSelection ();
if(c=='Y'||c=='y'){
boolean flag = customerList.deleteCustomer(index);
if (flag){
System.out.println("-----------------删除完成-----------------");
}else {
System.out.println("删除失败!");
}
}else {
System.out.println("已取消删除");
}
System.out.println();
break;
}else {
System.out.print("输入的编号有误,请重新输入:");
int index1 = sc.nextInt();
index = index1-1;
}
}
}
}
}
// 客户列表
private void listAllCustomers() {
// Customer[] Cust =customerList.getAllCustomers ();
if (customerList.getTotal () == 0) {
System.out.println ("当前无客户信息!!");
} else {
System.out.println ("---------------------------客户列表---------------------------");
System.out.println ("编号\t\t姓名\t\t性别\t\t年龄\t\t电话\t\t\t\t邮箱");
for (int i = 0; i < customerList.getTotal (); i++) {
System.out.println ((i+1) + "\t\t" + customerList.customers[i].getName () + "\t\t" + customerList.customers[i].getGender () + "\t\t" + customerList.customers[i].getAge () + "\t\t" +
customerList.customers[i].getPhone () + "\t\t" + customerList.customers[i].getEmail ()
);
}
System.out.println ("-------------------------客户列表完成-------------------------");
}
}
// 退出
private void quit() {
System.out.println ("是否退出:(Y/N)");
char ch =CMUtility.readConfirmSelection ();
if (ch == 'Y') {
System.out.println ("退出成功!!");
System.exit (0);
}
if (ch == 'N') {
System.out.println ("退出失败,请重新输入!!");
}
}
}
根据教学所给的ppt来写这些代码块。