Birincisi, Tarayıcı kullanarak yoluyla kimlik bilgilerini almak, burada Tarayıcı nesnesi oluşturmak için temel bir şekilde, her şeyden önce, senin kodun en başında aşağıdaki alma deyimi olması gerekir geçerli:
import java.util.Scanner;
yapın, bir Tarayıcı oluşturmak için: klavye olacak girdi akışından, okuma Tarayıcı söyler
Scanner scannerName = new Scanner(System.in);
. Tarayıcıdan veri almak için önce ihtiyaç duyduğunuz veriler için kullanıcıyı yönlendirin, daha sonra girişi almak ve bir değişkende saklamak için Scanner .next ___ yöntemlerinden birini kullanın. Hangisini kullanacağınızı söylemeyeceğim, Java API'sındaki Tarayıcı sayfasını kontrol edin ve kendi başınıza çözüp çözemeyeceğinizi görün.
Bu gibi görünmelidir:
System.out.println("Enter your name");
String userLoginString = scannerName.next____();
System.out.println("Enter your password");
String userPasswordString = scannerName.next____();
Dize değişkenleri saklanan kimlik bilgilerini aldıktan sonra, sana bazı saklanan değerlerin karşı bu kimlik bilgilerini doğrulamak gerekir, örnek olarak userLoginString ve userPasswordString kullanacağız. Yani, String değişkenleri bossName, bossPassword, workerName, workerPassword oluşturun.
Kullanıcının kimlik bilgilerini aldıktan sonra, bu giriş kimlik bilgilerinde doğrulama gerçekleştiririm.Çok gibi mantıksal operatörler ve String sınıfının yöntemlerini kullanarak bunu yapabilir:
if (userLoginString.equals(bossName) && userPasswordString.equals(bossPassword)) {
// print the boss menu
}
else if (userLoginString.equals(workerName) && userPasswordString.equals(workerPassword)) {
// print the user menu
}
mantıksal & & ("ve") operatörü doğru menü kullanıcının kimlik bilgileri eşleşen bu gösterilecektir sağlayacaktır kayıtlı kimlik bilgileri. Kullanıcı doğru adı (patron veya işçi) girerse ancak yanlış şifre girerse (veya tersi), parantez içindeki ifadeler İZİN VERMEZ.
GÜNCELLEME Burada şimdiye kadar daha iyi hale getirmek için nasıl bazı ipuçlarıyla kod yorumladı versiyonudur. Bu derlemek ve sadece üstündeki dize değişkenleri için değerler sağlamak eğer düzgün bir şekilde çalıştı ama ben biraz daha hoş hale getirmek için biraz daha öneri sahip olacaktır:
import java.util.Scanner;
public class Depot {
public static void main(String[] arguments){
// you need to initialize these to some value or else there is
// nothing to compare them with. I tried some dummy values and
// your code worked as expected, as long as the user entered the
// correct values in the prompt.
String bossName;
String bossPassword;
String workerName;
String workerPassword;
// you can just use one Scanner for the whole program, since they are
// both just reading input from the standard input stream. Replace the
// other Scanners with "input" and close "input" at the end
Scanner input = new Scanner(System.in);
System.out.println("Enter your name: ");
// not needed
Scanner authenticate = new Scanner(System.in);
String userName = authenticate.nextLine();
System.out.println("Your username is " + userName);
System.out.println("Enter your password: ");
String passWord = authenticate.nextLine();
System.out.println("Your password is " + passWord);
if (userName.equals(bossName) && passWord.equals(bossPassword)) {
// this could be declared at the top of the program instead of
// redeclaring in the if...else
int selection;
Scanner bossMenu = new Scanner(System.in);
System.out.println("1. Setup Worker Schedule");
System.out.println("2. View Worker Schedule");
System.out.println("3. Move Worker");
System.out.println("4. Quit");
do {
selection = bossMenu.nextInt();
if (selection == 1) {
System.out.println("1");
}
else if (selection == 2) {
System.out.println("2");
}
else if (selection == 3) {
System.out.println("3");
}
else {
System.out.println("4");
}
} while(selection != 4); // this is usually here
bossMenu.close();
}
else if (userName.equals(workerName) && passWord.equals(workerPassword)) {
// this could be declared at the top of the program instead of
// redeclaring in the if...else
int selection;
// don't need this one, just use "input" Scanner
Scanner userMenu = new Scanner(System.in);
System.out.println("1. View Worker Schedule");
System.out.println("2. Quit");
do {
selection = userMenu.nextInt();
if (selection == 1) {
System.out.println("1");
}
} while(selection != 2); // this is usually here
// you would close the "input" Scanner here
userMenu.close();
}
}
}
TEKRAR GÜNCELLEME !!! Patronu ve İşçiyi uygulamak için daha iyi bir yol, kalıtım ve polimorfizm kullanarak olacaktır. Patron ve İşçinin ortak özelliklerine sahip soyut bir süper sınıfı ile başlayın. Buna Çalışan Üst Sınıfı diyeceğim. Bu ad, soyadı ve şifre örnek değişkenler vardır ve her biri için Alıcılar ve ayarlayıcılar eklemek gerekir:
// abstract class, CANNOT be instantiated but can be used as the supertype
// in an ArrayList<Employee>
public abstract class Employee {
private String firstName;
private String lastName;
private String password;
public Employee() {
// don't have to do anything, just need this so you can instantiate
// a subclass with a no-arg constructor
}
// constructor that takes only the name of the Employee
public Employee(String firstName, String lastName) {
this(firstName, lastName, null);
}
// constructor that takes name and password
public Employee(String firstName, String lastName, String password) {
this.firstName = firstName;
this.lastName = lastName;
this.password = password;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
// and so on, for the lastName and password....
// you must implement this specifically in any subclass!
public abstract void getMenu();
}
Ardından, Boss ve İşçi sınıfları olabilir bu Çalışan sınıfını genişleten ve aynı yöntemlerin hepsi olurdu ve örnek değişkenler. Her biri, çalışan sınıfında soyut olduğu için, her ikisinde de geçersiz bir getMenu() yöntemi sağlamanız gerekir. İşte Boss sınıf gibi görünmelidir ne bir örnek, sen getMenu() uygulamak gerekir olduğunu kendiniz ve İşçi sınıfı: Eğer Çalışan, Worker ve Boss sınıfları sahip olduktan sonra
public class Boss extends Employee {
// notice we don't need the instance variables in the class declaration,
// but they are here since they are part of Employee
public Boss() {
// don't need to do anything here, just allows no-arg constructor
// to be called when creating a Boss
}
// just calls the superclass constructor, could do more if you want
public Boss(String firstName, String lastName) {
super(firstName, lastName);
}
// just calls the superclass constructor, could do more if you want
public Boss(String firstName, String lastName, String password) {
super(firstName, lastName, password);
}
@Override
public void getMenu() {
// put the print statment for Boss's menu here
}
// don't need to re-implement other methods, we can use them since
// they are part of the superclass
}
, hazırsın Daha önce yaptığınız gibi basit değişkenler yerine programınızı nesneler olarak yeniden denemek ve yeniden yazmak. İşte bunun nasıl başlayacağına dair bir örnek:
import java.util.Scanner;
public class EmployeeTester {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// can make workers and bosses able to be processed polymorphically
// by assinging their references to Employee variables, since
// and Employee is the supertype of each, a Worker "is an" Employee
// and a Boss "is an" Employee.
Employee worker1 = new Worker("Bob", "Worker");
Employee worker2 = new Worker("Sue", "Bush", "Password1");
Employee worker3 = new Worker();
Employee boss1 = new Boss("Jenny", "Boss");
Employee boss2 = new Boss("Bill", "OtherBoss", "Password2");
Employee boss3 = new Boss();
// if you're going to have a lot of workers and bosses, and you don't
// need named variables for each because their info will be included
// in their constructors, you could do this
Employee[] employees = {new Worker("Bob", "Bailey", "myPassword"),
new Worker("Sue", "Sarandon", "123Seven"),
new Boss("Jenny", "Strayhorn", "hardPassword"),
new Boss("Billy", "MeanGuy", "pifiaoanaei")};
// then, you could iterate through this list to check if a password
// entered matches a firstName, lastName, and password combination
// for ANY type of employee in the array, then call the getMenu()
// method on that employee, like so: (This could all be in a loop
// if you wanted to process multiple Employees...)
System.out.println("Enter firstName:");
// you figure out which Scanner method to use!
String firstName = input._____();
System.out.println("Enter lastName:");
String lastName = input._____();
System.out.println("Enter password:");
String password = input._____();
// figure out what get____() method of the Employee class
// needs to be called in each case, and what it should be
// compared to with the .equals() method.
for (int i = 0; i < employees.length; i++) {
if (employees[i].get______().equals(______) &&
employees[i].get______().equals(______) &&
employees[i].get______().equals(______)) {
// if all of those conditions are true, print the menu
// for this employee
employees[i].get_____();
// you could do more stuff here....
// breaks out of the loop, no need to check for anymore employees
break;
}
}
}
}
Oturum açma bilgilerini toplayın. Önemsiz olarak, kullanıcı adının kişinin bir Patron veya İşçi olduğunu göstermesine bağlı olarak menüyü görüntülemek için bir 'if' ifadesi kullanın. Aslında OO'yu kullanabilir ve işlemleri farklı sınıflara ayırabilirsiniz. Girişin bir parçası rolü seçmedikçe, Patron kimin hakkında bilgi sahibi olmanız gerekir. – KevinO
Hızlı yanıt için teşekkürler. Oturum açma bilgileri, Boss için bir kullanıcı adı ve şifre ve İşçi için bir kullanıcı adı ve şifre ile kodlanmış olabilir. Nereden başlayacağımı bilmediğim bir örnek gösterebilir miydin? –