Skip to content

Instantly share code, notes, and snippets.

@3355844
Created May 24, 2016 11:11
Show Gist options
  • Save 3355844/97ef432ac355bd0ff676d12851da10f2 to your computer and use it in GitHub Desktop.
Save 3355844/97ef432ac355bd0ff676d12851da10f2 to your computer and use it in GitHub Desktop.
CrUD_Full_Version
package CRUD_2;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
/**
* Created by 33558 on 09.05.2016.
*/
public class CRUD_Runner {
public static final List<User> users = new ArrayList<>();
public static final String WAY_TO_USER_FILE = "C:/Users/33558/IdeaProjects/OOP/src/CRUD_2/fileUsers";
public static void main(String[] args) {
System.out.println("Program is begin :");
menuVoids();
readUsersFromUserFile();
while (true) {
args = consoleLineReader(args);
if (commandExit(args[0])) break;
usersVoids(args);
}
writeUsersToUserFile();
}
private static void menuVoids() {
System.out.println("For using write commands & information ");
System.out.println("For creating use; -c Name(Andrei) sex(true/false) age(29)");
System.out.println("For deleting use; -d id(1)");
System.out.println("For updating use; -d id(1) Name(Andrei) sex(true/false) age(29)");
System.out.println("For information use; -i id(1)");
System.out.println("For END use: exit");
}
private static boolean commandExit(String arg) {
if (arg.equalsIgnoreCase("exit")) {
System.out.println("END program.");
return true;
}
return false;
}
private static String[] consoleLineReader(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
String line = br.readLine();
args = line.split(" ");
return args;
} catch (IOException e) {
e.printStackTrace();
}
return args;
}
private static void usersVoids(String[] args) {
switch (args[0]) {
case "-c": {
System.out.println("Start creating new User");
System.out.println(args[1] + " " + args[2] + " " + args[3]);
createUserToUserList(args);
System.out.println("New User is create.");
break;
}
case "-d": {
System.out.println("Start deleting User - " + args[1]);
dellUserFromUserList(args[1]);
System.out.println("Deleting complete.");
break;
}
case "-u": {
System.out.println("Start update user:" + args[1]);
updateUser(args);
System.out.println("Updating complete.");
break;
}
case "-i": {
System.out.println("Here information about User: " + args[1]);
getUserInformation(args);
System.out.println("Information complete.");
break;
}
default: {
System.out.println("Unknown operation:");
break;
}
}
}
private static void getUserInformation(String[] userIdArgs) {
for (User user : users) {
if (user.getId() == Integer.parseInt(userIdArgs[1])) {
System.out.println(user.toString());
break;
}
}
}
private static void updateUser(String[] args) {
for (User user : users) {
if (user.id == Integer.parseInt(args[1])) {
user.setName(args[2]);
user.setSex(Boolean.parseBoolean(args[3]));
user.setAge(Integer.parseInt(args[4]));
System.out.println("User update to :" + user.toString());
break;
}
}
}
private static void writeUsersToUserFile() {
try {
File file = new File(WAY_TO_USER_FILE);
FileOutputStream outFile = new FileOutputStream(file, false);
String tempFile = "";
tempFile = getUsersToString(tempFile);
byte[] temp = tempFile.getBytes();
outFile.write(temp);
outFile.flush();
outFile.close();
} catch (IOException e) {
System.out.println("new User is not writing, try again User consist from command (-c name sex age)");
}
}
private static String getUsersToString(String tempFile) {
for (User user : users) {
tempFile += user.id + " " + user.getName() + " " + user.sex + " " + user.age + "\n";
}
return tempFile;
}
private static void readUsersFromUserFile() {
File file = new File(WAY_TO_USER_FILE);
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(
new FileInputStream(file), StandardCharsets.UTF_8))) {
String line;
while ((line = reader.readLine()) != null) {
String[] userArgs = line.split(" ");
createUserToUserList(userArgs);
}
} catch (IOException e) {
// log error
}
}
private static void dellUserFromUserList(String dellUserArgs) {
User user = users.remove(Integer.parseInt(dellUserArgs));
System.out.println(user.toString());
}
private static void createUserToUserList(String[] newUserArgs) {
User tempUser = new User();
tempUser.setId(User.nextID++);
tempUser.setName(newUserArgs[1]);
tempUser.setSex(Boolean.parseBoolean(newUserArgs[2]));
tempUser.setAge(Integer.parseInt(newUserArgs[3]));
users.add(tempUser);
}
}
0 Tom false 22
1 Tom false 55
2 Tom false 55
3 Tom false 55
4 Sam true 20
5 Den true 35
6 Julia_Zainka false 22
7 Tom false 22
8 Tom false 55
9 Tom false 55
10 Tom false 55
11 Sam true 20
12 Den true 35
13 Julia_Zainka false 22
package CRUD_2;
/**
* Created by 33558 on 09.05.2016.
*/
public class User {
static int nextID = 0;
String name;
boolean sex;
int age;
int id;
public User() {
}
public static void setNextID(int nextID) {
User.nextID = nextID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isSex() {
return sex;
}
@Override
public String toString() {
return id + " " + name + " " +
" " + sex +
" " + age;
}
public void setSex(boolean sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment