JavaSE comprehensive practice big data mobile mall

preface

Hello, I'm ChinaManor, which translates to Chinese code farmer. I hope I can become a paver on the road to national rejuvenation, a cultivator in the field of big data, an ordinary but unwilling person.

The following are the exercises that our coder has done on the basis of learning Java. I hope it will be helpful for you to learn Java

Jar package required
Link: https://pan.baidu.com/s/1Y4CT_ePPKc2lDdh_ylMCew
Extraction code: didj

Mobile mall

Title stem:

Complete the following functions of the mobile mall according to the specified requirements:

Login, product list query, product details, display shopping cart products, access footprints, add to shopping cart, buy now

1, Login

Title stem:

Complete the [login] function:

The user enters the user name and password to complete the login function

Note: the user name and password are given in the initialization data (init method)

Requirements:

  1.    The user's user name and password are correct, dear xxx,You have successfully logged in, please shop happily!!!     
    
  2.    The user entered the user name and password incorrectly, prompting login failure, please log in again!!!     
    
  3.    Users can directly access the following functions whether they are logged in or not:     
    

a. Product list query, product details, display shopping cart products, access footprints, add to shopping cart

Note: users can access the payment function only when they are logged in!!!

2, Commodity list query

Title stem:

Complete the commodity list query function:

Requirements:

  1.    The effect diagram is shown in the figure below:     
    

3, Product details

Title stem:

Complete the [product details] function:

Requirements:

  1.    The effect diagram is shown in the figure below:     
    

4, Add to cart

Title stem:

Complete the [add to shopping cart] function:

Requirements:

  1.    Enter the wrong goods number and commodity quantity, and you will be prompted that adding failed     
    
  2.    Enter the correct goods number and quantity, and prompt: add to shopping cart successfully!     
    

5, Buy now

Title stem:

Complete the [buy now] function:

Requirements:

  1.    When you are not logged in, click the payment function, and you will be prompted: you are not logged in, please log in...     
    
  2.    Enter the correct goods number, purchase quantity and display the purchase list     
    
  3.    After payment is successful, the corresponding goods list will be queried here, and its inventory quantity will decrease (existing inventory quantity)=Original inventory quantity-Purchase quantity), as shown in Figure 3 below     
    
  4.    After the payment is successful, the purchase record (buyer, name of goods purchased, quantity purchased and total amount of goods purchased) will be written in orders.txt File, as shown in Figure 4 below     
    

a. If orders Txt file does not exist, please create it

b. If multiple purchase records are stored, they will be stored on a new line (one purchase record per line)

6, Display shopping cart items

Title stem:

Complete the [display shopping cart goods] function:

Requirements:

  1.    When there are no goods in the shopping cart, prompt: there are no goods in the shopping cart for the time being. Please go to the home page of the goods to shop!!!     
    
  2.    When the shopping cart is not empty, the shopping list is displayed     
    

7, Access footsteps

Title stem:

Complete the [access footprint] function:

Requirements:

  1.    When you have not accessed any products, you will be prompted that you have not accessed any products yet!     
    
  2.    After accessing the product details, the access footprint displays the access product list, as shown in Figure 2 and figure 3     
    

Reference code:

Shop.java

package Daily homework 17;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Created by dingw on 2019/11/17.
 */
public class Shop {
    //Brand collection
    static List<Brand> brandList = new ArrayList<>();
    //user
    static ArrayList<User> users = new ArrayList<>();
    //Shopping Cart
    static ArrayList<Item> carts = new ArrayList<>();
    //Access footsteps
    static List<Goods> goodsList = new ArrayList<>();
    //Determine the login ID
    static boolean flag = false;
    //Login person
    static String loginname = "";

    public static void main(String[] args) throws Exception {
        init();
        System.out.println("=====================================Welcome to big data mobile mall========================================");
        one:
        while (true) {
            System.out.println("==========================================================================================");
            System.out.println("[1] Sign in");
            System.out.println("[2] Commodity list query");
            System.out.println("[3] Product details");
            System.out.println("[4] Display shopping cart items");
            System.out.println("[5] Access footsteps");
            System.out.println("[6] sign out");
            System.out.print("Please select[1-6]:");
            System.out.println("Please enter your action");
            Scanner sc = new Scanner(System.in);
            String x = sc.nextLine();
            switch (x) {
                case "1":
                    login();
                    break;
                case "2":
                    findAll();
                    break;
                case "3":
                    findProductById();
                    two:
                    while (true) {
                        System.out.println("***************************************************************************");
                        System.out.println("[1] Add to cart");
                        System.out.println("[2] Buy now");
                        System.out.println("[3] sign out");
                        System.out.print("Please select[1-3]:");
                        System.out.println("Please enter your action");
                        Scanner sc1 = new Scanner(System.in);
                        String xs = sc1.nextLine();
                        switch (xs) {
                            case "1":
                                addCart();
                                break;
                            case "2":
                                pay();
                                break;
                            default:
                                break two;
                        }
                    }
                    break;
                case "4":
                    showCart();
                    break;
                case "5":
                    historyVisit();
                    break;
                default:
                    break one;
            }

        }

    }

    /**
     * Display shopping cart
     */
    public static void showCart() {
        System.out.println("===================================Shopping cart items are as follows============================================");
        if (carts.size() == 0) {
            System.out.println("There are no goods in the shopping cart for the time being. Please go to the home page of the goods to shop!!!");
        } else {
            System.out.println("Goods No\t\t Name of goods (trade name)+Color)\t\t\t Price\t\t\t\t\t colour\t\t Purchase quantity");
            for (Item item : carts) {
                System.out.println(item.getId() + "\t\t\t\t" + item.getName() + "\t\t\t\t" + item.getPrice() + "\t\t\t\t" + item.getColor() + "\t\t" + item.getNum());

            }
        }

    }

    /**
     * Check out
     */
    private static void pay() throws Exception {
        if (flag) {
            System.out.println("Please enter the goods number:");
            Scanner sc = new Scanner(System.in);
            String num = sc.nextLine();
            System.out.println("Please enter purchase quantity:");
            String kc = sc.nextLine();
            boolean ss = false;
            for (Brand brand : brandList) {
                for (Goods goods : brand.getGoodsList()) {
                    for (Item item : goods.getItemList()) {
                        if (item.getId().equals(num)) {
                            if (item.getNum() < Integer.parseInt(kc)) {
                                flag = true;
                            } else {
                                System.out.println("Goods No\t\t Name of goods (trade name)+Color)\t\t\t Price\t\t\t\t\t colour\t\t Purchase quantity");
                                System.out.println(item.getId() + "\t\t\t\t" + item.getName() + "\t\t\t\t" + item.getPrice() + "\t\t\t\t" + item.getColor() + "\t\t" + kc);
                                System.out.println("=======================================================================");
                                double total = (Math.round(item.getPrice() * 100 * Integer.parseInt(kc))) / 100.0;
                                System.out.println("Total payment:" + total);
                                item.setNum(item.getNum() - Integer.parseInt(kc));
                                FileUtils.write(new File("orders.txt"), loginname + ":" + item.getName() + "," + kc + "," + total + "\r\n", true);
                                return;
                            }
                        }

                    }

                }

            }
            System.out.println("Goods No. does not exist, please re-enter!!!");
            if (ss) {
                System.out.println("Insufficient inventory, please buy again!");
            }


        } else {
            System.out.println("You haven't logged in yet. Please log in.....");
            login();
        }
    }

    /**
     * Add to cart
     */
    private static void addCart() {
        System.out.println("Please enter the goods number:");
        Scanner sc = new Scanner(System.in);
        String num = sc.nextLine();
        System.out.println("Please enter product quantity:");
        String kc = sc.nextLine();
        for (Brand brand : brandList) {
            for (Goods goods : brand.getGoodsList()) {
                for (Item item : goods.getItemList()) {
                    if(carts.contains(item)){
                        for (Item cart : carts) {
                            if (cart.getName().equals(item.getName())&&(cart.getNum()+Integer.parseInt(kc))<=item.getNum()){
                                cart.setNum(cart.getNum()+Integer.parseInt(kc));
                                System.out.println("Successfully added to shopping cart!");
                                return;
                            }

                        }
                    }
                    if (item.getId().equals(num) && Integer.parseInt(kc) <= item.getNum()) {
                        carts.add(new Item(item.getId(), item.getName(), item.getColor(), item.getPrice(), Integer.parseInt(kc)));
                        System.out.println("Successfully added to shopping cart!");
                        return;
                    }

                }

            }

        }
        System.out.println("Add failed");

    }

    /**
     * Access footsteps
     */
    private static void historyVisit() {
        if (goodsList.size() == 0) {
            System.out.println("You haven't visited any products yet!");
        } else {
            System.out.println("Item number\t\t Trade name\t\t\t\t Product introduction");
            for (Goods goods : goodsList) {
                System.out.printf(goods.getGid() + "\t\t\t  " + goods.getGname() + "\t\t\t" + goods.getDesc());
                System.out.println();

            }
        }

    }

    /**
     * Product details
     */
    private static void findProductById() {
        findAll();
        System.out.println("============================================Product details========================================");
        System.out.println("Please enter the product number you want to query:");
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        System.out.println("Goods No\t\t Name of goods (trade name)+Color)\t\t\t Price\t\t\t\t\t colour\t\t Inventory number");
        for (Brand brand : brandList) {
            for (Goods goods : brand.getGoodsList()) {
                if (goods.getGid() == num) {
                    if (!goodsList.contains(goods)){
                        goodsList.add(goods);
                    }

                    for (Item item : goods.getItemList()) {
                        System.out.println(item.getId() + "\t\t\t\t" + item.getName() + "\t\t\t\t" + item.getPrice() + "\t\t\t\t" + item.getColor() + "\t\t" + item.getNum());

                    }
                }
            }

        }

    }


    /**
     * Query all: product list query
     */
    public static void findAll() {
        System.out.println("=========================================Commodity list query======================================");
        System.out.println("Item number\t\t Trade name\t\t\t\t Product introduction");
        for (Brand brand : brandList) {
            for (Goods goods : brand.getGoodsList()) {
                System.out.printf(goods.getGid() + "\t\t\t  " + goods.getGname() + "\t\t\t" + goods.getDesc());
                System.out.println();
            }

        }

    }

    /**
     * Sign in
     */
    public static void login() {
        System.out.println("=====================================Welcome to login=======================================");
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter user name:");
        String username = sc.nextLine();
        System.out.println("Please input a password: ");
        String password = sc.nextLine();
        for (User user : users) {
            if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
                System.out.println("dear" + username + ",You have successfully logged in, please shop happily!!!");
                flag = true;
                loginname = username;
                return;
            }

        }
        System.out.println("Login failed, please login again!!!");

    }

    /**
     * Initialization data
     */
    public static void init() {

        users.add(new User(1, "tom", "123"));
        users.add(new User(2, "jack", "456"));

        //Cargo collection
        List<Item> hwitemList = new ArrayList<>();
        List<Item> hwproitemList = new ArrayList<>();
        List<Item> mzitemList = new ArrayList<>();
        List<Item> mzPitemList = new ArrayList<>();
        List<Item> xmitemList = new ArrayList<>();
        hwitemList.add(new Item("1_1", "Huawei P20 Auroral color", "Auroral color", 3788, 10));
        hwitemList.add(new Item("1_2", "Huawei P20 Sapphire blue", "Sapphire blue", 3788, 10));
        hwproitemList.add(new Item("2_1", "Huawei P20 Pro Auroral color", "Auroral color", 4988, 10));
        hwproitemList.add(new Item("2_2", "Huawei P20 Pro Sapphire blue", "Sapphire blue", 4988, 10));
        mzitemList.add(new Item("3_1", "Meizu 16 th Yuanshanbai", "Yuanshanbai", 2698, 10));
        mzPitemList.add(new Item("4_1", "Meizu 16 th plus Yuanshanbai", "Yuanshanbai", 3198, 1));
        xmitemList.add(new Item("5_1", "Millet 8 Black", "black", 2699, 0));

        //Commodity collection
        List<Goods> hwgoodsList = new ArrayList<>();
        List<Goods> mzgoodsList = new ArrayList<>();
        List<Goods> xmgoodsList = new ArrayList<>();

        hwgoodsList.add(new Goods(1, "Huawei P20", "Huawei P20,You deserve it", hwitemList));
        hwgoodsList.add(new Goods(2, "Huawei P20 Pro", "Huawei P20 Pro,You deserve it", hwproitemList));
        mzgoodsList.add(new Goods(3, "Meizu 16 th", "Qualcomm snapdragon 845, off screen fingerprint, AI Dual camera optical anti shake", mzitemList));
        mzgoodsList.add(new Goods(4, "Meizu 16 th plus", "Qualcomm snapdragon 845, off screen fingerprint, AI Dual camera optical anti shake", mzPitemList));
        xmgoodsList.add(new Goods(5, "Xiaomi 8", "Snapdragon 845 processor / Infrared face unlock /AI Zoom double shot", xmitemList));


        brandList.add(new Brand(1, "Huawei", hwgoodsList));
        brandList.add(new Brand(2, "Meizu", mzgoodsList));
        brandList.add(new Brand(3, "millet", xmgoodsList));
    }


}

Item.java

package Daily homework 17;

/**
 * Created by dingw on 2019/11/17.
 */

/**
 * Goods category: refers to specific mobile phones
 */
public class Item {
    //Cargo id
    private String id;
    //Name of goods (trade name + mobile phone color)
    private String name;
    //Phone color
    private String color;
    //price
    private double price;
    //Inventory number
    private int num;

    public Item() {
    }

    public Item(String id, String name, String color, double price, int num) {
        this.id = id;
        this.name = name;
        this.color = color;
        this.price = price;
        this.num = num;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Item item = (Item) o;

        if (Double.compare(item.price, price) != 0) return false;
        if (num != item.num) return false;
        if (id != null ? !id.equals(item.id) : item.id != null) return false;
        if (name != null ? !name.equals(item.name) : item.name != null) return false;
        return color != null ? color.equals(item.color) : item.color == null;
    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = id != null ? id.hashCode() : 0;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (color != null ? color.hashCode() : 0);
        temp = Double.doubleToLongBits(price);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        result = 31 * result + num;
        return result;
    }
}

Goods.java

package Daily homework 17;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dingw on 2019/11/17.
 */
public class Goods {
    private int gid;
    private String gname;
    private String desc;
    private List<Item> itemList = new ArrayList<>();

    public Goods() {

    }

    public Goods(int gid, String gname, String desc, List<Item> itemList) {
        this.gid = gid;
        this.gname = gname;
        this.desc = desc;
        this.itemList = itemList;
    }

    public int getGid() {
        return gid;
    }

    public void setGid(int gid) {
        this.gid = gid;
    }

    public String getGname() {
        return gname;
    }

    public void setGname(String gname) {
        this.gname = gname;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public List<Item> getItemList() {
        return itemList;
    }

    public void setItemList(List<Item> itemList) {
        this.itemList = itemList;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Goods goods = (Goods) o;

        if (gid != goods.gid) return false;
        if (gname != null ? !gname.equals(goods.gname) : goods.gname != null) return false;
        if (desc != null ? !desc.equals(goods.desc) : goods.desc != null) return false;
        return itemList != null ? itemList.equals(goods.itemList) : goods.itemList == null;
    }

    @Override
    public int hashCode() {
        int result = gid;
        result = 31 * result + (gname != null ? gname.hashCode() : 0);
        result = 31 * result + (desc != null ? desc.hashCode() : 0);
        result = 31 * result + (itemList != null ? itemList.hashCode() : 0);
        return result;
    }
}

Brand.java

package Daily homework 17;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dingw on 2019/11/17.
 */
public class Brand {
    private int bid;
    private String bname;
    private List<Goods> goodsList = new ArrayList<>();

    public Brand() {
    }

    public Brand(int bid, String bname, List<Goods> goodsList) {
        this.bid = bid;
        this.bname = bname;
        this.goodsList = goodsList;
    }

    public int getBid() {
        return bid;
    }

    public void setBid(int bid) {
        this.bid = bid;
    }

    public String getBname() {
        return bname;
    }

    public void setBname(String bname) {
        this.bname = bname;
    }

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

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

User.java

package Daily homework 17;

/**
 * Created by dingw on 2019/11/17.
 */

/**
 * user
 */
public class User {
    //User id
    private int uid;
    //user name
    private String username;
    //password
    private String password;

    public User() {
    }

    public User(int uid, String username, String password) {
        this.uid = uid;
        this.username = username;
        this.password = password;
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

summary

The above is the big data mobile phone mall of JavaSE comprehensive practice. Your favorite partners are welcome to click three times!!!
I'm manor, a code farmer who believes that technology changes the world. I'll see you next time ~

Added by gardnc on Mon, 24 Jan 2022 08:18:35 +0200