Call print of Switch case - from a blueprint class to a main class - switch-statement

I am having class about java, and There is a assignment that I must do, but I am stuck. lol
This is what she asked:
Write a java blueprint class that models an Airline Reservation and adheres to the following
criteria:
o5 instance variables: first name, last name, flight number, seat number , ticket number
oA default constructor
oA constructor that takes five arguments
oA setter and getter for each field that simply sets and gets the field value.
oA method called “retrieveSeatingClass” which returns a “seating class” for each seat number (use
a switch statement)
▪seats 1-2: First Class
▪seats 3-4: Second Class
▪seats 5-6 Third Class
▪seats 7-8: Fourth Class
I did it. But now in the main class, she wants me to call the result. And I am struggling to do it. Here follows my code.
Entered code here:
public class AirlineReservation {
String firstName;
String lastName;
String flightNumber;
int seatNumber;
int ticketNumber;
public AirlineReservation() {
}
public AirlineReservation(String firstName, String lastName, String flightNumber, int seatNumber,
int ticketNumber) {
this.firstName = firstName;
this.lastName = lastName;
this.flightNumber = flightNumber;
this.seatNumber = seatNumber;
this.ticketNumber = ticketNumber;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setFlightNumber(String flightNumber) {
this.flightNumber = flightNumber;
}
public void setSeatNumber(int seatNumber) {
this.seatNumber = seatNumber;
}
public void setTicketNumber(int ticketNumber) {
this.ticketNumber = ticketNumber;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getFlightNumber() {
return flightNumber;
}
public int getSeatNumber() {
return seatNumber;
}
public int getTicketNumber() {
return ticketNumber;
}
public String retrieveSeatingClass() {
int classNumber = seatNumber;
switch (classNumber) {
case 1:
case 2:
System.out.println("First Class");
break;
case 3:
case 4:
System.out.println("Second Class");
break;
case 5:
case 6:
System.out.println("Third Class");
break;
case 7:
case 8:
System.out.println("Fourth Class");
break;
}
return "seat";
}
import java.util.Scanner;
public class AirlineReservationTestHarness {
public static void main (String[ ]args) {
AirlineReservation passager1 = new AirlineReservation("Raquel","Cristina", "400", 1, 456789);
System.out.println("Passager: " + passager1.getFirstName());
System.out.println("Last name: " + passager1.getLastName());
System.out.println("Flight Number" + passager1.getFlightNumber());
System.out.println("Seat: " + passager1.getSeatNumber());
System.out.println("Your ticket number is: " + passager1.getTicketNumber());
`enter code here` System.out.println("Your data is: " + passager1.getFirstName() + assager1.getLastName() + passager1.getFlightNumber() + passager1.getSeatNumber() + passager1.getTicketNumber() + " and your in a " + );
}
}
In my main, how do I call it for when the seat it 1 it appears First Class?

Related

why Mockito.spy on a class does not work with springboot

My mockito-core version is 2.23.4. I want to spy on MockitoTestService class and I do this code:
MockitoTestService mockTestServiceObj = Mockito.spy(MockitoTestService.class);
it does not work, the result is same to
Mockito.mock(MockitoTestService.class);
if I replace MockitoTestService.class with spyTestService, it does work.
Mockito.spy(spyTestService);
I am confused about it. When is the right time to use .class and instance
#SpringBootTest
#RunWith(SpringRunner.class)
public class SpyTestServiceImplTest {
#Resource
MockitoTestService spyTestService;
#Test
public void TestSpy() {
MockitoTestService mockTestServiceObj = Mockito.spy(MockitoTestService.class);
// Mockito.doReturn(2).when(mockTestServiceObj).mockMethodA(1);
Mockito.doReturn(0).when(mockTestServiceObj).mockMethodB();
Mockito.doReturn(true).when(mockTestServiceObj).mockMethodC();
// Mockito.doReturn("111").when(mockTestServiceObj).mockMethodD();
System.out.println("mockMethodA: " + mockTestServiceObj.mockMethodA(1));
System.out.println("mockMethodB: " + mockTestServiceObj.mockMethodB());
System.out.println("mockMethodC: " + mockTestServiceObj.mockMethodC());
System.out.println("mockMethodD: " + mockTestServiceObj.mockMethodD());
}
#Test
public void TestSpyMapper() {
MockitoTestMapper mockTestMapperObj = Mockito.spy(MockitoTestMapper.class);
ReflectionTestUtils.setField(spyTestService, "mockitoTestMapper", mockTestMapperObj);
Mockito.doReturn("20220701").when(mockTestMapperObj).getDate();
System.out.println("mockMethodA: " + spyTestService.mockMethodA(2));
System.out.println("mockMethodB: " + spyTestService.mockMethodB());
System.out.println("mockMethodC: " + spyTestService.mockMethodC());
System.out.println("mockMethodD: " + spyTestService.mockMethodD());
}
}
mockito implement
#Service
public class MockitoTestServiceImpl implements MockitoTestService {
#Resource
MockitoTestMapper mockitoTestMapper;
#Override
public int mockMethodA(int i) {
return i;
}
#Override
public Object mockMethodB() {
return "123";
}
#Override
public boolean mockMethodC() {
return true;
}
#Override
public String mockMethodD() {
return mockitoTestMapper.getDate();
}
}

How to read and write Identified Data Serializable in Hazelcast

i have a problem and the documentations and github examples doesn't provide a clear example about how to do it....
i have this class
public class KeySerializable implements IdentifiedDataSerializable{
private String claveReq;
private int id_interno_pe;
private String cod_nrbe_en;
private int num_sec_ac;
public KeySerializable(String claveReq, int id_interno_pe, String cod_nrbe_en, int num_sec_ac) {
this.claveReq = claveReq;
this.id_interno_pe = id_interno_pe;
this.cod_nrbe_en = cod_nrbe_en;
this.num_sec_ac = num_sec_ac;
}
public KeySerializable() {
}
public void writeData(ObjectDataOutput out) throws IOException {
out.writeString(claveReq);
out.writeInt(id_interno_pe);
out.writeString(cod_nrbe_en);
out.writeInt(num_sec_ac);
}
public void readData(ObjectDataInput in) throws IOException {
this.claveReq = in.readString();
this.id_interno_pe = in.readInt();
this.cod_nrbe_en = in.readString();
this.num_sec_ac = in.readInt();
}
public int getFactoryId() {
return KeySerializableFactory.FACTORY_ID;
}
public int getClassId() {
return KeySerializableFactory.KEY_SERIALIZABLE_TYPE;
}
#Override
public String toString() {
return "KeySerializable [claveReq=" + claveReq + ", id_interno_pe=" + id_interno_pe + ", cod_nrbe_en="
+ cod_nrbe_en + ", num_sec_ac=" + num_sec_ac + "]";
}
}
and this class
public class ResponseSerializablePlus implements IdentifiedDataSerializable{
private int id_interno_pe;
private String cod_nrbe_en;
private int num_sec_ac;
private int statusCode;
private HashMap<String,List<String>> headers;
private byte[] content;
public ResponseSerializablePlus(int id_interno_pe, String cod_nrbe_en, int num_sec_ac, int statusCode,
HashMap<String, List<String>> headers, byte[] content) {
this.id_interno_pe = id_interno_pe;
this.cod_nrbe_en = cod_nrbe_en;
this.num_sec_ac = num_sec_ac;
this.statusCode = statusCode;
this.headers = headers;
this.content = content;
}
public ResponseSerializablePlus() {
}
public void writeData(ObjectDataOutput out) throws IOException {
out.writeInt(id_interno_pe);
out.writeString(cod_nrbe_en);
out.writeInt(num_sec_ac);
out.write(statusCode);
out.writeObject(headers);
out.writeByteArray(content);
}
public void readData(ObjectDataInput in) throws IOException {
this.id_interno_pe = in.readInt();
this.cod_nrbe_en = in.readString();
this.num_sec_ac = in.readInt();
this.statusCode = in.readInt();
this.headers = in.readObject();
this.content = in.readByteArray();
}
public int getFactoryId() {
return ResponseSerializablePlusFactory.FACTORY_ID;
}
public int getClassId() {
return ResponseSerializablePlusFactory.RESPONSE_SERIALIZABLE_PLUS_CLASS;
}
#Override
public String toString() {
return "ResponseSerializablePlus [id_interno_pe=" + id_interno_pe + ", cod_nrbe_en=" + cod_nrbe_en
+ ", num_sec_ac=" + num_sec_ac + ", statusCode=" + statusCode + ", headers=" + headers + ", content="
+ Arrays.toString(content) + "]";
}
and this other class
public class ResponseSerializable implements IdentifiedDataSerializable{
private int statusCode;
private HashMap<String,List<String>> headers;
private byte[] content;
public ResponseSerializable(int statusCode, HashMap<String, List<String>> headers, byte[] content) {
this.statusCode = statusCode;
this.headers = headers;
this.content = content;
}
public ResponseSerializable() {
}
public void writeData(ObjectDataOutput out) throws IOException {
out.write(statusCode);
out.writeObject(headers);
out.writeByteArray(content);
}
public void readData(ObjectDataInput in) throws IOException {
this.statusCode = in.readInt();
this.headers = in.readObject();
this.content = in.readByteArray();
}
public int getFactoryId() {
return ResponseSerializableFactory.FACTORY_ID;
}
public int getClassId() {
return ResponseSerializableFactory.RESPONSE_TYPE;
}
#Override
public String toString() {
return "ResponseSerializable [statusCode=" + statusCode + ", headers=" + headers + ", content="
+ Arrays.toString(content) + "]";
}
}
and the factory it's always the same but with different classes
public class KeySerializableFactory implements DataSerializableFactory{
public static final int FACTORY_ID = 1;
public static final int KEY_SERIALIZABLE_TYPE = 1;
public IdentifiedDataSerializable create(int typeId) {
if ( typeId == KEY_SERIALIZABLE_TYPE ) {
return new KeySerializable();
} else {
return null;
}
}
}
and im always having this bunch errors
the documentation and the github examples from hazelcast doesn't provide a good example about how to use the getters and setters and i don't understand what to do here to write or read an object
any hint? can you help me?
You shouldn't be calling readData or writeData yourself. These methods are called by Hazelcast internally when the data is about to be serialized/deserialized.
On your side, you should only register the data serializable factories you have created to the Hazelcast.
Then, upon receiving an instance of a class that implements IdentifiedDataSerializable and has a factory registered for it, Hazelcast will call the methods I mentioned above in appropriate places and return you the object read.
From the code sample you shared, you need to drop the line starting with newResponse.writeData(..., and everything should be working, assuming you did the factory registration.
Also, please fix your ResponseSerializablePlus and ResponseSerializable classes: You need to use writeInt method to write the statusCode field. The readData and writeData methods should be consistent with each other.

issues with parcelable extension: getting unmarshalling unknown type code exception

long story short, I have one settlementItemeBase class as my father and two children. I want to make the father class parcelable so as extension happens, my two child classes be parcelable as well. I don't exactly know what I'm doing right or wrong. I searched little bit but nothing helped me.
here are my classes:
SettlementItemBase:
public class SettlementItemBase implements Parcelable{
public SettlementItemBase(){}
protected SettlementItemBase(Parcel in) {
}
public static final Creator<SettlementItemBase> CREATOR = new Creator<SettlementItemBase>() {
#Override
public SettlementItemBase createFromParcel(Parcel in) {
return new SettlementItemBase(in);
}
#Override
public SettlementItemBase[] newArray(int size) {
return new SettlementItemBase[size];
}
};
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
}
}
first child class:
public class FirstClass extends SettlementItemBase {
private int id;
private int cardId;
private String cardNumber;
private String expDate;
private String currency;
private String url;
public FirstClass(){
id = 0;
cardId = 0;
cardNumber = "";
expDate = "";
currency = "";
url = "";
}
protected FirstClass(Parcel in) {
super(in);
id = in.readInt();
cardId = in.readInt();
cardNumber = in.readString();
expDate = in.readString();
currency = in.readString();
url = in.readString();
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeInt(cardId);
dest.writeString(cardNumber);
dest.writeString(expDate);
dest.writeString(currency);
dest.writeString(url);
}
public int getId() {
return id;
}
public int getCardId() {
return cardId;
}
public String getCardNumber() {
return cardNumber;
}
public String getExpDate() {
return expDate;
}
public String getCurrency() {
return currency;
}
public String getUrl() {
return url;
}
}
second child class:
public class SecondClass extends SettlementItemBase{
private int id;
private String currency;
private String accountNumber;
private String ibanNumber;
public SecondClass(int id, String currency,
String accountNumber, String ibanNumber){
this.id = id;
this.currency = currency;
this.accountNumber = accountNumber;
this.ibanNumber = ibanNumber;
}
protected SecondClass(Parcel in){
super(in);
id = in.readInt();
currency = in.readString();
accountNumber = in.readString();
ibanNumber = in.readString();
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(currency);
dest.writeString(accountNumber);
dest.writeString(ibanNumber);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCurrency() {
return currency;
}
public String getAccountNumber() {
return accountNumber;
}
public String getIbanNumber() {
return ibanNumber;
}
}
I'm passing and getting an ArrayList of both child class' items with intent putParcelableArrayListExtra and getParcelableArrayListExtra methods and get the following error:
Parcel android.os.Parcel#2d0fde33: Unmarshalling unknown type code 3276849 at offset 172
any help would be appreciated 3>
well I'm posting this for those who may run into the same problem as me.
the problem was solved after adding the Creator statement to the child classes.
for one of the child classes it would be like:
public static final Creator<FirstClass> CREATOR = new Creator<FirstClass>() {
#Override
public FirstClass createFromParcel(Parcel in) {
return new FirstClass(in);
}
#Override
public FirstClass[] newArray(int size) {
return new FirstClass[size];
}
};
hope this helps. 3>

Using a string to specify an object in Java

I have a Combo Bx (Dropdown box) with an index range of 0-20. If there anyways I can use that index to specify which object I want data from? All of the objects use the same naming convention obj0, obj1, obj2, etc. Basically something like this...
public abstract class Person {
private String name;
private String title;
private String email;
private String job;
public Person(String name, String title, String email, String job){
this.name = name;
this.title = title;
this.email = email;
this.job = job;
}
//Getters and Setters
}
public class main extends javax.swing.JFrame {
...misc code...
private void btn_startActionPerformed(java.awt.event.ActionEvent evt) {
Person obj0 = new Person("Jon Doe",
"Program Coordinator",
"jon.doe#test.com",
"Faculty");
Person obj1 = ...
...
Person obj20 = ...
/*
Onclick it uses the index of the current index in the combobox (dropdown)
to specify which object to get the data from.
*/
private void btn_GetActionPerformed(java.awt.event.ActionEvent evt) {
//Uses the obj naming convention plus the index
string foo = "obj" + toString(combobox_Name.getSelectedIndex());
//Fills the textbox using the above string and the getName method
txtbox_username.setText(ToObject(foo).getName);
}
I have created a basic design of what I think you want:
This code creates 20 objects, adds them to a combobox and uses their predefined name when selected to change a textfield.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
class ObjExample {
String name;
public ObjExample(String name) {
this.name = name;
}
#Override
public String toString() {
return name;
}
}
public class Main extends JFrame implements ActionListener {
JComboBox jcb = new JComboBox();
JTextField jtf = new JTextField("Text Field");
public Main() {
setSize(200, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
for (int i = 0; i <= 20; i++) {
jcb.addItem(new ObjExample(Integer.toString(i)));
}
jcb.addActionListener(this);
add(jcb);
add(jtf);
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jcb) {
ObjExample obj = (ObjExample) jcb.getSelectedItem();
jtf.setText(obj.toString());
}
}
}

Having trouble writing a constructor for a object inside another class

Please don't get mad, I am amiting this is help on my homework but I like to learn this and can't find it anywhere. I have looked for it and couldn't find anything like it, idk if I am looking in all the wrong places or for the wrong thing I have two other classes inside my code and having trouble creating another class with class objectives inside of it, I will show what I attempted and see if yall can please help me out. Also, I want to make sure I am using the keyword " this" right in my other two classes. She wanted us to use this in our program and the comments are what we are told to do:
public class Person
{
private String lastName;
private String firstName;
//default constructor
public Person()
{
lastName= null;
firstName = null;
}
//two-parameter constructor
public Person(String lastName, String firstName)
{
this.lastName=lastName;
this.firstName=firstName;
}
//copy constructor
public Person(Person object2)
{
this.lastName=object2.lastName;
this.firstName=object2.firstName;
}
// standard accessor method for each of the two fields
public String getLastName(String lastName)
{
lastName = this.lastName;
return lastName;
}
public String getFirstName(String firstName)
{
firstName = this.firstName;
return firstName;
}
public void setLastName(String lastName)
{
this.lastName=lastName;
}
public void setFirstName(String firstName)
{
this.firstName=firstName;
}
//mutator method for both fields—using standard mutator methods
public void setName(String lastName,String firstName)
{
this.lastName= lastName;
this.firstName = firstName;
}
//toString method
public String toString()
{
String str = “Last Name: “ + lastName + “\nFirst Name: “ + firstName;
return str;
}
//equals method
public boolean equals(Person name2)
{
boolean status;
if (this.lastName.equals(name2.lastName) && this.firstName.equals(name2.firstName))
status = true;
else
status = false;
return status;
}
//copy method
public Person copy()
{
Person copyObject = new Person(lastName, firstName);
return copyObject;
}
}
public class Date
{
private int month;
private int day;
private int year;
//default constructor
public Date()
{
this (0,0,0);
}
//three-parameter constructor
public Date(int month, int day, int year)
{
this.month = month;
this.day = day;
this.year = year;
}
//copy constructor
public Date(Date object2)
{
this (object2.month, object2.day, object2.year);
}
//standard accessor method for each field
public int getMonth(int month)
{
month = this.month;
return month;
}
public int getDay(int day)
{
day = this.day;
return day;
}
public int getYear(int year)
{
year = this.year;
return year;
}
//standard mutator method for each field
public void setMonth(int month)
{
this.month = month;
}
public void setDay(int day)
{
this.day = day;
}
public void setYear(int year)
{
this.year = year;
}
//mutator method for both fields—using standard mutator methods
public void setDate(int month, int day, int year)
{
this.month = month;
this.day = day;
this.year= year;
}
//toString method
public String toString()
{
String str = "Date:" + month+ " " + day + ", " + year;
return str;
}
//equals method
public boolean equals (Date object2)
{
boolean status;
if (this.month == object2.month && this.day == object2.day && this.year == object2.year)
status = true;
else
status = false;
return status;
}
//copy method
public Date copy()
{
Date copyObject = new Date(month, day, year);
return copyObject;
}
}
And this is what I have been trying for my other class and It shows an ERROR:
public class PersonalInfo
{
private Person name;
private Date birthday;
private int idNumber;
// the default constructor
public PersonalInfo()
{
Person name = new Person();
Date birthday = new Date();
this.idNumber = 0;
}
// A constructor that passes 6 parameters
public PersonalInfo(String lastName, String firstName, int month, int day, int year, int idNumber )
{
Person name = new Person(lastName, firstName);
Date birthday= new Date(month, day, year);
this.idNumber = idNumber;
}
}
Please help! And thank you for looking
Since you haven't specified the language or the error message, this is only a qualified guess. If this isn't the actual problem, provide more details.
public PersonalInfo()
{
Person name = new Person();
Date birthday = new Date();
this.idNumber = 0;
}
You are declaring new local variables name and birthday here, rather than using the class members. That means the class members never get initialized, and I suspect that's what the error is trying to tell you.
The right way to do this is to just refer directly to the variables:
this.name = new Person();
this.birthday = new Date();
or, since this is implied when there is no local variable or parameter with the same name:
name = new Person();
birthday = new Date();

Resources