Skip to content

Instantly share code, notes, and snippets.

@Viacheslav77
Last active March 9, 2016 09:51
Show Gist options
  • Select an option

  • Save Viacheslav77/328f5ce67feda7501b09 to your computer and use it in GitHub Desktop.

Select an option

Save Viacheslav77/328f5ce67feda7501b09 to your computer and use it in GitHub Desktop.
Написать программу «База данных пользователей» с функциями добавления, удаления, поиска и вывода информации о пользователе по номеру
package UserDataBase;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class InputUser {
public static class InputStrNum {
private String io;
Scanner s = new Scanner(System.in);
public String getIo(){
return io;
}
public String getName(String txt){
io=null;
while(io==null){
System.out.print("Enter user " + txt + " : ");
try {
setName();
} catch (TextExceptions e) {
System.out.println (e.getMessage());
}
}
return io;
}
public Date getDate(){
SimpleDateFormat sdf =new SimpleDateFormat("dd.mm.yyyy");
Date dt = null;
io=null;
while(io==null){
System.out.print("Enter your date of birth in the format dd.mm.yyyy ");
try {
setDate();
} catch (NumeralExceptions e) {
System.out.println(e.getMessage());
}
try{
dt=sdf.parse(io);
} catch(ParseException e){
io = null;
System.out.println("Wrong date input format :)");
}
}
return dt;
}
public Integer getCode(){
io=null;
while(io==null){
System.out.print("Enter a code (numbers) :");
try {
setDate();
} catch (NumeralExceptions e) {
io = null;
System.out.println(e.getMessage());
}
}
return Integer.parseInt(io);
}
public String setDate() throws NumeralExceptions{
io = s.next();
StringBuilder sb = new StringBuilder(io);
for(int i = 0; i < sb.length(); i++){
if (sb.charAt(i)>=97&&sb.charAt(i)<=122){
throw new NumeralExceptions (" Wrong date input format :)");
}
}
return io;
}
public void setName() throws TextExceptions {
io = s.next();
StringBuilder sb = new StringBuilder(io);
for(int i = 0; i < sb.length(); i++){
StringBuilder sb1 = new StringBuilder ("-0123456789");
for(int j = 0; j < sb1.length(); j++){
if (sb.charAt(i)==sb1.charAt(j)){
io = null;
throw new TextExceptions (" Enter the letters :)");
}
}
}
}
}
public static class TextExceptions extends Exception {
public TextExceptions (String message){
super(message);
}
@Override
public String getMessage(){
return "TextExceptions\n-----------------" + super.getMessage();
}
}
public static class NumeralExceptions extends Exception {
public NumeralExceptions (String message){
super(message);
}
@Override
public String getMessage(){
return "NumeralExceptions\n------------------" + super.getMessage();
}
}
}
package UserDataBase;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import UserDataBase.InputUser.InputStrNum;
public class MainMenu {
private static UserDataBase obj;
private static BufferedReader in;
public static void Menu(UserDataBase obj1) throws IOException {
obj=obj1;
System.out.println("------------------------------------------------");
System.out.println("User Data Base");
System.out.println("------------------------------------------------");
in = new BufferedReader(new InputStreamReader(System.in));
helpMenu();
try {
while (true) {
System.out.print("Your choice ( '?' helpmenu ): ");
String line = in.readLine();
if (line.equals("0")) {
System.out.println("---------------------------------------------------");
in.close();
return;
}
selectMenu(line );
}
} finally {
in.close();
}
}
public static void selectMenu(String line ) throws IOException {
InputStrNum inp = new InputStrNum();
switch (line) {
case "1":
obj.add(inp.getCode(),new Person(inp.getName("name"), inp.getName("surname"), inp.getDate()));
System.out.println("---------------------------------------------------");
break;
case "2":
obj.del(inp.getCode());
System.out.println("---------------------------------------------------");
break;
case "3":
obj.find(inp.getCode());
System.out.println("---------------------------------------------------");
break;
case "4":
obj.PrintDataBase();
System.out.println("---------------------------------------------------");
break;
case "?":
helpMenu();
break;
}
}
public static void helpMenu() {
StringBuilder sb = new StringBuilder();
sb.append("Main menu.\n");
sb.append("Enter:\n");
sb.append("-------------------------------\n");
sb.append("New user 1\n");
sb.append("Delite user 2\n");
sb.append("Find user 3\n");
sb.append("Print all Database users 4\n");
sb.append("Help menu ?\n");
sb.append("-------------------------------\n");
sb.append("Exit 0\n");
sb.append("-------------------------------\n");
System.out.println(sb.toString());
}
}
package UserDataBase;
import java.io.IOException;
/*Написать программу «База данных пользователей» с функциями добавления,
удаления, поиска и вывода информации о пользователе по номеру.*/
public class MyClass {
public static void main (String[] args) throws IOException, ClassNotFoundException {
UserDataBase udb = new UserDataBase ();
udb.getUserDB("d:\\1\\UserDataBase.txt");
udb.PrintDataBase();
MainMenu.Menu(udb);
udb.setUserDB();
}
}
package UserDataBase;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Person implements Serializable{
private static final long serialVersionUID = 1L;
private String name;
private String surname;
private Date birth;
private static SimpleDateFormat sdf = new SimpleDateFormat ("d.MM.yyyy");
public Person (String name , String surname, Date birth){
this.name = name;
this.surname = surname;
this.birth = birth;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getBirth() {
return sdf.format(birth);
}
public void setBirth(Date birth) {
this.birth = birth;
}
}
package UserDataBase;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
public class UserDataBase implements Serializable{
private static final long serialVersionUID = 1L;
private Map <Integer, Person> db = new HashMap <Integer, Person> ();
private String path;
public void add (Integer key, Person value){
if(db.put(key, value) == null)
System.out.println("User " + " Key ..." + key +" " + db.get(key).getName() + " " + db.get(key).getSurname() + " " + db.get(key).getBirth() +" added ...");
else
System.out.println("User " + db.get(key) + " NO added ...");
}
public Person get(Integer key) {
return db.get(key);
}
public int getListLength() {
return db.size();
}
public void del(Integer key){
if(db.containsKey(key)){
System.out.println("Delite user " + "Key ... " + key +" " + db.get(key).getName() + " " + db.get(key).getSurname() + " " + db.get(key).getBirth());
db.remove(key);
}
else
System.out.println("User is not in Database");
}
public void find(Integer key){
if(db.containsKey(key)){
System.out.println("User " + " Key ... " + key +" " + db.get(key).getName() + " " + db.get(key).getSurname() + " " + db.get(key).getBirth() + " in the Database");
}
else
System.out.println("User is not in Database");
}
public void PrintDataBase(){
System.out.println("Print All users:");
System.out.println("----------------------------------------------");
db.forEach(new BiConsumer <Integer,Person> (){
int i=1;
public void accept(Integer t , Person p) {
System.out.println(i++ + ". Key ... " + t +" " + p.getName() + " " + p.getSurname() + " " + p.getBirth());
}
});
System.out.println("------------------------------------------------");
System.out.println("Total ..." + db.size() + " users\n");
}
public void getUserDB (String path) throws FileNotFoundException, IOException, ClassNotFoundException {
this.path=path;
File f = new File(path);
System.out.println("Starts the user database ...");
if (f.isFile() && f.exists() && f.length()!=0){
try (ObjectInputStream inSt = new ObjectInputStream (new FileInputStream (f))){
db = (Map <Integer, Person>)inSt.readObject();
}
System.out.println("User Database has been deserialized from "+ f +"\n");
}
else {
f.createNewFile();
db = loadStandart();
}
}
public void setUserDB () throws FileNotFoundException, IOException {
ObjectOutputStream obout = new ObjectOutputStream (new FileOutputStream(path));
obout.writeObject(db);
System.out.println("User Database has been serialized to "+ path+"\n");
obout.flush();
obout.close();
}
private Map <Integer, Person> loadStandart() {
Integer [] ib = {123456, 234567,345678, 456789,567890};
Person [] p =
{
new Person ("Taras ", "Nehcay ", new Date(86, 01, 11)),
new Person ("Sveta ", "Barash ", new Date(70, 3, 28)),
new Person ("Valia ", "Zytiy ", new Date(86, (1-1),11)),
new Person ("Stepan ", "Nenash ", new Date(86, 01, 11)),
new Person ("Uriy ", "Botchay ", new Date(56, 10, 12))
};
System.out.println("-------------------------------------------------------------");
System.out.println("File " + path + " is not found. Create new file ... ");
System.out.println("Add users to Database: ");
System.out.println("-------------------------------------------------------------");
Map <Integer, Person> db1 = new HashMap <Integer, Person> ();
for(int i=0; i < ib.length; i++){
if(db1.put(ib[i], p[i]) == null)
System.out.println("User " + " Key ... " + ib[i] +" " + db1.get(ib[i]).getName() + " " + db1.get(ib[i]).getSurname() + " " + db1.get(ib[i]).getBirth() +" added ...");
else
System.out.println("User " + db1.get(ib[i]).getName() + " NO added ...");
}
System.out.println("-------------------------------------------------------------");
return db1;
}
}
Starts the user database ...
-------------------------------------------------------------
File d:\1\UserDataBase.txt is not found. Create new file ...
Add users to Database:
-------------------------------------------------------------
User Key ... 123456 Taras Nehcay 11.02.1986 added ...
User Key ... 234567 Sveta Barash 28.04.1970 added ...
User Key ... 345678 Valia Zytiy 11.01.1986 added ...
User Key ... 456789 Stepan Nenash 11.02.1986 added ...
User Key ... 567890 Uriy Botchay 12.11.1956 added ...
-------------------------------------------------------------
Print All users:
----------------------------------------------
1. Key ... 123456 Taras Nehcay 11.02.1986
2. Key ... 456789 Stepan Nenash 11.02.1986
3. Key ... 234567 Sveta Barash 28.04.1970
4. Key ... 567890 Uriy Botchay 12.11.1956
5. Key ... 345678 Valia Zytiy 11.01.1986
------------------------------------------------
Total ...5 users
------------------------------------------------
User Data Base
------------------------------------------------
Main menu.
Enter:
-------------------------------
New user 1
Delite user 2
Find user 3
Print all Database users 4
Help menu ?
-------------------------------
Exit 0
Your choice ( '?' helpmenu ): 1
Enter a code (numbers) :333333
Enter user name : Stasiy
Enter user surname : Pitbyle
Enter your date of birth in the format dd.mm.yyyy 11.01.1967
User Key ...333333 Stasiy Pitbyle 11.01.1967 added ...
---------------------------------------------------
Your choice ( '?' helpmenu ): 1
Enter a code (numbers) :Alexiy
NumeralExceptions
------------------ Wrong date input format :)
Enter a code (numbers) :777777
Enter user name : 999999
TextExceptions
----------------- Enter the letters :)
Enter user name : Alexiy
Enter user surname : 333333
TextExceptions
----------------- Enter the letters :)
Enter user surname : Titanic
Enter your date of birth in the format dd.mm.yyyy 11.we.ff00
NumeralExceptions
------------------ Wrong date input format :)
Wrong date input format :)
Enter your date of birth in the format dd.mm.yyyy 11.02.2009
User Key ...777777 Alexiy Titanic 11.01.2009 added ...
---------------------------------------------------
Your choice ( '?' helpmenu ): 4
Print All users:
----------------------------------------------
1. Key ... 333333 Stasiy Pitbyle 11.01.1967
2. Key ... 123456 Taras Nehcay 11.02.1986
3. Key ... 456789 Stepan Nenash 11.02.1986
4. Key ... 234567 Sveta Barash 28.04.1970
5. Key ... 567890 Uriy Botchay 12.11.1956
6. Key ... 777777 Alexiy Titanic 11.01.2009
7. Key ... 345678 Valia Zytiy 11.01.1986
------------------------------------------------
Total ...7 users
---------------------------------------------------
Your choice ( '?' helpmenu ): 2
Enter a code (numbers) :123456
Delite user Key ... 123456 Taras Nehcay 11.02.1986
---------------------------------------------------
Your choice ( '?' helpmenu ): 2
Enter a code (numbers) :Stasiy
NumeralExceptions
------------------ Wrong date input format :)
Enter a code (numbers) :333333
Delite user Key ... 333333 Stasiy Pitbyle 11.01.1967
---------------------------------------------------
Your choice ( '?' helpmenu ): 4
Print All users:
----------------------------------------------
1. Key ... 456789 Stepan Nenash 11.02.1986
2. Key ... 234567 Sveta Barash 28.04.1970
3. Key ... 567890 Uriy Botchay 12.11.1956
4. Key ... 777777 Alexiy Titanic 11.01.2009
5. Key ... 345678 Valia Zytiy 11.01.1986
------------------------------------------------
Total ...5 users
---------------------------------------------------
Your choice ( '?' helpmenu ): 3
Enter a code (numbers) :456789
User Key ... 456789 Stepan Nenash 11.02.1986 is in the Database
---------------------------------------------------
Your choice ( '?' helpmenu ): 3
Enter a code (numbers) :Valia
NumeralExceptions
------------------ Wrong date input format :)
Enter a code (numbers) :345687
User is not in Database
---------------------------------------------------
Your choice ( '?' helpmenu ): 345678
Your choice ( '?' helpmenu ):
Your choice ( '?' helpmenu ): 345678
Your choice ( '?' helpmenu ): 3
Enter a code (numbers) :345678
User Key ... 345678 Valia Zytiy 11.01.1986 in the Database
---------------------------------------------------
Your choice ( '?' helpmenu ): 4
Print All users:
----------------------------------------------
1. Key ... 456789 Stepan Nenash 11.02.1986
2. Key ... 234567 Sveta Barash 28.04.1970
3. Key ... 567890 Uriy Botchay 12.11.1956
4. Key ... 777777 Alexiy Titanic 11.01.2009
5. Key ... 345678 Valia Zytiy 11.01.1986
------------------------------------------------
Total ...5 users
---------------------------------------------------
Your choice ( '?' helpmenu ): 0
---------------------------------------------------
User Database has been serialized to d:\1\UserDataBase.txt
Starts the user database ...
User Database has been deserialized from d:\1\UserDataBase.txt
Print All users:
----------------------------------------------
1. Key ... 456789 Stepan Nenash 11.02.1986
2. Key ... 234567 Sveta Barash 28.04.1970
3. Key ... 567890 Uriy Botchay 12.11.1956
4. Key ... 777777 Alexiy Titanic 11.01.2009
5. Key ... 345678 Valia Zytiy 11.01.1986
------------------------------------------------
Total ...5 users
------------------------------------------------
User Data Base
------------------------------------------------
Main menu.
Enter:
-------------------------------
New user 1
Delite user 2
Find user 3
Print all Database users 4
Help menu ?
-------------------------------
Exit 0
-------------------------------
Your choice ( '?' helpmenu ): 0
---------------------------------------------------
User Database has been serialized to d:\1\UserDataBase.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment