The examples in this article share the Java shopping system design and implementation code for your reference. The specific content is as follows
1. Requirements analysis and classification of shopping system
The shopping system itself is a very complex system, with many details and problems that will be more complicated if you study them deeply. In addition, the general shopping system is web page type and needs to have a friendly interface. However, as a simple project, this project is just to introduce the basic ideas of development to beginners of JAVA and how to design frameworks and implementation processes when object-oriented. Therefore, it is just a simple project developed based on eclipse, without the participation of the GUI, and many details and issues are used as follow-up research. The overall design is relatively simple, but it is enough to explain many design ideas and design concepts. So the basic needs are analyzed below.
As a simple shopping system, you need to have at least the following functions (these functions are distributed in different levels of menus):
(1) User login function and user account password modification function, registration function is not available for the time being;
(2) After the user successfully logs in, he needs to have the customer information management function, shopping settlement function, and some lottery activities;
(3) There are many functions that can be divided into below the customer information management function, such as: query, modification, addition, etc.;
(4) There are many functions that can be divided into below the shopping settlement function, such as: product selection, payment, billing, etc.;
(5) Various lottery forms can be designed under the lottery, which can be further divided into many new functional modules.
(6) The function of exiting the system should be provided in the first-level menu, the function of logging out of the second-level menu should be provided in the second-level menu, and other menus should be able to return to the previous menu.
The above functions are all relatively basic functions. If you design according to the process-oriented idea, you will divide many functional modules and then follow the process step by step. But now we use the object-oriented idea to design, how should we consider the design framework? The main idea of object-oriented is to abstract some requirements into many classes, and then establish connections between these classes. Through collaborative cooperation between different classes, all functions can be achieved. Therefore, the main task now is how to reasonably abstract these classes, what functions are these classes going to implement, and what are the connections between classes? The following is an analysis of this process based on the structure of this design.
(1) StartSMS class: used for system startup. Our system definitely needs a startup class. This class contains the main method to start the system. This class is at the top level, so it cannot involve too many underlying details. It only needs to implement some top-level basic processes. The main thing is to call some methods of other underlying classes to implement functions.
(2) Data class: used to store all our data information. This design mainly stores some pre-stored product information for purchase and registered member information. Why do you need this class? Think about it, in object-oriented design, we have a lot of data, and we must not define and modify it scatteredly everywhere. This will make the system aggregation too low, prone to many errors, and it is difficult to perform later function expansion and error modification. Therefore, we need to classify some of the public data used, then put it in a class, and provide methods to operate this data in this class.
(3) Menu class: used to display and process menus at all levels. Since we are designing a shopping system, even if it is simple, we need a basic menu to interact with users. Since there are many menus and the menus at each level are connected layer by layer, we need to manage the menus uniformly, so the menu category appears. Note that the menu here is just some top-level menu displays and basic function calls. The specific underlying algorithm still needs to be implemented by more underlying classes.
(4) Manager class: used to store user's account and password. Since we need users to log in, we must definitely need a separate class to manage the user's account and password, so that the system is more independent. The user designed this time has only one account and password, and only changes are allowed to be made to the account and password, but registration is not allowed.
(5) VerifyEqual class: used to verify login information. This class is equivalent to abstracting the login function into a class. This implementation is actually not very necessary, but in order to make the system function division clearer, this class is designed to verify the login information and existing accounts and passwords, thereby giving the verification result.
(6) CustManagement class: used for the management of customer information. This class implements some underlying functions, such as query, modification, addition, etc. When we enter the customer information management menu, we must do a lot of operations on the customer information. In order to facilitate management of these operations and take into account the subsequent expansion, all functions of customer information management are abstracted here and placed in this category. The previous menu realizes the management of customer information by calling the methods in this class.
(7) Pay class: used to handle shopping and checkout operations. The principle of this class is basically the same as the above class. When a customer chooses to shop, there must be many operations, such as what to buy, how much money, payment, change, etc. These functions are relatively fragmented, so we centrally manage them, thereby abstracting this class and implementing the underlying algorithm for menu options for shopping and settlement. The previous menu realizes the shopping and settlement functions by calling the method of this class, and can return to the previous menu.
(8) GiftManagement class: related functions used to handle lottery activities. The reasons for the existence of this class are basically the same as those in (6) and (7). This class uniformly manages the lottery activities. The previous menu only needs to call the method of this class to realize the lottery function.
(9) Gift class: used to manage gifts. Since the lottery session is designed, gifts are definitely needed. So what kind of gifts will we give? We can't list each gift in detail. This is very redundant and troublesome, so we simply abstract a gift class and save some attributes of the gift: the name and price of the gift into member variables of the class. Then we can easily manage the class. If the gift is needed, create a new gift object directly, and then modify and manage the attributes of the object. This implementation is similar to an interface, but it is completely different from the interface and has similar functions.
In short, the above classes are abstracted after being divided into some functional modules, and some places are not necessarily reasonable. They mainly need to look at the requirements and formulate different plans according to different needs. Here, I want to emphasize "Gift class" again. The design of this class is very consistent with the object-oriented idea. For example, if a lot of gifts are needed in the shopping system, such as mobile phones, computers, mobile power supplies, etc., then if we write these gifts one by one, the system code will be very redundant. Because the attributes of gifts are basically the same, we can abstract them into a class, so that we can only define one object when we need a gift, and then give certain attributes. For example, if we need a mobile phone or computer, then we only need to new a Gift class object, and then set its attributes to a mobile phone when we need a mobile phone, set its attributes to a computer when we need a computer, and set whatever we need. This makes our code simplified and the structure clearer. In more complex systems, it is actually more reasonable to use an interface to implement gifts, so that different gift categories can be implemented according to the interface, thereby meeting different needs. It is similar to the USB interface on our computer. With this interface, we can plug in many various peripheral devices, and the truth is similar.
2. The relationship and process between classes in the shopping system (denoted in graphic form)
The following figure shows the relationship between these 9 classes I used the Microsoft Office Visio 2003 drawing tool.
From the above figure, we can clearly see the relationship between various types. The general relationship and process are as follows:
(1) The StartSMS class is a startup class, which contains the main method. This class defines objects of the VerifyEqual class and Data class to store data and verification information. At the same time, the Data class contains the Manager class to store pre-stored user account information. Then, through certain logic in the main method, the showLoginMenu() method in the Menu class is called to process the first-level menu--login modification process;
(2) If the login is successful, call the showMainMenu() method in the Menu class to handle the main process of the second-level menu--the shopping system. If the login fails 3 times, you will directly exit the system;
(3) In the showMainMenu() method in the Menu class, by selecting different secondary menu options, the showCustMMenu() method in the Menu class is called to handle the customer information management process or to call the showSendMenu() method in the Menu class to handle the lottery process, or to call the calcPrice() method in the Pay class to handle the shopping settlement process;
(4) If the customer information management option in the secondary menu is selected, the showCustMMenu() method in the Menu class will be called. This method will call various methods in the CustManagement class to handle different operations in customer information management;
(5) If the shopping settlement option in the secondary menu is selected, the calcPrice() method in the Pay class will be called to handle the shopping settlement process. Note that the getDiscount() method in the Pay class is used to calculate the discount rate based on customer member information;
(6) If the sincere feedback option in the secondary menu is selected, i.e. the lottery, then the showSendMenu() method in the Menu class will be called. This method will call various methods in the GiftManagement class to handle different operations of the lottery;
Note that there is a returnLastMenu() method in the CustManagement class and the GiftManagement class, which is used to return to the previous menu.
3. Code implementation
One thing to note is that these codes should be placed in the cn.itcast package.
3.1 StartSMS class
package cn.itcast; import java.util.Scanner; /** * This class is the main method class of this system, used to start the shopping system* * @author * */ public class StartSMS { /** * Empty constructor* */ public StartSMS() { } /** * System main method* * @param args */ public static void main(String args[]) { // Create an object of the existing data class and initialize the existing product information and customer information Data data = new Data(); data.initial(); // Create an object of the menu class Menu menu = new Menu(); // Here the initialized existing data information is sent to the menu object menu.setData( data.goodsName, data.goodsPrice, data.custNo, data.custBirth, data.custScore); // Display the first-level menu, that is, the login interface menu.showLoginMenu(); // This flag is used to determine whether a system operation error has occurred. When the operation is incorrect, the flag is false, so as to exit the system. The default is no error boolean flag = true; // Processing the entire system process do { // An operation error occurs, exit the system if (!flag) break; // Create an object that verifies whether the user logged in the account and password is correct. Only objects are created here, and the verification method VerifyEqual is not executed. verifyequal = new VerifyEqual(); // Enter the selection in the first-level menu Scanner scanner = new Scanner(System.in); int i = scanner.nextInt(); // Make different responses to the selection of the first-level menu. Note that this is the classic switch-case usage switch (i) { case 1: // User selects "Login System" // Define a counter, which means that the user can only try 3 times at most, and directly exit the system with 3 input errors int j = 3; // Processing process for logging into the system do { if (verifyequal.verify(data.manager.username, data.manager.password)) { // The user logged in successfully and the second-level shopping menu is displayed! ! ! menu.showMainMenu(); break; } if (j != 1) { // The user input is incorrect and has not reached 3 times. Re-enter System.out.println("/nThe user name and password do not match, please re-enter: "); } else { // The 3 attempts end, set the exit flag, and exit the do-while loop System.out.println("/nYou do not have permission to enter the system! Thank you!"); flag = false; break; } // Decrement the counter by 1 every time you enter to indicate how many times j--; } while (true); break; case 2: // The user selects "Change administrator information" if (verifyequal.verify(data.manager.username, data.manager.password)) { // Before entering new information, you must verify the original information. This means that the verification has been successful. System.out.print("Please enter the new user name: "); data.manager.username = scanner.next(); System.out.print("Please enter the new password: "); data.manager.password = scanner.next(); System.out.println("Username and password have been changed!"); // The information has been changed successfully. Select the next operation System.out.println("/nPlease select, enter the number: "); } else { // Information verification failed, set the exit flag System.out.println("Sorry, you don't have permission to modify it!"); flag = false; } break; case 3: // The user selects "Exit" System.out.println("Thank you for your use!"); System.exit(0); break; default: // The first-level menu input is incorrect, and you need to re-select System.out.print("/n input is incorrect! Please select again and enter the number: "); break; } } while (flag); } }3.2 Data class
package cn.itcast; /** * The data class that stores the initialization data of the shopping system. This class only stores the existing product information and customer information* * @author * */ public class Data { /** * The default constructor, initializing variables. Since they are all array objects or class objects, they all need to use new * */ public Data() { goodsName = new String[50] ; goodsPrice = new double[50] ; custNo = new int[100] ; custBirth = new String[100]; custScore = new int[100] ; manager = new Manager() ; } /** * Initialize the data of this class* */ public void initial() { /*=====================================================================================================================/ goodsName [0] = "addidas sneakers"; goodsPrice[0] = 880D; goodsName [1] = "Kappa tennis skirt"; goodsPrice[1] = 200D; goodsName [2] = "Top racket"; goodsPrice[2] = 780D; goodsName [3] = "addidas T-shirt"; goodsPrice[3] = 420.7799999999999997D; goodsName [4] = "Nike sneakers"; goodsPrice[4] = 900D; goodsName [5] = "Kappa tennis"; goodsPrice[5] = 45D; goodsName [6] = "KappaT-shirt"; goodsPrice[6] = 245D; /*========================================================================================================================================================================================================================================================================================================================================================================================================= custNo [1] = 1711; custBirth[1] = "07/13"; custScore[1] = 4000; custNo [2] = 1623; custBirth[2] = "06/26"; custScore[2] = 5000; custNo [3] = 1545; custBirth[3] = "04/08"; custScore[3] = 2200; custNo [4] = 1464; custBirth[4] = "08/16"; custScore[4] = 1000; custNo [5] = 1372; custBirth[5] = "12/23"; custScore[5] = 3000; custNo [6] = 1286; custBirth[6] = "12/21"; custScore[6] = 10080; } /*========================================================================================================================================================================================================================================================================================================================================================================================================================================== Customer's birthday public int custScore []; // Customer's points public Manager manager; // Administrator class, only the administrator's username and password}3.3 Manager class
package cn.itcast; /** * Administrator class, only stores the administrator's username and password* * @author * */ public class Manager { /** * Set the default username and password* */ public Manager() { username = "itcast"; password = "itcast"; } /*================================== Define the variables owned by this class====================================================================================================================================================================================================================================================================================================================================================================================================3.4 VerifyEqual class
package cn.itcast; import java.util.Scanner; /** * Class that verifies whether the user logged in is correct* * @author * */ public class VerifyEqual { /** * Empty constructor* */ public VerifyEqual() { } /** * Method to perform verification* * @param s The correct username for verification* @param s The correct password for verification* @return */ public boolean verify(String s, String s1) { // Enter the username System.out.print("Please enter the username:"); Scanner scanner = new Scanner(System.in); String s2 = scanner.next(); // Enter the password from the user System.out.print("Please enter the password:"); scanner = new Scanner(System.in); String s3 = scanner.next(); // Determine whether the information entered by the user is consistent with the existing information return s2.equals(s) && s1.equals(s3); } }3.5 Menu Class
package cn.itcast; import java.util.Scanner; /** * Menu class, used to display all levels of menus for user selection* * @author * */ public class Menu { /** * Empty constructor* */ public Menu() { } /** * Set data information in menu class* * @param as * @param ad * @param ai * @param ai * @param ai * @param ai * @param ai1 */ public void setData(String as[], double ad[], int ai[], String as1[], int ai1[]) { goodsName = as; goodsPrice = ad; custNo = ai; custBirth = as1; custScore = ai1; } /** * Show the first-level menu, that is, the login interface* */ public void showLoginMenu() { System.out.println("/n/n/t/t/t/t Welcome to itcast Shopping Management System Version 1.0/n/n"); System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * System.out.println("/t/t/t/t 1. Log in to the system/n/n"); System.out.println("/t/t/t/t 2. Change administrator information/n/n"); System.out.println("/t/t/t/t/t 3. Exit/n/n"); System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /** * Display the secondary menu, that is, the main menu of the system. This method contains all the processes for processing this menu* */ public void showMainMenu() { // Display the secondary menu, that is, the main menu of the system System.out.println("/n/n/t/t/t/t/t/t/t/t Welcome to the Shopping Management System/n"); System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Customer Information Management/n"); System.out.println("/t/t/t/t 2. Shopping Settlement/n"); System.out.println("/t/t/t/t 3. True Reply/n"); System.out.println("/t/t/t/t 4. Logout/n"); System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * User selects the service item System.out.print("Please select, enter the number:"); Scanner scanner = new Scanner(System.in); // Set the flag to control the loop boolean flag = false; do { String s = scanner.next(); // User selects "Customer Information Management" if (s.equals("1")) { // Displays the customer information management menu and processes the entire process of this menu. When this process is processed, showCustMMenu(); break; } // User selects "Shopping Settlement" if (s.equals("2")) { // Defines the object of the shopping settlement class and handles the entire shopping settlement process Pay pay = new Pay(); pay.setData(goodsName, goodsPrice, custNo, custBirth, custScore); pay.calcPrice(); break; } // User selects "True Feedback" if (s.equals("3")) { // The entire processing flow of handling true Feedback showsSendGMenu(); break; } // User selects "Login" if (s.equals("4")) { // Displays the first-level menu, and it will return to the first-level menu processing flow in StartSMS class showLoginMenu(); break; } System.out.print("Input error, please re-enter the number: "); flag = false; } while (!flag); } /** * Show the three-level menu - customer information management and handle the process of all customer information management* */ public void showCustMMenu() { System.out.println("Shopping Management System> Customer Information Management/n"); System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * System.out.println("/t/t/t/t 2. Add customer information/n"); System.out.println("/t/t/t/t 3. Modify customer information/n"); System.out.println("/t/t/t/t 4. Query customer information/n"); System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * System.out.print("Please select, enter a number or press 'n' to return to the previous menu:"); Scanner scanner = new Scanner(System.in); boolean flag = true; do { // Create a customer information management object and set the data, the data here is still the original data. CustManagement custmanagement = new CustManagement(); custmanagement.setData(goodsName, goodsPrice, custNo, custBirth, custScore); String s = scanner.next(); // Customer selects "Show all customer information" if (s.equals("1")) { custmanagement.show(); break; } // Customer select "Add customer information" if (s.equals("2")) { custmanagement.add(); break; } // Customer select "Modify customer information" if (s.equals("3")) { custmanagement.modify(); break; } // Customer select "Query customer information" if (s.equals("4")) { custmanagement.search(); break; } // Customer select "Return to previous menu" if (s.equals("n")) { showMainMenu(); break; } System.out.println("Input error, Please re-enter the number: "); flag = false; } while (!flag); } /** * Show the third-level menu - sincere feedback and handle all sincere feedback processes* */ public void showSendGMenu() { System.out.println("Shopping Management System> true feedback/n"); System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Lucky Broadcast/n"); System.out.println("/t/t/t/t 2. Lucky Lottery/n"); System.out.println("/t/t/t/t/t 3. Birthday greeting/n"); System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Scanner(System.in); // Create a gift management object and set the data. The data here is still the original data. GiftManagement giftmanagement = new GiftManagement(); giftmanagement.setData(goodsName, goodsPrice, custNo, custBirth, custScore); boolean flag = true; do { String s = scanner.next(); // The customer selects "Lucky Broadcast" if (s.equals("1")) { giftmanagement.sendGoldenCust(); break; } // The customer selects "Lucky Braw" if (s.equals("2")) { giftmanagement.sendLuckyCust(); break; } // The client selects "Birthday greeting" if (s.equals("3")) { giftmanagement.sendBirthCust(); break; } // The client selects "Return to previous menu" if (s.equals("n")) { showMainMenu(); break; } System.out.println("Input error, please re-enter the number:"); flag = false; } while (!flag); } /*=====================================================================================*/ public String goodsName []; // product name public double goodsPrice[]; // product price public int custNo []; // customer membership number public String custBirth []; // customer's birthday public int custScore []; // customer's points}3.6 CustManagement Class
package cn.itcast; import java.util.Scanner; /** * Customer information management class* * @author * */ public class CustManagement { /** * Empty constructor* */ public CustManagement() { } /** * Set data information of customer information management class* * @param as * @param ad * @param ai * @param ai * @param as1 * @param ai1 */ public void setData(String as[], double ad[], int ai[], String as1[], int ai1[]) { goodsName = as; goodsPrice = ad; custNo = ai; custBirth = as1; custScore = ai1; } /** * Return to the previous menu, i.e. the second-level menu-customer information management menu* */ public void returnLastMenu() { System.out.print("/n/nPlease press 'n' to return to the previous menu:"); Scanner scanner = new Scanner(System.in); boolean flag = true; do if (scanner.next().equals("n")) { // Return to the previous menu, a new menu object was created here, but in the user's opinion, the same processing flow is actually the same, // However, for the program, a new secondary menu processing process has begun. Menu menu = new Menu(); menu.setData(goodsName, goodsPrice, custNo, custBirth, custScore); menu.showCustMMenu(); } else { System.out.print("Input error, please 'n' return to the previous menu: "); flag = false; } while (!flag); } /** * Add customer information* */ public void add() { System.out.println("Shopping Management System> Customer Information Management> Add customer information/n/n"); Scanner scanner = new Scanner(System.in); System.out.print("Please enter the member number (<4-digit integer>):"); int i = scanner.nextInt(); System.out.print("Please enter the member's birthday (month/day <denoted in two digits>):"); String s = scanner.next(); System.out.print("Please enter the integral:"); int j = scanner.nextInt(); int k = -1; int l = 0; do { if (l >= custNo.length) break; // Find the first empty location in the array to store new customer information if (custNo[l] == 0) { k = l; break; } l++; } while (true); custNo [k] = i; custBirth[k] = s; custScore[k] = j; System.out.println("New member added successfully!"); // Return to the previous menu returnLastMenu(); } /** * Modify customer information* */ public void modify() { System.out.println("Shopping Management System> Customer Information Management> Modify customer information/n/n"); System.out.print("Please enter the member number:"); Scanner scanner = new Scanner(System.in); int i = scanner.nextInt(); System.out.println(" member number birthday points"); System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- .append(custScore[k]).toString()); j = k; break; } k++; } while (true); // The member exists, then modify the information process if (j != -1) { System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /n"); System.out.println("/t/t/t1. Modify the member's birthday./n"); System.out.println("/t/t/t/t2.Modify member points./n"); System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /n"); System.out.print("Please select, enter the number: "); switch (scanner.nextInt()) { case 1: // "Modify member's birthday" System.out.print("Please enter the modified birthday: "); custBirth[j] = scanner.next(); System.out.println("Birthday information has been changed!"); break; case 2: // "Modify member points" System.out.print("Please enter the modified member points: "); custScore[j] = scanner.nextInt(); System.out.println("Member points have been changed!"); break; } } else { System.out.println("Sorry, there is no member you query."); } // Return to the previous menu returnLastMenu(); } /** * Query customer information* */ public void search() { System.out.println("Shopping Management System> Customer Information Management> Query Customer Information/n"); Scanner scanner = new Scanner(System.in); for (String s = "y"; s.equals("y"); s = scanner.next()) { System.out.print("Please enter the membership number: "); int i = scanner.nextInt(); System.out.println("Member number birthday points"); System.out.println("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 0; do { if (j >= custNo.length) break; // Show the information of the member if (custNo[j] == i) { System.out.println((new StringBuilder()).append(custNo[j]) .append("/t/t").append(custBirth[j]).append("/t/t") .append(custScore[j]).toString()); flag = true; break; } j++; } while (true); if (!flag) System.out.println("Sorry, there is no member information you query."); System.out.print("/nDo you want to continue querying (y/n):"); } // Return to the previous menu returnLastMenu(); } /** * Show all customer information* */ public void show() { System.out.println("Shopping Management System> Customer Information Management> Show customer information/n/n"); System.out.println("Member number birthday points"); System.out.println("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- System.out.println((new StringBuilder()).append(custNo[j]).append( "/t/t").append(custBirth[j]).append("/t/t").append(custScore[j]).toString()); // Return to the previous menu returnLastMenu(); } /*================================================================================================================================================================================================================================================================================================================================================================================================================================== custNo []; // Customer’s membership number public String custBirth []; // Customer’s birthday public int custScore []; // Customer’s points}3.7 Pay Class
package cn.itcast; import java.util.Scanner; /** * Class that handles customers' purchases and settlements* * @author * */ public class Pay { /** * Empty constructor* */ public Pay() { } /** * Set data information of shopping settlement class* * @param as * @param ad * @param ai * @param ai * @param as1 * @param ai1 */ public void setData(String as[], double ad[], int ai[], String as1[], int ai1[]) { goodsName = as; goodsPrice = ad; custNo = ai; custBirth = as1; custScore = ai1; } /** * Determine the discount rate based on member information* * @param i * @param ai * @param ai * @param ai1 * @return */ public double getDiscount(int i, int ai[], int ai1[]) { int j = -1; int k = 0; do { if (k >= ai.length) break; if (i == ai[k]) { j = k; break; } k++; } while (true); double d; if (ai1[j] < 1000) d = 0.94999999999999999999999996D; else if (1000 <= ai1[j] && ai1[j] < 2000) d = 0.900000000000000000000002D; else if (2000 <= ai1[j] && ai1[j] < 3000) d = 0.849999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999996D; else if (1000 <= ai1[j] && ai1[j] < 4000) d = 0.8000000000000000000004D; else if (4000 <= ai1[j] && ai1[j] < 6000) d = 0.75D; else if (6000 <= ai1[j] && ai1[j] < 8000) d = 0.69999999999999999999996D; else d = 0.5999999999999999998D; return d; } /** * Key methods of this class, used to handle shopping and checkout* */ public void calcPrice() { String s2 = ""; double d1 = 0.0D; double d2 = 0.0D; int l = 0; double d4 = 0; System.out.println("Shopping Management System> Shopping Checkout/n/n"); System.out.println("************************************************"); System.out.println("Please select the purchased product number: "); // Show all purchaseable product information, the information here is the initial initial product data for (l = 0; l < goodsName.length && goodsName[l] != null; l++) { d4++; System.out.println((new StringBuilder()).append(d4).append(": ") .append(goodsName[l]).append("/t").toString()); } System.out.println("*********************************************/n"); Scanner scanner = new Scanner(System.in); System.out.print("/tPlease enter the member number: "); int i = scanner.nextInt(); // Get discount information based on member information d4 = getDiscount(i, custNo, custScore); String s1; do { System.out.print("/tPlease enter the product number: "); int j = scanner.nextInt(); System.out.print("/tPlease enter the number: "); int k = scanner.nextInt(); double d = goodsPrice[j - 1]; String s = goodsName[j - 1]; d1 += d * (double) k; s2 = (new StringBuilder()).append(s2).append("/n").append(s) .append("/t").append("¥").append(d).append("/t/t") .append(k).append("/t/t").append("¥") .append(d * (double) k).append("/t").toString(); System.out.print("/t continue (y/n)"); s1 = scanner.next(); } while (s1.equals("y")); d2 = d1 * d4; // Total price after discount System.out.println("/n"); System.out.println("***************************************************************************************************************************** System.out.println("Item/t/t unit price/t/t number/t/t amount/t amount/t"); System.out.print(s2); System.out.println((new StringBuilder()).append("/n discount: /t").append(d4).toString()); System.out.println((new StringBuilder()).append("Total amount:/t¥").append(d2).toString()); System.out.print("Actual payment:/t¥"); double d3 = scanner.nextDouble(); System.out.println((new StringBuilder()).append("Find money:/t¥").append(d3 - d2).toString()); int i1 = ((int) d2 / 100) * 3; int j1 = 0; do { if (j1 >= custNo.length) break; if (custNo[j1] == i) { custScore[j1] = custScore[j1] + i1; System.out.println((new StringBuilder()).append("The points obtained in this shopping are: ") .append(i1).toString()); break; } j1++; } while (true); System.out.print("/nPlease 'n' to return to the previous menu:"); if (scanner.next().equals("n")) { // Return to the previous menu, a new menu object was created here, but in the user's opinion, the same processing flow was actually still the same, // However, for the program, a new secondary menu processing flow has started. Menu menu = new Menu(); menu.setData(goodsName, goodsPrice, custNo, custBirth, custScore); menu.showMainMenu(); } } /*====================定义该类所拥有的变量====================*/ public String goodsName []; // 商品的名称public double goodsPrice[]; // 商品的价格public int custNo []; // 顾客的会员号public String custBirth []; // 顾客的生日public int custScore []; // 顾客的积分}3.8 GiftManagement类
package cn.itcast; import java.util.Scanner; /** * 礼物管理类* * @author * */ public class GiftManagement { /** * 空构造方法* */ public GiftManagement() { } /** * 设置礼物管理类的数据信息* * @param as * @param ad * @param ai * @param as1 * @param ai1 */ public void setData(String as[], double ad[], int ai[], String as1[], int ai1[]) { goodsName = as; goodsPrice = ad; custNo = ai; custBirth = as1; custScore = ai1; } /** * 返回上一级菜单,即二级菜单-真情回馈菜单* */ public void returnLastMenu() { System.out.print("/n/n请按'n'返回上一级菜单:"); Scanner scanner = new Scanner(System.in); boolean flag = true; do if (scanner.next().equals("n")) { // 返回上一级菜单,这里新建了一个菜单对象,只是在用户看来其实还是同样的处理流程, // 不过对于程序来说却又开始了一个新的二级菜单处理流程Menu menu = new Menu(); menu.setData(goodsName, goodsPrice, custNo, custBirth, custScore); menu.showCustMMenu(); } else { System.out.print("输入错误, 请重新'n'返回上一级菜单:"); flag = false; } while (!flag); } /** * 生日问候* */ public void sendBirthCust() { System.out.println("购物管理系统> 生日问候/n/n"); System.out.print("请输入今天的日期(月/日<用两位表示>):"); Scanner scanner = new Scanner(System.in); String s = scanner.next(); System.out.println(s); String s1 = ""; boolean flag = false; for (int i = 0; i < custBirth.length; i++) if (custBirth[i] != null && custBirth[i].equals(s)) { s1 = (new StringBuilder()).append(s1).append(custNo[i]).append( "/n").toString(); flag = true; } // 这里的礼物是固定的,所以没有用礼物类if (flag) { System.out.println("过生日的会员是:"); System.out.println(s1); System.out.println("恭喜!获赠MP3一个!"); } else { System.out.println("今天没有过生日的会员!"); } // 返回上一级菜单returnLastMenu(); } /** * 幸运抽奖,注意这里是随机的抽奖,所以需要随机数,只需要自己制定一个抽奖规则就可以* */ public void sendLuckyCust() { System.out.println("购物管理系统> 幸运抽奖/n/n"); System.out.print("是否开始(y/n):"); Scanner scanner = new Scanner(System.in); if (scanner.next().equals("y")) { int i = (int) (Math.random() * 10D); // 产生一个随机数String s = ""; boolean flag = false; for (int k = 0; k < custNo.length && custNo[k] != 0; k++) { // 拿随机数与顾客会员号的相应结果进行比较,从而判断是否有顾客中奖int j = (custNo[k] / 100) % 10; if (j == i) { s = (new StringBuilder()).append(s).append(custNo[k]) .append("/t").toString(); flag = true; } } // 固定的奖品,所以不需要礼物类if (flag) System.out.println((new StringBuilder()).append("幸运客户获赠MP3:") .append(s).toString()); else System.out.println("无幸运客户。"); } // 返回上一级菜单returnLastMenu(); } /** * 幸运大放送,取积分最高的会员作为幸运者,送其奖品* */ public void sendGoldenCust() { System.out.println("购物管理系统> 幸运大放送/n/n"); int i = 0; int j = custScore[0]; for (int k = 0; k < custScore.length && custScore[k] != 0; k++) { // 找到积分最高的会员if (custScore[k] > j) { j = custScore[k]; i = k; } } System.out.println((new StringBuilder()).append("具有最高积分的会员是: ").append( custNo[i]).append("/t").append(custBirth[i]).append("/t") .append(custScore[i]).toString()); // 创建礼物类,并对礼物信息进行设置,这里的礼物是固定的信息Gift gift = new Gift(); gift.name = "苹果笔记本电脑"; gift.price = 12000D; System.out.print("恭喜!获赠礼品: "); System.out.println(gift); // 返回上一级菜单returnLastMenu(); } /*====================定义该类所拥有的变量====================*/ public String goodsName []; // 商品的名称public double goodsPrice[]; // 商品的价格public int custNo []; // 顾客的会员号public String custBirth []; // 顾客的生日public int custScore []; // 顾客的积分}3.9 Gift类
package cn.itcast; /** * 用来存放真情回馈中的礼物的类*/ public class Gift { /** * 空构造方法* */ public Gift() { } /** * 根据礼物对象的变量返回礼物的全部信息* */ public String toString() { return (new StringBuilder()).append("一个价值¥").append(price) .append("的").append(name).toString(); } /*====================定义该类所拥有的变量====================*/ public String name ; // 礼物名字public double price; // 礼物价格}3.10 代码总结
从上面的9个类的代码来看,有一些需要注意的地方:
(1)在许多类中都定义了与Data中基本一样的成员变量,只是没有Manager对象而已,这是为了让数据一层层保存和传递,通过setData()方法实现,不过这种方法其实并不是很好,并且一般来说成员变量应该最好设置为私有的,这里这样的设计是为了操作方便,使系统更简单一些,安全性不好。
(2)注意到所有要进行字符串拼接的地方都使用JAVA中的StringBuilder类,这是为了高效处理字符串拼接,防止String类带来的拼接数据冗余。
(3)这些设计中的流程并不是非常合理,大家可以自己的需要进行修改。
(4)代码中基本上没有考虑异常时的处理,所以当输入时出现错误的时候,普通错误可以进行重新输入,但是如果出现不匹配等错误,直接会出现异常,从而退出系统,这些也是本设计的缺陷,可以通过正则表达式等来完善一些。
总之,给出的代码仅供参考,大家可以根据需要进行修改,这些代码都是经过验证的,可以直接运行。
4. Summary
这个设计只是为了说明一些基本的设计思想和设计理念,以及设计过程中需要考虑的问题,主要还是为了说明怎样用面向对象的思想去解决现实生活中的问题,所以设计相对简单,不过希望大家可以通过这个设计理解这些基本的思想,从而帮助大家理想面向对象的基本思想。
总之,语言只是一种解决问题的工具,大家可以用C++、C#等其他语言来实现这一系统,只要有良好的设计理念和设计思想就可以。再次强调,本设计仅供参考,欢迎大家参与讨论,有错误的地方欢迎大家指正,谢谢。