Untitled
unknown
plain_text
9 months ago
6.0 kB
12
Indexable
import javax.sound.midi.Soundbank;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class Main {
static ArrayList<user> users = new ArrayList<>();
public static ArrayList<String> splitaval(String a) {
ArrayList<String> javab = new ArrayList<>();
for (int i = 0; i < a.length(); i++) {
if (a.charAt(i) == ' ') {
javab.add(a.substring(0, i));
javab.add(a.substring(i + 1, a.length() - 1));
}
}
return javab;
}
public static void signup(String a) {
a = a.substring(7, a.length());
String name = splitaval(a).get(0);
String password = splitaval(a).get(1);
user newuser = new user(name, password, 0.0);
users.add(newuser);
}
public static int checklogin(String a, String b) {
for (int i = 0; i < users.size(); i++) {
if (users.get(i).name.equals(a)) {
if (users.get(i).password.equals(b)) {
return i;
}
return -1;
}
}
return -2;
}
public static void login(int userlocate){
user activeuser = users.get(userlocate);
Scanner scan = new Scanner(System.in);
while (true) {
String a = scan.nextLine();
if (a.equals("1")) {
System.out.println(activeuser.balance);
}
if (a.equals("2")) {
if (activeuser.history.size() == 0) {
System.out.println("No History");
continue;
}
ArrayList<tara> mova = activeuser.history;
for (int i = 0; i < mova.size(); i++) {
tara taramova = mova.get(i);
if (taramova.type.equals("deposit")) {
System.out.println("deposit" + ':' + ' ' + taramova.balance + " by " + taramova.first + " in " + '(' + taramova.date + ')');
} else if (taramova.type.equals("withdraw")) {
System.out.println("withdraw" + ':' + ' ' + taramova.balance + " in " + '(' + taramova.date + ')');
} else if (taramova.type.equals("transfer")) {
System.out.println("transfer" + ':' + ' ' + taramova.balance + "to" + taramova.first + " in " + '(' + taramova.date + ')');
}
}
}
if (a.equals("3")) {
System.out.println("Amount?");
double v = scan.nextInt();
scan.nextLine();
activeuser.balance = activeuser.balance + v;
tara newtara = new tara("deposit", v, "System");
activeuser.history.add(newtara);
users.set(userlocate, activeuser);
}
if (a.equals("4")) {
System.out.println("Amount?");
double v = scan.nextInt();
scan.nextLine();
if (activeuser.balance < v) {
System.out.println("Not enough balance");
continue;
}
activeuser.balance = activeuser.balance - v;
tara newtara = new tara("withdraw", -v, "System");
activeuser.history.add(newtara);
users.set(userlocate, activeuser);
}
if (a.equals("5")) {
System.out.println("Your target username?");
String a5 = scan.nextLine();
if (finduserlocation(a5) == -1) {
System.out.println("This account does not exist.");
continue;
}
user activeuser2 = users.get(finduserlocation(a5));
System.out.println("Amount?");
double v5 = scan.nextInt();
scan.nextLine();
if (activeuser.balance < v5) {
System.out.println("Not enough balance");
continue;
}
activeuser.balance = activeuser.balance - v5;
activeuser2.balance = activeuser2.balance + v5;
tara newtara5 = new tara("transfer", -v5, a5);
tara newtara52 = new tara("deposit", v5, activeuser.name);
activeuser.history.add(newtara5);
activeuser2.history.add(newtara52);
users.set(userlocate, activeuser);
users.set(finduserlocation(a5), activeuser2);
}
if (a.equals("6")) break;
else System.out.println("Wrong command");
}
}
public static int finduserlocation(String a) {
for (int i = 0; i < users.size(); i++) {
if (users.get(i).name.equals(a)) {
return i;
}
}
return -1;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (true) {
String a = scan.nextLine();
String b = a.substring(0, 5);
String c = a.substring(0, 6);
if (c.equals("signup")) {
signup(a);
continue;
}
if (b.equals("login")) {
a = a.substring(6, a.length());
String name = splitaval(a).get(0);
String password = splitaval(a).get(1);
if (checklogin(name, password) == -1) {
System.out.println("wrong password");
}
else if (checklogin(name, password) == -2) {
System.out.println("wrong username");
}
else {
int userlocate = checklogin(name, password);
login(userlocate);
}
}
}
}
}Editor is loading...
Leave a Comment