String与StringBuileder用法及客户信息管理软件项目

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类中的方法很多,我们可以通过百度搜索其常用的方法,便于大家进一步巩固自己以及弥补自己的不足。

在此希望大家多多评论,让我发现自己的不足,最后谢谢大家。

  • 11
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值