Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Inventory CSV files

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    31
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Inventory CSV files

    I need help figuring out the best way to write a program that would read an existing inventory file (which is for my online store) and a new inventory file of around 10000 lines (my physical inventory) delete any lines not on my online inventory spreadsheet this way I can easily u[date my online inventory the columns on physical inventory is sku, price, qty. Then I want the updated information to have the same information except qty updated and price to multiply by 1.38. Any help is much appreciated.


  2. #2
    Junior Member
    Join Date
    Dec 2013
    Posts
    5
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Inventory CSV files

    I have recently completed an assignment with the same operation except with studentids, lastnames, firstnames, classes, marks, all that needs to be done is replace those terms with what you need. It took me a month to create a menu, binary search, and a ShellSort for it. If you would like it just reply and i don't mind sharing it

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    31
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Inventory CSV files

    Quote Originally Posted by GrabACup View Post
    I have recently completed an assignment with the same operation except with studentids, lastnames, firstnames, classes, marks, all that needs to be done is replace those terms with what you need. It took me a month to create a menu, binary search, and a ShellSort for it. If you would like it just reply and i don't mind sharing it
    Yes if you don't mind that would be great.

  4. #4
    Junior Member
    Join Date
    Dec 2013
    Posts
    5
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Inventory CSV files

    There are Two Classes Here's The Main one
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package studentrecs;

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Scanner;
    import static studentrecs.StudentRecs.binarySearch;
    import static studentrecs.StudentRecs.numstu;
    import static studentrecs.StudentRecs.stu;

    /**
    *
    * @author 066992389
    */
    //TO MR.MARTIN THIS METHOD IS NOT INCLUSIVE IN WHAT YOU ARE MARKING NOT HIDING ANYTHING
    //I SIMPLY DON'T WANT YOU TO WASTE YOUR TIME READING THIS
    //MY MAIN WORKS FOR THE PROJECT IS STUDENTRECS.JAVA & STUREC.JAVA



    public class Menu {
    //DECLARATION OF PUBLIC VARIABLES THAT NEED TO BE ACCESSED BY MULTIPLE METHODS

    /** IN THIS CLASS THE PROCCESS OF READING A FILE SPECIFIED BY THE USER IS BEING RUN
    * A MENU PROMPTS UP WITH AN ASSORTMENT OF OPTIONS SUCH AS VIEW AVERAGES
    * AND CHANGE SPECIFIC MARKS
    * THESE OPERATIONS ARE HANDLDED BY DIFFERENT METHODS IN ORDER TO KEEP ORDER
    * THE DECLARATION OF EACH SECTION OF THE STYLESHEET IS ALSO IMPLEMENTED HERE
    * STATING THAT FOR EVERY SECTION BEFORE A COMMA THAT IS THE NAME AND THE BEHOLDER
    * OF THAT METHOD. EG(14504871,THOMASON) THE SECTION BEFORE THE COMMA IS THE STUDENTID
    */
    public static String other;
    public static String user;
    public int StuRec;
    public static int numstu;
    public static double average;
    public static String choice;
    public static StuRec[] stu = new StuRec[10000]; //Declaring that sturec has 1000lines of field

    public static Boolean readFile(String filename) throws IOException { //readFile method reads the prompted file
    float timer = System.nanoTime(); //BEGGINING TIMER VARIABLE TO EVAULTE THE STARTUP TIME PROCCESS
    try { //since this is handling files with possibles errors try must be instianiated in cases of error handling
    Scanner userInput = new Scanner(System.in); //scanner for user input
    System.out.println("Type R To Read a File or Type Default for the default file"); //prompt for what the first step is
    user = userInput.nextLine(); //whatever the user types
    if (user.equalsIgnoreCase("r")) { //if the user types the key r (case ignored)
    user = userInput.nextLine(); //user equals whatever the userInput is
    }
    filename = user; //the filename is what the user types
    if (user.equalsIgnoreCase("default")) { //if user types default (case ignored)
    filename = "newreg2.csv"; //then the file being read will be the newreg2.csv premade file
    }


    other = filename; //declaration for another method below for the file to be read outside
    Scanner input = new Scanner(new FileReader(filename)); //Scanner to scan the given file
    while (input.hasNext()) { //while the file has another field
    in(input.nextLine());
    numstu++; //counter for the amount of fields

    }
    input.close(); //closing the file
    return true;

    } catch (IOException e) { //error catching
    System.err.println(e.getMessage()); //error response
    }
    return false;
    }

    public static void in(String reader) {
    String splitter[]; //SPLITTER TO SPLIT THE FIELDS
    splitter = reader.split(","); //THE SPLLITER WORKS IN DIVISIONS OF THE COMMAS
    stu[numstu] = new StuRec();
    stu[numstu].studentID = splitter[0]; //STUDENTID = FIELD BEFORE THE SPLITTER
    stu[numstu].lastName = splitter[1]; //LASTNAME = FIELD BEFORE THE SPLITTER
    stu[numstu].firstName = splitter[2];//FIRSTNAME = FIELD BEFORE THE SPLITTER
    stu[numstu].phoneNumber = splitter[3];//PHONENUMBER = FIELD BEFORE THE SPLITTER
    stu[numstu].courseCode = splitter[4];//COURSECODE = FIELD BEFORE THE SPLITTER
    stu[numstu].periodNumber = Integer.parseInt(splitter[5]); // parseInt turns a string of digits into an integer //PERIODNUMBER = FIELD BEFORE THE SPLITTER
    stu[numstu].mark = Integer.parseInt(splitter[6]); //MARK IN THAT COURSE
    }

    public static boolean menu() throws IOException {
    //THIS MENU PROMPTS ALLOWS YOU TO WORK WITH THE FILE AND READ AND WRITE SPECIFIC DATA
    Scanner userInput = new Scanner(System.in); //SCANNER FOR THE USERS INPUT
    do { //DO LOOP FOR AN EXECUTE FOR A GIVEN NUMBER OF TIMES (NUMBER OF TIMES DECLARED BY WHILE SOMETHING)
    choice = "";
    System.out.println("============================== ==============="); //MENU OPTIONS (THEY ARE LOOPED TO BE DONE INDEFINATLY)
    System.out.println("Type R To Read Another File");
    System.out.println("Type L To Print all File Records");
    System.out.println("Type AA To Print The Average Of All The Marks");
    System.out.println("Type AC To Print Marks For a Specific Course");
    System.out.println("Type NM To Change a Students Mark");
    System.out.println("Type X To Exit The Program");
    choice = userInput.nextLine(); //READS THE USERS CHOICE FOR WHAT THEY WANT TO DO WITH THE FILE
    if (choice.equalsIgnoreCase("L")) { //IF THE CHOICE WAS THE LETTER L THEN RUN METHOD BELOW IT (SO IT CALLS THAT METHOD AND THAT METHOD IS RUN)
    printList();
    } else if (choice.equalsIgnoreCase("R")) {
    fileChange();
    } else if (choice.equalsIgnoreCase("AA")) {
    average();
    } else if (choice.equalsIgnoreCase("X")) {
    exit();
    } else if (choice.equalsIgnoreCase("AC")) {
    courseAverage();
    } else if (choice.equalsIgnoreCase("NM")) {
    newMark();
    } else if (choice.equalsIgnoreCase("RM")) {
    } else {
    System.err.println("Unknown Key Try Again..."); //IF THE USER INPUT WAS NONE OF THESE CHOICES THEN THROW THEM A NOTICE
    }
    } while (!choice.equalsIgnoreCase("x")); //DO WHILE THE CHOICE IS NOT X WHICH EXITS THE PROGRAM
    return false;
    }

    public static void fileChange() throws IOException { //FILE CHANGE METHOD READS ANOTHER FILE THAT IS ASKED BY THE USER
    readFile(user); //CALLS THE READFILE METHOD IN ORDER TO READ THAT FILE AND RUN IT PROPERLY AND IT READS THE FILE PROMPTED BY THE USER
    }

    public static void printList() { //PRINTS OUT ALL THE FIELDS OF DATA FOR EVERY STUDENT
    for (int i = 0; i < numstu; i++) { //FROM 0 TO THE END OF THE FIELDS IN THE FILE
    System.out.println(stu[i].toString()); //PRINT OUT THE REQUESTED CLASS
    }
    }

    public static void average() { //AN AVERAGE CALCULATOR FOR THE ENTIRE STUDENT BODY
    for (int i = 0; i < numstu; i++) { //FOR LOOP TO READ EVERY STUDENT RECORD
    average += stu[i].mark; // keep adding to average //FOR EVERY RECORD ADD THE MARK OF THAT RECORD TO THE VARIABLE AVERAGE
    }
    // dividision by zero protection
    if (choice.equalsIgnoreCase("AA") && numstu > 0) {
    average = average / numstu; // compute the average. Always use the size in terms of a variable whenever possible.
    System.out.println(average); // as noted below, if this is an integer value, < #of students computations will eval to 0.
    } else if (!choice.equalsIgnoreCase("AA") && numstu < 0) { //IF THE MARK IS LESS THEN 0 THEN THERE MUST BE A PROBLEM
    System.out.println("Oops! No Marks To Calculate!"); //PRINT OUT A NOTICE OF THE ISSUE
    }
    }
    //THIS METHOD CALCULATES THE AVERAGE FOR A SPECIFIED COURSE

    public static void courseAverage() {
    System.out.println("Please Type In The Class Course Code"); //PROMPTS USER TO SELECT A COURSE TO CALCULATE
    int markAdder = 0; //COUNTER FOR THE MARKS (IT ADDS UP)
    int counter = 0; //COUNTER FOR THE AMOUNT OF STUDENTS IN THAT COURSE
    int finalAverage; //FINALAVERAGE CALCULATOR
    String choicer; //USERINPUT CHOICE
    Scanner reader = new Scanner(System.in); //SCANNER FOR WHAT THE USER TYPES
    choicer = reader.nextLine(); //WHATEVER THE USER TYPED IS STORED IN THE VARIABLE CHOICER
    for (int i = 0; i < numstu; i++) { //FOR LOOP EVERY SINGLE FIELD IN THE FILE
    if (stu[i].courseCode.equalsIgnoreCase(choicer)) { //IF THE COURSECODE EQUALS TO WHAT THE USER TYPES THEN...
    markAdder += stu[i].mark; //GO TO THAT STUDENTS MARK AND ADD IT TO THE MARKADDER VARIABLE
    counter++; //THIS COUNTS THE AMOUNT OF STUDENTS IN THAT COURSE FOR THE FINAL CALCULATION
    }
    }
    if (numstu > 0) { //SIMPLE ERROR CHECKING THAT SOME FIELDS WERE CALCULATED
    finalAverage = markAdder / counter; //THE AVERAGE EQUALS THE MARKS DIVIDED BY THE NUMBER OF STUDENTS IN THAT COURSE
    System.out.println(finalAverage);
    }
    }
    //THIS METHOD WRITES A NEW MARK TO EITHER THE FILE A NEW FILE OR TEMPORARILY VIEWED FILE (WHICH IS THE MENU VIEW)

    public static void newMark() throws IOException { //FILE HANDLING, SO ERROR CHECKING IS A MUST
    System.out.println("Type In The Student ID"); //PROMPT FOR STUDENTID
    Scanner input = new Scanner(System.in); //USER INPUT SCANNER FOR WHAT THEY TYPE
    int newMarkk; //VARIABLE FOR THEIR NEW MARK
    String choiceForID; //THE ID THEY TYPE
    choiceForID = input.nextLine(); //A REQUEST FOR THE USER TO FILL IN THE REQUIRED FIELD
    for (int i = 0; i < numstu; i++) { //FOR EVERY FIELD IN THE FILE
    if (stu[i].studentID.equalsIgnoreCase(choiceForID)) { //IF THE STUDENTID AT THE POINT OF THE FILE IS THE SAME AS WHAT THE USER TYPED
    System.out.println("Please Type The New Mark"); //PROMPT FOR NEW UP TO DATE MARK
    newMarkk = input.nextInt(); //NEWMARK EQUALS WHATEVER THEY TYPE
    stu[i].setMark(newMarkk); //SET WHAT THEY TYPE AS THE NEW MARK
    }else
    System.out.println("error...");
    //saver(); //CALL THE METHOD THAT WILL GIVE THEM CHOICES OF SAVING
    }
    }

    //SAVER MENU THIS MENU OPERATES THE TYPE OF SAVE THAT THE USER WOULD LIKE

    public static void saver() throws IOException { //FILE WRITING SO THERE IS ERROR HANDLING
    Scanner input = new Scanner(System.in); //SCANS USER INPUT
    System.out.println("Type S To Save It Here");
    System.out.println("Type SA To Save It To a New File");
    System.out.println("Type LP For a Temporary Save");
    choice = input.nextLine();
    if (choice.equalsIgnoreCase("s")) { //IF CHOICE EQUALS S
    saveMark(); //GO TO METHOD SAVEMARK
    } else if (choice.equalsIgnoreCase("sa")) { //IF CHOICE EQUALS SA
    saveAs(); //GO TO SAVEAS METHOD
    } else if (choice.equalsIgnoreCase("lp")) {
    menu(); //IF THEY CHOOSE TEMPORARY SAVE THEN GO BACK TO THE MAIN MENU AS STU[I].SETMARK ALREADY EQUALS THE MARK THEY GAVE THEM
    }
    }
    //THIS METHOD UPDATES THE MARK

    public static void saveMark() throws IOException {
    File fileWriter = new File(other);
    PrintWriter writer = new PrintWriter(new FileWriter(fileWriter));
    for (int i = 0; i < numstu; i++) {
    writer.println(stu[i].lastName + ", " + stu[i].firstName + ", " + stu[i].studentID + ", " + stu[i].phoneNumber + ", " + stu[i].courseCode + ", " + stu[i].periodNumber + ", " + stu[i].mark);
    }
    writer.close();
    }
    //THIS METHOD EXITS THE PROGRAM AND ENDS IT

    public static void exit() {
    for (int i = 0; i < numstu; i++) {
    System.exit(i);
    }
    }
    //THIS METHOD SAVES THE MARK TO ANOTHER FILE THAT IS PROMPTED BY THE USER

    public static void saveAs() throws IOException {
    System.out.println("Type In The New File Name");
    Scanner fileChoice = new Scanner(System.in);
    String newFile;
    newFile = fileChoice.nextLine();
    File fileWriter = new File(newFile);
    PrintWriter writeNew = new PrintWriter(new FileWriter(newFile));

    for (int i = 0; i < numstu; i++) {
    writeNew.println(stu[i].lastName + ", " + stu[i].firstName + ", " + stu[i].studentID + ", " + stu[i].phoneNumber + ", " + stu[i].courseCode + ", " + stu[i].periodNumber + ", " + stu[i].mark);
    }
    writeNew.close();

    }
    public static int binarySearch(String id) {
    int lo = 0;
    int hi = numstu - 1;
    while (lo <= hi) {
    // Key is in a[lo..hi] or not present.
    int mid = lo + (hi - lo) / 2;
    if (stu[mid].studentID.compareTo(id) > 0) { //A STUDENT ID COMPARED TO ANOTHER TO COMPARE WHICH IS GREATEST TO 0
    hi = mid - 1;
    } else if (stu[mid].studentID.compareTo(id) < 0) { //A STUDENT ID COMPARED TO ANOTHER TO COMPARE WHICH IS LESS THAN 0
    lo = mid + 1;
    } else {
    return mid;

    }
    }
    return -1;
    }
    public static void changeMarkBin(String id, int newmark) {
    binarySearch(id);
    int indx = binarySearch(id); // indx equals the index of the id that was searched
    if (indx >= 0) { //if the index is greater then 0
    stu[indx].setMark(newmark); //change that index's mark to whatever the arguement said so
    System.out.println("The Student " + id + " " + "is at index " + indx + ", " + newmark); //it prints the index
    }
    }
    }


    --- Update ---

    Here's the second one ~ mainly for data encapsulation
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package studentrecs;

    /**
    *
    * @author 066992389
    */
    //this class alocates meomery for every field in the file as well as holds some simple methods
    //for data encapsulation
    public class StuRec {
    public String studentID;
    public String lastName;
    public String firstName;
    public String phoneNumber;
    public String courseCode;
    public int periodNumber;
    public int mark;

    @Override
    //this method makes it easier to print out every field in the record
    public String toString() {
    String s = "";
    s = studentID + ", " + lastName + ", " + firstName + ", " + phoneNumber + ", " + courseCode + ", " + periodNumber + ", " + mark;
    return s;
    }
    //this method is simple error checking and passes the mark change through the arguement
    public void setMark(int markSet) {
    if (mark > -1 && mark < 101) {
    mark = markSet;
    }
    }
    //this method compares one record with any other record to check if they are similar as objects
    //and it returns which out of the two methods are valuable in what is specified later (eg. > 0)
    public int compareTo(Object obj) {
    StuRec otherstu = (StuRec) obj; // cast the other object to become a proper sturec
    return studentID.compareTo(otherstu.studentID);

    }
    }

Similar Threads

  1. inventory System
    By w4nz in forum What's Wrong With My Code?
    Replies: 25
    Last Post: March 17th, 2013, 10:55 AM
  2. Inventory Management System
    By Luffy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 26th, 2013, 03:11 AM
  3. Inventory Management System
    By Luffy in forum Java Theory & Questions
    Replies: 0
    Last Post: February 26th, 2013, 01:50 AM
  4. Help with inventory program
    By curmudgeon in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 13th, 2013, 10:53 PM
  5. Reading CSV files into a program
    By Wrathgarr in forum Java Theory & Questions
    Replies: 2
    Last Post: April 15th, 2010, 10:41 AM