新手易懂的Java客戶管理小項目
每日一語:花開蝶自來!!!
前言:隨著我把狂神的Java的基礎篇看完,我覺得我應該是把Java的基礎應該沒什么問題了,所以我決定找一個小項目寫寫,所以我就看了尚硅谷的基礎小項目,不看不知道,一看嚇一跳,我發(fā)現(xiàn)我雖然看完了基礎的部分,但是我自己用起來還是有很多不足的地方,好在我請教了一些大佬們幫我解決這些問題,在這里也是很感謝他們?。?!接下來話不多說我們直接上代碼!?。?/p>
成果展示
初始化界面:
功能一:添加客戶
我們來看看添加后的效果:
可以看見我們是添加成果了,我們可以繼續(xù)下面的操作。
功能二:修改客戶
我們來看看是否修改成功:
可以看到我們是修改成功的?。?!
功能三:客戶刪除
從圖上看我們是刪除成功的
功能四:展示客戶列表
因為剛才把鯊魚辣椒刪了所以現(xiàn)在只剩鐵甲小寶了哈哈哈哈哈哈哈哈哈哈。
思路分析
我們可以把這個項目分為三個部分:
1.數(shù)據(jù)的存儲部分。
2.一些功能函數(shù)部分。
3.可視化界面部分。
1.
首先我們來看看數(shù)據(jù)的存儲是怎么構建的:
我們先創(chuàng)建一個Customer的一個Java文件,用來存儲數(shù)據(jù),使用構造器初始化數(shù)據(jù),并且用private進行封裝一些數(shù)據(jù)。并且用 set 和 get 獲取數(shù)據(jù)。
2.
四個功能函數(shù)的實現(xiàn)
第一個就是添加客戶數(shù)據(jù),我們看下面的代碼實現(xiàn)?。?/p>
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.
也就是我們在上面看見的可視化的部分,所以我們來構建這部分:
先創(chuàng)建能讓我們看見的部分:
我們在使用功能的時候也是用的數(shù)字選擇,我們可以使用switch結構進行選擇,并且在相應的數(shù)字里面調用相對應的函數(shù):
代碼部分
1.數(shù)據(jù)存儲部分:
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.函數(shù)功能部分:
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',"2222@qq.com","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:繼續(xù)?。。?); 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<custs.length ; i++) { Customer cust = custs[i]; System.out.println(i+1+"\t"+cust.getName()+"\t"+cust.getAge()+"\t"+cust.getEmail()+"\t"+cust.getPhone()+"\t"+cust.getGrade()); } } } }
項目總結
最后也來說說這個項目吧,因為是練手的小項目,也是我的第一個Java小項目,所以寫一篇博客記錄一下,并不是什么高級項目,如果一些大佬覺得寫的垃圾,也可以給我說一下,我會更加努力的改進,總得來說任重而道遠?。?!
到此這篇關于新手易懂的Java客戶管理小項目的文章就介紹到這了,更多相關Java 客戶管理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Redisson分布式閉鎖RCountDownLatch的使用詳細講解
分布式鎖和我們java基礎中學習到的synchronized略有不同,synchronized中我們的鎖是個對象,當前系統(tǒng)部署在不同的服務實例上,單純使用synchronized或者lock已經(jīng)無法滿足對庫存一致性的判斷。本次主要講解基于rediss實現(xiàn)的分布式鎖2023-02-02Springboot集成JUnit5優(yōu)雅進行單元測試的示例
這篇文章主要介紹了Springboot集成JUnit5優(yōu)雅進行單元測試的示例,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2020-10-10java基礎-給出一個隨機字符串,判斷有多少字母?多少數(shù)字?
這篇文章主要介紹了java基礎-給出一個隨機字符串,判斷有多少字母?多少數(shù)字?文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-04-04