The problem is that I need to create new incremental ID for each new customer and add it to the Set, I'm trying to do it with the while loop, but it seems not to be right
public class Bank {
private String name;
private Set<Customer> customers = new HashSet<Customer>();
private Set<AbstractAccount> accounts = new HashSet<AbstractAccount>();
private Integer lastCustomerId = 0;
private Integer lastAccountId = 0;
public Integer addCustomer(String firstName, String lastName) {
// generate id from lastCustomerId and increment it
// add to Set
return lastCustomerId++;
}
public Integer addAccount(Integer ownerId, AccountType type) {
// add to Set
}
}
Im not sure if is that what you want...
public class Bank
{
private String name;
private Set<Customer> customers = new HashSet<Customer>();
private Set<AbstractAccount> accounts = new HashSet<AbstractAccount>();
private static int lastCustomerId = 0;
private static int lastAccountId = 0;
public static int GetNextCustomerID()
{
lastCustomerId++;
return lastCustomerId;
}
public static int GetNextAccountID()
{
lastAccountId++;
return lastAccountId;
}
public int addCustomer(String firstName, String lastName)
{
// generate id from lastCustomerId and increment it
int customerId = GetNextCustomerID();
// add to Set
}
public int addAccount(int ownerId, AccountType type)
{
// add to Set
int accountId = GetNextAccountID();
}
}
Related
I want to make card number of the customer is always set to -1 when a new customer is created.
Image of how code looks likes
public Customer(int cardNumber, int yearOfBirth) {
this.cardNumber = cardNumber;
this.yearOfBirth = yearOfBirth;
}
public int getCardNumber() {
return cardNumber;
}
public void setCardNumber(int cardNumber) {
this.cardNumber = cardNumber;
}
This is one way to do it. Add an initializer to the field declaration.
public class Customer {
private int cardNumber = -1;
private int yearOfBirth;
public Customer(int yearOfBirth) {
this.yearOfBirth = yearOfBirth;
}
public int getCardNumber() {
return cardNumber;
}
public void setCardNumber(int cardNumber) {
this.cardNumber = cardNumber;
}
}
Another alternative would be to explicitly initialize the field to its default value in the constructor; e.g.
public Customer(int yearOfBirth) {
this.yearOfBirth = yearOfBirth;
this.cardNumber = -1;
}
I'm currently a newbie on Java and im trying to figure out how to generate a sequential number whenever a user inputs into a scanner. Currently I've been baffling between using atomicint and atomiclong for their number generator . However, my point is that whenever a user-entered all the necessary data from the class (Id,name,email,phone..etc) , it will auto-assign that user as Guest_01 at the beginning of the console
(e.g Guest_01: s4842(ID),Jason Schlong(name),JasonSC234#gmail.com(email))
Is there a specific method to illustrate the output above?
(Please keep in mind that i need to generate the sequential number first before putting it into a scanner(sounds dumb but that's how i did it )
public class Main {
private static final int TOTAL_NUMBER_IN_SEQUENCE =20;
private static final int TOTAL_NUMBER_IN_THREAD =50;
public static void main(String[] args) {
Lead lead = new Lead(TOTAL_NUMBER_IN_THREAD,TOTAL_NUMBER_IN_SEQUENCE);
Thread t1 = new Thread(new SequenceGenerator(lead,1),"Lead_");
Thread t2 = new Thread(new SequenceGenerator(lead,3),"Lead_");
Thread t3 = new Thread(new SequenceGenerator(lead,2),"Lead_");
t1.start();
t2.start();
t3.start();
}
public class SequenceGenerator implements Runnable{
public Lead lead;
public int result;
public SequenceGenerator(Lead lead, int result) {
super();
this.lead = lead;
this.result = result;
}
public void run(){
lead.printNumber(result);
}
}
public class Lead {
private int number = 1;
private int NumberofThread;
private int totalNumbersequence;
public Lead(int numberofThread, int totalNumbersequence) {
super();
NumberofThread = numberofThread;
this.totalNumbersequence = totalNumbersequence;
}
public void printNumber(int resutl){
synchronized (this){
while(number < totalNumbersequence-1){
while (number % NumberofThread!= resutl){
try {
wait();
}catch (InterruptedException e){
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+" "+number++);
notify();
}
}
}
}
You don't need this multi-threaded approach. You can just maintain an integer and every time you create a guest you simply increment it.
static final class Guest {
private final int sequenceId;
private final String id;
private final String name;
private final String email;
Guest(int sequenceId, String id, String name, String email) {
this.sequenceId = sequenceId;
this.id = id;
this.name = name;
this.email = email;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public int getSequenceId() {
return sequenceId;
}
#Override
public String toString() {
return String.format("Guest_%s: %s, %s, %s",
sequenceId, id, name, email);
}
}
private Guest createGuest(int sequence) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter id:");
String id = scanner.nextLine();
System.out.println("Enter name:");
String name = scanner.nextLine();
System.out.println("Enter email:");
String email = scanner.nextLine();
return new Guest(sequence, id, name, email);
}
public static void main(String[] args) {
GuestGenerator generator = new GuestGenerator();
int sequence = 0;
while (sequence < 2) {
Guest guest = generator.createGuest(sequence++);
System.out.println(guest);
}
}
Guest_0: 0, jason0, email0
Guest_1: 0, jason1, email1
Create gets and sets methods:
getStudentName() and setstudentName()
getStudentNumber() and setStudentNumber()
I am confused on what to put in the public void printGrades() and printAverage()
import java.util.Scanner;
public class studentGrader
{
Scanner input = new Scanner(System.in);
private String studentName;
private String studentNumber;
private String[] testNames;
private int[] testGrades;
private int currentTestPointer;
private int maxTestCount = 10;
private int averageGrade;
private int testScore;
public studentGrader(String studentNameL,String studentNumberL)
{
studentName = studentNameL;
studentNumber = studentNumberL;
testNames = new String[maxTestCount];
testGrades = new int[maxTestCount];
currentTestPointer = 0;
averageGrade = 0;
}
public void addTest(String testName, int testScore)
{
testNames[currentTestPointer] = testName;
}
public void printGrades()
{
}
public void printAverage()
{
}
}
//Most Getters are very simple.
//The goal is to simply return some variable that is privately stored in a class
//Notice that the variable "studentName" is of type "String" and the method is returning a type "String"
public String getStudentName() {
return studentName;
}
//Most Setters are the same as getters, except they set the variable instead
//This method takes in a parameter(in this case "name") and then sets the desired variable to given parameter
public void setstudentName(String name) {
studentName = name;
}
The getStudentNumber and setStudentNumber are the same and I'll leave that left undone as a mental exercise.
//This is an example of how to print something
public void printAverage() {
//There may be additional logic in here to determine the correct average grade
system.out.print(averageGrade);
}
There are some rules from Java code style (that not flow to error, but they are used by everyone) along with some JVM defaults:
Class name should be in camel-case: class studentGrader - class StudentGrader
Scanner class is used to work with inbound data (console, files, ets); this is not a data holder. Should be removed from the class and used as local variable in method.
In constructor (usually), same name for local properties and method parameters should be used. To get access to the local properties, do use this: this.studentName = studentName
Arrays testNames and testGrades are objects with know size. So you should declare it in the class definition and make it final (reference is final, but not array's content): private final String[] testNames = new String[10]; and private final int[] testGrades = new int[10];
Class local parameters are initialized to the default values. For int it is a 0. So no need to do it in the constructor for currentTestPointer and averageGrade
void addTest(String testName, int testScore), in your current increment currentTestPointer and check for arrays out of bound (not more than 10) (I think, it is better to use Map)
averageGrade should be double (I think): private double averageGrade;``
Finally, your class could look like this:
public class StudentGrader {
private static final int MAX_TEST_AMOUNT = 10;
private String studentName;
private String studentNumber;
private final String[] testNames = new String[MAX_TEST_AMOUNT];
private final int[] testGrades = new int[MAX_TEST_AMOUNT];
private int currentTestPointer;
private double averageGrade;
private int testScore;
public StudentGrader(String studentName, String studentNumber) {
this.studentName = studentName;
this.studentNumber = studentNumber;
}
public void addTest(String testName, int testScore) {
if (currentTestPointer < MAX_TEST_AMOUNT) {
testNames[currentTestPointer] = testName;
testGrades[currentTestPointer] = testScore;
currentTestPointer++;
}
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getStudentNumber() {
return studentNumber;
}
public void setStudentNumber(String studentNumber) {
this.studentNumber = studentNumber;
}
public void printGrades() {
// <testName>: <testGrade>
for (int i = 0; i < currentTestPointer; i++)
System.out.println(testNames[i] + ": " + testGrades[i]);
}
public void printAverage() {
averageGrade = 0;
if (currentTestPointer > 0) {
for (int i = 0; i < currentTestPointer; i++)
averageGrade += testGrades[i];
averageGrade /= currentTestPointer;
}
System.out.println(averageGrade);
}
}
I take in a file which has a name (table) and the number of seats:
table1 6
table2 2
table3 4
I have an array of class Reservation which will take in the the name and the seat. I am having trouble converting the number of seats into the array. How do i go about doing so?
public class Reservable {
protected String id;
private Reservation[] arrayRes = new Reservation[10];
public Reservable (Scanner fileIn) {
while(fileIn.hasNext()) {
id = fileIn.next();
for(int i = 0; i < arrayRes.length; i++) {
int seat = fileIn.nextInt();
arrayRes[i] = seat;
}
}
}
here is my Reservation class:
public class Reservation {
private String name;
private int timeSlot;
private Reservable myReservable;
public Reservation(String name, int timeSlot) {
this.name = name;
this.timeSlot = timeSlot;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getTimeSlot() {
return timeSlot;
}
public void setTimeSlot(int timeSlot) {
this.timeSlot = timeSlot;
}
public Reservable getMyReservable() {
return myReservable;
}
public void setMyReservable(Reservable myReservable) {
this.myReservable = myReservable;
}
public boolean isActive() {
return false;
}
You can read line by line since your file has a reservation by line.
I propose you to have a Reservation constructor with two parameters (name and nbSeat).
A remark : you array of reservation has a fixed size : 10. If you file has more than 10 elements, a ArrayIndexOutOfBoundsException will be risen.
If the number of reservation may be superior to 10 or is variable, you should use a List rather than a array.
protected String id;
private Reservation[] arrayRes = new Reservation[10];
public Reservable (Scanner fileIn) {
int i=0;
while(fileIn.hasNextLine()) {
String line = fileIn.nextLine();
String[] token = line.split("\\s");
String name = token[0];
int nbSeat= Integer.valueOf(token[1)];
// add in your array
Reservation reservation = new Reservation(name,nbSeat);
arrayRes[i] = reservation ;
}
i++;
}
And Reservation :
public class Reservation{
public Reservation(String name, int nbSeat){
this.name=name;
this.nbSeat=nbSeat;
}
}
You need to show us what your Reservation class looks like, but if it uses the conventional getters and setters this is the correct way:
public Reservable (Scanner fileIn) {
while(fileIn.hasNext()) {
for(int i = 0; i < arrayRes.length; i++) {
int seat = fileIn.nextInt();
arrayRes[i].setSeat(seat);
id = fileIn.next();
arrayRes[i].setId(id);
}
}
}
I need to create a method with a default constructor, which sets name to an empty string and sets both credits and contactHours to zero. How to do it? Thanks, Pieter.
Methods don't have constructors... classes do. For example:
public class Dummy
{
private int credits;
private int contactHours;
private String name;
public Dummy()
{
name = "";
credits = 0;
contactHours = 0;
}
// More stuff here, e.g. property accessors
}
You don't really have to set credits or contactHours, as the int type defaults to 0 for fields anyway.
You're likely to want at least one constructor which takes initial values - in which case your parameterless one can delegate to that:
public class Dummy
{
private String name;
private int credits;
private int contactHours;
public Dummy()
{
this("", 0, 0);
}
public Dummy(String name, int credits, int contactHours)
{
this.name = name;
this.credits = credits;
this.contactHours = contactHours;
}
// More stuff here, e.g. property accessors
}
public class Test {
private String name;
private int credits;
private int contactHours;
public Test {
this( "", 0, 0);
}
public Test (String name, int credits, int contactHours) {
this.name = name;
this.credits = credits;
this.contactHours = contactHours;
}
// more code here
}
public class Bibabu{
private String name;
private int credits;
private int contactHours;
public Bibabu(){
name = ""; // you could also write this.name and so on...
credits = 0;
contactHours= 0;
}
// more code here
}
You don't need a constructor:
public class Dummy
{
private int credits = 0;
private int contactHours=0;
private String name="";
/*
public Dummy()
{
name = "";
credits = 0;
contactHours = 0;
}
*/
// More stuff here, e.g. property accessors
}
//constructor
public Account(int id, double balance, Person owner){
this.id = id;
this.balance = balance;
this.owner = owner;