Notes
Notes - notes.io |
import java.io.*;
import java.util.*;
public class VirtualBank {
private static int countofAccount = 0;
private int ID;
private double balance;
private String username;
private String password;
public VirtualBank(int ID) {
this.ID = ID;
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("virtualBankAccounts.txt"));
String line;
while ((line = reader.readLine()) != null) {
String[] accountData = line.split(",");
int accountID = Integer.parseInt(accountData[0]);
if (accountID == ID) {
this.username = accountData[1];
this.password = accountData[2];
this.balance = Double.parseDouble(accountData[3]);
break;
}
}
} catch (IOException | NumberFormatException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public VirtualBank(String username, String password) {
this.username = username;
this.balance = 0;
this.password = password;
this.createAccount();
}
private void createAccount() {
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter("virtualBankAccounts.txt", true));
int maxID = getMaxID();
this.ID = maxID + 1;
writer.write("n" + this.ID + "," + this.username + "," + this.password + "," + this.balance);
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (writer != null) {
writer.close();
this.ID = VirtualBank.countofAccount;
VirtualBank.countofAccount++;
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
public void deposit(double balance) {
this.setBalance(this.balance + balance);
this.information(true);
}
public void transfer(double thisBalance, VirtualBank toThisBank) {
if (thisBalance <= this.balance) {
this.setBalance(this.getBalance() - thisBalance);
toThisBank.setBalance(toThisBank.getBalance() + thisBalance);
this.information(true);
}
else {
this.information(false);
}
}
public void purchase(double balance) {
if (balance <= this.balance) {
this.setBalance(this.balance - balance);
this.information(true);
}
else {
this.information(false);
}
}
private void information(boolean OP) {
if (OP == true) {
System.out.println("Your transaction has been completed successfully");
}
else if (OP == false) {
System.out.println("Your transaction ended unsuccessfully");
}
else {
System.out.println("Unknown error");
}
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
writeToFile(this.ID, 1, this.getUsername());
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
writeToFile(this.ID, 2, this.getPassword());
}
public int getID() {
return this.ID;
}
public double getBalance() {
return balance;
}
private void setBalance(double balance) {
this.balance = balance;
String strValue = String.valueOf(this.balance);
writeToFile(this.ID, 3, strValue);
}
@Override
public String toString() {
return "Username: " + this.username + "nBalance: " + this.balance + "nPassword: " + this.password + "nID: " + this.ID;
}
private static int getMaxID() {
int maxID = 0;
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("virtualBankAccounts.txt"));
String line;
while ((line = reader.readLine()) != null) {
String[] accountData = line.split(",");
int accountID = Integer.parseInt(accountData[0]);
if (accountID > maxID) {
maxID = accountID;
}
}
}
catch (IOException | NumberFormatException e) {
e.printStackTrace();
}
finally {
try {
if (reader != null) {
reader.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
return maxID;
}
private void writeToFile(int ID, int index, String element) {
BufferedReader reader = null;
BufferedWriter writer = null;
ArrayList<String> data = new ArrayList<>();
try {
String line;
reader = new BufferedReader(new FileReader("virtualBankAccounts.txt"));
while ((line = reader.readLine()) != null) {
data.add(line);
}
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (reader != null) {
reader.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
try {
writer = new BufferedWriter(new FileWriter("virtualBankAccounts.txt"));
for (String rowData : data) {
String[] accountData = rowData.split(",");
if (Integer.parseInt(accountData[0]) == ID) {
switch (index) {
case 1:
accountData[1] = element;
break;
case 2:
accountData[2] = element;
break;
case 3:
accountData[3] = element;
break;
default:
System.out.println("Invalid index!");
break;
}
}
writer.write(String.join(",", accountData));
writer.newLine();
}
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (writer != null) {
writer.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team
