Somehow I try to make a user selection of which cook has to prepare the food. I already created the variable whichcook and place that where cook1 used to stay. But I don't know how to carry on. I want to let the user select between either "Jan de Vries" or "SinBad" to prepare. So the methode deliverer.delivered(whichcook, customer); carries on with the selected name. I figure if have to use instanceof I guess, but don't know how to really do that. I know how to make a user-input and cases etc that's not the problem. It's more about how to isolate the right instance!!
Someone a key suggestion???
java
package KebabStore;
public class DamascusKebab
{
public static int cooksnumber;
public static int deliverersnumber;
#SuppressWarnings("unused")
public static void main(String[] args)
{
Cook cook1 = new Cook ("Jan de Vries", "Butcherknife 1", "1212-IS", "Allahmelo", 123456);
Cook cook2 = new Cook ("Sinbad", "Camelhumb 2","2323-IS", "Halal-lem", 654321);
Deliverer deliverer1 = new Deliverer ("Ali Baba", "Helmgras 11", "3434-JH", "Ji-Hattem",456789);
Deliverer deliverer2 = new Deliverer ("Muammar", "Zadeldreef 22", "4545-JH", "Moskemenade", 987654);
Customer customer = new Customer ("Piet Hein", "Klantlaan 25", "5656-KL", "Darmstadt");
cooksnumber = Cook.numberofcooks;
deliverersnumber = Deliverer.numberofdeliverers;
Cook whichcook = cook1;
deliverer.delivered(whichcook, customer);
}
}
How are going to get the user input?
if it's from the Scanner
Cook whichcook;
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt()
if (choice == 1)
whichcook = cook1;
else
whichcook = cook2;
deliverer.delivered(whichcook, customer);
Related
I doing a bigger school project (first part of basic objective programming in java - so not touched extended, polyphorism etc yet, thats next part), but run in to a small problem and tried for couple of days to find solution (thru books and internet). I constructed different ArrayLists in one class and different classes (at least two) should get access to them.
public class Customer
{
public void subMenuCustomer()
{
............code............
int subMenuCust;
ServiceLogic addCustomer = new ServiceLogic();
ServiceLogic listAllCustomers = new ServiceLogic();
while(true)
{
System.out.println("Please Choose your preference: ");
System.out.println("Create account, press \"1\": ");
System.out.println("Get list of clustomers, press \"2\": ");
System.out.println("Log out, press \"0\": ";
subMenuCust = input.nextInt();
switch(subMenuCust)
{
case 1 ://Call method createCustomer in class ServiceTech to add new customers
addCustomer.createCustomer(name, lastname, ssNo);
break;
case 3
listAllCustomers.getCustomer();
............more code..............
}
}
When user has added details (social secuity number, name and lastname) it is stored in seperate ArrayList. These three ArrayList are added(merge/concat) together to a fourth ArayList, listCustomer , so that all elements from the three ArrayList end up in same index [101 -54 Clark Kent, 242-42 Linus Thorvalds, ...].
import java.util.ArrayList;
import java.util.Scanner;
public class ServiceLogic
{
//Create new ArrayLists of Strings
private ArrayList<String> listSSNoCustomers = new ArrayList<>();
private ArrayList<String> listNameCustomers = new ArrayList<>();
private ArrayList<String> listLastnameCustomers = new ArrayList<>();
private ArrayList<String> listCustomers;
Scanner input = new Scanner(System.in);
public boolean createCustomer(String name, String lastname, String ssNo) //
{
System.out.println("Write social security number; ");
ssNo = input.next();
//loop to check that it is a uniq social security number
for(String ssNumber : listSSNoCustomers)
{
if (ssNumber.equals(ssNo))
{
System.out.println("This customer already exist. Must be uniq social security number.");
return true;
}
}
//If social security number is not on list, add it
//and continue add first name and surname
listSSNoCustomers.add(ssNo);
System.out.println(ssNo);
System.out.println("Write firstname; ");
name = input.next();
listNameCustomers.add(name);
System.out.println(name);
System.out.println("Write lastnamame; ");
surname = input.next();
listSurnameCustomers.add(lastname);
System.out.println(lastname);
return false;
}
public void setListCustomer(ArrayList<String> listCustomers)
{
this.listCustomers = listCustomers;
}
public ArrayList<String> getCustomer()
{
//ArrayList<String> listCustomers = new ArrayList<>();
for(int i = 0; i <listSSNoCustomers.size(); i++)
{
listCustomers.add(listSSNoCustomers.get(i) + " " + listNameCustomers.get(i) + " " + listFirstnameCustomers.get(i));
}
System.out.println("customer" + listCustomers);
return listCustomers;
}
}
According to the specification we got, when user want to see list of all customer the outputs should be in format [666-66 Bruce Wayne, 242-42 Linus Thorvalds, ...].
When user (staff) choose to enter details in class Customer ( Case 1 ) it works and elements get stored in the Arraylists for social security numbers, name and lastname (have checked that) .
The problem: when I run I can add customers, but when I try to get a list of customer the output: [] . I tried different solution, but same output only empty between the brackets.
So the question, how do I get ouput to work when user choose case 2 to get a list of all cutomers?
I have a task where I need to create a program for "TotalCompetitions", which consists of multiple "Competition". I need to create an application which allows user to do various options related to TotalCompetition.
If the first option ("Create a new competition") is selected, by typing "1" and then pressing Enter,
the program should call the addNewCompetition method of the TotalCompetitions class to add
a new competition with a given name. After creating a new competition, the program should goes
back to the main menu.
The second option is to add entries to the competition. However, when I try to call the addEntry() method located in the Competition class to the newly created competition, it doesn't work as it is still of TotalCompetitions type. How can I access the newly created competition to access the required method?
At the moment, this is my code:
public class TotalCompetitions {
private ArrayList<Competition> competitions;
public TotalCompetitions() {
this.competitions = new ArrayList<Competition>();
}
public ArrayList<Competition> getCompetitions() {
return competitions;
}
public void setCompetitions(ArrayList<Competition> competitions) {
this.competitions = competitions;
}
public Competition addNewCompetition(String name, int id) {
Competition newCompetition = new Competition(name, id);
return newCompetition;
}
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
TotalCompetitions sc = new TotalCompetitions();
int competitionId = 0;
while (true) {
System.out.println("Please select an option. Type 5 to exit.");
System.out.println("1. Create a new competition");
System.out.println("2. Add new entries");
System.out.println("3. Draw winners");
System.out.println("4. Get a summary report");
System.out.println("5. Exit");
String command = keyboard.next();
if (command.equals("1")) {
keyboard.nextLine();
System.out.println("Competition name:");
String name = keyboard.nextLine();
competitionId += 1;
sc.addNewCompetition(name, competitionId);
System.out.println("A new competition has been created!");
System.out.println("Competition ID: " + competitionId + ", Competition Name: " + name);
}
else if (command.equals("2")) {
sc.addEntry();
}
else if (command.equals("5")) {
System.out.println("The end");
break;
}
}
sc.addNewCompetition(name, competitionId); returns a new object of the type Competition, however, this new object is not used by your program. The variable sc is still of the type TotalCompetition which is not related to the type Competition. Furthermore, your method addNewCompetition(String id, int id) does not add the new Object to the ArrayList. The solution would be to store the object which is returned by the sc.addNewCompetition(...) call and call addEntry() on this object.
public class Catalogue() {
private List<Book> booksAvailable;
private List<Book> booksRented:
public Catalogue() {
booksAvailable.add(new Book("Matrix", 1999, new Genre("SciFi"), 3));
booksAvailable.add(new Book("Jurassic Park", 1999, new Genre("SciFi"), 3));
boosAvailable.add(new Book("Terminator", 1999, new Genre("SciFi"), 3));
booksRented = new LinkedList<Book> ();
}
public void rentBook() {
System.out.println("Rent a book:");
System.out.println("Enter the title of a book you want to rent: ");
String name = In.NextLine();
for (Book book: booksAvailable) {
if (book.getName.equals(name)) {
System.out.println("Renting " + name);
booksAvailable.remove(book);
booksRented.add(book);
break;
} else {
System.out.println("No such books found");
}
}
}
}
While running this code can only rent the Matrix book. When I try to rent another book like Jurassic park it says that no books found. When I close the program and again run it and try to rent the second book then it again says the books not found. Please help me with this problem. What is the problem that i have in this code. Thanks
As others have pointer out modifying a list while you're iterating over it is dangerous.
I would recommend trying it with a HashMap instead, especially if name is the only field you're looking at.
public class Catalogue {
private Map<String, Book> booksAvailable;
private Map<String, Book> booksRented;
public Catalogue() {
booksAvailable = new HashMap<>();
booksAvailable.put("Matrix", new Book("Matrix", 1999, new Genre("SciFi"), 3));
booksAvailable.put("Jurassic Park", new Book("Jurassic Park", 1999, new Genre("SciFi"), 3));
booksAvailable.put("Terminator", new Book("Terminator", 1999, new Genre("SciFi"), 3));
booksRented = new HashMap<>();
}
public void rentBook() {
System.out.println("Rent a book:");
System.out.println("Enter the title of a book you want to rent: ");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
if (booksAvailable.containsKey(name)) {
Book book = booksAvailable.get(name);
System.out.println("Renting " + name);
booksAvailable.remove(name);
booksRented.put(name, book);
} else {
System.out.println("No such books found");
}
}
}
I just tried your code(after modifying it to compile) and it works.
However when running on for example Jurasic Park it will say that the book is not found and then it will rent it, because that print statement is in the for loop.
I tried rewriting it to use streams and optionals, and I got this code that seems to be working
public void rentBook() {
System.out.println("Rent a book:");
System.out.println("Enter the title of a book you want to rent: ");
String name = "Jurassic Park";
Optional<Book> book = booksAvailable.stream().filter(b -> b.name.equals(name)).findFirst();
if(book.isPresent()) {
System.out.println("Renting " + name);
booksAvailable.remove(book.get());
booksRented.add(book.get());
}
else
System.out.println("No such book found");
}
When you call booksAvailable.remove() it will effectively removed from current iteration.
Hence when you access next(), it might result in unexpected behavior.
Edit
You cannot rent other book other than first book because of your code handling.
You have System.out.println("No such books found"); in the else statement inside loop. So if you input a book other than the first book, the test fail and the statement is printed.
To correct this you can use a flag to indicate a book is found or not, and print the statement outside the loop;
boolean rented = false;
for (Book b : books) {
if (found) {
rented = true;
}
}
if (!rented) {
// print no such book message
}
The exercise is, to create an arraylist for a class, where a User can enter "Guestnumber" + "Guestname" + "Guestemail".
In the menu you could remove an existing "Guest" with all the Information. Thats the code for it: (it works)
public void gastAendern() {
System.out.println("Guestnumber to delete:");
Scanner sc = new Scanner(System.in);
String input = sc.next();
for (Gast test : verwaltungG) {
int nummer = Integer.parseInt(input);
if (test.getgNr() == nummer) {
verwaltungG.remove(test);
a = 1;
break;
}
}
if(a==0) {
System.out.println("Guestnumber is not used");
verwaltungG is the ArrayList
Gast is the class for get+set
But now I got a problem to change an existing Guest, like for example:
I ask to type in the Guestnumber OR the Guestname OR the Guestmail to change it (I have to do it for all 3). So I have really no idea how to change it. I looked through Stackoverflow, google etc. but it only shows how to change them with List.set, but I don't know if it works with my kind of Problem, because I don't know how to use it.
You can do something similar as you are doing for delete, instead of remove - you need to change the details you need update :
System.out.println("Guestnumber to update :");
Scanner sc = new Scanner(System.in);
String input = sc.next();
for (Gast test : verwaltungG) {
int nummer = Integer.parseInt(input);
if (test.getgNr() == nummer) {
System.out.println("Enter new guest name for guest number : "+nummer );
String name = sc.nextLine();
System.out.println("Enter new guest email for guest number : "+nummer );
String email = sc.nextLine();
// now update the details
test.setName(name);
test.setEmail(email);
break;
}
}
i need to retrive info form a private object but im stuck at this.. its keep say there is a
constructor error
public class Screen{
private movie movieObject;
private Screen(movie movieObject){
this.movieObject = movieObject;
}
private ArrayList<movie> movie = new ArrayList<movie>
public void add(){
String title = keyboard.readString("Enter movie title > ");
String name = keyboard.readString("Enter theatre name > ");
movie add = new movie(title,name) // there is error in this part
Maybe: Line 9 above...
new ArrayList<movie>();
Also if you use standard naming conventions for classes would help us and yourself.
Your last line is quite strange to me. Try instead:
movie.add(new movie(title,name));
Also maybe the movie list instanciation should be done in the constructor ?
Your letter casing is wrong
movie add = new movie(..)
Your class is Movie with capital M, not movie. You should be getting error all over the place, not just the above line
Also, you're missing the (); here new ArrayList<movie>();
And a ; here movie add = new movie(..). <--
But that still doesn't take away from the problems with the letter casing.
It does look like you want something more like this
ArrayList<Movie> movies = new ArrayList<Movie>();
public void add(){
String title = keyboard.readString("Enter movie title > ");
String name = keyboard.readString("Enter theatre name > ");
movies.add(new Movie(title, name));
}
Or maybe you should do something more like this, where your method add a movie, and doesn't concern itself with getting input from keyboard
public void addMovie(Movie movie) {
movies.add(movie);
}
Then in your main you can do something like this
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
Screen screen = new Screen();
System.out.println("Enter a title");
String title = keyboard.nextLine();
System.out.println("Enter a name:);
String name = keyboard.nextLine();
screen.addMovie(new Movie(title, name));
}
The last examples make a lot more sense than the way you are trying to do it.