每日一语:花开蝶自来!!!
前言:随着我把狂神的Java的基础篇看完,我觉得我应该是把Java的基础应该没什么问题了,所以我决定找一个小项目写写,所以我就看了尚硅谷的基础小项目,不看不知道,一看吓一跳,我发现我虽然看完了基础的部分,但是我自己用起来还是有很多不足的地方,好在我请教了一些大佬们帮我解决这些问题,在这里也是很感谢他们!!!接下来话不多说我们直接上代码!!!
成果展示
初始化界面:
功能一:添加客户
我们来看看添加后的效果:
可以看见我们是添加成果了,我们可以继续下面的操作。
功能二:修改客户
我们来看看是否修改成功:
可以看到我们是修改成功的!!!
功能三:客户删除
从图上看我们是删除成功的
功能四:展示客户列表
因为刚才把鲨鱼辣椒删了所以现在只剩铁甲小宝了哈哈哈哈哈哈哈哈哈哈。
思路分析
我们可以把这个项目分为三个部分:
1.数据的存储部分。
2.一些功能函数部分。
3.可视化界面部分。
1.
首先我们来看看数据的存储是怎么构建的:
我们先创建一个Customer的一个Java文件,用来存储数据,使用构造器初始化数据,并且用private进行封装一些数据。并且用 set 和 get 获取数据。
2.
四个功能函数的实现
第一个就是添加客户数据,我们看下面的代码实现!!
public boolean addcust(Customer customer){ if (total >= customers.length){return false; }else{ customers[total++] = customer; } return true; }
第二个修改:
public boolean replacecust(int index , Customer customer){ if (index<0 || index >= customers.length){ return false; }else{ customers[index] = customer; } return true; }
第三个删除:
public boolean deletecust(int index){ if (index<0 || index >= customers.length){ return false; } for(int i = index; i< total - 1;i++){ customers[i] = customers[i+1]; } customers[total-1 ]= null; total --; return true; }
第四个查看所有的客户:
public Customer[] getCustomers(){ Customer[] cus = new Customer[total]; for(int i = 0; i < total; i++){ cus[i] = customers[i]; } return cus;
嘿嘿嘿,我就偷个懒,思路我就不具体写了,大家可以看代码嘿嘿嘿!
3.
也就是我们在上面看见的可视化的部分,所以我们来构建这部分:
先创建能让我们看见的部分:
我们在使用功能的时候也是用的数字选择,我们可以使用switch结构进行选择,并且在相应的数字里面调用相对应的函数:
代码部分
1.数据存储部分:
package cus; public class Customer { private String name; private char grade; private int age; private String phone; private String email; public void setName(String name) { this.name = name; } public String getName() { return name; } public void setAge(int age) { this.age = age; } public int getAge() { return age; } public void setGrade(char grade) { this.grade = grade; } public char getGrade() { return grade; } public void setEmail(String email) { this.email = email; } public String getEmail() { return email; } public void setPhone(String phone) { this.phone = phone; } public String getPhone() { return phone; } public Customer(){ } public Customer(String name ,int age , char grade, String email,String phone ){ this.name = name; this.email = email; this.grade = grade; this.age = age; this.phone = phone; } }
2.函数功能部分:
package cus; public class CustomerList { private Customer[] customers; private static int total = 0; public CustomerList(int totalCustomerList){ customers = new Customer[totalCustomerList]; } public boolean addcust(Customer customer){ if (total >= customers.length){return false; }else{ customers[total++] = customer; } return true; } public boolean replacecust(int index , Customer customer){ if (index<0 || index >= customers.length){ return false; }else{ customers[index] = customer; } return true; } public boolean deletecust(int index){ if (index<0 || index >= customers.length){ return false; } for(int i = index; i< total - 1;i++){ customers[i] = customers[i+1]; } customers[total-1 ]= null; total --; return true; } public Customer[] getCustomers(){ Customer[] cus = new Customer[total]; for(int i = 0; i < total; i++){ cus[i] = customers[i]; } return cus; } public Customer getCust(int indsx){ if(indsx<0 || indsx >= total){ return null; } return customers[indsx]; } public int getTotal(){ return total; } }
3.可视化界面部分:
package cus; import java.util.Scanner; public class View { private static CustomerList customerList = new CustomerList(10); public View(){ Customer customer = new Customer("李华",18,'8',"[email protected]","123445697"); customerList.addcust(customer); } public void enterMain(){ System.out.println("1.添加用户"); System.out.println("2.修改客户"); System.out.println("3.删除客户"); System.out.println("4.客户列表"); System.out.println("5.退出"); } public static void main(String[] args) { View view = new View(); Scanner scanner = new Scanner(System.in); boolean ifFage = true; while (ifFage){ view.enterMain(); System.out.println("请输入:"); switch (scanner.nextInt()){ case 1: addNewcust(); break; case 2: modifyCust(); break; case 3: System.out.println("请输入序号:"); deleetCust(); break; case 4: listAllCustomer(); break; case 5: System.out.println("是否退出?(1:退出,2:继续!!)"); if (scanner.nextInt() == 1){ System.out.println("退出!"); ifFage = false;} } } } private static void addNewcust(){ Scanner scanner = new Scanner(System.in); System.out.println("姓名:"); String name = scanner.nextLine(); System.out.println("年龄:"); int age = scanner.nextInt(); System.out.println("性别:"); char grade = (char)scanner.nextInt(); System.out.println("邮箱:"); String email = scanner.next(); System.out.println("电话:"); String phone = scanner.next(); Customer customer = new Customer(name,age,grade,email,phone); customerList.addcust(customer); System.out.println("添加成功!"); // System.out.println("方法1"); } private static void modifyCust(){ Scanner scanner = new Scanner(System.in); Customer cust = null ; int t; for (;;) { System.out.println("输入-1退出!"); t = scanner.nextInt(); if (t == -1 ) break; cust = customerList.getCust(t-1); if(cust == null){ System.out.println("没有该用户!"); }else{ break; } } System.out.println("姓名("+cust.getName()+")"); System.out.println("修改为:"); String name = scanner.next(); System.out.println("年龄("+cust.getAge()+")"); System.out.println("修改为:"); int age = scanner.nextInt(); System.out.println("性别("+cust.getGrade()+")"); System.out.println("修改为:"); char grade = (char)scanner.nextInt(); System.out.println("邮箱("+cust.getEmail()+")"); System.out.println("修改为:"); String email = scanner.next(); System.out.println("手机("+cust.getPhone()+")"); System.out.println("修改为:"); String phone = scanner.next(); Customer customer = new Customer(name,age,grade,email,phone); boolean i = customerList.replacecust(t-1,customer); if (i == false ){ System.out.println("修改失败!"); }else{ System.out.println("修改成功!"); } } private static void deleetCust(){ int total = customerList.getTotal(); Scanner scanner = new Scanner(System.in); int a = scanner.nextInt(); if(a <0 || a>total) {System.out.println("没有该用户!");}else{ boolean customer1 = customerList.deletecust(a-1); if (customer1 == false){ System.out.println("删除失败!"); }else { System.out.println("删除成功!!"); } } } private static void listAllCustomer(){ int total = customerList.getTotal(); if (total == 0){ System.out.println("没有客户记录!"); }else{ System.out.println("客户名单:"); Customer[] custs = customerList.getCustomers(); for(int i = 0; i
项目总结
最后也来说说这个项目吧,因为是练手的小项目,也是我的第一个Java小项目,所以写一篇博客记录一下,并不是什么高级项目,如果一些大佬觉得写的垃圾,也可以给我说一下,我会更加努力的改进,总得来说任重而道远!!!
到此这篇关于新手易懂的Java客户管理小项目的文章就介绍到这了,更多相关Java 客户管理内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!