...............
Printable View
...............
Code:import java.util.*;
public class CashierTest
{
public static void main(String[] args)
{
InputReader reader = new InputReader();
Cashier cashier = new Cashier();
cashier.generateResponse(Cashier.CREATE, reader);
}
}
class Cashier
{
HashMap<String, Account> accounts;
final static String CREATE = "create";
public Cashier()
{
accounts = new HashMap<String, Account>();
}
public void generateResponse(String word, InputReader reader)
{
if(word.equals(CREATE))
{
String email = reader.getText("enter email address: ");
String password = reader.getText("enter password: ");
if(accounts.containsKey(email)) {
System.out.println("The account already exists");
} else {
Account account = new Account(email, password);
accounts.put(email, account);
System.out.println("Your account has been created");
}
}
}
}
class InputReader
{
Scanner scanner = new Scanner(System.in);
public String getText(String prompt)
{
System.out.println(prompt);
return scanner.nextLine();
}
public int getNumber(String prompt)
{
System.out.println(prompt);
return scanner.nextInt();
}
}
why is this needed?
Quote:
class InputReader
{
Scanner scanner = new Scanner(System.in);
public String getText(String prompt)
{
System.out.println(prompt);
return scanner.nextLine();
}
public int getNumber(String prompt)
{
System.out.println(prompt);
return scanner.nextInt();
}
why is this needed?
Because an object of type InputReader is specified in the method signature of this method:
So we need an InutReader class to pass as an argument to the method.Code:public void generateResponse(String word, InputReader reader)
does any of this code do that already by any chance?Code:import java.io.InputStream;
import java.util.Scanner;
import java.util.*;
/**
*
*/
public class InputReader
{
private Scanner reader;
Scanner in=new Scanner(System.in);
//scanner object to get input from keyboard later
/**
* Create a new InputReader that reads text from the text terminal.
*/
public InputReader()
{
reader = new Scanner(System.in);
}
/**
* Create a new InputReader that reads text from an input stream.
* @param in An input stream to read from.
*/
public InputReader(InputStream in) {
reader = new Scanner(in);
}
It looks like it's headed in that direction, yes.
Excuse me, but what is the topic for this thread?:confused: