Click here to Skip to main content
15,844,477 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
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
Posted
Updated 23-Apr-13 9:45am
v2
Comments
[no name] 23-Apr-13 15:09pm    
What is the question?
captaindoit 23-Apr-13 15:26pm    
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?
[no name] 23-Apr-13 15:51pm    
Repeating the same thing that is in your narrative does not magically turn this into a question. "I feel like I'm running myself in circles" does not mean anything to me as it does not tell me what the specific problem related to your code that you are experiencing is. What you have here is basically a code dump and request for someone to rewrite it for you.
Richard MacCutchan 23-Apr-13 16:18pm    
Why are you using text files rather than a database?

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900