Write a simple database program. Your program will need to access a file named List.txt that holds the employee's weekly paycheck amount and their name. Your program must correctly list the contents of this file and be able to append to it.
Many parts of the program should be kept in their own methods. Pseudocode for the main method is below.
Main:
Loop: As long as the user wants to perform another action on the database
Get the action
If action == 1
List the contents of the entire database
If action == 2
Add a line to the database
If action == anything else
Tell user "Unknown command."
End Loop
End Main
You should have 5 methods (including the main method) in your program. Analyzing the above pseudocode should help you identify what each of the methods should do.
Those are the instructions. I already have the List.txt saved on my desktop (it just includes 3 employees and their paycheck), which looks like this (since you may need to save it to your computer for the program to run).:
Ricki $ 800.35
Henrietta $ 1002.75
Antoine $ 975.23
And here is the code I have so far:
/**
-access file named List.txt
-confirm that file exists
- if entered 1:
list contents of entire file
-if entered 2:
add a line to the file
-if entered 3:
unknown command
need 5 methods
needs to loop
*/
import java.util.Scanner; //needed for scanner class
import java.util.*; //needed for classes
import java.io.*; //needed for File I/O classes
public class proj4
{
public static void main(String[] args) throws IOException
{
int number; //a number entered by user
double sum = 0.0; //Accumulator, initialized at 0
//Create a Scanner object for keyboard input.
Scanner reader = new Scanner(System.in);
//Make sure the fil exists
File file = new File("List.txt");
if (!file.exists())
{
System.out.println("The file List.txt is not found");
System.exit(0);
}
//Open the file for reading
Scanner inputFile = new Scanner(file);
//Enter 1 2 or 3
System.out.println("Enter 1 if you would like to list the contents of List.txt.");
System.out.println("Enter a 2 if you would like to add a line to List.txt.");
number = reader.nextInt();
//Determine the number entered.
switch (number)
{
case 1:
System.out.println("Here are the contents of List.txt");
listContents();
break;
case 2:
System.out.println("You will now be able to add a line to List.txt");
lineAdd();
break;
default:
System.out.println("Unknown command");
break;
}
}
public static void listContents() throws IOException
{
//create a scanner object for keyboard input
Scanner reader = new Scanner(System.in);
//Filename
System.out.print("Please verify that you would like to have the contents");
System.out.print(" of List.txt shown by typing in 'List.txt':");
String filename = reader.nextLine();
//Open the file
File file = new File(filename);
Scanner inputFile = new Scanner(file);
//Read the lines from the file until no more are left
while (inputFile.hasNext())
{
//Read the next name
String name = inputFile.nextLine();
//Display the last name read
System.out.println(name);
}
}
public static void lineAdd() throws IOException
{
String filename; //filename
String newLine; // new line
int numLine; //number of lines
//Create a Scanner object for keyboard input
Scanner reader = new Scanner(System.in);
//Get the number of lines user wishes to add
System.out.print("How many lines would you like to add to List.txt? : ");
numLine = reader.nextInt();
//Consume the remaining newline character
reader.nextLine();
//Get the filename.
System.out.print("Please verify that you are adding lines to the correct ");
System.out.print("file by typing in 'List.txt' now: ");
filename = reader.nextLine();
//open the file
PrintWriter outputFile = new PrintWriter(filename);
//Get data abnd write it to the file
for (int i = 1; i <= numLine; i++)
{
//Get the information
System.out.print("Please enter the employee and the paycheck "
+ "amount: ");
newLine = reader.nextLine();
//Write the information to the file
outputFile.println(newLine);
}
}
//Close the file
//outputFile.close();
}
There are many things I am still trying to figure out with this program. I'll list the few that come to my head:
1. When I try to add new information to the list, it deletes everything from the list.
2. I need it to loop around so that the user will have to say that they wish to exit the program when they are done. For instance, if they add a new line and then wish to view the list again, they can do so without restarting the program.
3. Can I use something other than the switch method?
Thank you for all your help!
This post has been edited by macosxnerd101: 17 November 2010 - 12:23 PM
Reason for edit:: Please use code tags

New Topic/Question
Reply
MultiQuote








|