Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

When Display all records is chosen from the main menu, the program will read the contents of the file and display all records. Make sure

  1. When "Display all records" is chosen from the main menu, the program will read the contents of the file and display all records. Make sure to close the file after reading. The program will then pause, waiting for the user to press any key and enter to continue.
  2. When "Enter a new record" is chosen from the main menu:
  • The program will read the file to determine the last record number used. This will number will be incremented by one and the result will be used as the record number for the new record. The file will be closed after this read. If there is no data other than the header in the file then the beginning record number will be 1 (one).
  • New data will be stored temporarily in an array.
  • The user will then be guided though supplying data for each field in the record.
  • After data for the last field is supplied, the new record will be displayed and the program will ask the user to press 1 and enter if it is correct or 0 and enter to escape to the main menu.
  • If the user presses 1 and enter the record in the array will be appended to the existing file. The file will be closed after appending the new record.
  • If the user presses 0 and enter the newly supplied data will be abandoned and the user will be returned to the main menu.

I am struggling with number 2. I created my program, however, I can't seem to understand how I can get the program to "hold" information and when the user confirms it, it will post it to the array or my CVS file. Did not include the imports:

public class inventorySys {

//Menu to display at all times. static void menu() { System.out.println("1 - Display all records: "); System.out.println("2 - Enter a new record: "); System.out.println("3 - Quit Program "); } public static void main(String[] args) throws IOException {

int option; Scanner sc = new Scanner(System.in); ArrayList inventList = new ArrayList(); menu(); do { inventory s = new inventory(); option = sc.nextInt(); switch(option) { case 1: s.displayItems(inventList); break; case 2: int input = 0; do { System.out.println("Enter Record Number: "); s.setrecordNumber(sc.nextInt()); sc = new Scanner(System.in); System.out.println("Enter name of part: "); s.setName(sc.nextLine()); System.out.println("Enter Part Number: "); s.setpartNumber(sc.nextInt()); sc = new Scanner(System.in); System.out.println("Enter Manufacturer"); s.setmanufacturer(sc.nextLine()); System.out.println("Enter Purchase Price: "); s.setpurchasePrice(sc.nextDouble()); System.out.println("Enter Sales Price: "); s.setsalePrice(sc.nextDouble()); System.out.println("More Records to add? For yes, Press 1, for No press 0"); sc.nextInt(); s.addNewInv(s); inventList.add(s); menu(); } while(input != 0); break; case 3: break; } }while (option != 3);

**********************************************************************************************************************************

Support class:

public class inventory {

private int recordNumber; private String nameOfPart; private int partNumber; private String manufacturer; private double purchasePrice; private double salePrice;

//getter and setters public void setrecordNumber(int recNo) { this.recordNumber = recNo; recNo = 0; } public int getrecordNumber() { return this.recordNumber; } public void setName(String name) { this.nameOfPart = name; } public String getName() { return this.nameOfPart; } public void setpartNumber(int partNo) { this.partNumber = partNo; } public int getpartNumber() { return this.partNumber; } public void setmanufacturer(String manName) { this.manufacturer = manName; } public String getmanufacturer() { return this.manufacturer; } public void setpurchasePrice(double purPrice) { this.purchasePrice = purPrice; } public double getpurchasePrice() { return this.purchasePrice; } public void setsalePrice(double salePrice) { this.salePrice = salePrice; } public double getsalePrice() { return this.salePrice; } //Function public void addNewInv(inventory inventory) throws IOException { this.recordNumber = inventory.recordNumber; this.nameOfPart = inventory.nameOfPart; this.partNumber = inventory.partNumber; this.manufacturer = inventory.manufacturer; this.purchasePrice = inventory.purchasePrice; this.salePrice = inventory.salePrice; File csvFile = new File("inventory.csv"); FileWriter fw = new FileWriter(csvFile, true); Writer output = new BufferedWriter(fw); output.write(recordNumber + "," + nameOfPart + "," + partNumber + "," + manufacturer + "," + purchasePrice + "," + salePrice + " " ); output.close(); } public void displayItems(ArrayList arr) { for (inventory inventory : arr) { System.out.println("-------------------------------------------"); System.out.println("Record Number: " + inventory.recordNumber); System.out.println("Name of part: " + inventory.nameOfPart); System.out.println("Part Number: " + inventory.partNumber); System.out.println("Manufacturer: " + inventory.manufacturer); System.out.println("Purchase Price: " + inventory.purchasePrice); System.out.println("Sale Price: " + inventory.salePrice); System.out.println("-------------------------------------------");

} }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamental Managerial Accounting Concepts

Authors: Edmonds, Tsay, olds

6th Edition

71220720, 78110890, 9780071220729, 978-0078110894

More Books

Students also viewed these Programming questions