Untitled

 avatar
unknown
plain_text
a year ago
9.2 kB
2
Indexable
import java.util.HashMap;
import java.io.*;
import java.util.Map;
import java.util.Scanner;
import java.util.LinkedHashMap;

public class Accounting
{
    //HashMap anlegen mit Key für Benutzername und Value für PW
    String finalUsername;
    int finalId;
    
    InGame ingame;
    Map<String, String> linkedHashMap = new LinkedHashMap<>();
    Map<String, Integer> linkedHashMap2 = new LinkedHashMap<>();
    public Accounting()
    {
        load();
        
    }
    
    public void main(){
        clear();
        Scanner scanner = new Scanner(System.in);

        int auswahl = scanner.nextInt();

        switch (auswahl) {
            case 1:
                login();
                ingame = new InGame(finalUsername, finalId);
                side();
                break;
            case 2:
                register();
                main();
                break;
            default:
                System.out.println("Ungültige Auswahl.");
                main();
                break;
        }
    }
    
    public void side(){
        ingame.main();
    }
    
    public void loadHashMap() 
    {
        try (BufferedReader reader = new BufferedReader(new FileReader("!hashmap.txt"))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split(":");
                String key = parts[0].trim();
                String value = parts[1].trim();
                linkedHashMap.put(key, value);
            }
        } catch (IOException | NumberFormatException e) {
            e.printStackTrace();
        }
    }
    
    public void loadAccCoins() 
    {
        try (BufferedReader reader = new BufferedReader(new FileReader("!accdata.txt"))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split(":");
                String key = parts[0].trim();
                int value = Integer.parseInt(parts[1].trim());
                linkedHashMap2.put(key, value);
            }
        } catch (IOException | NumberFormatException e) {
            e.printStackTrace();
        }
    }
    
    public void saveHashMap()
    {
        //System.out.println("Saving data to file.");
        loadHashMap();
        try (BufferedWriter writer = new BufferedWriter(new FileWriter("!hashmap.txt"))) {
            for (Map.Entry<String, String> entry : linkedHashMap.entrySet()) {
                writer.write(entry.getKey() + ":" + entry.getValue());
                writer.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
            //System.out.println("Data not saved.");
        }
        //System.out.println("Data saved successfully.");
    }
    
    public void saveAccCoins() {
        //System.out.println("Saving data to file.");
        try (BufferedWriter writer = new BufferedWriter(new FileWriter("!accdata.txt"))) {
            for (Map.Entry<String, Integer> entry : linkedHashMap2.entrySet()) { 
                writer.write(entry.getKey() + ":" + entry.getValue());
                writer.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
            //System.out.println("Data not saved.");
        }
        //System.out.println("Data saved successfully.");
    }
    
    
    public void changeCoins(int index, int val){
        int currentIndex = 0;
        for (Map.Entry<String, Integer> entry : linkedHashMap2.entrySet()) {
            if (currentIndex == index) {
                String currentUsername = entry.getKey();
                int newValue = entry.getValue() + val;
                linkedHashMap2.put(currentUsername, newValue);
            }
            currentIndex++;
        }
        
    }
    
    public void changeAccPW(int index, String val){
        int currentIndex = 0;
        for (Map.Entry<String, String> entry : linkedHashMap.entrySet()) {
            if (currentIndex == index) {
                String currentUsername = entry.getKey();
                String newPW = val;
                linkedHashMap.put(currentUsername, val);
            }
            currentIndex++;
        }
    }
    
    public int getCoins(int index){
        int currentIndex = 0;
        for (Map.Entry<String, Integer> entry : linkedHashMap2.entrySet()) {
            if (currentIndex == index) {
                return entry.getValue();
            }
            currentIndex++;
        }
        System.out.println("not found. ");
        return -1;
    }
    
    public String getAccPW(int index){
        int currentIndex = 0;
        for (Map.Entry<String, String> entry : linkedHashMap.entrySet()) {
            if (currentIndex == index) {
                return entry.getValue();
            }
            currentIndex++;
        }
        System.out.println("not found. ");
        return "";
    }
    
    public void load(){
        loadHashMap();
        loadAccCoins();
    }
    
    public void save(){
        saveHashMap();
        saveAccCoins();
    }
    
    public void register()
    {
        load();
        clear();
        Scanner sc = new Scanner(System.in);
        System.out.println("username: ");
        String username = sc.nextLine();
        if(!checkUsername(username)){
            System.out.println("this username already exists.");
            delay(1000);
            register();
        }
        clear();
        System.out.println("password: ");
        String pw = sc.nextLine();
        clear();
        System.out.println("repeat your password: ");
        String pw2 = sc.nextLine();
        clear();
        if(pw.equals(pw2)){
            linkedHashMap.put(username, pw);
            linkedHashMap2.put(username, 0);
            System.out.println("your account has been registered. ");
            
            save();
        } else {
            System.out.println("the passwords do not match, try again. ");
            delay(1000);
            register();
        }
    }
    
    public void delete()
    {
        
    }
    
    public void login()
    {
        load();
        
        clear();
        Scanner sc = new Scanner(System.in);
        System.out.println("username: ");
        String username = sc.nextLine();
        if(checkUsername(username)){
            System.out.println("this account does not exist.");
            delay(1000);
            login();
        }
        clear();
        System.out.println("password: ");
        String pw = sc.nextLine();
        String pwcheck = linkedHashMap.get(username);
        if(pw.equals(pwcheck)){
            clear();
            System.out.println("you are now logged in @" + username + ".");
            finalUsername = username;
            finalId = getIndex(username);
        } else {
            clear();
            System.out.println("the password is incorrect, try again.");
            delay(1000);
            login();
        }
    }
    
    public void change()
    {
        load();
        
        clear();
        Scanner sc = new Scanner(System.in);
        System.out.println("current password: ");
        String pw = sc.nextLine();
        if(pw.equals(getAccPW(finalId))){
            System.out.println("enter your new password: ");
            String pw2 = sc.nextLine();
            System.out.println("enter your new password again: ");
            String pw3 = sc.nextLine();
            if(pw2.equals(pw3)){
                System.out.println("your password has been successfully changed. ");
            } else{
                System.out.println("the passwords do not match. ");
            }
        } else {
            clear();
            System.out.println("the password is incorrect.");
        }
    }
    
    //Überprüft ob der Username einmalig ist.
    public boolean checkUsername(String key){
        if (linkedHashMap.containsKey(key)) {
            return false;
        } else {
            return true;
        }
    }
    
    public int getIndex(String username){
        int index = 0;
        for (String bsp : linkedHashMap.keySet()) {
            if (bsp.trim().equals(username.trim())) {
                return index;
            }
            index++;
        }
        return -1;
    }
    
    //Überprüft ob der Username einmalig ist, aber mit einem DoubleKey.
    //public boolean checkUsernameSubKey(String key){
    //    for (DoubleKey dKey : hashMap.keySet()) {
    //        if (dKey.getSubKey1().equals(key)) {
    //            return false;
    //        }
    //    }
    //    return true;
    //}
    
    public void clear()
    {
        System.out.print('\u000C');    
    }
    
    public void delay(int ms)
    {
        try {
            Thread.sleep(ms);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}
Editor is loading...
Leave a Comment