库存程序包含对txt文件的读写 [英] inventory program containing read and write to txt file

查看:68
本文介绍了库存程序包含对txt文件的读写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须编写一个程序,允许用户从库存中订购产品。用户选择产品,输入数量,如果手头有足够的数量来填充订单,则从产品阵列中检索价格,计算成本,在表格上显示订单信息,从中扣除数量订单库存数组,并将订单写入输出文件。



这是我到目前为止编写的代码。我觉得自己在圈子里奔跑,我不知道如何设置一切。任何有关我已经编写过的代码的帮助,或者特别是我还没写过的代码,我将不胜感激。也许我可以得到一些例子来帮助我看看我的代码应该是什么样子?



I have to write a program that allows the user to order a product from inventory. The user selects the product, enters the quantity, and if there is sufficient quantity on hand to fufill the order, the price is retreived from the product array, the cost is calculated, order info is displayed on form, quantity order is deducted from the inventory array, and the order is written to an output file.

This is the code I''ve written so far. I feel like I''m running myself in circles and I''m not understanding how to set everything up. Any help with the code I''ve already written or especially code I''ve yet to write would be much appreciated. Maybe I could get some examples in order to help me see what my code should look like?

Option Strict On
Imports System.IO

Public Class OderEntryForm1

    ''Structure to hold product record info.

    Structure ProductRecord

        ''to hold product name.

        Dim strProduct As String

        ''to hold quantity on hand.

        Dim strQuantity As String

        ''to hold price.

        Dim strPrice As String

        ''******************************************************************************

        ''This procedure uses an array to list items in the list box.

        ''******************************************************************************

        Dim lstProducts As ListBox

lstProducts.Items.Add= ("A45", "B24", "C12", "D78", "E45")

    End Structure

    ''******************************************************************************

    ''This procedure reads data from a text file into an array of the ProductRecord

    '' structure.  It then writes out the array data to an output text file.

    ''******************************************************************************

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs)

        ''declare variables for the input text file.

        Dim textFileIn As StreamReader

        ''declare variables for the output text file.

        Dim textFileOut As StreamWriter

        ''declare variables for the input and output file names.

        Dim strFileTextIn As String

        Dim strFileTextOut As String

     
   ''declare a variable of the structure ProductRecord.

        Dim productRecord(5) As ProductRecord

        Dim intIndex As Integer

        Dim sngPrice As Single

        Dim sngQuantity As Single

        Dim intCount As Integer


        ''assign the input data file.

        strFileTextIn = "Textfile.txt"

        ''test for existence of input data file.

        If File.Exists(strFileTextIn) Then

            ''open the data file for input.

            textFileIn = File.OpenText(strFileTextIn)

            ''initialize array index.

            intIndex = 0
            ''test for end of input data file.

            Do Until textFileIn.Peek = -1

                ''read fields from input data file into array of ProductRecord.

                With textFileIn

                    productRecord(intIndex).strProduct = .ReadLine()

                    productRecord(intIndex).strQuantity = .ReadLine()

                    productRecord(intIndex).strPrice = .ReadLine()

                    ''assign price field to a price variable.

                    sngPrice = CSng(productRecord(intIndex).strPrice)

                    ''calculate a price.

                    sngPrice = CSng(sngPrice * sngQuantity)

                    ''assign the price to the price field in the ProductRecord array.

                    productRecord(intIndex).strPrice = CStr(sngPrice)

                End With

                ''increment the array index.

                intIndex += 1

            Loop

            ''close the input data file.

            textFileIn.Close()


            ''assign the output data file.

            strFileTextOut = "Textfile.txt"

            ''create the data file for ouput.

            textFileOut = File.CreateText(strFileTextOut)

            ''create or open existing file for appending new records.

            textFileOut = File.AppendText(strFileTextOut)

            ''calculate the number of records read from file.

            intCount = intIndex - 2

            ''write records to output data file.

            For intIndex = 0 To intCount

                MsgBox(productRecord(intIndex).strProduct)


                textFileOut.WriteLine(productRecord(intIndex).strProduct)

                textFileOut.WriteLine(productRecord(intIndex).strPrice)

                textFileOut.WriteLine(productRecord(intIndex).strQuantity)

            Next

            ''close the output data file to write any remaining records to file.

            textFileOut.Close()

        Else

            MsgBox("File does not exist.")

        End If

    End Sub

    Private Sub lblPrice_Click(sender As System.Object, e As System.EventArgs)

    End Sub

    Private Sub lstProducts_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lstProducts.SelectedIndexChanged

    End Sub
End Class

推荐答案

这里有几个原因让你受到惊吓。



首先,你刚刚在你的问题中抛出了一堆代码。为了帮助您,我们希望有代码,但只有相关代码。而且你问的是一个非常普遍的问题,似乎并不一定要特定于任何代码...所以在这种情况下它只是有点讨厌。



其次,这是一个非常普遍的问题。您应该 google [ ^ ]。



话虽如此,如果你真的想要一个具有你所讨论的所有功能的程序,你需要使用数据库而不是文本文件。您将需要许多不同的表。一个用于跟踪产品,一个用于跟踪订单,一个用于跟踪客户,一个等等......当数据库可以更轻松地处理数据时,您不希望弄乱各种文本文件。



如果您根本不熟悉数据库, google [ ^ ]。然后,当你开始研究它并卡住时,在这里发布一个更具体的问题。



祝你好运。
You''re getting dinged for a couple of reasons here.

First, you just dumped a bunch of code in your question. To help you, we like to have code, but only the relevant code. And you are asking a very general question that doesn''t seem to necessarily be specific to any code...so it''s just kind of annoying in this case.

Second, this is a very general question. This is something that you should google[^].

With that said, if you really want a program with all of the functionality that you are talking about, you need to use a database not a text file. You are going to want many different tables. One to track products, one to track orders, one to track customers, and on and on... you don''t want to be messing with all kinds of text files to do that when a database can handle it so much easier.

If you''re not familiar with databases at all, google[^] for some tutorials to get started. Then when you''re starting to work on it and get stuck, post a more specific question here.

Good luck.


这篇关于库存程序包含对txt文件的读写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆