Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class UserDataBase {
- private class User{
- public int account;
- public int password;
- public int balance;
- public String language;
- public User(int acc, int pass, int bal, String lang) {
- account = acc;
- password = pass;
- balance = bal;
- language = lang;
- }
- @Override
- public String toString() {
- super.toString();
- return account + "\t" + password + "\t" + balance + "\t" + language;
- }
- }
- private ArrayList<User> users = new ArrayList<User>();
- private String filename;
- public UserDataBase(String databasefile) {
- filename = databasefile;
- File file = new File(databasefile);
- if (!file.exists()) {
- // create the file
- try {
- FileWriter fw = new FileWriter(databasefile);
- BufferedWriter writer = new BufferedWriter(fw);
- writer.write("");
- writer.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return;
- }
- try {
- Scanner sc = new Scanner(file);
- sc.useDelimiter("\t");
- while (sc.hasNextLine()) {
- String line = sc.nextLine();
- String[] ray = line.split("\t");
- int account = Integer.parseInt(ray[0]);
- int password = Integer.parseInt(ray[1]);
- int balance = Integer.parseInt(ray[2]);
- String language = ray[3];
- User user = new User(account, password, balance, language);
- users.add(user);
- }
- } catch (FileNotFoundException e) {
- return;
- }
- }
- public User getUser(int acc){
- for (User user : users) {
- if (user.account == acc) {
- return user;
- }
- }
- return null;
- }
- public boolean findUser(int acc){
- User user = getUser(acc);
- if(users.contains(user)){
- return true;
- } else {
- return false;
- }
- }
- public int getBalance(int acc){
- User user = getUser(acc);
- if(findUser(acc)) {
- return user.balance;
- }else{
- System.out.println("User not found in database");
- return -1;
- }
- }
- public int getPassword(int acc){
- User user = getUser(acc);
- if(findUser(acc)) {
- return user.password;
- }else{
- System.out.println("User not found in database");
- return -1;
- }
- }
- public void addUser(int acc, int pass, int bal, String lang){
- if (findUser(acc)) {
- System.out.println("User is already in the database");
- }
- User user = new User(acc,pass,bal,lang);
- users.add(user);
- writeToFile();
- }
- public boolean removeUser(int account){
- User user = getUser(account);
- boolean result = users.remove(user);
- writeToFile();
- return result;
- }
- public boolean decreaseBalance(int account,int amount){
- User user = getUser(account);
- if (user == null) {
- return false;
- }
- user.balance = user.balance - amount;
- return true;
- }
- public boolean increaseBalance(int account,int amount){
- User user = getUser(account);
- if (user == null) {
- return false;
- }
- user.balance = user.balance + amount;
- return true;
- }
- public void writeToFile() {
- // loop through the users and write to file
- try {
- FileWriter fw = new FileWriter(filename);
- BufferedWriter writer = new BufferedWriter(fw);
- Boolean firstline = true;
- for (User user : users) {
- if (!firstline) {
- writer.newLine();
- }
- writer.write(user.toString());
- firstline = false;
- }
- writer.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment