import java.io.*;
import javax.swing.JOptionPane;
import java.util.Scanner;
public class ClientList
{
public static void main (String args[]) throws IOException
{
// Main class ClientList creates Clients.txt file that is to be opened up and appended whenever clients are to be added
FileWriter outFile = new FileWriter("Clients.txt");
BufferedWriter outStream = new BufferedWriter(outFile);
String outString = "Client Listing";
outStream.write(outString);
outStream.newLine();
outStream.close();
System.out.println("Clients.txt is created\n");
// "Primitive" menu asks user what he/she wants to do and accepts commands via keyboard scanner
Scanner menu = new Scanner(System.in);
System.out.println("Welcome to ClientList");
System.out.println();
System.out.println("To add clients to the file, enter 1");
System.out.println("To open file and view its contents, enter 2");
// System.out.println("To search for a client in the file, enter 3");
int command = menu.nextInt();
switch (command)
{
case 1: Clients client = new Clients();
client.enterData();
break;
case 2: Clients client2 = new Clients();
client2.getClients();
break;
// case 3: Clients client = new Clients();
// client.searchClients();
// break;
default: System.out.println("Doesn't exist!");
System.out.print("Enter a valid number ===> ");
int l = menu.nextInt();
break;
}
}
}
class Clients
{
// Initializing variables for client data
String firstName;
String lastName;
String age;
String height;
String weight;
String startDate;
String phNum;
public void enterData() throws IOException
{
// Initializing boolean variable isValid to check the number of clients user inputs
// and make sure it really IS a number
boolean isValid = false;
String number;
// Ask user how many clients he/she wishes to enter data for
do
{
number = JOptionPane.showInputDialog("Enter the number of clients you wish to save:");
// Loop to check what the user entered against the allowed charcters (only numbers between 0 and 9 allowed)
if (number.matches("[0-9]*"))
{
isValid = true;
}
else
{
isValid = false;
}
}
while (isValid != true); // Keeps prompting user for a number until a valid number between 0 and 9 is entered
// Finally, parse String number and change to an integer for use in data input loop
int numOfClients = Integer.parseInt(number);
// Data input prompts are put into a loop that repeats until data for all clients is entered
for (int k = 0; k
Sunday, 27 January 2013
Get user input using java swing and write to text file
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment