Created
June 5, 2018 03:51
-
-
Save MenukaIshan/2af4d16dccbdf780d730fd8bf5ce4e56 to your computer and use it in GitHub Desktop.
A example code which created for a stackoverflow question https://stackoverflow.com/q/50691564/2940265
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com; | |
import java.io.File; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.util.LinkedList; | |
import java.util.List; | |
import java.util.Scanner; | |
import static java.lang.System.out; | |
public class Assignment | |
{ | |
public static void main(String[] args) | |
{ | |
try | |
{ | |
new | |
BankAccountApp("C:\\Users\\hSenid\\IdeaProjects\\JavaTest\\src\\dsds"); | |
} | |
catch (IOException ioe) | |
{ | |
out.printf("Missing file: %sC:/Users/USER/workspace/assignment/data/AssignmentData? \n\n", | |
new Assignment().getClass().getPackage().getName()); | |
} | |
} | |
} | |
class BankAccountApp | |
{ | |
private List<BankAccount> accounts; | |
public BankAccountApp(String filename) throws IOException | |
{ | |
readAccountData(filename); | |
displayMenu(); | |
int opt = 0; | |
try (Scanner input = new Scanner(System.in)) | |
{ | |
while (true) | |
{ | |
out.print("\nSelect an option: "); | |
opt = input.nextInt();// format exception if string | |
if (opt < 0 || opt > 7) | |
out.println("Invalid. Must be 1 - 7 (or 0 for menu)."); | |
else | |
break; | |
} | |
} | |
switch (opt) | |
{ | |
case 0: displayMenu(); break; | |
case 1: doDeposit(); break; | |
case 2: doWithdraw(); break; | |
case 3: displayHighestBalanceAccount(); break; | |
case 4: displayMostActiveAccount(); break; | |
case 5: displayAllAccounts("C:/Users/USER/workspace/assignment/data/AssignmentData"); break; | |
case 6: | |
{ | |
int total = getTotalTransactions(accounts); | |
out.println("Total transactions: " + total); | |
break; | |
} | |
case 7: farewell(); return; | |
} | |
} | |
public void readAccountData(String fn) throws IOException | |
{ | |
accounts = new LinkedList<>(); | |
Path path = new File("C:/Users/USER/workspace/assignment/data/AssignmentData").toPath(); | |
List<String> content = Files.readAllLines(path); | |
for(String line : content){ | |
String[] items = line.split(","); | |
String name = items[0]; | |
int age = Integer.valueOf(items[1]); | |
String accountID = items[2]; | |
double balance = Double.valueOf(items[3]); | |
int accountType = Integer.valueOf(items[4]); | |
BankAccount b = new BankAccount(name, age, accountID, balance, accountType); | |
accounts.add(b); | |
} | |
} | |
public void displayAllAccounts(String fn) throws IOException | |
{ | |
accounts = new LinkedList<BankAccount>(); | |
Path path = new File(fn).toPath(); | |
List<String> content = Files.readAllLines(path); | |
for(String line : content){ | |
String[] items = line.split(","); | |
String name = items[0]; | |
int age = Integer.valueOf(items[1]); | |
String accountID = items[2]; | |
double balance = Double.valueOf(items[3]); | |
int accountType = Integer.valueOf(items[4]); | |
BankAccount b = new BankAccount(name, age, accountID, balance, accountType); | |
accounts.add(b); | |
out.printf("---All Bank Accounts---\n"); | |
out.println("Name: "+name); | |
out.println("Age: "+age); | |
out.println("Account ID: "+accountID); | |
out.printf("Account Balance: %f",balance); | |
out.println("\nAccount Type: "+accountType+"\n"); | |
} | |
return; | |
} | |
public void displayHighestBalanceAccount() | |
{ | |
BankAccount mostMoney = accounts.get(3); | |
for(BankAccount b : accounts){ | |
if(mostMoney.getbalance() < b.getbalance()){ | |
mostMoney = b; | |
} | |
} | |
String name = mostMoney.getname(); | |
int age = mostMoney.getage(); | |
String accountID = mostMoney.getaccountID(); | |
double balance = mostMoney.getbalance(); | |
int accountType = mostMoney.accountType(); | |
out.printf("---Bank account with the highest balance---\n"); | |
out.println("Name: "+name); | |
out.println("Age: "+age); | |
out.println("Account ID: "+accountID); | |
out.printf("Account Balance: %f",balance); | |
out.println("\nAccount Type: "+accountType); | |
} | |
public void displayMostActiveAccount() | |
{ | |
} | |
public int getTotalTransactions(List<BankAccount> list) | |
{ | |
return 0; | |
} | |
public void doDeposit() | |
{ | |
out.print("Enter an account ID for deposit: "); | |
} | |
public void doWithdraw() | |
{ | |
} | |
public void farewell() | |
{ | |
out.println("Thanks for using the service. Bye!"); | |
} | |
public void displayMenu() | |
{ | |
out.println("*********************************"); | |
out.println("* Bank Account Operation Menu *"); | |
out.println("***********************************\n"); | |
out.println("0. Display Menu"); | |
out.println("1. Deposit"); | |
out.println("2. Withdraw"); | |
out.println("3. Display Highest Balance Account"); | |
out.println("4. Display Most Active Account"); | |
out.println("5. Display All Accounts"); | |
out.println("6. Display Total Number of Transactions"); | |
out.println("7. Exit"); | |
} | |
} | |
class BankAccount extends Person1 | |
{ | |
private String accountID; | |
private double balance; | |
private int accountType; | |
public BankAccount(String name, int age, String accountID, double balance, int accountType) | |
{ | |
super(name, age); | |
this.accountID = accountID; | |
this.balance = balance; | |
this.accountType = accountType; | |
} | |
public String getaccountID() { | |
return accountID; | |
} | |
public void setaccountID(String accountID) { | |
this.accountID = accountID; | |
} | |
public double getbalance() { | |
return balance; | |
} | |
public void setbalance(double balance) { | |
this.balance = balance; | |
} | |
public int accountType() { | |
return accountType; | |
} | |
public void setaccountType(int accountType) { | |
this.accountType = accountType; | |
} | |
} | |
class Person1 { | |
private String name; | |
private int age; | |
public Person1(String name, int age) | |
{ | |
this.name = name; | |
this.age = age; | |
} | |
public String getname() { | |
return name; | |
} | |
public void setname(String name) { | |
this.name = name; | |
} | |
public int getage() { | |
return age; | |
} | |
public void setage(int age) { | |
this.age = age; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment