samedi 11 juin 2016

Java: else if statement (binarysearch)

So I have spent a whole day working on a little java project and thought I should try asking here before I spend another three hours googling.

So what I am trying to do is use binary search in an if-else statement to access an array that is connected to a class somewhere else in the code.

and so you can see the code:

public class BankProject {

public static void main(String[] args) {

Account[] accountArray = new Account[10];
accountArray[0] = new Account("Dillen Newton", 5555, 9000000.0, 10.0);
accountArray[1] = new Account("Meteor", 5556, 10000.0, 5.5);
accountArray[2] = new Account("Meteorite", 5557, 20000.5, 20.0);
accountArray[3] = new Account("Super Mario", 5558, 100.0, 7.0);
accountArray[4] = new Account("A Person", 5559, 234567.0, 6.0);
accountArray[5] = new Account("Noone", 5560, 97498237.99, 50.0);
accountArray[0] = new Account("Yes", 5561, 5.5, 100.0);
accountArray[0] = new Account("Anonymous", 5562, 222.2, 13.0);
accountArray[0] = new Account("Five Hours", 5563, 7600.0, 40.0);
accountArray[0] = new Account("Who Knows", 5564, 9533.8, 99.0);

Scanner reader = new Scanner(System.in);  // Reading from System.in
System.out.println("Please Enter A Specified Name");
System.out.println("Dillen Newton");
System.out.println("Meteor");
System.out.println("Meteorite");
System.out.println("Super Mario");
System.out.println("A Person");
System.out.println("Noone");
System.out.println("Yes");
System.out.println("Anonymous");
System.out.println("Five Hours");
System.out.println("Who Knows");
System.out.println("");
String n = reader.next(); // Scans the next token of the input as an int.

if (n == "Dillen Newton"){
        Arrays.binarySearch(accountArray, "Dillen Newton");
        System.out.println("Welcome, Dillen");
} else if (n == "Meteor"){
        Arrays.binarySearch(accountArray, "Meteor");
        System.out.println("Welcome, Meteor!");     
} else if (n == "Meteorite"){
    Arrays.binarySearch(accountArray, "Meteorite");
    System.out.println("Hello, Meteorite!");        
} else if (n == "Super Mario"){
    Arrays.binarySearch(accountArray, "Super Mario");
    System.out.println("Welcome, Mario");       
} else if (n == "A Person"){
    Arrays.binarySearch(accountArray, "A Person");
    System.out.println("Hi Person!");       
} else if (n == "Noone"){
    Arrays.binarySearch(accountArray, "Noone");
    System.out.println("Welcome, Nobody!");     
} else if (n == "Yes"){
    Arrays.binarySearch(accountArray, "Yes");
    System.out.println("Yes");  
} else if (n == "Anonymous"){
    Arrays.binarySearch(accountArray, "Anonymous");
    System.out.println("Hello There!");     
} else if (n == "Five Hours"){
    Arrays.binarySearch(accountArray, "Five Houres");
    System.out.println("Has it been that long already?");       
} else if (n == "Who Knows"){
    Arrays.binarySearch(accountArray, "Who Knows");
    System.out.println("I don't");      
} else{
    System.out.println("Incorrect Account. Please restart and try again");
    reader.close();
}


    /*  Account testAccount = new Account("Dillen Newton", 1122, 20000.0, 4.5);
    testAccount.withdraw(2500);
    testAccount.deposit(3000);
    java.util.Date dateCreated = new java.util.Date();
    System.out.println("Date Created:" + dateCreated );
    System.out.println("Name:" + testAccount.getName());
    System.out.println("Account ID:" + testAccount.getId());
    System.out.println("Balance:" + testAccount.getBalance());
    System.out.println("Monthly Interest:" + testAccount.getMonthlyInterest());

    System.out.println("Process completed.");
    }
    */
} 

}

class Account {

private String name;
private int id;
private double balance;
private double annualInterestRate;
private Date dateCreated;

public Account(){
    name = "";
    id = 0;
    balance = 0.0;
    annualInterestRate = 0.0;
}

public Account(int newId, double newBalance){
    id = newId;
    balance = newBalance;
}

public Account(String newName, int newId, double newBalance, double newAnnualInterestRate) {
    name = newName;
    id = newId;
    balance = newBalance;
    annualInterestRate = newAnnualInterestRate;
}

public String getName(){
    return name;
}

public int getId(){
    return id;
}
public double getBalance(){
    return balance;
}
public double getAnnualInterestRate(){
    return annualInterestRate;
}

public void setName(String newName){
    name = newName;
}

public void setId(int newId){
    id = newId;
}
public void setBalance(double newBalance){
    balance = newBalance;
}
public void setAnnualInterestRate(double newAnnualInterestRate){
    annualInterestRate = newAnnualInterestRate;
}
public Date getDateCreated(){
    return dateCreated;
}
public double getMonthlyInterestRate(){
    return annualInterestRate / 12;
}
public double getMonthlyInterest(){
return (balance*getMonthlyInterestRate()/100);
}

public void withdraw(double amount)
{
    balance = balance - amount;
}

public void deposit(double amount)
{
    balance = balance + amount;
}
}

So I believe that everything so far is correct. But the if-else statement is only considering everything as Incorrect. I don't know if I need to try something else, or if I am just missing something really simple. Anyway any help would be appreciated!

Aucun commentaire:

Enregistrer un commentaire