Home › Forums › General Chat › Playing with Objects and Methods
- This topic has 3 replies, 4 voices, and was last updated 15 years, 5 months ago by
Jil.
-
AuthorPosts
-
29 August 2009 at 00:38 #3108
David
ParticipantWow, reading the odinms source actually did help with this crap. So next time you say developing private servers is a waste of time – not really. 😛
code said:
import java.util.Scanner;class CheckingAccount {
private String accountNumber, accountHolder;
private int balance = 0, useCount = 0;CheckingAccount( String accNumber, String holder, String password, int start ) {
accountNumber = accNumber ;
accountHolder = holder ;
balance = start ;
}int getBalance() {
return balance;
}void processDeposit( int amount ) {
balance += amount;
System.out.println(“Deposited ” + amount + ” into account.”);
System.out.println(“Balance is now ” + balance);
skipLine(2);
}void processDeposit (int amount, int charge) {
balance += (amount-charge);
System.out.println(“Deposited ” + amount + ” into account with a ” + charge + ” cent fee.”);
System.out.println(“Balance is now ” + balance);
skipLine(2);
}void processCheck( int amount ) {
int charge;
if ( balance < 100000 ) {
charge = 15;
} else {
charge = 0;
}
balance -= (amount + charge) ;
System.out.println(“Withdrew ” + amount + ” from account with a ” + charge + ” cent fee.”);
System.out.println(“Balance is now ” + balance);
skipLine(2);
}void display() {
System.out.println(“Account number: ” + accountNumber + ” || Owner: ” + accountHolder + ” || Current Balance: ” + getBalance());
skipLine(2);
}void skipLine(int c) {
for (int i = 0; i < c; i++) {
System.out.println(“”);
}
}void payOther(int amount, CheckingAccount c) {
processCheck(amount);
c.processDeposit(amount);
System.out.println(
“Paid ” + amount + ” to another account, current balance is now ” + balance +
“. Other person’s balance is now ” + c.getBalance() + “.”
);
skipLine(2);
}
}class methods {
int i = 0, status = 0, j = 0, response = 0;
String input = “”;Scanner scan = new Scanner( System.in );
CheckingAccount David = new CheckingAccount( “92a-44-35”, “David”, “123123”, 10000000 );
CheckingAccount Mei = new CheckingAccount( “92a-44-33”, “Mei”, “123123”, 0 );
CheckingAccount Feng = new CheckingAccount( “92a-44-34”, “Feng”, “123123”, 50000);
CheckingAccount currentAccount = null;
CheckingAccount destinedAccount = null;
String[] userNames = {“David”, “Mei”, “Feng”};
String[] passKeys = {“123123”, “123123a”, “123123a”};
String thisUser = “”, menuResponse = “”;public void login() {
System.out.println(“Please enter your account name:”);
String username = scan.next();
for (i = 0; i<userNames.length; i++) {
if (username.equalsIgnoreCase(userNames[i])) {
thisUser = userNames[i];
j = i;
status = 2;
}
}
}public void welcome() {
System.out.println(
“What would you like to access today? ” +
“rn1: account”
);
input = scan.next();
if (input.equalsIgnoreCase(“account”)) {
status = 1;
}
}public void checkPassword() {
String passKey = scan.next();
if (passKey.equalsIgnoreCase(passKeys[j])) {
System.out.println(“Account confirmed.”);
status = 3;
} else {
System.out.println(“Wrong password.”);
}
}public int getStatus() {
return status;
}public void mainMenu() {
System.out.println(“Main menu:rn1. Depositrn2. Withdrawrn3. Transfer Funds (xfer)rn4. Check balancern5. Logout”);
menuResponse = scan.next();
postMenu();
}public void postMenu() {
if (menuResponse.equalsIgnoreCase(“deposit”)) {
response = 1;
status = 4;
} else if (menuResponse.equalsIgnoreCase(“withdraw”)) {
response = 2;
status = 4;
} else if (menuResponse.equalsIgnoreCase(“xfer”)) {
response = 3;
status = 4;
} else if (menuResponse.equalsIgnoreCase(“balance”)) {
response = 4;
status = 4;
} else if (menuResponse.equalsIgnoreCase(“logout”)) {
response = 5;
status = 4;
} else {
System.out.println(“Invalid, please try another option.”);
mainMenu();
}
}public void accessAccount() {
if (thisUser.equalsIgnoreCase(“David”)) {
System.out.println(“Confirmed. Please enter your password:”);
currentAccount = David;
} else if (thisUser.equalsIgnoreCase(“Mei”)) {
System.out.println(“Confirmed. Please enter your password:”);
currentAccount = Mei;
} else if (thisUser.equalsIgnoreCase(“Feng”)) {
System.out.println(“Confirmed. Please enter your password:”);
currentAccount = Feng;
}
}public void redirection() {
if (response == 1) {
deposit();
} else if (response == 2) {
withdraw();
} else if (response == 3) {
xfer();
} else if (response == 4) {
checkThisBalance();
} else if (response == 5) {
logout();
}
}public void deposit() {
System.out.println(“How much would you like to deposit?”);
String amount = scan.next();
int thisAmount = Integer.parseInt(amount);
currentAccount.processDeposit(thisAmount);
status = 3;
mainMenu();
}public void withdraw() {
System.out.println(“How much would you like to withdraw?”);
String amount = scan.next();
int thisAmount = Integer.parseInt(amount);
currentAccount.processCheck(thisAmount);
status = 3;
mainMenu();
}public void xfer() {
System.out.println(“Who do you want to wire your money to?”);
String userInput = scan.next();
if (userInput.equalsIgnoreCase(“David”)) {
destinedAccount = David;
} else if (userInput.equalsIgnoreCase(“Mei”)) {
destinedAccount = Mei;
} else if (userInput.equalsIgnoreCase(“Feng”)) {
destinedAccount = Feng;
} else {
destinedAccount = currentAccount;
}
System.out.println(“Please enter the amount you want to wire over:”);
String amountString = scan.next();
int thisAmount = Integer.parseInt(amountString);
currentAccount.payOther(thisAmount, destinedAccount);
status = 3;
mainMenu();
}public void checkThisBalance() {
currentAccount.display();
status = 3;
mainMenu();
}public void logout() {
currentAccount = null;
System.out.println(“Logged off! Goodbye.”);
status = 0;
}}
class CheckingAccountTester {
public static void main( String[] args ) {
methods m = new methods();
int omg = 0;
while (omg == 0) {
if (m.getStatus() == 0) {
m.welcome();
}
if (m.getStatus() == 1) {
m.login();
m.accessAccount();
}
if (m.getStatus() == 2) {
m.checkPassword();
}
if (m.getStatus() == 3) {
m.mainMenu();
}
if (m.getStatus() == 4) {
m.redirection();
} else {
m.redirection();
}
}
}
}29 August 2009 at 09:40 #15727Reve
Participant-dies-
29 August 2009 at 15:47 #15728Pirkid
ParticipantI prefer Console myself.
2 September 2009 at 09:25 #15890Jil
ParticipantScrew objects. It’s all about continuations.
-
AuthorPosts
- You must be logged in to reply to this topic.