Refactor a stateful class - java

I have a stateful class in java
class Container {
int capacity;
String name;
public int getCapacity() {
return capacity;
}
public void consumeContainer(int numberOfUnits) {
this.capacity = this.capacity - numberOfUnits;
}
}
I am looking for options to refactor this so I don't have to mutate the class everytime the container is consumed. Can the experts throw in ideas on how to do that?
Thanks

If you want to make your class immutable, this is an option:
class ImmutableContainer {
final int capacity;
final String name;
public ImmutableContainer(int capacity, String name) {
this.capacity = capacity;
this.name = name;
}
public int getCapacity() {
return capacity;
}
public ImmutableContainer consumeContainer(int numberOfUnits) {
return new ImmutableContainer(this.capacity - numberOfUnits, name);
}
}
Or, even simpler, as a record:
record ImmutableContainer(int capacity, String name) {
public ImmutableContainer consumeContainer(int numberOfUnits) {
return new ImmutableContainer(this.capacity - numberOfUnits, name);
}
}

Related

Have a Queue with Generics implementation print specific Object attributes

I have created a simple Queue of type which is also contains a print() function to it.
public class ArrayQueue implements Queue {
private T[] theArray;
private int currentSize;
private int front;
private int back;
private static final int DEFAULT_CAPACITY = 10;
public ArrayQueue() {
theArray = (T[]) new Object[DEFAULT_CAPACITY];
currentSize = 0;
front = 0;
back = -1;
}
public boolean isEmpty() {
return currentSize == 0;
}
public T dequeue() throws EmptyQueueException {
if (isEmpty())
throw new EmptyQueueException("ArrayQueue dequeue error");
T returnValue = theArray[front];
front = increment(front);
currentSize--;
return returnValue;
}
public void enqueue(T x) {
if (currentSize == theArray.length)
doubleQueue();
back = increment(back);
theArray[back] = x;
currentSize++;
}
private int increment(int x) {
if (++x == theArray.length)
x = 0;
return x;
}
public void print() {
if (isEmpty()) {
System.out.printf("Empty queue\n");
return;
}
System.out.printf("The queue is: ");
for (int i = front; i != back; i = increment(i)) {
System.out.print(theArray[i] + " ");
}
System.out.print(theArray[back] + "\n");
}
I have also created a Song object with 3 variables
public class Song {
private int id;
private String name;
private int likes;
public Song() {
this(1,"Test",10);
}
public Song(int id,String name, int likes) {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getLikes() {
return likes;
}
public void setLikes(int likes) {
this.likes = likes;
}
Is there a way modify this function in order to print a specific object's information or do i need to write a different print method during my implementation?
For example i would like my Print method to show all the objects variables , if i call just like this is will only get the object pointer
ArrayQueue<Song> arrayQueue = new ArrayQueue<Song>();
Queue<Song> queue = arrayQueue; //arrayQueue instance is also a Queue
Song s = new Song();
arrayQueue.enqueue(s);
arrayQueue.print();
Result is
The queue is: Song#15db9742
My modification would print :
The queue is : 1 Test 10
You need to override the toString() method of Song.
For example, add this to Song:
#Override
public String toString() {
return id + " " + name + " " + likes;
}

Trouble deep cloning an array object

I believe my code is wrong but could someone correct me on my error. I am trying to deep clone an array of objects but class A doesn't seem to be a deep copy as i am having trouble with it. Could some please help. I have trouble copying the array A.
Class A implements Cloneable{
private int year;
private double data;
A(int year, double data)
{
setInt(year);
setDouble(data);
}
public void setInt(int year)
{
this.year = year;
}
public void setDouble(double data)
{
this.data = data;
}
public int getYear()
{
return year;
}
public double getData()
{
return data;
}
public Object clone()
{
A clonedA = new A(this.getYear(), this.getData());
return clonedA;
}}
class B implements Cloneable{
private A[] a;
private String name;
private int arraylength;
private int index;
public B(String name, int length)
{
this.name = name;
this.arraylength = length;
a = new A[array.length];
index = 0;
}
public void addToA(int year, double data)
{
a[index] = new A(year, data);
index++;
}
public String getName(){
return name; }
public int getLength(){
return array length;}
public void setName(String name)
{
this.name= name
}
public Object clone()
{
B clonedB = new B(this.getName(), this.getLength());
for(A clonedArray: a)
{
clonedB.addToA(clonedArray.getYear(), clonedArray.getData());
}
return clonedB;
}
Your clone method in class B seems to be wrong:
I suggest you do sth like
public Object clone()
{
B newB = new B(this.getName(), this.getLength());
for(int i =0;i<newB.a.length;i++)
{
newB.a[i] = a[i];
}
return newB;
}
You could also try a copy constructor;

how to Externalise Java enums with Attributes into XML

i have a Java Enum like below
public enum TestEnum{
{
A("a","b","c"),
B("a1","b1","c1"),
C("a2","b2","c2");
TestEnum(String a,String b,String c){
}
private String a;
private String b;
private String c;
}
I want to externalize this config to an XML file but XSDs donot seem to support attributes on Enum Element type. Is there a way to work this around or an alternate to it.
You could do something like this (even though for enum, this looks too verbose)
#XmlJavaTypeAdapter(CountXmlAdapter.class)
public enum Count {
ONE(1, "one"),
TWO(2, "two"),
THREE(3, "three");
private final int index;
private final String name;
private Count(int index, String name) {
this.index = index;
this.name = name;
}
#XmlAccessorType(XmlAccessType.FIELD)
public static class CountWrapper {
private int index;
private String name;
public CountWrapper() {
}
public CountWrapper(int index, String name) {
this.index = index;
this.name = name;
}
}
public static class CountXmlAdapter extends XmlAdapter<CountWrapper, Count> {
#Override
public Count unmarshal(CountWrapper v) throws Exception {
return v != null ? Count.valueOf(v.name.toUpperCase()) : null;
}
#Override
public CountWrapper marshal(Count v) throws Exception {
return v != null ? new CountWrapper(v.index, v.name) : null;
}
}
}

NullPointerException on array with length one initializing

This is my method to updateHighScoreRecords():
public GameRecord[] updateHighScoreRecords(GameRecord[] highScoreRecords, String name, int level, int score) {
GameRecord[] gameRecord = null;
if (highScoreRecords.length == 0) { // Rule one
gameRecord = new GameRecord[1];
gameRecord[0].setName(name);
gameRecord[0].setLevel(level);
gameRecord[0].setScore(score);
System.out.println("Role one Done");
}
return gameRecord;
}
And this is my GameRecord class:
public class GameRecord {
private String name;
private int level;
private int score;
public GameRecord(String name, int level, int score) {
this.name = name;
this.level = level;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
But there is a nullPointer exception on this line:
gameRecord[0].setName(name);
Why?
I want to return an array of GameRecord type when highScoreRecords length is zero.
You never initialized zeroth element. First you have to add element at zero position and then access it.
gameRecord[0] = new GameRecord();
gameRecord[0].setName(name);
When you wrote
gameRecord = new GameRecord[1];
That means you are just initializing an array to store 2 GameRecord elements. Nothing else. Initially those are null's. You need to initialize each element to use them further. Otherwise they are still null.

Java constructor overloading ambiguity

How to solve the following constructor overloading problem? This is an interview question but I am curious to know the solution.
class Player
{
int nationalRank;
int internationalRank;
String name;
Player(String name, int nationalRank)
{
this.name= name;
this.nationalRank = nationalRank;
this.internationalRank=0;
}
Player(String name, int internationalRank)
{
this.name= name;
this.nationalRank = 0;
this.internationalRank=internationalRank;
}
}
Here, the compiler will give an error because argument types are same for both constructor. But logically they both are different. How can I solve this problem without adding any extra arguments? Is there any design pattern specifically for this?
class Player
{
int nationalRank;
int internationalRank;
String name;
private Player(){}
public static Builder builder()
{
return new Builder();
}
public static class Builder
{
int nationalRank = -1;
int internationalRank = -1;
String name;
public Builder nationalRank(int nationalRank)
{
this.nationalRank = nationalRank;
return this;
}
public Builder internationalRank(int internationalRank)
{
this.internationalRank = internationalRank;
return this;
}
public Builder name(String name)
{
this.name = name;
return this;
}
public Player build()
{
if (nationalRank == -1 && internationalRank = -1)
throw new IllegalStateException("both ranks haven't been initialized");
if (null == name)
throw new IllegalStateException("name hasn't been initialized");
Player result = new Player();
result.nationalRank = this.nationalRank;
result.internationalRank = this.internationalRank;
result.name = this.name;
return result;
}
}
}
Usage:
Player player = Player.builder().name("John").internationalRank(522).build();
You've got various options.
The simplest is to add factory methods like this:
public class Player
{
private int nationalRank;
private int internationalRank;
private String name;
private Player()
{
}
public static Player newNationalPlayer(String name, int nationalRank)
{
Player nationalPlayer = new Player();
nationalPlayer.name= name;
nationalPlayer.nationalRank = nationalRank;
nationalPlayer.internationalRank = 0;
return nationalPlayer;
}
public static Player newInternationalPlayer(String name, int internationalRank)
{
Player internationalPlayer = new Player();
internationalPlayer.name= name;
internationalPlayer.nationalRank = 0;
internationalPlayer.internationalRank = internationalRank;
return internationalPlayer;
}
...
}
However, this leaves an unused variable which isn't very nice. A better solution would be to add a PlayerType enum:
public enum PlayerType
{
NATIONAL,
INTERNATIONAL
}
public class Player
{
private int rank;
private String name;
private PlayerType type;
public Player(String name, PlayerType type, int rank)
{
this.name= name;
this.type = type;
this.rank = rank;
}
...
}
Which is best is down to the exact use case.
Just reverse the parameters of one of the constructors and you are good to go.... I made this answer thinking that it's an interview question....perhaps the interviewer has this in mind...
class Player
{
int nationalRank;
int internationalRank;
String name;
Player(String name, int nationalRank)
{
this.name= name;
this.nationalRank = nationalRank;
this.internationalRank=0;
}
Player( int internationalRank,String name)
{
this.name= name;
this.nationalRank = 0;
this.internationalRank=internationalRank;
}
}
As suggested by a comment, just use static factory methods. In fact, this solution goes further than that and uses a builder. You will note a clear advantage: all instance variables are now final.
public class Player
{
private final String name;
private final int nationalRank;
private final int internationalRank;
// Constructor becomes private
private Player(final Builder builder)
{
name = builder.name;
nationalRank = builder.nationalRank;
internationalRank = builder.internationalRank;
}
public static Builder withName(final String name)
{
return new Builder(name);
}
// Inner builder class
public static class Builder
{
private final String name;
private int nationalRank;
private int internationalRank;
private Builder(final String name)
{
this.name = name;
}
public Builder withNationalRank(int rank)
{
nationalRank = rank;
return this;
}
public Builder withInternationalRank(int rank)
{
internationationalRank = rank;
return this;
}
public Player build()
{
return new Player(this);
}
}
}
Usage:
Player player1 = Player.withName("foo").withNationalRank(1).build();
// etc

Categories