Please help and answer this in java :) Description: read user input from keyboar
ID: 3888790 • Letter: P
Question
Please help and answer this in java :)
Description:
read user input from keyboard
provide user choices with a menu
use repetition statements
use selection statements
override toString
create a jar file that includes the Java source code
In this assignment I encourage you to work with a partner so you can help each other refresh concepts that have been covered in CSIS-1400. It is important to be responsive and that each partner contributes his/her fair share. If you have concerns about your partner bring it up as early as possible. First try to resolve the issue with your partner. If that is difficult talk to your instructure.
Each code file should include a comment on top that lists both students and the name of the assignment.
Together write a program that keeps track of a list of items and that provides the user with a menu that allows the user
to add, remove, list items, etc. For more details read the instructions below.
Instructions:
Decide which items you would like to store in the list. (e.g. books, bikes, gemstones, etc.) It can by any thing but not cars (I used that for the example) and not people.
Create a class that represents the item you chose. Here are some requirements your class needs to fulfill:
It can not be cars or people (see above)
The class needs to have at least 3 attributes you are keeping track of (e.g. year, make, model)
It needs to have two additional fields:
o a unique id that cannot be changed once created (like a primary key in a database)
o a static count that is used to initialize the id with a unique number Notice: At this point, we have a total of at least 5 attributes
It needs a parameterized constructor.
It allows the user to provide values for all the attributes you are keeping track of but not for the id nor for the count. (in our case that would be 3 parameters: year, make, and model)
The constructor creates a unique id – e.g. an 8 digit number for each item based on the static field count. (e.g. 12345678 + count++; In this example the smallest id would be 12345678)
It needs a getter for each of the fields except for the static count (in our case that would be 4 getters) The static count should not be exposed to another class. It is only used to initialize the unique id in the
constructor.
Hint: http://stackoverflow.com/questions/7221691/is-there-a-way-to-automatically-generate-getters-and-setters-in-eclipse It needs to override the method toString.
The method toString has no parameters and returns a string that represents an instance of the class.
This string needs to include the values of all fields except for count (e.g. 2015 Honda Accord id: 12345679 ) Labels should be added when the value itself is not self-explanatory. (e.g. id)
Sample Output :
Instructions continued:
At this point you have one class that represents one item (e.g. Book, Bike, Gemstone, etc.). This class does not contain the list of items nor should it include any print statements.
You still need at least one more class (optionally two) that include(s) the list of items, the menu with options, and the user communication (input / output)
Create one or two additional classes to do the following:
Declare an ArrayList of items ( each element in the list should be an instance of the new Java type you just wrote)
Initialize the list with four different elements (hard coded).
Display a menu with the following choices until the user chooses to exit:
Important: replace the word item and items with the actual type you store in the list. (e.g. Display all cars)
Provide the functionality chosen by the user.
Use private methods to print the menu and to display, find, add, or delete items. Using private methods in such a way structures the code and to makes the code more clear and easier to read.
When the user wants to find or delete an item s/he should be prompted for the id number.
If the user provides and id number that doesn’t exist, an appropriate message should be displayed.
(e.g. The id ... could not be found.)
If the id number could be found the item information should be displayed as part of the response
(e.g. 2015 Honda Accord id:12345679 if the item was found or 2015
Honda Accord has been deleted if it was deleted)
If the user enters an invalid choice from the menu (e.g. 9) an
appropriate message should be printed (e.g. Enter a selection 1 - 6) If the user enters 6 a message should be printed before terminating the program (e.g. Good bye)
Below you can find a sample output. This sample is about cars. Your output needs show elements of the item type you chose above.
Explanation / Answer
Note : i selected a Bike class.So I will develop the program Based on that.
_______________
Bike.java
public class Bike {
//Declaring static variable
static int count=12345678;
//Declaring instance variables
private int id;
private String name;
private double price;
//Parameterized constructor
public Bike(String name, double price) {
super();
id=count++;
this.name = name;
this.price = price;
}
//getters and setters
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "id=" + id + ", name=" + name + ", price=" + price ;
}
}
_________________
Test.java
import java.util.ArrayList;
import java.util.Scanner;
public class Test {
static Scanner sc = new Scanner(System.in);;
static ArrayList < Bike > arl = new ArrayList < Bike > ();
public static void main(String[] args) {
//Creating an Bike class objects and Store them in the ArrayList
Bike b1 = new Bike("Hyosung", 450000);
Bike b2 = new Bike("Trimph", 750000);
Bike b3 = new Bike("Royal Enfield", 150000);
Bike b4 = new Bike("Harley Davidson", 950000);
//Adding bikes to the ArrayList
arl.add(b1);
arl.add(b2);
arl.add(b3);
arl.add(b4);
/* This while loop continues to execute
* until the user enters a 6 as input choice
*/
while (true) {
int choice = menu();
switch (choice) {
case 1:
{
displayBikes();
continue;
}
case 2:
{
addBike();
continue;
}
case 3:
{
findBike();
continue;
}
case 4:
{
deleteBike();
continue;
}
case 5:
{
numbersOfItemsInList();
continue;
}
case 6:
{
System.out.println("** Program Exit **");
break;
}
default:
{
System.out.println("** Invalid.Selection Must be between 1-6 **");
continue;
}
}
break;
}
}
//This method will search for the bikes in the list
private static void findBike() {
int id;
System.out.print("Enter id :");
id = sc.nextInt();
int flag = 0;
for (int i = 0; i < arl.size(); i++) {
if (arl.get(i).getId() == id) {
System.out.println(arl.get(i).getId() + " " + arl.get(i).getName() + " " + arl.get(i).getPrice());
flag = 1;
}
}
if (flag == 0) {
System.out.println("** Bike not found **");
}
}
private static void numbersOfItemsInList() {
System.out.println("No of Bikes :" + arl.size());
}
//This method will delete a bike from the list
private static void deleteBike() {
int id;
int flag = 0;
System.out.print("Enter id :");
id = sc.nextInt();
int i = 0;
while (i < arl.size()) {
if (arl.get(i).getId() == id) {
System.out.println(arl.get(i).getId() + " " + arl.get(i).getName() + " " + arl.get(i).getPrice() + " has been deleted.");
arl.remove(i);
flag = 1;
break;
} else {
i++;
}
}
if (flag == 0)
System.out.println(id + " could not be found.");
}
//This method will add a bike to the list
private static void addBike() {
String name;
double price;
sc.nextLine();
System.out.print("Enter name :");
name = sc.nextLine();
System.out.print("Enter Price :");
price = sc.nextDouble();
Bike b = new Bike(name, price);
arl.add(b);
}
//This method will display the bikes in the list
private static void displayBikes() {
if (arl.size() > 0) {
for (int i = 0; i < arl.size(); i++) {
System.out.println(arl.get(i).getId() + " " + arl.get(i).getName() + " " + arl.get(i).getPrice());
}
} else {
System.out.println("** No Bikes in the List **");
}
}
//This method will display the menu
private static int menu() {
System.out.println(" 1. Display all Bikes");
System.out.println("2. Add a Bike");
System.out.println("3. Find a Bike");
System.out.println("4. Delete a Bike");
System.out.println("5. Number of Bikes in list");
System.out.println("6. Exit");
System.out.println("Enter your selection:");
int choice = sc.nextInt();
return choice;
}
}
______________________
Output:
1. Display all Bikes
2. Add a Bike
3. Find a Bike
4. Delete a Bike
5. Number of Bikes in list
6. Exit
Enter your selection:
1
12345678 Hyosung 450000.0
12345679 Trimph 750000.0
12345680 Royal Enfield 150000.0
12345681 Harley Davidson 950000.0
1. Display all Bikes
2. Add a Bike
3. Find a Bike
4. Delete a Bike
5. Number of Bikes in list
6. Exit
Enter your selection:
2
Enter name :KTM
Enter Price :250000
1. Display all Bikes
2. Add a Bike
3. Find a Bike
4. Delete a Bike
5. Number of Bikes in list
6. Exit
Enter your selection:
3
Enter id :123456789
** Bike not found **
1. Display all Bikes
2. Add a Bike
3. Find a Bike
4. Delete a Bike
5. Number of Bikes in list
6. Exit
Enter your selection:
3
Enter id :12345678
12345678 Hyosung 450000.0
1. Display all Bikes
2. Add a Bike
3. Find a Bike
4. Delete a Bike
5. Number of Bikes in list
6. Exit
Enter your selection:
4
Enter id :12345678
12345678 Hyosung 450000.0 has been deleted.
1. Display all Bikes
2. Add a Bike
3. Find a Bike
4. Delete a Bike
5. Number of Bikes in list
6. Exit
Enter your selection:
1
12345679 Trimph 750000.0
12345680 Royal Enfield 150000.0
12345681 Harley Davidson 950000.0
12345682 KTM 250000.0
1. Display all Bikes
2. Add a Bike
3. Find a Bike
4. Delete a Bike
5. Number of Bikes in list
6. Exit
Enter your selection:
5
No of Bikes :4
1. Display all Bikes
2. Add a Bike
3. Find a Bike
4. Delete a Bike
5. Number of Bikes in list
6. Exit
Enter your selection:
6
** Program Exit **
______________Could u plz check now...