如何在Java中编写一个非常简单的用户登录?

huangapple 未分类评论47阅读模式
英文:

How to code a very simple user login in Java?

问题

//class attributes
String password;
int privilegeLevel;
boolean loggedIn;
Scanner console = new Scanner(System.in);

// ...

// Method to perform login
public boolean login() {
    System.out.println("Please enter your password.");
    String passwordAttempt = console.next();

    if (password.equals(passwordAttempt)) {
        System.out.println("Log in successful");
        loggedIn = true;
        return true;
    } else {
        loggedIn = false;
        return false;
    }
}

// ...

// Method to set the price of a Content object
public void setPrice(Content content, double newPrice) {
    if (loggedIn) {
        content.setContentPrice(newPrice);
        System.out.println("Price updated successfully.");
    } else {
        System.out.println("You need to log in first to set the price.");
    }
}

To use the setPrice() method, you can do something like this:

Admin a2 = new Admin("admin123", "Admin User", "password123", 2);
Book b1 = new Book("book123", "Sample Book", 19.99, "Sample Publisher", 200, new String[]{"Author 1", "Author 2"});

if (a2.login()) {
    b1.setPrice(b1, 14.25);
}

Please note that the setPrice() method now takes a Content object as its first parameter and the login status is checked within the method itself. Also, make sure to handle the import statements and other parts of the code structure appropriately in your Java application.

英文:

I'm doing a coding exercise where I'm basically building a little shop application and I'm very very new to Java

I'm having some trouble though, I want to write a method called setPrice() that allows a user to change the price of a Content object by logging in and declaring a price

This is the Admin class (it extends user, I'll include that too). The Admin class has some commented out code that I tried to use to invoke the login (I'm not sure this is the right way to go about this though:

import java.util.Scanner;

public class Admin extends User {
	
	//class attributes
	String password;
	int privilegeLevel;
	boolean loggedIn;
	Scanner console = new Scanner(System.in);
	
	public Admin(String id, String name, String pass, int level) {
		super(id, name);
		password = pass;
		privilegeLevel = level;
	}
	
	public String getPassword() {
		return password;
	}
	
	public int getLevel() {
		return privilegeLevel;
	}
	
//	public void login() {
//		System.out.println("Please enter your password.");
//		String passwordAttempt = console.next();
//		
//		if (password.contentEquals(passwordAttempt)) {
//			System.out.println("Log in successful");
//			loggedIn = true;
//		}else {
//			loggedIn = false;
//		}
//	}

}



public abstract class User {
	//Content Attributes
	private String ID;
	private String userName;
	
	public User(String id, String name) {
		ID = id;
		userName = name;
	}
	
	public void changeName(String newName) {
		this.userName = newName;
		
	}
	
	public String getUserID() {
		return ID;
	}
	
	public String getUsername() {
		return userName;
	}
	

	
}

The piece of code I've been given that's supposed that has to invoke the setPrice() method looks like this: b1.setPrice(a2.login(), 14.25);. Where b1 is a Book which is an extension of the Content class:

public abstract class Content {

	// Content Attributes
	private String contentName;
	private String contentID;
	private double contentPrice;
	private int downloadCount= 0;
	private Comment[] comments = new Comment[100];
	private int commentCount = 0;

	// Constructor
	public Content(String ID, String Name, double price) {
		this.contentID = ID;
		this.contentName = Name;
		this.contentPrice = price;

	}

	public Content(String ID, String Name) {
		this.contentID = ID;
		this.contentName = Name;
		this.contentPrice = 0;

	}
import java.util.ArrayList;

public class Book extends Publication{
	//attributes
	private String[] authorNames;
	
	
	// constructor
	public Book(String ID, String name, double price, String publisher, int numberPages, String[] authors1){
		super(ID, name, price, publisher, numberPages);
		this.authorNames = authors1;
	}

	// accessor methods
	public String[] getAuthorName() {
		return authorNames;
	}
	
	public String[] getAuthorNames() {
		return authorNames;
	}
	}

Basically I can work out what to write within the method, I just can't work out how to let the method accept a2.login() as a parameter?

答案1

得分: 0

你可以将你的login()代码修改如下:

public boolean isloggedIn() {
    System.out.println("请输入密码:");
    String passwordAttempt = console.next();

    if (password.contentEquals(passwordAttempt)) {
        System.out.println("登录成功");
        loggedIn = true;
    } else {
        loggedIn = false;
    }
    return loggedIn;
}

你的setPrice()方法修改如下:

void setPrice(boolean isLoggedIn, Double value) {
    if (isLoggedIn) {
        // 设置价格
    }
}

然后你可以调用:

b.setPrice(a2.isloggedIn(), 14.25);
英文:

You can modify your login() code as below :

public boolean isloggedIn() {
      System.out.println("Please enter your password.");
      String passwordAttempt = console.next();

      if (password.contentEquals(passwordAttempt)) {
          System.out.println("Log in successful");
          loggedIn = true;
      }else {
          loggedIn = false;
      }
      return loggedIn;
  }

And your setPrice() method as below :

void setPrice(boolean isLoggedIn, Double value)
  {
      if(isLoggedIn)
      {
          //set the price
      }
  }

Then you can call:

b.setPrice(a2.isloggedIn(), 14.25);

huangapple
  • 本文由 发表于 2020年5月29日 12:17:46
  • 转载请务必保留本文链接:https://java.coder-hub.com/62078646.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定