Return to Snippet

Revision: 61876
at January 21, 2013 23:55 by eiger824


Updated Code
package com.santi;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;

public class Main {

	
	public static void main(String[] args) {
		//declaration of arraylist-type variables
		ArrayList<String> names=new ArrayList<String>();
		ArrayList<String> last_names=new ArrayList<String>();
		ArrayList<String> phones=new ArrayList<String>();
		ArrayList<String> emails=new ArrayList<String>();
		//declaration of a Date-type variable
		Date date1 = new Date();
		//rest of auxiliary variables
		int cont=0;
		boolean a =true;
		//end of variable declarations
		System.out.println("Program started on " + date1);
		//main while loop
		while (a==true){
			if(names.size()==0 && last_names.size()==0 && phones.size()==0 && emails.size()==0) System.out.flush();
		System.out.println("Please select an option: ");
		String option2 = getInput("1. Add contact." +
				"2. Show contacts." +
				"3. Remove contact. 0. Exit program. : ");
		int opt=Integer.parseInt(option2);
		switch (opt) {
		case 1:
				names.add(getInput("Name: "));
				last_names.add(getInput("Last name: "));
				phones.add(getInput("Phone number: "));
				emails.add(getInput("Email: "));
			
			break;
		case 2:
			if (names.isEmpty() && last_names.isEmpty() && phones.isEmpty() && emails.isEmpty()) {
				System.out.println("There are no contacts yet!");
			}
			else {
				for (int i = 0; i < names.size(); i++) {
					System.out.println(i+1 + "- ");
					String aux_name = names.get(i);
					System.out.println(aux_name);
					String aux_last_name = last_names.get(i);
					System.out.println(aux_last_name);
					String aux_phone = phones.get(i);
					System.out.println(aux_phone);
					String aux_email = emails.get(i);
					System.out.println(aux_email);
				}

		}
			break;
			
		case 3:
			if (names.isEmpty() && last_names.isEmpty() && phones.isEmpty() && emails.isEmpty()) {
				System.out.println("There are no contacts yet!");
			}
			else if(names.size()==1){
				names.clear();
				last_names.clear();
				phones.clear();
				emails.clear();
				System.out.println("All contacts succesfully erased! No contacts in database anymore.");
				break;
			}
			else {
				cont=0;
				String option=getInput("1.REMOVE BY NAME. 2.REMOVE ALL. : ");
				int option3 = Integer.parseInt(option);
				switch (option3){
				case 1:
				String chc = getInput("Please enter the name of the contact you wish to erase: ");
				for (String name : names) {
					if (chc.equals(name)) {
						//remove all fields of data in that position
						names.remove(chc);
						last_names.remove(chc);
						phones.remove(chc);
						emails.remove(chc);
						System.out.println("Contact succesfully erased!");
						break;
					}cont++;
				}
				if (cont==names.size()) {
					System.out.println("WARNING: No matches between provided name and stored names. Remember: this program is case-sensitive!");
				}
				

				break;
				case 2:
					names.clear();
					last_names.clear();
					phones.clear();
					emails.clear();
					System.out.println("All contacts succesfully erased! No contacts in database anymore.");
					break;
				}
				break;
			}
			break;
		case 0:
			Date date2 = new Date();
			System.out.println("Program ended on " + date2);
			System.out.println("Developed by Santi Pagola.");
			a=false;
			break;
		}
		
	}
	
}

	private static String getInput(String prompt) {
		BufferedReader stdin = new BufferedReader(
				new InputStreamReader(System.in));
		
		System.out.print(prompt);
		System.out.flush();
		
		try {
			return stdin.readLine();
		} catch (Exception e) {
			return "Error: " + e.getMessage();
		}
	
	}

}

Revision: 61875
at January 21, 2013 23:14 by eiger824


Updated Code
package com.santi;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;

public class Main {

	
	public static void main(String[] args) {
		//declaration of arraylist-type variables
		ArrayList<String> names=new ArrayList<String>();
		ArrayList<String> last_names=new ArrayList<String>();
		ArrayList<String> phones=new ArrayList<String>();
		ArrayList<String> emails=new ArrayList<String>();
		//declaration of a Date-type variable
		Date date1 = new Date();
		//rest of auxiliary variables
		int cont=1;
		boolean a =true;
		//end of variable declarations
		System.out.println("Program started on " + date1);
		//main while loop
		while (a==true){
			if(names.size()==0 && last_names.size()==0 && phones.size()==0 && emails.size()==0) System.out.flush();
		System.out.println("Please select an option: ");
		String option2 = getInput("1. Add contact." +
				"2. Show contacts." +
				"3. Remove contact. 0. Exit program. : ");
		int opt=Integer.parseInt(option2);
		switch (opt) {
		case 1:
				names.add(getInput("Name: "));
				last_names.add(getInput("Last name: "));
				phones.add(getInput("Phone number: "));
				emails.add(getInput("Email: "));
			
			break;
		case 2:
			if (names.isEmpty() && last_names.isEmpty() && phones.isEmpty() && emails.isEmpty()) {
				System.out.println("There are no contacts yet!");
			}
			else {
				for (int i = 0; i < names.size(); i++) {
					System.out.println(i+1 + "- ");
					String aux_name = names.get(i);
					System.out.println(aux_name);
					String aux_last_name = last_names.get(i);
					System.out.println(aux_last_name);
					String aux_phone = phones.get(i);
					System.out.println(aux_phone);
					String aux_email = emails.get(i);
					System.out.println(aux_email);
				}

		}
			break;
			
		case 3:
			if (names.isEmpty() && last_names.isEmpty() && phones.isEmpty() && emails.isEmpty()) {
				System.out.println("There are no contacts yet!");
			}
			else {
				String option=getInput("1.REMOVE BY NAME. 2.REMOVE ALL. : ");
				int option3 = Integer.parseInt(option);
				switch (option3){
				case 1:
				String chc = getInput("Please enter the name of the contact you wish to erase: ");
				for (String name : names) {
					if (chc.equals(name)) {
						//remove all fields of data in that position
						names.remove(chc);
						last_names.remove(chc);
						phones.remove(chc);
						emails.remove(chc);
						System.out.println("Contact succesfully erased!");
						break;
					}cont++;
						
				}
				if (cont==names.size()+1) {
					System.out.println("WARNING: No matches between provided name and stored names. Remember: this program is case-sensitive!");
				}
				

				break;
				case 2:
					names.clear();
					last_names.clear();
					phones.clear();
					emails.clear();
					System.out.println("All contacts succesfully erased! No contacts in database anymore.");
					break;
				}
			}
			break;
		case 0:
			Date date2 = new Date();
			System.out.println("Program ended on " + date2);
			System.out.println("Developed by Santi Pagola.");
			a=false;
			break;
		}
		
	}
	
}

	private static String getInput(String prompt) {
		BufferedReader stdin = new BufferedReader(
				new InputStreamReader(System.in));
		
		System.out.print(prompt);
		System.out.flush();
		
		try {
			return stdin.readLine();
		} catch (Exception e) {
			return "Error: " + e.getMessage();
		}
	
	}

}

Revision: 61874
at January 21, 2013 21:23 by eiger824


Initial Code
package com.santi;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;

public class Main {

	
	public static void main(String[] args) {
		//declaration of arraylist-type variables
		ArrayList<String> names=new ArrayList<String>();
		ArrayList<String> last_names=new ArrayList<String>();
		ArrayList<String> phones=new ArrayList<String>();
		ArrayList<String> emails=new ArrayList<String>();
		//declaration of a Date-type variable
		Date date1 = new Date();
		//rest of auxiliary variables
		int num = 0;
		boolean a =true;
		System.out.println("Program started on " + date1);
		//main while loop
		while (a==true){
		System.out.println("Please select an option: ");
		String option2 = getInput("1. Add contact." +
				"2. Show contacts." +
				"3. Remove contact. 0. Exit program. : ");
		int opt=Integer.parseInt(option2);
		switch (opt) {
		case 1:
				names.add(getInput("Name: "));
				last_names.add(getInput("Last name: "));
				phones.add(getInput("Phone number: "));
				emails.add(getInput("Email: "));
			
			break;
		case 2:
			if (names.isEmpty() && last_names.isEmpty() && phones.isEmpty() && emails.isEmpty()) {
				System.out.println("There are no contacts yet!");
			}
			else {
				for (int i = 0; i < names.size(); i++) {
					System.out.println(i+1 + "- ");
					String aux_name = names.get(i);
					System.out.println(aux_name);
					String aux_last_name = last_names.get(i);
					System.out.println(aux_last_name);
					String aux_phone = phones.get(i);
					System.out.println(aux_phone);
					String aux_email = emails.get(i);
					System.out.println(aux_email);
				}

		}
			break;
			
		case 3:
			if (names.isEmpty() && last_names.isEmpty() && phones.isEmpty() && emails.isEmpty()) {
				System.out.println("There are no contacts yet!");
			}
			else {
			String chc = getInput("Please enter the name of the contact you wish to erase: ");
			for (String name : names) {
				if (chc.equals(name)) {
					int pos = names.indexOf(chc);
					//remove all fields of data in that position
					names.remove(pos);
					last_names.remove(pos);
					phones.remove(pos);
					emails.remove(pos);
					break;
				}
				else{ System.out.println("No matches between provided name and stored names. This program is case-sensitive!");break;}
					
			}
			}
			break;
		case 0:
			Date date2 = new Date();
			System.out.println("Program ended on " + date2);
			System.out.println("Developed by Santi Pagola.");
			a=false;
			break;
//			return;
		
		}
		
	}
	
}

	private static String getInput(String prompt) {
		BufferedReader stdin = new BufferedReader(
				new InputStreamReader(System.in));
		
		System.out.print(prompt);
		System.out.flush();
		
		try {
			return stdin.readLine();
		} catch (Exception e) {
			return "Error: " + e.getMessage();
		}
	
	}

}

Initial URL
http://programmingeiger824.blogspot.com

Initial Description
A basic database made in Java. It resembles a mobile phone contacts activity in which user is prompted to introduce some data of new contacts.

Initial Title
Basic Database in Java. Updated Version

Initial Tags
database, java

Initial Language
Java