java基础案例7-2商城进货交易记录

package com.itheima;

import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
import static java.lang.System.exit;

public class Main{
    static Scanner input = new Scanner(System.in);
    public static ArrayList<Goods> goodsList = new ArrayList<>();
    public static void main(String[] args)throws IOException {
        int choice;
        Init();//初始化
        while(true) {
            menu();
            choice = input.nextInt();
            switch (choice) {
                case 1:
                    query();
                    break;
                case 2:
                    addStock();
                    break;
                case 3:
                    addGoods();
                    break;
                case 4:
                    printAllGoods();
                    break;
                case 5:
                    exit(0);
                    break;
                default:
                    System.out.println("选择有误!请再次选择。");
            }
        }
    }
    //显示所有商品的相关信息
    public static void printAllGoods(){
        for(Goods goods: goodsList)
            System.out.println(goods);
    }
    //查询商品信息(连下)
    public static void query(){
        System.out.print("请输入商品编号:");
        String id = input.next();
        try{
           System.out.println(((Goods)queryID(id)).currentRecord());
        }catch(NullPointerException ex){
            System.out.println("无此编号");
            exit(1);
        }
    }
    //看看有没有
    public static Object queryID(String id){
        for(Goods goods : goodsList)
        {
            if(goods.getId().equals(id))
                return goods;
        }
        return null;
    }

    //增加库存
    public static void addStock()throws IOException{
        System.out.print("请输入进货数量:");
        int number = input.nextInt();
        System.out.print("请输入商品编号:");
        String id = input.next();
        try{
            Goods good = (Goods)queryID(id);
            good.setStock(number + good.getStock());
            GoodsRecorder.save(good);//保存到进货记录
            GoodsRecorder.saveRealRecord(goodsList);//商品信息保存到进货
            System.out.println(good.currentRecord());
        }catch(NullPointerException ex){
            System.out.println("无此编号");
            exit(2);
        }
    }
    //初始化
    public static void Init()throws IOException{
        goodsList.add(new Goods("1001", 4.5,"百事可乐", 100, "张三"));
        goodsList.add(new Goods("1002", 4.0, "可口可乐",100, "李四"));
        goodsList.add(new Goods("1003", 3.8, "百事雪碧", 100,"王五"));
        for (Goods goods : goodsList) {  //将商品信息添加到进货记录
            GoodsRecorder.save(goods);
        }
        GoodsRecorder.saveRealRecord(goodsList); //将商品信息添加到进货实时记录
    }
    //添加新商品
    public static void addGoods()throws IOException{
        System.out.print("请输入商品编号:");
        String id = input.next();
        if(((Goods)queryID(id)) != null){
            System.out.println("已有此编号,不可重复");
            exit(3);
        }
        System.out.print("请输入商品名称:");
        String goodsname = input.next();

        System.out.print("请输入购买数量:");
        int amount = input.nextInt();

        System.out.print("请输入单价:");
        double unitPrice = input.nextDouble();

        System.out.print("请输入联系人:");
        String custumer = input.next();

        goodsList.add(new Goods(id, unitPrice, goodsname, amount, custumer));
        GoodsRecorder.save(goodsList.get(goodsList.size()-1));
        GoodsRecorder.saveRealRecord(goodsList);
        System.out.println("商品进货成功.\n");
    }
    public static void menu(){
        System.out.println("-----------商品进货信息及操作------------");
        System.out.println("1.查询                 2.进货          ");
        System.out.println("3.增加商品              4.显示所有商品信息");
        System.out.println("5.退出"                                );
        System.out.print("请选择需要进行的操作:");
    }
}



package com.itheima;

public class Goods{
    private String id;//编号
    private double unitPrice;//单价
    private String goodsName;//商品名称
    private int stock;//库存
    private String custumerName;//联系人

    public Goods(String id, double unitPrice, String goodsName, int stock, String custumerName)
    {
        this.id = id;
        this.unitPrice = unitPrice;
        this.goodsName = goodsName;
        this.stock = stock;
        this.custumerName = custumerName;
    }
    //返回总价(库存*单价)
    public double getTotalPrice(){
        return stock * unitPrice;
    }
    //重写toString
    public String toString(){//返回商品的相关信息
        return "进货记录编号:"+id+"\n商品名称:"+goodsName+"\n联系人:"+custumerName+"\n单价:"+unitPrice+"\n库存数量:"+stock+"\n";
    }
    public String currentRecord(){//当前商品
        return "当前商品库存信息" + toString();
    }
    public String getId(){
        return id;
    }
    public void setId(String id){
        this.id = id;
    }
    public double getUnitPrice(){
        return unitPrice;
    }
    public void setUnitPrice(double unitPrice) {
        this.unitPrice = unitPrice;
    }

    public String getGoodsName() {
        return goodsName;
    }

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

    public int getStock() {
        return stock;
    }

    public void setStock(int stock) {
        this.stock = stock;
    }

    public String getCustumerName() {
        return custumerName;
    }

    public void setCustumerName(String custumerName) {
        this.custumerName = custumerName;
    }
}


package com.itheima;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class GoodsRecorder {//整理好文件名准备进行写入
    public static void save(Goods good)throws IOException {
        DateFormat date = new SimpleDateFormat("yyyyMMdd");
        String fileName = "进货记录"+date.format(new Date()) +".cvs";
        File file = new File("D:\\java商品进货记录", fileName);
        if(file.exists())
            save(good,file,true);
        else
            save(good, file, false);
    }
    //一个文件的写入
    public static void save(Goods good, File file, boolean flag)throws IOException{//将商品写入文件
        if(flag)//在原有的基础上进行追加
        {
            byte[] b = ToStringBuffer(good).getBytes();//返回字符串的字节数
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file, true));
            bufferedOutputStream.write(b, 0, b.length);//输出
            bufferedOutputStream.close();//关闭
        }
        else
        {
            StringBuffer sb = new StringBuffer();//字符串缓冲器对象
            String string = "商品编号 "+"商品名称 "+"购买数量 "+"单价 "+"总价 "+"联系人\n"+ToStringBuffer(good);
            sb.append(string);
            byte[] b = sb.toString().getBytes();
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
            bufferedOutputStream.write(b, 0, b.length);
            bufferedOutputStream.close();
        }
    }
    public static String ToStringBuffer(Goods good)//将商品信息进行整理
    {
        StringBuffer sb = new StringBuffer();
        sb.append(good.getId()).append(" ");
        sb.append(good.getGoodsName()).append(" "); //缓冲器添加商品名称
        sb.append(good.getStock()).append(" "); //缓冲器添加商品库存
        sb.append(good.getUnitPrice()).append(" "); //缓冲器添加商品单价
        sb.append(good.getTotalPrice()).append(" "); //缓冲器添加商品总价
        sb.append(good.getCustumerName()).append("\n");//缓冲器添加商品联系人名称

        return sb.toString();
    }
    //一个链表的写入
    public static void saveRealRecord(ArrayList<Goods> list)throws IOException {
        DateFormat date = new SimpleDateFormat("yyyyMMdd");
        String fileName = "商品进货实时记录" + date.format(new Date()) + ".cvs";
        File file = new File("D:\\java商品进货记录", fileName);

        if (file.exists()) {
            String string = "商品编号 " + "商品名称 " + "购买数量 " + "单价 " + "总价 " + "联系人\n";

            for (Goods goods : list) { //字符串string依次加入各商品的相关信息
                string += ToStringBuffer(goods);
            }

            byte[] b = string.getBytes();
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
            bufferedOutputStream.write(b, 0, b.length);
            bufferedOutputStream.close();
        } else {  //如果文件不存在,创造文件并再次调用本方法
            file.createNewFile();
            saveRealRecord(list);
        }
    }
}

  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,我可以为您讲解Java基础案例7-3日记本。这个案例是一个简单的Java GUI应用程序,可以让用户创建、查看和编辑日记条目。以下是该程序所包含的一些主要功能: 1. 创建新的日记条目:用户可以在程序中创建新的日记条目,输入标题和内容。 2. 查看日记条目列表:程序可以显示所有已创建的日记条目的列表,用户可以通过点击列表中的条目来查看它们的详细内容。 3. 编辑日记条目:用户可以编辑现有的日记条目,包括更改标题和内容。 4. 删除日记条目:用户可以删除现有的日记条目。 下面是该程序的一些关键代码片段: ``` public class DiaryGUI extends JFrame { // ... GUI component declarations ... public DiaryGUI() { // ... GUI initialization code ... // Set up action listeners for buttons newButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { createNewEntry(); } }); viewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { viewSelectedEntry(); } }); editButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { editSelectedEntry(); } }); deleteButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { deleteSelectedEntry(); } }); } // Method to create a new diary entry private void createNewEntry() { // ... code to prompt user for new entry title and content ... DiaryEntry newEntry = new DiaryEntry(title, content); // ... code to add new entry to list of entries ... } // Method to view a selected diary entry private void viewSelectedEntry() { // ... code to get selected entry from list ... // ... code to display selected entry in a new window ... } // Method to edit a selected diary entry private void editSelectedEntry() { // ... code to get selected entry from list ... // ... code to prompt user for new entry title and content ... selectedEntry.setTitle(title); selectedEntry.setContent(content); } // Method to delete a selected diary entry private void deleteSelectedEntry() { // ... code to get selected entry from list ... // ... code to remove selected entry from list ... } } ``` 以上是该程序的一些基本代码片段,它们提供了创建、查看、编辑和删除日记条目的功能。当然,这个程序还有很多细节可以完善,比如数据的持久化和数据的输入验证等等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值