Simple Database program help

i need help writing a simple database program

Page 1 of 1

4 Replies - 3701 Views - Last Post: 17 November 2010 - 01:23 PM Rate Topic: -----

#1 Guest_Adele*


Reputation:

Simple Database program help

Posted 17 November 2010 - 12:12 PM

I need help writing a "simple" data base program in Java. I'm fairly new to Java, so I'm not quite sure what I need to do from what I have. Here are the project guidelines:

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


Is This A Good Question/Topic? 0

Replies To: Simple Database program help

#2 drew_z_2  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 84
  • Joined: 24-April 08

Re: Simple Database program help

Posted 17 November 2010 - 12:52 PM

Instead of the
 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;
        }



have user input 1 = print list
2 = add line
3 = exit


you can make something like
while(number != 3)
{

// do all your printing and adding in here
// i would do it as
if (number == 1){
}
if (number == 2){
}
}


this way the while loop breaks when you want to exit.... however you need to put your
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();



put this inside the loop so that the user knows their choices everytime


//Enter 1 2 or 3
		while(number != 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();
		  
		  if (number == 1){
		  		listContents();
			}
			
			if (number == 2){
					lineAdd();
			}




this way seems more simple

This post has been edited by drew_z_2: 17 November 2010 - 12:53 PM

Was This Post Helpful? 0
  • +
  • -

#3 Guest_Adele*


Reputation:

Re: Simple Database program help

Posted 17 November 2010 - 01:10 PM

it is telling me that :


variable number might not have been initialized
while (number != 3)


i definitely initialized it when i said :
int num;

how do i fix this?
Was This Post Helpful? 0

#4 jhar131  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 149
  • Joined: 03-February 10

Re: Simple Database program help

Posted 17 November 2010 - 01:20 PM

You declared it. You initalize it like this:


int number = 0;




or this
int number;

number = 0;



You must give the variable a starting value.
Was This Post Helpful? 0
  • +
  • -

#5 k1ngcor3y  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 69
  • Joined: 27-August 10

Re: Simple Database program help

Posted 17 November 2010 - 01:23 PM

int num; is a declaration

int num = 0; thats initializing
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1