Commit ef159dc3 authored by Yahia Khaled's avatar Yahia Khaled
Browse files

Java Email Application

parents
# JavaEmailApp
This is a simple text based user interface Java application to create and add emails to employees, print information and delete. Just run the app from the main method and choose the operation that you want to do
<?xml version="1.0" encoding="UTF-8"?>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.) -->
<!-- By default, only the Clean and Build commands use this build script. -->
<!-- Commands such as Run, Debug, and Test only use this build script if -->
<!-- the Compile on Save feature is turned off for the project. -->
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
<!-- in the project's Project Properties dialog box.-->
<project name="Email_application" default="default" basedir=".">
<description>Builds, tests, and runs the project Email_application.</description>
<import file="nbproject/build-impl.xml"/>
<!--
There exist several targets which are by default empty and which can be
used for execution of your tasks. These targets are usually executed
before and after some main targets. They are:
-pre-init: called before initialization of project properties
-post-init: called after initialization of project properties
-pre-compile: called before javac compilation
-post-compile: called after javac compilation
-pre-compile-single: called before javac compilation of single file
-post-compile-single: called after javac compilation of single file
-pre-compile-test: called before javac compilation of JUnit tests
-post-compile-test: called after javac compilation of JUnit tests
-pre-compile-test-single: called before javac compilation of single JUnit test
-post-compile-test-single: called after javac compilation of single JUunit test
-pre-jar: called before JAR building
-post-jar: called after JAR building
-post-clean: called after cleaning build products
(Targets beginning with '-' are not intended to be called on their own.)
Example of inserting an obfuscator after compilation could look like this:
<target name="-post-compile">
<obfuscate>
<fileset dir="${build.classes.dir}"/>
</obfuscate>
</target>
For list of available properties check the imported
nbproject/build-impl.xml file.
Another way to customize the build is by overriding existing main targets.
The targets of interest are:
-init-macrodef-javac: defines macro for javac compilation
-init-macrodef-junit: defines macro for junit execution
-init-macrodef-debug: defines macro for class debugging
-init-macrodef-java: defines macro for class execution
-do-jar: JAR building
run: execution of project
-javadoc-build: Javadoc generation
test-report: JUnit report generation
An example of overriding the target for project execution could look like this:
<target name="run" depends="Email_application-impl.jar">
<exec dir="bin" executable="launcher.exe">
<arg file="${dist.jar}"/>
</exec>
</target>
Notice that the overridden target depends on the jar target and not only on
the compile target as the regular run target does. Again, for a list of available
properties which you can use, check the target you are overriding in the
nbproject/build-impl.xml file.
-->
</project>
#Tue, 03 Aug 2021 15:09:49 +0300
C\:\\Users\\yahia\\Desktop\\Java_Email_App-master=
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build
This diff is collapsed.
build.xml.data.CRC32=b64ddf1f
build.xml.script.CRC32=cd3dcf30
build.xml.stylesheet.CRC32=f85dc8f2@1.95.0.48
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=b64ddf1f
nbproject/build-impl.xml.script.CRC32=a90b2045
nbproject/build-impl.xml.stylesheet.CRC32=f89f7d21@1.95.0.48
user.properties.file=C:\\Users\\yahia\\AppData\\Roaming\\NetBeans\\12.0\\build.properties
<?xml version="1.0" encoding="UTF-8"?>
<project-private xmlns="http://www.netbeans.org/ns/project-private/1">
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
</project-private>
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.processor.options=
annotation.processing.processors.list=
annotation.processing.run.all.processors=true
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
build.classes.dir=${build.dir}/classes
build.classes.excludes=**/*.java,**/*.form
# This directory is removed when the project is cleaned:
build.dir=build
build.generated.dir=${build.dir}/generated
build.generated.sources.dir=${build.dir}/generated-sources
# Only compile against the classpath explicitly listed here:
build.sysclasspath=ignore
build.test.classes.dir=${build.dir}/test/classes
build.test.results.dir=${build.dir}/test/results
# Uncomment to specify the preferred debugger connection transport:
#debug.transport=dt_socket
debug.classpath=\
${run.classpath}
debug.modulepath=\
${run.modulepath}
debug.test.classpath=\
${run.test.classpath}
debug.test.modulepath=\
${run.test.modulepath}
# Files in build.classes.dir which should be excluded from distribution jar
dist.archive.excludes=
# This directory is removed when the project is cleaned:
dist.dir=dist
dist.jar=${dist.dir}/Email_application.jar
dist.javadoc.dir=${dist.dir}/javadoc
dist.jlink.dir=${dist.dir}/jlink
dist.jlink.output=${dist.jlink.dir}/Email_application
excludes=
includes=**
jar.compress=false
javac.classpath=
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
javac.external.vm=true
javac.modulepath=
javac.processormodulepath=
javac.processorpath=\
${javac.classpath}
javac.source=14
javac.target=14
javac.test.classpath=\
${javac.classpath}:\
${build.classes.dir}
javac.test.modulepath=\
${javac.modulepath}
javac.test.processorpath=\
${javac.test.classpath}
javadoc.additionalparam=
javadoc.author=false
javadoc.encoding=${source.encoding}
javadoc.html5=false
javadoc.noindex=false
javadoc.nonavbar=false
javadoc.notree=false
javadoc.private=false
javadoc.splitindex=true
javadoc.use=true
javadoc.version=false
javadoc.windowtitle=
# The jlink additional root modules to resolve
jlink.additionalmodules=
# The jlink additional command line parameters
jlink.additionalparam=
jlink.launcher=true
jlink.launcher.name=Email_application
main.class=Application.Main
manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=false
platform.active=default_platform
run.classpath=\
${javac.classpath}:\
${build.classes.dir}
# Space-separated list of JVM arguments used when running the project.
# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
# To set system properties for unit tests define test-sys-prop.name=value:
run.jvmargs=
run.modulepath=\
${javac.modulepath}
run.test.classpath=\
${javac.test.classpath}:\
${build.test.classes.dir}
run.test.modulepath=\
${javac.test.modulepath}
source.encoding=UTF-8
src.dir=src
test.src.dir=test
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.java.j2seproject</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/j2se-project/3">
<name>Email_application</name>
<source-roots>
<root id="src.dir"/>
</source-roots>
<test-roots>
<root id="test.src.dir"/>
</test-roots>
</data>
</configuration>
</project>
package Application;
import java.util.Random;
/**
*
* @author yahia
*/
public class Email {
private Employee worker;
private String password;
private int defaultPasswordLength;
private String companySuffix;
//Constructer to receive the new employee
public Email(Employee worker) {
this.worker = worker;
this.defaultPasswordLength = 10;
this.password = randomPassword(defaultPasswordLength);
this.companySuffix = "company.com";
}
//Creating email for new worker
private String createEmail(){
return worker.getFirstName().toLowerCase() + "." + worker.getLastName().toLowerCase() + "@" +
worker.getDepartment().toLowerCase() + "." + this.companySuffix;
}
//Generate a random password for the email
private String randomPassword(int length){
String characters = "aBcDeFGHijKlmNpqrSTuVwXyZ£$%^&*123456789";
int charactersLength = characters.length();
Random random = new Random();
String randomPassword = "";
char[] passwordText = new char[length];
for(int i = 0; i < length; i++){
passwordText[i] = characters.charAt(random.nextInt(charactersLength));
randomPassword += passwordText[i];
}
return randomPassword;
}
//Change password
public void changePassword(String newPassword){
this.password = newPassword;
}
public String getPassword() {
return password;
}
//This method to be used later in other class
public String emailFirstName(){
return worker.getFirstName();
}
//This method to be used later in other class
public String emailLastName(){
return worker.getLastName();
}
//Employee and email info
@Override
public String toString() {
return worker.toString() + "\n"+
"The email is: " + createEmail() + "\n" +
"The password is: " + password;
}
}
package Application;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
*
* @author yahia
*/
public class EmailBook {
private Employee employee;
private List<Email> emails;
private Scanner reader;
public EmailBook() {
this.reader = reader;
this.employee = employee;
this.emails = new ArrayList<Email>();
}
//Rigestering email for the new workers
public void add(Employee worker){
this.emails.add(new Email(worker));
}
//Getting information about specific worker
public String info(String workerFistName, String workerLastName){
for(Email email : emails){
if(email.emailFirstName().equals(workerFistName) && email.emailLastName().equals(workerLastName)){
return email.toString();
}
}
return "Not found";
}
//Printing all information about workers that have been added
public void printAll(){
for(Email email : emails){
System.out.println(email.toString());
System.out.println("");
}
}
//Deleting an employee using his first and last name
public void deleting(String workerFistName, String workerLastName){
for(Email email : emails){
if(email.emailFirstName().equals(workerFistName) && email.emailLastName().equals(workerLastName)){
this.emails.remove(email);
}
return;
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Application;
import java.util.Scanner;
/**
*
* @author yahia
*/
public class Employee {
private String firstName;
private String lastName;
private String department;
private String alternateEmail;
//Constructer to receive first name and last name
public Employee(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.alternateEmail = setAlternateEmail();
this.department = setDepartment();
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getDepartment() {
return department;
}
public String getAlternateEmail() {
return alternateEmail;
}
//Ask for department
private String setDepartment(){
System.out.print("Department codes\n1 for Sales\n2 for Development\n3 for Accounting\n0 for None\nEnter department code: ");
Scanner reader = new Scanner(System.in);
String departmentChoice = reader.nextLine();
if(departmentChoice.equals("1")){
return "sales";
}else if(departmentChoice.equals("2")){
return "development";
}else if(departmentChoice.equals("3")){
return "accounting";
}else {
return "";
}
}
//Set the alternate email
private String setAlternateEmail(){
System.out.print("Enter an alternate email: ");
Scanner reader = new Scanner(System.in);
String altEmail = reader.nextLine();
return altEmail;
}
//Employee info
@Override
public String toString() {
return "Employee's name: " + firstName + " " + lastName + "\n"+
"Alternate email: " + this.getAlternateEmail() + "\n" +
"Department: " + this.getDepartment();
}
}
\ No newline at end of file
package Application;
import java.util.Scanner;
/**
*
* @author yahia
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
TextUserInterface ui = new TextUserInterface(reader);
ui.start();
}
}
package Application;
import java.util.Scanner;
import java.util.Map;
import java.util.TreeMap;
/**
*
* @author yahia
*/
public class TextUserInterface {
private Scanner reader;
private Map<String, String> commands;
private Email email;
private EmailBook emails;
private Employee worker;
//The constructor
public TextUserInterface(Scanner reader) {
this.reader = reader;
emails = new EmailBook();
commands = new TreeMap<String, String>();
commands.put("1", "press 1 to create an email for a new employee.");
commands.put("2", "press 2 to search info about specific employee.");
commands.put("3", "press 3 to delete an employee.");
commands.put("4", "press 4 to print all info for all adedd employees.");
commands.put("x", "press x to exit.");
}
//Printing instructions
private void printInstructions(){
for(String command : commands.values()){
System.out.println(" " + command);
}
}
//Creating an email
private void createEmail(){
System.out.print("Employee's first name: ");
String firstName = reader.nextLine();
System.out.print("Employee's Last name: ");
String lastName = reader.nextLine();
Employee newWorker = new Employee(firstName, lastName);
this.emails.add(newWorker);
System.out.println("---------------------------------");
}
//Searching for an employee's info
private void searchInfo(){
System.out.print("Employee's first name: ");
String firstName = reader.nextLine();
System.out.print("Employee's last name: ");
String lastName = reader.nextLine();
System.out.println( emails.info(firstName, lastName));
System.out.println("---------------------------------");
}
////Deleting an employee's info
private void delete(){
System.out.print("Employee's first name: ");
String firstName = reader.nextLine();
System.out.print("Employee's last name: ");
String lastName = reader.nextLine();
this.emails.deleting(firstName, lastName);
System.out.println("---------------------------------");
}
//Printing all info that have been added
private void printAll(){
this.emails.printAll();
}
//Starting our text based user interface
public void start(){
System.out.println("Welcome to our email application.");
System.out.println();
while(true){
System.out.println("Choose an operation.");
printInstructions();
System.out.println("");
System.out.print("command: ");
String command = reader.nextLine();
if(!commands.keySet().contains(command)){
System.out.println("invalid command.");
printInstructions();
}
if(command.equals("x")){
System.out.println("The following employees have been added:");
System.out.println("");
printAll();
System.out.println("Thank you for using our application.");
break;
}
if(command.equals("1")){
createEmail();
}
if(command.equals("2")){
searchInfo();
}
if(command.equals("3")){
delete();
}if(command.equals("4")){
printAll();
}
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment