The constructor Client(String, String, String, String, String) is undefined - java

I have an assignment where I am supposed to simulate a management and billing application for a phone company. I created some classes, and one object for each class, but I get this error: "The constructor Client(String, String, String, String, String) is undefined"
Here are the relevant classes:
public class CreateUsers {
public static void main(String[] args) {
Client client = new Client("user1", "user", "user", "Client", "12345678");
Admin admin = new Admin("user2", "user", "user", "Admin");
Seller seller = new Seller("user3", "user", "user", "Seller");
}
}
public class Client extends Users {
String AFM;
public Client(String u, String n, String s, String p, String i){
this.AFM = i;
this.username = u;
this.name = n;
this.surname = s;
this.property= p;
UsersCount++;
}
public void ViewBill()
{
System.out.println("The bill");
}
public void ViewHistory()
{
System.out.println("The history");
}
public void PayBill()
{
System.out.println("Bill payment");
}
public String getAFM() {
return AFM;
}
}
public class Admin extends Users
{
public Admin() {
UsersCount++;
}
public void CreateSeller()
{
System.out.println("Seller was created!");
}
public void DeleteSeller()
{
System.out.println("Seller was deleted!");
}
public void CreateUser()
{
System.out.println("User was created!");
}
public void DeleteUser()
{
System.out.println("User was deleted!");
}
public void CreateNewProgram()
{
System.out.println("A new program was created!");
}
}
public class Seller extends Users
{
int salary;
String shift;
String pt_or_ft;
public void NewCustomer()
{
System.out.println("Customer was created!");
}
public void IssueBill()
{
System.out.println("Bill was issued!");
}
public void ChangeProgram()
{
System.out.println("Program was changed!");
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public String getShift() {
return shift;
}
public void setShift(String shift) {
this.shift = shift;
}
public String getPt_or_ft() {
return pt_or_ft;
}
public void setPt_or_ft(String pt_or_ft) {
this.pt_or_ft = pt_or_ft;
}
public Seller() {
UsersCount++;
}
}
public class Users
{
String username;
String name;
String surname;
String property;
static int UsersCount = 0;
static void register()
{
System.out.println("User registerd!");
}
static void login()
{
System.out.println("Login succesful!");
}
static void logout()
{
System.out.println("Logout succesful!");
}
public String getUserame()
{
return username;
}
public void setUsername(String newUsername)
{
this.username = newUsername;
}
public String getName()
{
return name;
}
public void setName(String newName)
{
this.name = newName;
}
public String getSurname()
{
return surname;
}
public void setSurname(String newSurname)
{
this.surname = newSurname;
}
public String getProperty()
{
return property;
}
public void setProperty(String newProperty)
{
this.property = newProperty;
}
public Users() {}
}
I am getting these errors in the CreateUsers class, in the lines where I declared the objects "admin" and "seller".
What am I doing wrong? How can I fix this?

instead of having the public static void main(String[] args) {}
you can have like a public fuction with a method that will include all your variables as parameters for example this is how I would have done it.
public class CreateUsers {
public ClientData(String Client, String Admin, String) {
Client client = new Client("user1", "user", "user", "Client", "12345678");
Admin admin = new Admin("user2", "user", "user", "Admin");
Seller seller = new Seller("user3", "user", "user", "Seller");
}
}

Related

Data integrity when using parallel stream

I have the implemented the below and I have not tested it yet with large amount of data.
public class Main {
public static void main(String[] args) {
CombinedData combinedData = new CombinedData();
List<String> nameList = Arrays.asList("x1", "x3", "x2");
nameList.parallelStream().forEach(name -> {
populateDetails(name, combinedData);
});
}
static void log(String str) {
System.out.println(str);
}
static void populateDetails(String name, CombinedData combinedData) {
if (combinedData.getOrg() == null) {
combinedData.setOrg(MyUtil.getOrg(name));
}
if (combinedData.getRawData() != null) {
combinedData.setRawData(combinedData.getRawData() + name);
}
combinedData.addToDetails(new Details(name, MyUtil.getAdd(name)));
}
}
class CombinedData {
private String org;
private List<Details> details;
private String rawData;
public String getOrg() {
return org;
}
public void setOrg(String org) {
this.org = org;
}
public void addToDetails(Details details) {
this.details.add(details);
}
public List<Details> getDetails() {
return details;
}
public void setDetails(List<Details> details) {
this.details = details;
}
public String getRawData() {
return rawData;
}
public void setRawData(String rawData) {
this.rawData = rawData;
}
}
class Details {
private String name;
private String address;
public Details(
String name,
String address) {
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
If you check the line 13 and 16 I need to add the org to combined response only once and I have added the check there and also I need to append the name to the rawData. My question is that since I'm using parallel stream how would that behave.
Im sure there might be a scenario that when threads are in parallel thing might break.
How can I go ahead and handle that scenario?

How to remove a user from an Array List?

package main;
import java.util.ArrayList;
public class UserGroup {
ArrayList<User> userGroup = new ArrayList<>();
User userOne;
public UserGroup() {
addUser(new User("lnb1g16", "Student", "Lee"));
addUser(new User("hpf1g17", "Staff", "Harry"));
addUser(new User("jks1g25", "Student", "Jordon"));
}
public void addUser(User inUser) {
//userGroup.add(new User("LeeB123", "Staff", "Lee"));
userGroup.add(inUser);
}
public ArrayList<User> getUserGroup() {
return userGroup;
}
public void removeFirstUser() {
userGroup.remove(0)
}
}
I've tried creating a method that removes the first user and seems to be compiling but does not actually remove the user when the program is compiled. Any ideas on how to fix this would be great, thanks.
Main method for calling the program
package main;
public class Main{
public static void main(String[] args) {
for (int counter=2; counter<=40; counter+=2) {
System.out.println(counter);
}
System.out.println("For loop complete.");
int counter = 1;
int increment = 2;
int loopexeccounter = 0;
while (counter <= 500) {
loopexeccounter = loopexeccounter + 1;
System.out.println(counter);
counter = counter + increment++;
}
System.out.print("This loop iteratted "+loopexeccounter+" times.\n");
{
//public User callUserGroup;
UserGroup userGroupObject = new UserGroup();
for (User curUser : userGroupObject.getUserGroup()) {
System.out.println(curUser.toString());
}
}
}
}
User method for declaring user's details
package main;
class User {
String username;
String userType;
String name;
User(String username, String userType, String name) {
this.username = username;
this.userType = userType;
this.name = name;
}
public String getUsername() {
return username;
}
public String getUserType() {
return userType;
}
public String getName() {
return name;
}
public String setUserType(String admin) {
return userType = admin;
}
#Override
public String toString() {
return username + " " + userType;
}
}
It's working good :
` public static void main(String[] args) {
UserGroup MyUser = new UserGroup();
for (User curUser : MyUser.getUserGroup()) {
System.out.println(curUser.toString());
}
MyUser.removeFirstUser();
System.out.println("your user removed!");
for (User curUser : MyUser.getUserGroup()) {
System.out.println(curUser.toString());
}
}`
RESULT :
`User#4554617c 1
User#74a14482 2
User#1540e19d 3
your user removed!
User#74a14482 1
User#1540e19d 2`

how to write junit test cases for Row Mapper class in spring

Hi This is my Row Mapper class.
public class UserRowMapper implements RowMapper<UserData> {
#Override
public UserData mapRow(ResultSet resultSet, int line) throws SQLException {
UserData userData = new UserData();
try
{
userData.setUserID(resultSet.getString("User_ID"));
userData.setUserName(resultSet.getString("User_Name"));
userData.setUserPassword(resultSet.getString("User_Password"));
userData.setUserRole(resultSet.getString("User_Role"));
userData.setUserStatus(resultSet.getString("User_Status"));
userData.setUserLogStatus(resultSet.getString("UserLog_Status"));
userData.setUserAccountName(resultSet.getString("User_AccountName"));
userData.setUserAccountID(resultSet.getString("User_AccountID"));
userData.setUserEmailID(resultSet.getString("User_EmailID"));
userData.setUserPasswordStatus(resultSet.getString("User_Password_ExpiryStatus"));
userData.setUserIDStatus(resultSet.getString("User_ID_Status"));
userData.setAcatTenantID(resultSet.getLong("acatTenant_ID"));
userData.setUserRoleCode(resultSet.getLong("User_Role_Code"));
userData.setUserSkillSetCode(resultSet.getLong("User_SkillSet_Code"));
userData.setUserAccountCode(resultSet.getLong("User_Account_Code"));
return userData;
}
catch (EmptyResultDataAccessException e)
{
return null;
}
}
}
and this is my Model class.
public class UserData {
private String userID;
private String userPassword;
private String userRole;
private String userStatus;
private String userLogStatus;
private String userName;
private String userAccountName;
private String userAccountID;
private String userIDStatus;
private String userPasswordStatus;
private String userEmailID;
private String userAdminID;
private String deactivationComment;
private String reqPageID;
private String userSessionID;
private String reqFunctionalityID;
private long userAccountCode;
private long userRoleCode;
private long userSkillSetCode;
private long acatTenantID;
public long getUserAccountCode() {
return userAccountCode;
}
public void setUserAccountCode(long userAccountCode) {
this.userAccountCode = userAccountCode;
}
public long getUserRoleCode() {
return userRoleCode;
}
public void setUserRoleCode(long userRoleCode) {
this.userRoleCode = userRoleCode;
}
public long getUserSkillSetCode() {
return userSkillSetCode;
}
public void setUserSkillSetCode(long userSkillSetCode) {
this.userSkillSetCode = userSkillSetCode;
}
public long getAcatTenantID() {
return acatTenantID;
}
public void setAcatTenantID(long acatTenantID) {
this.acatTenantID = acatTenantID;
}
public String getReqFunctionalityID() {
return reqFunctionalityID;
}
public void setReqFunctionalityID(String reqFunctionalityID) {
this.reqFunctionalityID = reqFunctionalityID;
}
public String getReqPageID() {
return reqPageID;
}
public void setReqPageID(String reqPageID) {
this.reqPageID = reqPageID;
}
public String getUserSessionID() {
return userSessionID;
}
public void setUserSessionID(String userSessionID) {
this.userSessionID = userSessionID;
}
public String getUserAdminID() {
return userAdminID;
}
public void setUserAdminID(String userAdminID) {
this.userAdminID = userAdminID;
}
public String getDeactivationComment() {
return deactivationComment;
}
public void setDeactivationComment(String deactivationComment) {
this.deactivationComment = deactivationComment;
}
public String getUserIDStatus() {
return userIDStatus;
}
public void setUserIDStatus(String userIDStatus) {
this.userIDStatus = userIDStatus;
}
public String getUserPasswordStatus() {
return userPasswordStatus;
}
public void setUserPasswordStatus(String userPasswordStatus) {
this.userPasswordStatus = userPasswordStatus;
}
public String getUserEmailID() {
return userEmailID;
}
public void setUserEmailID(String userEmailID) {
this.userEmailID = userEmailID;
}
public String getUserAccountID() {
return userAccountID;
}
public void setUserAccountID(String userAccountID) {
this.userAccountID = userAccountID;
}
public String getUserAccountName() {
return userAccountName;
}
public void setUserAccountName(String userAccountName) {
this.userAccountName = userAccountName;
}
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public String getUserRole() {
return userRole;
}
public void setUserRole(String userRole) {
this.userRole = userRole;
}
public String getUserStatus() {
return userStatus;
}
public void setUserStatus(String userStatus) {
this.userStatus = userStatus;
}
public String getUserLogStatus() {
return userLogStatus;
}
public void setUserLogStatus(String userLogStatus) {
this.userLogStatus = userLogStatus;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
The above classes are my Row Mapper and Model class.i do not know how to write junit test class for Row Mapper class .please any one guide me how to write junit test for these classes.
Hi here is my Junit test code for above classes.
public class UserRowMapperTest {
UserRowMapper userRowMapper=null;
#Before
public void runBeforeEachTest(){
userRowMapper= new UserRowMapper();
}
#After
public void runAfterEachTest(){
userRowMapper=null;
}
#Test
public void testMapRow(){
userRowMapper.mapRow(resultSet, line);
}
}
From my point of view there is nothing to test here.
You should create unit tests only for the methods which have some business logic. I don't see the reason to test the methods which are using just getters and setters because in general they don't do anything.
However, if you want just to practice this is the advice what you could do for the unit test. First of all check some questions on how to write the unit tests because it feels like you don't understand what you need/want to achieve.
In general this is the sketch of what you want:
#Test
public void testMapRow(){
// SETUP SUT
UserRowMapper userRowMapper = new UserRowMapper()
// fill (prepare) in the Object that you want to pass to a method.
ResultSet resultSet = createResultSet();
// EXERCISE
UserData resultData = userRowMapper.mapRow(resultSet, line);
// VERIFY
Assert.assertEquals(expectedValue, resultData.getSomeValue())
}
p.s. By the way, there is no point in line parameter in this method because you don't use it.
And about the NullPointerException, please, have a look to quite popular question about it.

OGNL setValue target is null

public class Customer {
private User user;
private String name;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public static void main(String[] args) {
try {
Customer customer = new Customer();
Object tree = Ognl.parseExpression("user.name");
Ognl.setValue(tree, customer, "hello");
} catch (OgnlException e) {
e.printStackTrace();
}
}
ognl.OgnlException: target is null for setProperty(null, "name", hello)
how to let ognl to create user auto.
Try this
public static void main(String[] args) {
try {
Customer customer = new Customer();
User user = new User();
customer.setUser(user);
Object tree = Ognl.parseExpression("user.name");
Ognl.setValue(tree, customer, "hello");
} catch (OgnlException e) {
e.printStackTrace();
}
}
The problem with your sample code is that your instance "customer" has a null user. So OGNL is essentially calling customer.getUser().setName("hello"), where "customer.getUser()" returns null.

checking login algorithm

i am currently stucked on how to create a login algorithm which will login a user based on 2 HashMap objects namely Students and StaffMembers from a class DataStorage, i do not know after getting the texts input from LoginHandler() what do i do with them to compare it with my DataStorage .
/* Class DataStorage*/
public class DataStorage
{
HashMap<String, Student> students = new HashMap<String, Student>();
HashMap<String, Staff> staffMembers = new HashMap<String, Staff>();
//Default constructor
public DataStorage(){
}
public void addStaffMember(Staff aAcc)
{
staffMembers.put(aAcc.getAccID(),aAcc);
}
public void addStudentMember(Student aAcc)
{
students.put(aAcc.getAccID(),aAcc);
}
public Staff getStaffMember(Staff aAcc)
{
return staffMembers.get(aAcc.getAccID());
}
public Student getStudent(Student aAcc)
{
return students.get(aAcc.getAccID());
}
public Account authUser(String user, String pass)
{
}
}
/* Class Account*/
public class Account {
private String name;
private String department;
private String username;
private String password;
private String accountID;
public Account()
{
}
public Account(String nm,String dep,String user,String pass, String accID)
{
name = nm;
department = dep;
username = user;
password = pass;
accountID = accID;
}
public void setName(String nm)
{
name = nm;
}
public String getName()
{
return name;
}
public void setDep(String d)
{
department = d;
}
public String getDep()
{
return department;
}
public void setUser(String u)
{
username = u;
}
public String getUser()
{
return username;
}
public void setPass(String p)
{
password = p;
}
public String getPass()
{
return password;
}
public void setAccID(String a)
{
accountID = a;
}
public String getAccID()
{
return accountID;
}
}
/* Class Staff extends account*/
public class Staff extends Account {
public Staff(String n, String id, String dep, String user, String pass)
{
super(n, dep, user, pass, id);
}
}
/** Class Student extends Account**/
public class Student extends Account {
private String studentNRIC;
public Student(String n, String nr, String id, String dep, String user, String pass)
{
super(n, dep, user, pass, id);
studentNRIC = nr;
}
public void setStudentNRIC(String nr)
{
studentNRIC = nr;
}
public String getStudentNRIC()
{
return studentNRIC;
}
}
/* class loginHandler which will handle a login button/
class LoginHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String tempUser;
String tempPass;
tempUser = txfUser.getText();
tempPass = txfPass.getText();
}
}
First of all you should change the getters in DataStorage to have String parameters, so that you can look up a student (or staff member) based on her account ID:
public Student getStudent(String aAcc)
{
return students.get(aAcc);
}
You can then lookup the username in actionPerformed, like
Student student = dataStorage.getStudent(tempUser);
if (student != null && student.getPass().equals(tempPass))
{
// login successful
...
} else {
// Login failed - display error message
}
Similarly for staff members.

Categories

Resources