Java案例

一、案例描述

用面向对象做一个在控制台运行的一个购物车功能,能够实现用户登录,注册,添加商品,查看单个商品,查看购物车的功能。

二、思路分析

首先确定有用户类,商品类,还有一个测试类。如果该用户未注册,该怎么判断该用户能否登录。商品的所有信息应该怎么保存才能够对应。

三、源码

package fs_cs_zy_0708;

public class CustomerInformation {
    private String designation;
    private String cipher;
    private String name;

    public CustomerInformation() {
        this.designation = designation;
        this.cipher = cipher;
        this.name = name;
    }


    public String getDesignation() {
        return designation;
    }

    public void setDesignation(String designation) {
        this.designation = designation;
    }

    public String getCipher() {
        return cipher;
    }

    public void setCipher(String cipher) {
        this.cipher = cipher;
    }

    public String getName() {
        return name;
    }

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

    public CustomerInformation(String designation, String cipher, String name) {
        this.designation = designation;
        this.cipher = cipher;
        this.name = name;
    }
}


package fs_cs_zy_0708;

public class Product {
    private String id;
    private String proName;
    private int amount;
    private double price;
    private double sum;

    public String getId() {
        return id;
    }

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

    public String getProName() {
        return proName;
    }

    public void setProName(String proName) {
        this.proName = proName;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public double getPrice() {
        return price;
    }

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

    public double getSum() {
        return sum;
    }

    public void setSum(double sum) {
        this.sum = sum;
    }

    public Product(String id, String proName, int amount, double price, double sum) {
        this.id = id;
        this.proName = proName;
        this.amount = amount;
        this.price = price;
        this.sum = sum;
    }
}
package fs_cs_zy_0708;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.*;

public class Test {
    public static void main(String[] args) {
        boolean flag1=true;
        boolean flag2=true;
        String[] array_designation=new String[10];
        String[] array_cipher=new String[10];
        String[] array_name=new String[10];
        String[] sp_id=new String[10];
        String[] sp_name=new String[10];
        Integer[] sp_amount=new Integer[10];
        double[] sp_price=new double[10];
        List<CustomerInformation> customerInformationList=new ArrayList<>();
        List<Product> productList=new ArrayList<>();

        ObjectOutputStream cusOut= null;
        try {
            cusOut = new ObjectOutputStream(new FileOutputStream("C:\\Users\\Lenovo\\Desktop\\作业序列化.txt"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        CustomerInformation customerInformation1=new CustomerInformation();
        Map<String,String> cus=new HashMap<>();
        Map<String[], Product> mapPro=new HashMap<>();
        Map<CustomerInformation,Product> map=new HashMap<>();
        int sun=0;
        int i=0;
        int k=0;
        while (flag1){
            System.out.println("------------------家乐福购物商城系统-------------------");
            System.out.println("1、顾客注册操作");
            System.out.println("2、顾客登录操作");
            Scanner scanner=new Scanner(System.in);
            System.out.print("请选择操作项:");
            int oot=scanner.nextInt();
            if (oot==1){
                for (i=i;i<10;i++){
                    System.out.println("请输入注册用户登录名称:");
                    array_designation[i]=scanner.next();
                    customerInformation1.setDesignation(array_designation[i]);
                    System.out.print("请输入注册顾客登录密码:");
                    array_cipher[i]=scanner.next();
                    customerInformation1.setCipher(array_cipher[i]);
                    System.out.print("请输入注册顾客真实姓名:");
                    customerInformation1.setName(scanner.next());
                    cus.put(array_designation[i],array_cipher[i]);
                    CustomerInformation customerInformation = new CustomerInformation(array_designation[i],array_cipher[i],array_name[i]);
                    customerInformationList.add(customerInformation);
                    System.out.println("恭喜顾客("+array_designation[i]+")注册成功!");
                    try {
                        cusOut.writeObject(customerInformation);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    map.put(customerInformation1,null);

                    System.out.println("-----------------------------------------------------------------");
                    break;
                }
            }

            else if (oot==2){
                System.out.println("请输入顾客登录名称:");
                String arr1=scanner.next();
                System.out.println("请输入顾客登录密码:");
                String arr2=scanner.next();
                int j=0;
                if (cus.containsKey(arr1)){
                    if (arr2.equals(cus.get(arr1))){
                        System.out.println("恭喜顾客("+arr1+")登录成功!");
                        System.out.println("-----------------------------------------------------------------");
                        while (flag2){
                            System.out.println("1、选购商品操作");
                            System.out.println("2、查询商品详情");
                            System.out.println("3、查询所有商品");
                            System.out.println("4、结束购物");
                            System.out.println("请选择操作项:");
                            int choice=scanner.nextInt();
                            System.out.println("-----------------------------------------------------------------");
                            if (choice==1){
                                for (int m=0;m<sp_id.length;m++){
                                    System.out.println("请输入商品编号:");
                                    sp_id[m]=scanner.next();
                                    System.out.println("请输入商品名称:");
                                    sp_name[m]=scanner.next();
                                    System.out.println("请输入商品数量:");
                                    sp_amount[m]=scanner.nextInt();
                                    System.out.println("请输入商品单价:");
                                    sp_price[m]=scanner.nextDouble();
                                    Product product=new Product(sp_id[m],sp_name[m],sp_amount[m],sp_price[m],(sp_amount[m]*sp_price[m]));
                                    productList.add(product);
                                    map.put(customerInformation1,product);
                                    mapPro.put(sp_id,product);
                                    try {
                                        cusOut.writeObject(product);
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                    System.out.println("-----------------------------------------------------------------");
                                    break;
                                }
                            }
                            else if (choice==2){
                                System.out.println("请输入商品编号:");
                                String choice1=scanner.next();
                                if (mapPro.containsKey(choice1)){
                                    System.out.println(mapPro.get(choice1));
                                }
                                else {
                                    System.out.println("没有该商品!");
                                }

                            }
                            else if (choice==3){
                                System.out.println("商品编号  "+"商品名称  "+"购买数量  "+"商品单价  "+"商品小计  ");
                                for (int n=0;n<productList.size();n++){
                                    System.out.println(sp_id[n]+"   "+sp_name[n]+"   "+sp_amount[n]+"   "+sp_price[n]+"   "+(sp_amount[n]*sp_price[n]));
                                    sun+=(sp_amount[n]*sp_price[n]);
                                }
                                System.out.println("总计金额为:"+sun);

                            }
                            else if (choice==4){
                                System.out.println("谢谢光临,欢迎下次再来!");
                                System.exit(0);
                            }
                        }
                    }
                }
                else {
                    System.out.println("对不起,顾客未注册,不能执行登录操作!");
                }

            }
            else {
                System.out.println("请输入正确的序号");
            }
        }


    }
}

四、总结

这是一个运用到知识点比较多的一个案例,需要我们对面向对象各个知识点的充分理解和运用,用户和商品之间的关联可以用键值对来完成。 

 

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值