Question
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
- 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.
- 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
**********************************************************************************************************************************
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
} }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started