상품 관리 시스템 자바 구현

169802 단어 Java자바
상품 관리 시스템
실현 기능:상품 조회,신규,가격 변경,삭제 먼저 세 가지 기본 유형의 구축 상품 류,계 정 류,브랜드 류 1,상품 류
public class Goods {
     
    //    :    ,    ,    ,    ,        
    private String goodsName;
    private double goodsPrice;
    private int goodsSales;
    private String goodsCategories;
    private int brandsNum;
    private String GoodsNum;

    public Goods(String goodsName, double goodsPrice, int goodsSales, String goodsCategories,int brandsNum, String goodsNum) {
     
        this.goodsName = goodsName;
        this.goodsPrice = goodsPrice;
        this.goodsSales = goodsSales;
        this.goodsCategories = goodsCategories;
        this.brandsNum = brandsNum;
        GoodsNum = goodsNum;
    }

    public int getBrandsNum() {
     
        return brandsNum;
    }

    public void setBrandsNum(int brandsNum) {
     
        this.brandsNum = brandsNum;
    }

    public Goods() {
     
    }

    public String getGoodsNum() {
     
        return GoodsNum;
    }

    public void setGoodsNum(String goodsNum) {
     
        GoodsNum = goodsNum;
    }

    public String getGoodsName() {
     
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
     
        this.goodsName = goodsName;
    }

    public double getGoodsPrice() {
     
        return goodsPrice;
    }

    public void setGoodsPrice(double goodsPrice) {
     
        this.goodsPrice = goodsPrice;
    }

    public int getGoodsSales() {
     
        return goodsSales;
    }

    public void setGoodsSales(int goodsSales) {
     
        this.goodsSales = goodsSales;
    }

    public String getGoodsCategories() {
     
        return goodsCategories;
    }

    public void setGoodsCategories(String goodsCategories) {
     
        this.goodsCategories = goodsCategories;
    }

    public int getBrands() {
     
        return brandsNum;
    }

    public void setBrands(int brandsNum) {
     
        this.brandsNum = brandsNum;
    }
}


각 속성의 get 과 set 방법 계 정 클래스 를 실현 하 였 습 니 다.
public class Accounts {
     
    //    :    ,    
    private String userName;
    private String userPassword;

    public Accounts(String userName, String userPassword) {
     
        this.userName = userName;
        this.userPassword = userPassword;
    }

    public Accounts() {
     
    }

    public String getUserName() {
     
        return userName;
    }

    public void setUserName(String userName) {
     
        this.userName = userName;
    }

    public String getUserPassword() {
     
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
     
        this.userPassword = userPassword;
    }
}

브랜드 클래스
public class Brands {
     
    //      :      ,        
    private String brandsName;
    private int brandsNum;

    public Brands(String brandsName, int brandsNum) {
     
        this.brandsName = brandsName;
        this.brandsNum = brandsNum;
    }

    public String getBrandsName() {
     
        return brandsName;
    }

    public void setBrandsName(String brandsName) {
     
        this.brandsName = brandsName;
    }

    public int getBrandsNum() {
     
        return brandsNum;
    }

    public void setBrandsNum(int brandsNum) {
     
        this.brandsNum = brandsNum;
    }
}

그 다음 에 이 세 가지 기본 적 인 정 보 를 저장 하 는 데이터 류 로 List 를 정의 하여 세 가지 유형의 저장 데이터 류 를 실현 한다.
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class Database {
     
    //      
    private List<Accounts>accountsList=new ArrayList<>();
    //      
    private List<Goods>goodsList=new ArrayList<>();
    //      
    private List<Brands>brandsList=new ArrayList<>();
    public Database(){
     
        //       
                ,    ,    
    }

    public List<Accounts> getAccountsList() {
     
        return accountsList;
    }

    public void setAccountsList(List<Accounts> accountsList) {
     
        this.accountsList = accountsList;
    }

    public List<Goods> getGoodsList() {
     
        return goodsList;
    }

    public void setGoodsList(List<Goods> goodsList) {
     
        this.goodsList = goodsList;
    }

    public List<Brands> getBrandsList() {
     
        return brandsList;
    }

    public void setBrandsList(List<Brands> brandsList) {
     
        this.brandsList = brandsList;
    }
}

그 다음 에 세 가지 기본 적 인 유형 이 각각 삭제 하고 검사 하 는 방법의 실현 이다.이것 은 프로그램의 수요 에 따라 상품 의 상호작용 유형 을 쓴다.
import java.util.ArrayList;
import java.util.List;


public class GoodsDAO {
     
    //     
    private Database database;
    //     DAO ,     
    public GoodsDAO(Database database){
     
        this.database=database;
    }
    //         
    public List<Goods>hotSell(){
     
        List<Goods> temporList=database.getGoodsList();
        for(int i=0;i<temporList.size();i++){
     
            for(int j=0;j<temporList.size()-1-i;j++){
     
                if(temporList.get(j).getGoodsSales()<temporList.get(j+1).getGoodsSales())
                {
     
                    Goods e=temporList.get(j+1);
                    temporList.set(j+1,temporList.get(j));
                    temporList.set(j,e);
                }
            }
        }
        return temporList;
    }
    //          
    public Goods searchById(int id){
     
        Goods goods;
        goods=database.getGoodsList().get(id-1);
        return goods;
    }
    //          
    public List<Goods>searchByBranchId(int brandsId){
     
        List<Goods>temporList=new ArrayList<>();
        for(Goods each:database.getGoodsList()){
     
            if(each.getBrandsNum()==brandsId)
                temporList.add(each);
        }
        if(temporList.isEmpty())
            temporList=null;
        return  temporList;
    }
    //          
    public List<Goods>searchByPrice(double minPrice,double maxPrice){
     
        List<Goods>temporList=new ArrayList<>();
        for(Goods each:database.getGoodsList()){
     
            if(each.getGoodsPrice()>=minPrice&&each.getGoodsPrice()<=maxPrice) {
     
                temporList.add(each);
            }
        }
        if(temporList.isEmpty())
            temporList=null;
        return  temporList;
    }
    //    
    public void addGoods(Goods goods){
     
        database.getGoodsList().add(goods);
    }
    //      
    public void modify(String goodsId,double price){
     
        for(Goods each:database.getGoodsList()){
     
            if(each.getGoodsNum().equals(goodsId))
                each.setGoodsPrice(price);
        }
    }
    //    
    public void delete(String goodsId){
     
        Goods goods=null;
        for(Goods each:database.getGoodsList()){
     
            if(each.getGoodsNum().equals(goodsId))
                goods=each;
        }
        database.getGoodsList().remove(goods);
        //        ArrayList   ,             ,        。
    }
    //        
    public void changeByBranchId(int brandsId){
     
        for(Goods each:database.getGoodsList()){
     
            if(each.getBrandsNum()==brandsId)
                each.setBrandsNum(brandsId-1);
        }
    }
}


계 정 상호작용 클래스 등록,로그 인 수요 실현
public class AccountDAO {
     
    //     
    private Database database;
    //     DAO ,     
    public AccountDAO(Database database){
     
        this.database=database;
    }
    //          
    public Accounts searchAccounts(String userName){
     
        Accounts accounts=null;
        for(Accounts each:database.getAccountsList()){
     
            if(each.getUserName().equals(userName))
                accounts=each;
        }
        return accounts;
    }
    //     
    public void addAccount(Accounts accounts){
     
        database.getAccountsList().add(accounts);
    }
}

브랜드 클래스
public class BrandsDAO {
     
    //     
    private Database database;
    //     DAO ,     
    public BrandsDAO(Database database){
     
        this.database=database;
    }
    //        
    public Brands findByName(String brandName){
     
        Brands brands=null;
        for(Brands each:database.getBrandsList()){
     
            if(each.getBrandsName().equals(brandName)) {
     
                brands = each;
                break;
            }
        }
        return brands;
    }
    //      
    public void findAll(){
     
        System.out.println("    :");
        for(Brands each:database.getBrandsList()){
     
            System.out.println(each.getBrandsNum()+"\t"+each.getBrandsName());
        }
    }
    //            
    public String findNameById(int id){
     
        String brandName;
        Brands brands=null;
        for(Brands each:database.getBrandsList()){
     
            if(each.getBrandsNum()==id){
     
                brands=each;
            }
        }
        return brands.getBrandsName();
    }
    //        
    public boolean findBranch(String brandsName){
     
        boolean choose=false;
        for(Brands each:database.getBrandsList())
        {
     
            if(each.getBrandsName().equals(brandsName))
                choose=true;
        }
        return choose;
    }
    //      
    public void addBranch(Brands brands){
     
        database.getBrandsList().add(brands);
    }
    //          
    public int brandsNumbers(){
     
        int num=database.getBrandsList().size();
        return num;
    }
    //        
    public void deleteBrands(int num){
     
        Brands brands=null;
        for(Brands each:database.getBrandsList()){
     
            if(each.getBrandsNum()==num)
                brands=each;
        }
        database.getBrandsList().remove(brands);
    }
    //    
    public void moveBrandsNum(int brandsNum){
     
        int index;
        for(int i=0;i<database.getBrandsList().size();i++){
     
            if(database.getBrandsList().get(i).getBrandsNum()>brandsNum)
                database.getBrandsList().get(i).setBrandsNum(database.getBrandsList().get(i).getBrandsNum()-1);
        }
    }
}

서비스 류 는 프로젝트 에 필요 한 기능 을 실현 하고 상호작용 인터페이스 를 구축한다.
public class GoodsSystem {
     
    Scanner scanner=new Scanner(System.in);
    Random random=new Random();
    AccountDAO accountDAO;
    BrandsDAO brandsDAO;
    GoodsDAO goodsDAO;
    //   DAO
    public GoodsSystem(Database database){
     
        accountDAO=new AccountDAO(database);
        brandsDAO=new BrandsDAO(database);
        goodsDAO=new GoodsDAO(database);
    }
    public void start(){
     
//        1.  
        System.out.println("----------------    ---------------");
        System.out.println("1.  ");
//        2.  
        System.out.println("2.  ");
//        3.    
        System.out.println("3.    ");
        System.out.println("   :");
        String choose=scanner.next();
        switch(choose){
     
            //  
            case "1":
                login();
                break;
            //  
            case "2":
                regist();
                break;
            //    
            case "3":
                System.out.println("      ");
                break;
            default:
                System.out.println("    ,     :");
                start();
        }
    }
    //  
    public void login(){
     
        System.out.println("      :");
        String userName=scanner.next();
        System.out.println("     :");
        String passWord=scanner.next();
        Accounts accounts=accountDAO.searchAccounts(userName);
        String testNum="";
        if(accounts!=null){
     
            if(accounts.getUserPassword().equals(passWord)){
     
                for(int i=0;i<4;i++){
     
                    int num=random.nextInt(10);
                    testNum=testNum+num;
                }
                System.out.println("   :"+testNum);
                System.out.println("      :");
                String testNumInput=scanner.next();
                if(testNumInput.equals(testNum)) {
     
                    System.out.println("    ");
                    mainMenu();
                }
                else{
     
                    System.out.println("     ,     ");
                    login();
                }
            }
            else{
     
                System.out.println("    ,     ");
                System.out.println("         ,   0       ");
                String choose=scanner.next();
                if(choose.equals("0"))
                    start();
                else
                    login();
            }
        }
        else{
     
            System.out.println("      ,     :");
            System.out.println("         ,   0       ");
            String choose=scanner.next();
            if(choose.equals("0"))
                start();
            else
                login();
        }
    }
    //  
    public void regist(){
     
        System.out.println("----  ----");
        //   
        System.out.println("      :");
        String userName=scanner.next();
        Accounts accounts=accountDAO.searchAccounts(userName);
        while(accounts!=null){
     
            System.out.println("        ,     ");
            userName=scanner.next();
            accounts=accountDAO.searchAccounts(userName);
        }
        //  
        System.out.println("     :");
        String userPassWord=scanner.next();
        System.out.println("    :");
        String testPassWord=scanner.next();
        while(!userPassWord.equals(testPassWord)){
     
            System.out.println("            ,       ");
            System.out.println("     :");
            userPassWord=scanner.next();
            System.out.println("    :");
            testPassWord=scanner.next();
        }
        //   
        String testNum="";
        for(int i=0;i<4;i++){
     
            int num=random.nextInt(10);
            testNum=testNum+num;
        }
        System.out.println("   :"+testNum);
        System.out.println("      :");
        String testNumInput=scanner.next();
        if(testNumInput.equals(testNum)) {
     
            accountDAO.addAccount(new Accounts(userName,userPassWord));
            System.out.println("    ");
            start();
        }
        else{
     
            System.out.println("     ,     ");
            System.out.println("         ,   0       ");
            String choose=scanner.next();
            if(choose.equals("0"))
                start();
            else
                regist();
        }
    }
    //   
    public void mainMenu(){
     
        System.out.println("-------------   ------------");
//        1.    
        System.out.println("1.    ");
//        2.    
        System.out.println("2.    ");
//        3.    
        System.out.println("3.    ");
        System.out.println("4.    ");
        System.out.println("       :");
        String choose=scanner.next();
        switch (choose){
     
            case "1"://    
                hotSell();
                break;
            case "2"://    
                searchGoods();
                break;
            case "3"://    
                controlGoods();
                break;
            case "4"://    
                System.out.println("      ");
                break;
            default:
                mainMenu();
        }
    }
    //    
    public void hotSell(){
     
        System.out.println("------------    ------------");
        List <Goods>tempor=goodsDAO.hotSell();
        System.out.println("  \t\t"+"  "+"\t\t\t\t\t  \t");
        int num=10;
        if(tempor.size()<10)
            num=tempor.size();
        for(int i=0;i<num;i++){
     
            System.out.println((i+1)+"\t\t"+tempor.get(i).getGoodsName()+"\t\t\t\t\t"+tempor.get(i).getGoodsSales()+"\t");
        }
        System.out.println("         ID   0       。");
        String chooseEnd=scanner.next();
        if(isNumeric(chooseEnd)==true)
        {
     
            int choose=Integer.parseInt(chooseEnd);
            if(choose==0){
     
                mainMenu();
            }
            else
            {
     
                if(choose<=num&&choose>=0) {
     
                    Goods goods = goodsDAO.searchById(choose);
                    System.out.println("    \t\t  \t\t  \t\t  ");
                    System.out.println(goods.getGoodsName() + "\t\t\t" + brandsDAO.findNameById(goods.getBrandsNum()) + "\t" + goods.getGoodsPrice() + " \t" + goods.getGoodsCategories());
                    mainMenu();
                }
                else
                {
     
                    System.out.println("         ,     ");
                    hotSell();
                }
            }
        }
        else
        {
     
            System.out.println("      ,     ");
            hotSell();
        }
    }
    //    
    public void searchGoods(){
     
        System.out.println("---------    ----------");
//        1.    
        System.out.println("1.    ");
//        2.    
        System.out.println("2.    ");
        System.out.println("3.       ");
        System.out.println("       :");
        String choose=scanner.next();
        switch(choose){
     
            case "1"://    
                searchByBranch();
                break;
            case "2"://    
                searchByPrice();
                break;
            case "3":
                mainMenu();
                break;
            default:
                searchGoods();
        }
    }
    //    
    public void controlGoods(){
     
        System.out.println("--------    --------");
//        1.    
        System.out.println("1.    ");
//        2.    
        System.out.println("2.    ");
//        3.    
        System.out.println("3.    ");
        System.out.println("4.     ");
        System.out.println("     :");
        String choose=scanner.next();
        switch (choose){
     
            case "1"://    
                addGoods();
                break;
            case "2"://    
                changePrice();
                break;
            case "3"://    
                deleteGoods();
                break;
            case "4"://     
                mainMenu();
                break;
            default:
                controlGoods();
        }
    }
    //      
    public void searchByBranch(){
     
        brandsDAO.findAll();
        System.out.println("           :");
        String brandsNum1=scanner.next();
        if(isNumeric(brandsNum1)) {
     
            int brandsNum=Integer.parseInt(brandsNum1);
            List<Goods> temporlist = goodsDAO.searchByBranchId(brandsNum);
            if (temporlist == null) {
     
                System.out.println("        ");
                searchGoods();
            }
            for (int i = 0; i < temporlist.size(); i++) {
     
                System.out.println("  " + (i + 1) + "\t" + "    :" + temporlist.get(i).getGoodsName());
            }
            System.out.println("         ID   0       。");
            String choose1 = scanner.next();
            if(isNumeric(choose1)) {
     
                int choose=Integer.parseInt(choose1);
                if (choose == 0) {
     
                    searchGoods();
                } else {
     
                    if(choose>=1&&choose<=temporlist.size()){
     
                        Goods goods = temporlist.get(choose - 1);
                        System.out.println("    :" + goods.getGoodsName() + "\t" + "  :" + brandsDAO.findNameById(goods.getBrandsNum()) + "\t" + "  :" + goods.getGoodsPrice() + " \t" + "  :" + goods.getGoodsCategories());
                        searchGoods();
                    }
                    else{
     
                        System.out.println("          ,     ");
                        searchByBranch();
                    }
                }
            }
            else{
     
                System.out.println("      ,     ");
                searchByBranch();
            }
        }
        else{
     
            System.out.println("      ,     ");
            searchByBranch();
        }
    }
    //      
    public void searchByPrice(){
     
        System.out.println("           :          ");
        double minPrice=scanner.nextDouble();
        double maxPrice=scanner.nextDouble();
        List<Goods>temporlist=goodsDAO.searchByPrice(minPrice,maxPrice);
        if(temporlist==null){
     
            System.out.println("        ");
            searchGoods();
        }
        for(int i=0;i<temporlist.size();i++){
     
            System.out.println("  "+(i+1)+"\t"+"    :"+temporlist.get(i).getGoodsName());
        }
        System.out.println("         ID   0       。");
        String choose1 = scanner.next();
        if(isNumeric(choose1)) {
     
            int choose=Integer.parseInt(choose1);
            if (choose == 0) {
     
                searchGoods();
            }
            else {
     
                if(choose>=1&&choose<=temporlist.size()) {
     
                    Goods goods = temporlist.get(choose - 1);
                    System.out.println("    :" + goods.getGoodsName() + "\t" + "  :" + brandsDAO.findNameById(goods.getBrandsNum()) + "\t" + "  :" + goods.getGoodsPrice() + " \t" + "  :" + goods.getGoodsCategories());
                    searchGoods();
                }
                else{
     
                    System.out.println("        ,     ");
                    searchByPrice();
                }
            }
        }
        else{
     
            System.out.println("      ,     ");
            searchByPrice();
        }
    }
    //    
    public void addGoods(){
     
        System.out.println("       :");
        String goodsName=scanner.next();
        System.out.println("       :");
        double goodsPrice=scanner.nextDouble();
        System.out.println("     ");
        String brandsName=scanner.next();
        int brandsNum;
        if(brandsDAO.findBranch(brandsName)){
     
            brandsNum=brandsDAO.findByName(brandsName).getBrandsNum();
        }
        else{
     
            brandsNum=brandsDAO.brandsNumbers()+1;
            Brands brands=new Brands(brandsName,brandsNum);
            brandsDAO.addBranch(brands);
        }
        System.out.println("       ");
        String goodsCate=scanner.next();
        Goods goods=new Goods(goodsName,goodsPrice,0,goodsCate,brandsNum, UUID.randomUUID().toString());
        goodsDAO.addGoods(goods);
        System.out.println("      ");
        controlGoods();
    }
    //    
    public void changePrice(){
     
        System.out.println("--------    ------");
        brandsDAO.findAll();
        System.out.println("           :");
        String  brandsNum1=scanner.next();
        if(isNumeric(brandsNum1)) {
     
            int brandsNum=Integer.parseInt(brandsNum1);
            List<Goods> temporlist = goodsDAO.searchByBranchId(brandsNum);
            if (temporlist == null) {
     
                System.out.println("        ");
                changePrice();
            }
            for (int i = 0; i < temporlist.size(); i++) {
     
                System.out.println("  " + (i + 1) + "\t" + "    :" + temporlist.get(i).getGoodsName() + "\t  " + temporlist.get(i).getGoodsPrice() + " ");
            }
            System.out.println("           ID   0       。");
            String choose1 = scanner.next();
            if(isNumeric(choose1)) {
     
                int choose=Integer.parseInt(choose1);
                if (choose == 0) {
     
                    controlGoods();
                }
                else if(choose<=temporlist.size())
                {
     
                    System.out.println("         :");
                    double price = scanner.nextDouble();
                    System.out.println("        : / ");
                    String endChoose = scanner.next();
                    if (endChoose.equals(" ")) {
     
                        goodsDAO.modify(temporlist.get(choose - 1).getGoodsNum(), price);
                        System.out.println("      ");
                        controlGoods();
                    } else {
     
                        controlGoods();
                    }
                }
                else{
     
                    System.out.println("        ,     ");
                    changePrice();
                }
            }
            else{
     
                System.out.println("      ,     ");
                changePrice();
            }
        }
        else{
     
            System.out.println("      ,     ");
            changePrice();
        }
    }
    //    
    public void deleteGoods(){
     
        System.out.println("--------    ------");
        brandsDAO.findAll();
        System.out.println("           :");
        String  brandsNum2=scanner.next();
        if(isNumeric(brandsNum2)) {
     
            int brandsNum=Integer.parseInt(brandsNum2);
            List<Goods> temporlist = goodsDAO.searchByBranchId(brandsNum);
            if (temporlist == null) {
     
                System.out.println("        ");
                deleteGoods();
            }
            for (int i = 0; i < temporlist.size(); i++) {
     
                System.out.println("  " + (i + 1) + "\t" + "    :" + temporlist.get(i).getGoodsName());
            }
            System.out.println("         ID   0       。");
            String choose1 = scanner.next();
            if(isNumeric(choose1)) {
     
                int choose=Integer.parseInt(choose1);
                if (choose == 0) {
     
                    controlGoods();
                }
                else if(choose<=temporlist.size()){
     
                    System.out.println("      : / ");
                    String endChoose = scanner.next();
                    if (endChoose.equals(" ")) {
     
                        int brandsNum1 = temporlist.get(choose - 1).getBrandsNum();
                        goodsDAO.delete(temporlist.get(choose - 1).getGoodsNum());
                        System.out.println("    ");
                        if (goodsDAO.searchByBranchId(brandsNum1) == null) {
     
                            brandsDAO.deleteBrands(brandsNum1);
                            brandsDAO.moveBrandsNum(brandsNum1);
                            for (int i = brandsNum1; i <= brandsDAO.brandsNumbers(); i++)
                                goodsDAO.changeByBranchId(i + 1);
                        }
                        controlGoods();
                    } else {
     
                        controlGoods();
                    }
                }
                else{
     
                    System.out.println("        ,     ");
                    deleteGoods();
                }
            }
            else{
     
                System.out.println("      ,     ");
                deleteGoods();
            }
        }
        else{
     
            System.out.println("      ,     ");
            deleteGoods();
        }
    }
    public boolean isNumeric(String str){
     
        for (int i = str.length();--i>=0;){
     
            if (!Character.isDigit(str.charAt(i))){
     
                return false;
            }
        }
        return true;
    }
}


상기 코드 부분 내부 클래스 가 져 오기 미 작성 테스트 클래스 가 필요 합 니 다.

좋은 웹페이지 즐겨찾기