Truthfully, I mixed some codes.
I need output like below:
Parking ticket #:
Fined amount:
Car issued to:
Issued by officer:
But here is current output:
Fined amount:
Car issued to:
Parking ticket #:null
Fined amount:
Issued by officer:
So looks like the main issue is with generating a parking ticket #
Code is
import java.util.Scanner;
public class ParkingTicketSimulator
{
public static void main(String[] args)
{
String make, model, color, license, name, badge;
int parkedMinutes, meterMinutes;
Scanner keyboard = new Scanner(System.in);
System.out.print("=== Parking Ticket Simulator ===\n\n");
System.out.print("---------\n");
System.out.print("Car Data\n");
System.out.print("---------\n\n");
System.out.print("Enter the car make: ");
make = keyboard.nextLine();
System.out.print("Enter the car model: ");
model = keyboard.nextLine();
System.out.print("Enter the car color: ");
color = keyboard.nextLine();
System.out.print("Enter the car license number: ");
license = keyboard.nextLine();
System.out.print("Enter minutes car has been parked:");
parkedMinutes = keyboard.nextInt();
System.out.print("\n----------\n");
System.out.print("Meter Data\n");
System.out.print("----------\n\n");
System.out.print("Enter minutes purchased by driver:");
meterMinutes = keyboard.nextInt();
keyboard.nextLine();
System.out.print("\n-------\n");
System.out.print("PO Data\n");
System.out.print("-------\n\n");
System.out.println();
System.out.print("Enter police officer's name:");
name = keyboard.nextLine();
System.out.print("Enter police officer's badge number:");
badge = keyboard.nextLine();
System.out.print("\n---------------------\n");
System.out.print("Parking Ticket Issued\n");
System.out.print("---------------------\n\n");
ParkedCar car = new ParkedCar(make,model,color,license,parkedMinutes);
ParkingMeter meter1 = new ParkingMeter (meterMinutes);
PoliceOfficer officer1 = new PoliceOfficer(name,badge,car,meter1);
System.out.println(officer1.getExpired(officer1));
}
public static class ParkedCar {
private String make;
private String model;
private String color;
private String license;
private int minutesParked;
public ParkedCar() {
make = "";
model = "";
color = "";
license = "";
minutesParked = 0;
}
public ParkedCar(ParkedCar carDetails){
make = carDetails.make;
model = carDetails.model;
color = carDetails.color;
license = carDetails.license;
minutesParked = carDetails.minutesParked;
}
public ParkedCar(String make, String model, String color, String license, int minutesParked){
this.make = make;
this.model = model;
this.color = color;
this.license = license;
this.minutesParked = minutesParked;
}
public void setMake (String make){
this.make = make;
}
public void setModel (String model){
this.model = model;
}
public void setColor (String color){
this.color = color;
}
public void setLicense (String license){
this.license = license;
}
public void minutesParked (int minutesParked){
this.minutesParked = minutesParked;
}
public String getMake(){
return make;
}
public String getModel(){
return model;
}
public String getColor(){
return color;
}
public String getLicense(){
return license;
}
public int getMinutesParked(){
return minutesParked;
}
public String toString(){
String str = "\nMake: " + make + "\nModel: " + model + "\nColor: " + color + "\nLicense: " + license + "\nMinutes parked: " + minutesParked;
return str;
}
}
public static class ParkingMeter {
private int minutes;
public ParkingMeter() {
minutes = 0;
}
public ParkingMeter(int minutes){
this.minutes = minutes;
}
public ParkingMeter (ParkingMeter minutesDetail){
minutes = minutesDetail.minutes;
}
public void setMinutes (int minutes){
this.minutes = minutes;
}
public int getMinutes(){
return minutes;
}
public String toString(){
String str ="\nMinutes: " + minutes;
return str;
}
}
public static class ParkingTicket{
private PoliceOfficer officer; //Calls the officer details.
private ParkedCar car;
private ParkingMeter meter;
private int fees;
public ParkingTicket(PoliceOfficer officer){
this.officer = new PoliceOfficer(officer);
this.meter = officer.getMeter();
this.car = officer.getParkedCar();
}
public ParkingTicket(){
}
public int fees(){
int time;
if (car != null || meter != null){
time = car.getMinutesParked() - meter.getMinutes() - 1;
fees = 25;
while (time > 0){
time = time - 1;
fees = fees+10;
}
}
return fees;
}
public String toString(){
if (fees() == 0 ){
String str = "There is no ticket issued.";
return str;
}
else{
String str = "\n" + officer +"\nTime over: " + (car.getMinutesParked() - meter.getMinutes()) + "\nThe fee is $" + fees();
return str;
}
}
}
public static class PoliceOfficer{
private String name;
private String badge;
private ParkedCar car;
private ParkingMeter meter;
private ParkingTicket ticket;
public PoliceOfficer(String name, String badge, ParkedCar carDetails, ParkingMeter time) {
this.name = name;
this.badge = badge;
car = new ParkedCar(carDetails);
meter = new ParkingMeter(time);
}
public PoliceOfficer(PoliceOfficer officerDetails){
name = officerDetails.name;
badge = officerDetails.badge;
car = officerDetails.car;
meter = officerDetails.meter;
}
public void setName (String name){
this.name = name;
}
public void setBadge (String badge){
this.badge = badge;
}
public String getName(){
return name;
}
public String getBadge(){
return badge;
}
public ParkedCar getParkedCar() {
return new ParkedCar(car);
}
public ParkingMeter getMeter(){
return new ParkingMeter(meter);
}
public ParkingTicket getExpired(PoliceOfficer officer){
if (meter.getMinutes() - car.getMinutesParked() < 0){
ticket = new ParkingTicket(officer);
return ticket;
}
else
ticket = new ParkingTicket();
return ticket;
}
public String toString(){
String str = "Officer Details\nOfficer's Name: " + name + "\nOfficer's Badge Number: " + badge + "\n\nCar Information: " + car + "\n\nMeter Information: " + meter;
return str;
}
}
Related
Based on the following UML class diagram I am trying to get the total population of all the House and ApartmentBuilding objects using an interface (Dwelling) and I am stuck on how to proceed. I have included the code I have so far.
Dwelling:
interface Dwelling {
int getNumberOfOccupants();
}
House:
import javafx.scene.canvas.GraphicsContext;
import java.util.Scanner;
public class House extends Building implements Dwelling {
private final int bedrooms;
private final int occupants;
private House(String name, double xPosition,int bedrooms, int occupants){
super(name,xPosition);
this.bedrooms = bedrooms;
this.occupants = occupants;
}
public static House create() {
Scanner scan = new Scanner(System.in);
House a;
System.out.println("Enter name of the House: ");
String name = scan.nextLine();
System.out.println("Enter XPosition of the House: ");
int xPosition = scan.nextInt();
System.out.println("Enter number of bedrooms: ");
int bedrooms = scan.nextInt();
System.out.println("Enter number of occupants: ");
int occupants = scan.nextInt();
a = new House(name, xPosition, bedrooms, occupants);
return a;
}
public void draw(GraphicsContext canvas){
}
#Override
public String toString(){
return "House: " + "bedrooms= " + bedrooms + " occupants= " + occupants + "\n" + super.toString();
}
#Override
public int getNumberOfOccupants() {
return occupants;
}
}
ApartmentBuilding:
import javafx.scene.canvas.GraphicsContext;
import java.util.Scanner;
public class ApartmentBuilding extends HighRise implements Dwelling{
private final int occupantsPerFloor;
private ApartmentBuilding(String name, double xPosition, int numberOfFloors, int occupantsPerFloor){
super(name, xPosition, numberOfFloors);
this.occupantsPerFloor = occupantsPerFloor;
}
public static ApartmentBuilding create() {
Scanner scan = new Scanner(System.in);
ApartmentBuilding a;
System.out.println("Enter name of the Apartment Building: ");
String name = scan.nextLine();
System.out.println("Enter XPosition of the Apartment Building: ");
int xPosition = scan.nextInt();
System.out.println("Enter number of floors: ");
int numberOfFloors = scan.nextInt();
System.out.println("Enter number of occupants per floor: ");
int occupantsPerFloor = scan.nextInt();
a = new ApartmentBuilding(name, xPosition, numberOfFloors, occupantsPerFloor);
return a;
}
public void draw(GraphicsContext canvas){
}
#Override
public String toString(){
return "Apartment Building: " + "occupantsPerFloor= " + occupantsPerFloor + "\n" + super.toString() + "\n";
}
#Override
public int getNumberOfOccupants() {
return numberOfFloors * occupantsPerFloor;
}
}
Building:
import javafx.scene.canvas.GraphicsContext;
public class Building implements Drawable {
private final String name;
private final double xPosition;
public Building(String name, double xPosition){
this.name = name;
this.xPosition = xPosition;
}
public String getName(){
return name;
}
public void draw(GraphicsContext canvas) {
}
public double getXPosition() {
return xPosition;
}
#Override
public String toString(){
return "Type... Building: " + "name= " + getName() + ", xPosition= " + getXPosition() + "\n";}
}
HighRise:
public class HighRise extends Building{
int numberOfFloors;
public HighRise(String name, double xPosition, int numberOfFloors) {
super(name, xPosition);
this.numberOfFloors=numberOfFloors;
}
public int getNumberOfFloors(){
return numberOfFloors;
}
#Override
public String toString() {
return "Type... HighRise: " + "numberOfFloors= " + getNumberOfFloors() + "\n" + super.toString();
}
}
Village:
import javafx.scene.canvas.GraphicsContext;
import java.util.Scanner;
public class Village extends Building{
private static String name;
private static int xPosition;
public static final double Y_FLOOR = 300;
private int size;
private final String villageName;
private final Building[] buildings;
private Village(String villageName, int size){
super(name, xPosition);
this.size = size;
this.villageName = villageName;
this.buildings = new Building[size];
}
public static Village create() {
Scanner scan = new Scanner(System.in);
Village a;
System.out.println("Enter name of village: ");
String villageName = scan.nextLine();
System.out.println("Enter number of buildings: ");
int num = scan.nextInt();
a = new Village(villageName, num);
for(int i = 0; i < num; i++) {
System.out.println("Enter type of Building: 1= House, 2= Apartment, 3= Store ");
int choice = scan.nextInt();
if (choice == 1){
a.buildings[i] = House.create();
}
if (choice == 2){
a.buildings[i] = ApartmentBuilding.create();
}
}
return a;
}
public int getPopulation(){
return size;
}
public void draw(GraphicsContext canvas){
}
public String toString(){
String str = "\n"+ "Village of " + villageName + "\n\n";
for (int i=0; i<buildings.length; i++) {
str = str + buildings[i].toString() + "\n"; // this adds each buildings information to the string
}
return str;
}
}
I am new at programming and trying my best to learn but I am getting stuck on this, unfortunately.
..I am trying to get the total population of all the House and ApartmentBuilding objects using an interface (Dwelling)..
You can not get the population while using that UML. Please try modify a bit as below.
First, Dwelling must declaire getPopulation();
Implementing getPopulation() within House and ApartmentBuilding
You need a variable to keep the population within House and ApartmentBuilding class as well (it will be returned while calling getPopulation()).
After that, you able to cast House and ApartmentBuilding to Dwelling. Then dwelling.getPopulation(). Hope it is useful.
When I try to set my Dog object to dog2 and then have it print out the information I obtained for dog1, it keeps saying one of my variables is undefined. Below is my code for each component class as well as for main. If I take out dog2 and just call print for dog1, everything works as it should. I do not know what to do. I have been working on this for the past two days with no headway.
public class Owner {
private String ownerName;
private int ownerSSN;
private int ownerPhone;
public String getOwnerName () {
return ownerName;
}
public int getOwnerSSN () {
return ownerSSN;
}
public int getOwnerPhone () {
return ownerPhone;
}
public void setOwnerName (String own) {
this.ownerName = own;
}
public void setOwnerSSN (int ssn) {
this.ownerSSN = ssn;
}
public void setOwnerPhone (int phone) {
this.ownerPhone = phone;
}
// created the default constructor and the overloaded constructor
public Owner () {
ownerName = "John Doe";
ownerSSN = 0;
ownerPhone = 0;
}
public Owner (String own, int ssn, int phone) {
this.ownerName = own;
this.ownerSSN = ssn;
this.ownerPhone = phone;
}
}
public class DOB {
private int day;
private int month;
private int year;
public int getDay () {
return day;
}
public int getMonth () {
return month;
}
public int getYear () {
return year;
}
public void setDay (int day) {
this.day = day;
}
public void setMonth (int month) {
this.month = month;
}
public void setYear (int year) {
this.year = year;
}
// created the default constructor and the overloaded constructor for the DOB class
public DOB () {
day = 00;
month = 00;
year = 0000;
}
public DOB (int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
}
public class DogLicense {
private int licenseNum;
private int licenseYear;
private String licenseAuthority;
public int getLicenseNum () {
return licenseNum;
}
public int getLicenseYear () {
return licenseYear;
}
public String getLicenseAuthority () {
return licenseAuthority;
}
public void createLicenseNum (int license) {
licenseNum = (license * 705);
}
// created the default contructor and the overloaded constructor for the DogLicense class
public DogLicense () {
licenseNum = 0;
licenseYear = 0000;
licenseAuthority = "PA";
}
public DogLicense (int lNum, int lYear, String authority) {
this.licenseNum = lNum;
this.licenseYear = lYear;
this.licenseAuthority = authority;
}
}
public class Dog {
private String name;
private DogLicense licenseNum;
private Owner own;
private DOB dob;
// created the getters for the Dog Class
public String getName () {
return name;
}
public DogLicense getLicenseNum () {
return licenseNum;
}
public Owner getOwn () {
return own;
}
public DOB getDOB () {
return dob;
}
// created the setters for the Dog class
public void setName (String name) {
this.name = name;
}
public void setLicenseNum (DogLicense licenseNum) {
this.licenseNum = licenseNum;
}
public void setOwner (Owner own) {
this.own = own;
}
public void setDOB (DOB dob) {
this.dob = dob;
}
// created the default constructor and the overloaded constructor with references to other classes
public Dog () {
name = "New Puppy";
licenseNum = null;
own = null;
dob = null;
}
public Dog (String name, DogLicense licenseNum, Owner own, DOB dob) {
this.name = name;
this.licenseNum = licenseNum;
this.own = own;
this.dob = dob;
}
public void printDogInfo () {
System.out.println(this.own.getOwnerName() + ", here is the following information pertaining to your dog.");
System.out.println("Your dog's name is: " + this.name);
System.out.println(this.name + "'s licensing number is: " + this.licenseNum.getLicenseNum());
System.out.printf(this.name + "'s date of birth is: " + this.dob.getMonth () + "/" + this.dob.getDay () + "/" + this.dob.getYear ());
System.out.println();
System.out.println("Your phone number is: " + this.own.getOwnerPhone ());
System.out.println("Your social security number is: " + this.own.getOwnerSSN ());
}
}
import java.util.*;
public class TestDog {
#SuppressWarnings("resource")
public static void main(String[] args) {
Scanner animal = new Scanner (System.in);
Dog dog1 = new Dog ();
System.out.print("Please enter the name of your dog here: ");
String name = animal.next ();
dog1.setName (name);
System.out.println();
// created the information pertaining to the Owner
System.out.print("Please enter the name of the owner: ");
String own = animal.next ();
System.out.print("Please enter your 9 digit Social Security Number. Do not include dashes. ");
int ssn = animal.nextInt ();
System.out.print("Please enter your phone number. Please, do not include the area code or any dashes. ");
int phone = animal.nextInt ();
Owner owner1 = new Owner (own, ssn, phone);
dog1.setOwner (owner1);
System.out.println();
// created the information for Date of Birth
System.out.print("Please enter the day your dog was born (dd mm yyyy): ");
int day = animal.nextInt ();
int month = animal.nextInt ();
int year = animal.nextInt ();
DOB dob = new DOB (day, month, year);
dog1.setDOB (dob);
System.out.println();
// created information for Dog License
System.out.print("Please enter a one to four digit identifier number for your dog: ");
int license = animal.nextInt ();
System.out.print("Please enter the year you are obtaining your license number in: ");
int lYear = animal.nextInt ();
System.out.print("PLease enter the licensing authority in which you are registering your dog: ");
String authority = animal.next ();
System.out.println();
DogLicense dog1License = new DogLicense (license, lYear, authority);
dog1.setLicenseNum (dog1License);
dog1License.createLicenseNum(license);
dog1.printDogInfo ();
System.out.println();
System.out.println();
Dog dog2 = new Dog (name, license, own, dob);
dog2.printDogInfo ();
}
}
I'm new-ish to Java. I'm sorry this is so long. Directly below is the output of my code so far The part with ** is where I'm having a problem. I'm supposed to be comparing the speeds of two Car objects, but each Owner object is either a "safe" or "rash" driver. If they are rash, when they accelerate they speed up by 10. If they are safe, they speed up by 5. So far that aspect is working, however, only the first Owner object will store information. So whatever the first Owner object is (Safe/rash) they will both be. This isn't occurring for my Car objects, as each separate object is storing the correct information which is where I'm having difficulty. Can anyone help me figure out why?
Car Name: Betty
Year: 2002
Car Make: Kia
Car Model: Soul
Car Mileage: 50000
Car Speed: 65
Car owner name: Jane
Car owner type: rash
Car Name: Duke
Year: 2002
Car Make: Ford
Car Model: F250
Car Mileage: 50000
Car Speed: 65
Car owner name: John
Car owner type: safe
Betty is a 2002 Kia Soul.
The current mileage is 50000 and current speed is 65.
Jane is the owner of the car and is a rash driver.
Duke is a 2002 Ford F250.
The current mileage is 50000 and current speed is 65.
**Jane is the owner of the car and is a rash driver.**
The total mileage of both cars is 100000
Betty is going 75.
**Duke is going 75.**
This is what I have for class Car:
class Car{
//---INSTANCE VARIABLES---
private String carName;
private int yearModel;
private String make;
private String model;
private int speed;
private int mileage;
private Owner carOwner;
private static int totalMileage;
//---CAR CONSTRUCTOR---
public Car(String n, int y, String ma, String m, int ml, int s, Owner o) {
carName = n;
yearModel = y;
make = ma;
model = m;
speed = s;
mileage = ml;
carOwner = o;
totalMileage += ml;
}
//---MUTATOR & ACCESSOR METHODS---
public void setName(String n) {
carName = n;
}
public String getName() {
return carName;
}
public void setYearModel(int y) {
yearModel = y;
}
public int getYear() {
return yearModel;
}
public void setMake(String ma) {
make = ma;
}
public String getMake() {
return make;
}
public void setModel(String m) {
model = m;
}
public String getModel() {
return model;
}
public void setSpeed(int s) {
speed = s;
}
public int getSpeed() {
return speed;
}
public void setMileage(int ml) {
mileage = ml;
}
public int getMileage() {
return mileage;
}
public void setOwnerName(Owner n) {
carOwner = n;
}
public Owner getOwnerName() {
return carOwner;
}
//---ACCELERATE()---
public void accelerate() {
if(carOwner.getOwnType().equalsIgnoreCase("rash")) {
speed += 10;
} else {
speed += 5;
}
}
//---BRAKE---
public void brake() {
if(speed == 0) {
System.out.println("You are already stopped!");
} else {
speed -= 5;
}//end if-else statement
}
//---COMPARE()---
public void compare() {
}
//---TOSTRING()---
public String toString() {
String response = "";
response += "\n\n"+ carName +" is a " + yearModel + " " + make + " " + model+ ".";
response += "\nThe current mileage is " + mileage + " and current speed is " + speed;
response += ".\n" + carOwner.toString();
return response;
}
//---TOTALMILEAGE---
public static int getTotalMileage() {
return totalMileage;
}
}//---END CLASS CAR
class Owner:
public class Owner {
private String ownName;
private String ownType;
public Owner (String ownerName, String ownerType) {
ownName = ownerName;
ownType = ownerType;
}
public void setOwnType(String ownerType) {
ownType = ownerType;
}
public String getOwnType() {
return ownType;
}
public void setOwnName(String ownerName) {
ownName = ownerName;
}
public String getOwnName() {
return ownName;
}
public String toString() {
return ownName + " is the owner of the car and is a " + ownType + " driver.";
}
}//end Owner class
This is where my main method is:
import java.util.*;
public class carDemo {
public static void main(String[] args) {
Car car = null;
Owner owner = null;
Car car2 = null;
Owner owner2 = null;
String carName, make, model, ownerName, ownerType;
int yearModel, speed, mileage;
Scanner kb = new Scanner(System.in);
for(int i = 1; i <= 2; i++) {
System.out.print("Car Name: ");
carName = kb.nextLine();
System.out.print("Year: ");
yearModel = kb.nextInt();
kb.nextLine();
System.out.print("Car Make: ");
make = kb.nextLine();
System.out.print("Car Model: ");
model = kb.nextLine();
System.out.print("Car Mileage: ");
mileage = kb.nextInt();
kb.nextLine();
System.out.print("Car Speed: ");
speed = kb.nextInt();
kb.nextLine();
System.out.print("Car owner name: ");
ownerName = kb.nextLine();
System.out.print("Car owner type: ");
ownerType = kb.nextLine();
if(i == 2) {
owner2 = new Owner(ownerName, ownerType);
car2 = new Car(carName, yearModel, make, model, mileage, speed, owner);
} else {
owner = new Owner(ownerName, ownerType);
car = new Car(carName, yearModel, make, model, mileage, speed, owner);
}
} //end for loop
System.out.print(car);
System.out.print(car2);
System.out.println("\n\nThe total mileage of both cars is " + Car.getTotalMileage());
car.accelerate();
car2.accelerate();
System.out.println(car.getName() + " is going " + car.getSpeed() + ".");
System.out.println(car2.getName() + " is going " + car2.getSpeed() + ".");
} //end main
} //end carDemo
Both car and car2 gets the same owner instance in the constructor. One of them should be owner2.
car2 = new Car(carName, yearModel, make, model, mileage, speed, owner2);
// here ^
I've written this code and everything seems to be correct but unfortunately its not giving the correct units sold. im trying to find out if the salesperson ID exists and update that record. Sometimes it prints the right information and sometimes it does not.
import java.util.*;
public class salesPerson {
//salesPerson fields
private int salespersonID;
private String salespersonName;
private String productType;
private int unitsSold = 0;
private double unitPrice;
//Constructor method
public salesPerson(int salespersonID, String salespersonName, String productType, int unitsSold, double unitPrice)
{
this.salespersonID = salespersonID;
this.salespersonName = salespersonName;
this.productType = productType;
this.unitsSold = unitsSold;
this.unitPrice = unitPrice;
}
//Accessor for salesPerson
public int getSalesPersonID(){
return salespersonID;
}
public String getSalesPersonName(){
return salespersonName;
}
public String getProductType(){
return productType;
}
public int getUnitsSold(){
return unitsSold;
}
public double getUnitPrice(){
return unitPrice;
}
public double getTotalSold(){
return unitsSold * unitPrice;
}
//Mutoators for salesPerson
public void setSalesPersonID(int salespersonID){
this.salespersonID = salespersonID;
}
public void setSalesPersonName(String salespersonName) {
this.salespersonName = salespersonName;
}
public void setProductType(String productType){
this.productType = productType;
}
public void setUnitsSold(int unitsSold){
this.unitsSold = this.unitsSold + unitsSold;
}
public void setUnitProce(double unitPrice){
this.unitPrice = unitPrice;
}
public static void main(String[] args)
{
ArrayList<salesPerson> salesPeople = new ArrayList<salesPerson>();
Scanner userInput = new Scanner(System.in);
boolean newRecord = true;
int salespersonID;
String salespersonName;
String productType;
int unitsSold = 0;
double unitPrice;
do{
System.out.println("Please enter the Salesperson Inoformation.");
System.out.print("Salesperson ID: ");
salespersonID = userInput.nextInt();
System.out.print("Salesperson Name: ");
salespersonName = userInput.next();
System.out.print("Product Type: ");
productType = userInput.next();
System.out.print("Units Sold: ");
unitsSold = userInput.nextInt();
System.out.print("Unit Price: ");
unitPrice = userInput.nextDouble();
if(salesPeople.size() == 0)
{
salesPerson tmp = new salesPerson(salespersonID, salespersonName, productType, unitsSold, unitPrice);
salesPeople.add(tmp);
}
else
{
for(int i=0; i < salesPeople.size(); i++) {
if(salesPeople.get(i).getSalesPersonID() == salespersonID)
{
salesPeople.get(i).setUnitsSold(unitsSold);
}
else
{
salesPerson tmp = new salesPerson(salespersonID, salespersonName, productType, unitsSold, unitPrice);
salesPeople.add(tmp);
}
//System.out.println(salesPeople.get(i).getSalesPersonName());
}
}
System.out.print("Would you like to enter more data?(y/n)");
String askNew = userInput.next();
newRecord = (askNew.toLowerCase().equals("y")) ? true : false;
}while(newRecord == true);
for(int i=0; i < salesPeople.size(); i++) {
System.out.println(salesPeople.get(i).getSalesPersonName() + ": "+salesPeople.get(i).getUnitsSold());
}
}
}
This method is probably wrong:
public void setUnitsSold(int unitsSold){
this.unitsSold = this.unitsSold + unitsSold;
}
Replace it by:
public void setUnitsSold(int unitsSold){
this.unitsSold = unitsSold;
}
You also have a problem on your main() method: The for creates a new SalesPerson instance for each element that has a different id from the one you've received on the input.
It's not related with your problem, but you should always (and I mean ALWAYS) start the name of Java classes with capital letters.
I'm doing a program for school that requires me to do things like add/remove/edit/search bands on the Hall of Fame, with which the user adds bands to. I'm having trouble getting the index of a band in the arraylist hallOfFame. Can anybody recommend any solution to this?
Here is my HallofFame Class:
import java.util.*;
public class HallofFame
{
public static ArrayList<Band> hallOfFame = new ArrayList<Band>();
public static Scanner scan = new Scanner(System.in);
public static void main(String[]args){
boolean running = true;
while(running == true){
System.out.println("What would you like to do?");
System.out.println("");
System.out.println("1. Add");
System.out.println("2. Remove");
System.out.println("3. Edit");
System.out.println("4. Clear");
System.out.println("5. Search");
System.out.println("6. Quit");
System.out.println("");
String choice = scan.nextLine();
if(choice.equals ("1")){
add();
}
else if(choice.equals ("2")){
remove();
}
else if(choice.equals ("3")){
edit();
}
else if(choice.equals ("4")){
clear();
}
else if(choice.equals ("5")){
search();
}
else if(choice.equals ("6")){
running = false;
}
}
}
public static void add(){
Scanner booblean = new Scanner(System.in);
System.out.println("What is the name of the band you would like to add?");
String name = scan.nextLine();
System.out.println("What kind of genre is this band?");
String genre = scan.nextLine();
System.out.println("How many members are in the band?");
int numMem = scan.nextInt();
System.out.println("How many songs does this band have?");
int numSongs = scan.nextInt();
System.out.println("How many albums does this band have?");
int numAlbs = scan.nextInt();
System.out.println("Is this band currently active?");
String yesno = booblean.nextLine();
boolean isActive = false;
if(yesno.equalsIgnoreCase ("yes")){
isActive = true;
}
Band b1 = new Band(name, genre, numMem, numSongs, numAlbs, isActive);
hallOfFame.add(b1);
System.out.println("");
System.out.println("The band " + name + " has been added to the database.");
System.out.println("");
}
public static void remove(){
}
public static void edit(){
System.out.println("What band info do you want to edit?");
String editband = scan.nextLine();
}
public static void search(){
System.out.println("What band are you looking for?");
String searchband = scan.nextLine();
}
public static void clear(){
hallOfFame.clear();
}
}
And here's the code to the class Band:
public class Band
{
//1.Class Variables
private String nameOfBand;
private String[] members;
private String genre;
private int numberOfMembers;
private int numberOfSongs;
private int numberOfAlbums;
private boolean isActive;
//2. Constructors
public Band(String name, String genre, int numMem, int numSongs, int numAlbs, boolean isActive)
{
}
//3. Methods
//setters
public void setName(String newName)
{
nameOfBand = newName;
}
public void setGenre(String s)
{
genre = s;
}
public void setNumberOfMembers(int num)
{
numberOfMembers = num;
}
public void setNumberOfSongs(int numsongs){
numberOfSongs = numsongs;
}
public void setNumberOfAlbums(int numalbs){
numberOfAlbums = numalbs;
}
public void setIsActive(boolean isactive){
isActive = isactive;
}
public String getName()
{
return nameOfBand;
}
public String getGenre(){
return genre;
}
public int getNumberofMembers(){
return numberOfMembers;
}
public int getNumberofSongs(){
return numberOfSongs;
}
public int getNumberofAlbums(){
return numberOfAlbums;
}
public boolean getIsActive(){
return isActive;
}
#Override public String toString()
{
String output = "";
output += "Name: " + nameOfBand + "\n";
output += "Genre: " + genre + "\n";
output += "Number of members: " + numberOfMembers + "\n";
output += "Number of songs: " + numberOfSongs + "\n";
output += "Number of albums: " + numberOfAlbums + "\n";
output += "Is this band active: " + isActive + "\n";
return output;
}
}
You can do the following code to search Band with name in ArrayList.
public static void search(){
System.out.println("What band are you looking for?");
String searchband = scan.nextLine();
boolean bandFooud = false;
for(Band band : hallOfFame)
{
if(band.getName().equals(searchband))
{
bandFooud = true;// Set the flag to true to indicate the band is found.
//Make your code to display the band information here.
break;
}
}
if(!bandFooud){
System.out.printf("Band %s is not found.");
}
}
Maybe you want something like this
public static Band search(){
System.out.println("What band are you looking for?");
String searchband = scan.nextLine();
for (Band b : hallOfFame){
if (searchBand.equals(b.name){
return b;
break;
}
}
return null;
}
Also, consider override the toString() method in Band if you want the search() method to return void and just print out the Band info.
public class Band
{
//1.Class Variables
private String nameOfBand;
private String[] members;
private String genre;
private int numberOfMembers;
private int numberOfSongs;
private int numberOfAlbums;
private boolean isActive;
//2. Constructors
public Band(String name, String genre, int numMem, int numSongs,
int numAlbs, boolean isActive)
{
nameOfBand = name;
this.genre = genre;
numberOfMemembers = numMem;
numberOfSongs = numSongs;
numberOfAlbums = numAlbs;
this.isActive = isActive;
}
#Override
public String toString(){
return "Name: " + nameOfBand "
+ "Genre: " + genre
+ " Members: " + numOfMembers
+ " Songs: " + numOfSongs
+ " Albums:" + numOfAlbums
+ " Active? " + isActive;
}
Then you could do this
public static void search(){
System.out.println("What band are you looking for?");
String searchband = scan.nextLine();
for (Band b : hallOfFame){
if (searchBand.equals(b.name){
System.out.println(b);
break;
}
}
}