On the JAVA implementation of the shopping system __java

Source: Internet
Author: User
1. Shopping system requirements analysis and classification
The shopping system itself is a very complex system, there are a lot of details if the probe will be more complex, and the general shopping system is the type of Web page, to have a friendly interface, but as a simple project, the project is only to give Java beginners to introduce the basic idea of development, and object-oriented How to design the framework and implementation process, so just based on the eclipse development of a simple project, and no GUI participation, and a lot of details as a follow-up study, the overall design is relatively simple, but enough to explain a lot of design ideas and design concepts, Then face the basic requirements of the analysis.
As a simple shopping system, you need at least the following features (these functions are distributed in different levels of the menu): (1) User login function, user account password modification function, temporarily does not provide the registration function, (2) after the user successfully logged in, needs to have the customer information management function, the shopping clearing function as well as some lottery activities and so on (3) The Customer information management function can be divided into many functions below, for example: query, modify, increase, etc. (4) shopping clearing function can be divided into many functions, such as: commodity purchase, payment, bills, etc. (5) lottery activities can be designed under a variety of lottery forms, which further divides into Xu     Many new functional modules. (6) In the first level of the menu to provide the function of exiting the system, in the level two menu to provide logout login function, the other level menu to be able to return to the previous level menu.
These features are some of the more basic functions, then, if the process based on the idea of design, will be divided into a lot of functional modules, and then follow the process step by step, but now we adopt object-oriented thinking to design, then how to consider the design framework. The main idea of object-oriented is to abstract some requirements into many classes, then establish the relationships between these classes, through the collaboration between different classes, can achieve all the functions. So, the main task now is how to abstract these classes reasonably, and what functions these classes are going to implement, and what the relationships are between classes. The following is an analysis of this process through the structure of this design.
(1) Startsms class: For system startup. Our system definitely needs a startup class, this class contains the main method, which is used to start the system, this class is the topmost, so it can not involve too much low-level detail implementation, only need to implement some of the top-level basic flow on the line, mainly to call the underlying other classes of methods to implement the function.
(2) Data class: Used to store all of our data information, this design is mainly stored in a number of stores have been stored for the purchase of merchandise information and has registered membership information. Why do you need this class? Let's think about it, in the object-oriented design, our data is more, certainly can not be scattered everywhere definition, arbitrary modification, this will make the system is too low aggregation, prone to many errors, and difficult to carry out later function expansion and error modification, so we want to use some of the public data to classify, It is then placed in a class and provides a way to manipulate the data in that class.
(3) Menu class: Used to display and process menus at all levels. Since we designed a shopping system, then even simple, but also need a basic menu, used to interact with the user, because the menu is more, and all levels of the menu between the layers of links, so we need to the menu for unified management, so the emergence of the menu class. Note that the menu here is just some top-level menu display and basic function calls, the actual underlying algorithm or the need for more low-level classes to implement.
(4) Manager class: For storing user's account and password. Now that we need a user to log in, we definitely need a separate class to manage the user's account and password, making the system more independent. This design user only has one account and the password, only allows to modify the account and the password, but does not allow the registration.
(5) Verifyequal class: for verifying login information. This class is equivalent to the login to the function of abstraction into a class, this implementation is not very necessary, but in order to make the system functional division more clearly, so designed this class, to the login information and existing accounts and passwords to verify, so as to give the results of the verification.
(6) Custmanagement class: For customer information management, this class to achieve some of the underlying functions, such as query, modify, increase and so on. When we get into the customer information Management menu, there is certainly a lot to do with customer information, to facilitate the management of these operations, and to take into account the subsequent extensibility, where all the features of customer information management are abstracted, placed in this class, the previous menu by invoking the methods in the class to implement customer information management.
(7) Pay class: For processing shopping and settlement operations. The class and the class above are basically the same principle, when customers choose to do shopping, there must be a lot of operations, such as what to buy, how much money, payment, change, etc., these features are relatively fragmented, so we have centralized management, thus abstracting the class, shopping and checkout of the bottom of the menu options to implement the algorithm, The previous level menu implements the shopping and billing functionality by invoking the class's methods, and can return to the previous level menu.
(8) Giftmanagement class: Used to deal with the lottery activities related functions. This class and (6), (7) The existence of the class is basically consistent, this class of lottery activities are unified management, the previous level menu only by calling the method of this class can achieve the lottery function.
(9) Gift class: for managing gifts. Since the design of the lottery link, then definitely need a gift, so what kind of gift do we give, we can't always list every gift in detail, this is very redundant, also very troublesome, so we simply abstract a gift class, the gift of some attributes: Gift name and price, etc. to save into the class member variable, Then you can easily manage the class, what kind of gift you need to create a new object directly, and then modify and manage the properties of the object, such an implementation is similar to an interface, but and the interface is completely different, the function is similar.
In short, the above classes are separated by a number of functional modules after the abstraction, and some places are not necessarily reasonable, the main still need to see demand, according to different needs to develop different programs. Over here, I would like to emphasize the "gift class" again, the design of this class is very consistent with object-oriented thinking, for example, if the shopping system requires a lot of gifts, such as mobile phones, computers, mobile power and so on, then if we write these gifts, will make the system code very redundant, Because the properties of the gift are basically the same, so we can abstract into a class, so that when we need a gift to define only one object, and then give certain attributes, such as the need for mobile phones, computers, then we just need to new a gift class object, and then when the phone needs to set its properties for the phone, When you need a computer to set its properties for the computer, what you need to set what can be, so that our code has been simplified, but also make the structure more clear. In a more complex system, in fact, the gift with the interface to achieve more reasonable, so that the interface can be implemented according to different gift classes to meet different needs, similar to our computer USB interface, only need this interface, we can plug in a lot of various peripherals, the truth is similar.
2. Relationships and processes between classes in the shopping system (shown in graphical notation)
The following figure is the relationship between these 9 classes I drew with the Microsoft Office Visio 2003 drawing tools.

The relationship between the various categories can be clearly seen from the diagram above, and the approximate relationship and process are as follows:
(1) The Startsms class is the startup class, which contains the main method, which defines the object of the Verifyequal class and the data class to store the data and verify the information, and the data class includes the manager class to store the stored user account information. Then in the main method through a certain logic, to invoke the Showloginmenu () method in the menu class, for processing the first level menu---login modification process;
(2) If the login is successful, call the Showmainmenu () method in the menu class to handle the mainstream of the menu---shopping system of level two, if the login fails 3 times, it exits the system directly;
(3) in the Showmainmenu () method in the menu class, invoke the Showcustmmenu () method in the menu class to process the customer information management process or invoke the Showsendmenu () in the menu class by selecting a different Level two menu option. Methods to process the lottery, or call the CalcPrice () method in the pay class to process the shopping settlement process;
(4) If the Customer Information management option in the level two menu is selected, then the Showcustmmenu () method in the menu class is invoked, which invokes various methods in the Custmanagement class to handle the different operations of customer information management;
(5) If you choose the Shopping settlement option in Level two menu, then you will call the CalcPrice () method in the pay class to handle the shopping settlement process, and note that the Getdiscount () method in the Pay class is used to calculate the discount rate based on customer member information;
(6) If you choose the level two menu of the true feedback option, that is, lottery activities, then call the Showsendmenu () method in the menu class, this method will call various methods in the Giftmanagement class to handle the different operation of the lottery;
Note that there is a Returnlastmenu () method in both the Custmanagement class and the Giftmanagement class, which is used to return to the previous level menu.
3. Code implementation
The point to note is that the code should be placed under the Cn.itcast package.
3.1 Startsms class
[Java] View plain copy package cn.itcast;      import java.util.scanner;      /**   *  This class is the Main method class for this system, used to start the shopping system    *    *  @author     *    */   public class startsms {           /**       *  Empty construction method        *         */       public startsms ()  {        }          /**        *  System Main method        *        *   @param  args       */       public  Static void main (string args[])  {          & NBsp;           //  Create objects of existing data classes and initialize existing product and customer information            data data = new data ();            data.initial ();                       //  Creating object for menu class             menu menu = new menu ();                       //  Here, the initialized data information is given to the menu object            menu.setdata (                   data.goodsname ,                     data.goodsprice,                     data.custNo,                    data.custBirth,                     data.custscore);                       //  Display level menu, login interface             menu.showloginmenu ();                       //  This flag is used to determine if a system operation error has occurred. When the improper operation of the flag is false, thus exiting the system, the default is error-free            boolean flag  = true;                       //  processes the entire system            do {                //  operation error, exit system                 if  (!flag)                     break;                                //  create an object that verifies that the user's login account and password are correct and that only objects are created here, and no validation methods are executed                 VerifyEqual verifyequal = new  Verifyequal ();                              //  input One-level menu selection                 scanner scanner = new scanner (System.in);                int i =  Scanner.nextint ();                               //  According to the user's choice of the first level menu to make different responses, Note that this is the classic switch-case usage                 switch  (i)  {                    case 1: //  User Select "Login System"                                                 //   Define counters, indicating that users can only try 3 times, 3 input Error Direct exit system                         int j = 3;                                                //   Process the login system                         do {                            if  (Verifyequal.verify (data.manager.username,                                                     Data.manager.password))  {                                //  User login successful, show shopping level two menu ...                                 menu.showmainmenu ();                                 break;                            }                                                         if  (j != 1)  {                                //  user input error not reached 3 times, allow re-enter                                 system.out.println ("\ n username and password do not match, please re-enter: ");                            } else {                              &nBsp;  // 3 try to finish, set exit flag, and exit Do-while cycle                                  system.out.println ("\ n) you do not have permission to enter the system. Thank you. ");                                flag = false;                                 break;                             }                                                        //  reduce the counter by 1 per input to indicate how many times you have tried                             j--;                         } while  (true);                        break;                            case 2: //  User Select "Change administrator Information"                                                 if  (Verifyequal.verify ( data.manager.username,                                                data.manager.password))  {                             //  verify the original information before entering the new information, which indicates that the success has been validated                              system.out.print ("Please enter a new user name:");                             Data.manager.username = scanneR.next ();                            system.out.print ("Please enter a new password:");                             data.manager.password = scanner.next ();                             system.out.println ("User name and password changed.") ");                                                        //  Information changed successfully, choose Next action                             system.out.println ("\ n Please select , enter the number: ");                        } else {                            //  Information validation failed, setting exit flag                             system.out.println ("Sorry, you don't have permission to change.") ");                            flag = false;                        }   

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.