Question

In: Computer Science

This is my code I need to complete it? //The code package arraylists; import java.util.ArrayList; import...

This is my code I need to complete it?

//The code

package arraylists;

import java.util.ArrayList;
import java.util.Scanner;

/**
*
*
*/
public class SoftOpening {
  
public static void main(String[] args) {
  
  
Scanner input = new Scanner(System.in);
  
ArrayList foodList = generateMenu();
  
User user = generateUser(input);
user.introduce();
  
userBuyFood(foodList, user, input);
user.introduce();
}
public static ArrayList generateMenu(){
  
ArrayList foodList = new ArrayList<>();
  
Food pizza1 =new Food(1,"pizza","Seafood",11,12);
Food pizza2 =new Food(2,"pizza","Beef",9,10);
Food Friedrice =new Food(3,"fried rice","Seafood",5,12);
Food Noodles =new Food(4,"noodles","Beaf",6,14);
  
foodList.add(pizza1);
foodList.add(pizza2);
foodList.add(Friedrice);
foodList.add(Noodles);
  
return foodList ;
}
  
public static void getMenu(ArrayList foodlist){
  
  
for (int i =0;i
System.out.println(foodlist.get(i));

}
  
}
  
}
Q1: generateUser(Scanner in) that generates a user whose account and money are added by using the Scanner parameter. ?

Q2: userBuyFood(ArrayList, User user, Scanner in) to invoke the getMenu(), ask the user to selects the foods in the menu, compute the cost and invoke the addExpense() from the User class?

Q3: Invoke the method introduce() of the User object to show his/her balance ?

public class Food {
private int id;
private String name;
private String type;
private int size;
private double price;
  
public Food(int id, String name,String type,int size,double price){
this.id= id;
this.name= name;
this.type= type;
this.size= size;
this.price= price;
  
}

public void setId(int id) {
this.id = id;
}
  
public void setName(String name) {
this.name = name;
}

public void setType(String type) {
this.type = type;
}

public void setSize(int size) {
this.size = size;
}
  
public void setPrice(double price) {
this.price = price;
}
  
public int getId() {
return id;
}
  
public String getName() {
return name;
}
  
public String getType() {
return type;
}

public double getPrice() {
return price;
}

public int getSize() {
return size;
}

  
@Override
public String toString(){
return " [Id] "+id + " [Name] "+ name+ " [Type] "+size+" [Size] "+type+" [Type] "+price+" [Price] ";

}   
}

public class FoodTest {
  
public static void main(String[] args) {
  
ArrayList foodList = new ArrayList<>();
Food pizza1 =new Food(1,"pizza","Seafood",11,12);
Food pizza2 =new Food(2,"pizza","Beef",9,10);
Food Friedrice =new Food(3,"fried rice","Seafood",5,12);
Food Noodles =new Food(4,"noodles","Beaf",6,14);
  
foodList.add(pizza1);
foodList.add(pizza2);
foodList.add(Friedrice);
foodList.add(Noodles);
  
for (int i =0;i
System.out.println(foodList.get(i));

}
  
  
  
  
}
  
}

--------------

package arraylists;

import java.util.Scanner;

/**

*

*

*/

public class User {

   private String accountName;

   private String password ;

   private double money;

public void setAccountName(String accountName) {

this.accountName = accountName;

}

  

public void setPassword(String password) {

this.password = password;

}

  

public void setMoney(double money) {

this.money = money;

}

public String getAccountName() {

return accountName;

}

  

public String getPassword() {

return password;

}

  

public double getMoney() {

return money;

}

  

  

public void introduce() {

  

  

System.out.println(" The account Name is "+accountName+" The balance is "+money+" $ ");

  

}

  

   public void addExpense(double value, Scanner in){

   if (value > money)

   System.out.println(" Plan to expence "+value+" $ but no sufficient ");

   else {

   System.out.println(" Plan to expence "+value+" $ ");

   System.out.println(" please enter password ");

  

   String password1 = in.nextLine();

   if (password.equals(password1))

   {

   money =money-value;

  

   System.out.println(" expence "+value+" $ and balance "+money+" $ ");

  

   }

  

   }

   }

public void addMoney(double value){

  

   money += value;

   System.out.println(" Got "+value+" $ as income , balance is "+money+" $ ");

   //System.out.println(accountName+"has a balance of "+money);

}

}

Solutions

Expert Solution


Please find answers to all your questions ( At the end of this I have added screenshot of my output and java code for all classes along with function so you can directly refer to it ):

Q1: generateUser(Scanner in) that generates a user whose account and money are added by using the Scanner parameter. ?

// generateUser mehote with Scanner input parameter to read input from user
        private static User generateUser(Scanner input) {

                // get accountName
                System.out.println("Enter your accountName: ");
                String accountName = input.nextLine();
                
                // get password         
                System.out.println("Enter your password: ");
                String password = input.nextLine();
                
                // get add money amount
                System.out.println("How much money to add in your account : ");
                String money = input.nextLine();
                double money1 = Double.parseDouble(money);
                
                User u1 = new User();
                // call respective setter to set the value for user object and then return that user obj
                u1.setAccountName(accountName);
                u1.setPassword(password);
                u1.setMoney(money1);
                
                return u1;
        }

Q2: userBuyFood(ArrayList, User user, Scanner in) to invoke the getMenu(), ask the user to selects the foods in the menu, compute the cost and invoke the addExpense() from the User class?

    // needed userBuyFood method with ArrayList, User user, Scanner in parameters
        private static void userBuyFood(ArrayList<Food> foodList, User user,
                        Scanner input) {
                // call getMenu to show ask the user to selects the foods in the menu
                getMenu(foodList);
                // Here i am asking user to enter id of food to match with foodlist.
                System.out.println(" Select food from the menu ( Enter Id ): ");
                int id = Integer.parseInt(input.nextLine());
                double price = 0;
                
                // check the matching id and get the corresponding price of that food item
                for (Food food : foodList) {
                        if(food.getId() == id){
                                price = food.getPrice();
                                break;
                        }
                }
                // if price is 0 means user has entered wrong data in this case just return.
                if(price == 0) {
                        System.out.println("Wrong input");
                        return;
                }
                user.addExpense(price, input);
        }

Q3: Invoke the method introduce() of the User object to show his/her balance ?

This method was already there no need to change anything it is working properly.

Please see entire code for SoftOpening.java i have not done any changes in User.java and Food.java

1. SoftOpening.java

package arraylists;

import java.util.ArrayList;
import java.util.Scanner;

public class SoftOpening {

        public static void main(String[] args) {

                Scanner input = new Scanner(System.in);
                
                ArrayList<Food> foodList = generateMenu();

                User user = generateUser(input);
                user.introduce();

                userBuyFood(foodList, user, input);
                user.introduce();
                
        }

        // needed userBuyFood method with ArrayList, User user, Scanner in parameters
        private static void userBuyFood(ArrayList<Food> foodList, User user,
                        Scanner input) {
                // call getMenu to show ask the user to selects the foods in the menu
                getMenu(foodList);
                // Here i am asking user to enter id of food to match with foodlist.
                System.out.println(" Select food from the menu ( Enter Id ): ");
                int id = Integer.parseInt(input.nextLine());
                double price = 0;
                
                // check the matching id and get the corresponding price of that food item
                for (Food food : foodList) {
                        if(food.getId() == id){
                                price = food.getPrice();
                                break;
                        }
                }
                // if price is 0 means user has entered wrong data in this case just return.
                if(price == 0) {
                        System.out.println("Wrong input");
                        return;
                }
                user.addExpense(price, input);
        }

        // generateUser mehote with Scanner input parameter to read input from user
        private static User generateUser(Scanner input) {

                // get accountName
                System.out.println("Enter your accountName: ");
                String accountName = input.nextLine();
                
                // get password         
                System.out.println("Enter your password: ");
                String password = input.nextLine();
                
                // get add money amount
                System.out.println("How much money to add in your account : ");
                String money = input.nextLine();
                double money1 = Double.parseDouble(money);
                
                User u1 = new User();
                // call respective setter to set the value for user object and then return that user obj
                u1.setAccountName(accountName);
                u1.setPassword(password);
                u1.setMoney(money1);
                
                return u1;
        }

        public static ArrayList<Food> generateMenu(){

                ArrayList<Food> foodList = new ArrayList<Food>();

                Food pizza1 = new Food(1,"pizza","Seafood",11,12);
                Food pizza2 = new Food(2,"pizza","Beef",9,10);
                Food Friedrice = new Food(3,"fried rice","Seafood",5,12);
                Food Noodles = new Food(4,"noodles","Beaf",6,14);

                foodList.add(pizza1);
                foodList.add(pizza2);
                foodList.add(Friedrice);
                foodList.add(Noodles);

                return foodList ;
        }

        public static void getMenu(ArrayList<Food> foodlist){
                for (int i =0; i < foodlist.size(); i++)
                        System.out.println(foodlist.get(i));
        }
}

Output screen shot :


Related Solutions

Java: Complete the methods marked TODO. Will rate your answer! -------------------------------------------------------------------------------------------------- package search; import java.util.ArrayList; import...
Java: Complete the methods marked TODO. Will rate your answer! -------------------------------------------------------------------------------------------------- package search; import java.util.ArrayList; import java.util.List; /** * An abstraction over the idea of a search. * * @author liberato * * @param <T> : generic type. */ public abstract class Searcher<T> { protected final SearchProblem<T> searchProblem; protected final List<T> visited; protected List<T> solution; /**    * Instantiates a searcher.    *    * @param searchProblem    *            the search problem for which this searcher will find and   ...
create code for deletestudentbyID Number for choice == 4 package courseecom616; import java.util.Scanner; import java.util.ArrayList; public...
create code for deletestudentbyID Number for choice == 4 package courseecom616; import java.util.Scanner; import java.util.ArrayList; public class CourseeCOM616 {        private String courseName;     private String[] studentsName = new String[1];     private String studentId;        private int numberOfStudents;        public CourseeCOM616(String courseName) {         this.courseName = courseName;     }     public String[] getStudentsName() {         return studentsName;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public String getStudentId() {         return studentId;    ...
This is the code that needs to be completed... import java.util.ArrayList; import java.util.Collections; /** * Write...
This is the code that needs to be completed... import java.util.ArrayList; import java.util.Collections; /** * Write a description of class SpellChecker here. * * @author (your name) * @version (a version number or a date) */ public class SpellChecker { private ArrayList words; private DictReader reader; /** * Constructor for objects of class SpellChecker */ public SpellChecker() { reader = new DictReader("words.txt"); words = reader.getDictionary(); } /** * This method returns the number of words in the dictionary. * Change...
Download labSerialization.zip and unzip it: ListVsSetDemo.java: package labSerialization; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List;...
Download labSerialization.zip and unzip it: ListVsSetDemo.java: package labSerialization; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; /** * Demonstrates the different behavior of lists and sets. * Author(s): Starter Code */ public class ListVsSetDemo { private final List<ColoredSquare> list; private final Set<ColoredSquare> set; /** * Initializes the fields list and set with the elements provided. * * @param elements */ public ListVsSetDemo(ColoredSquare... elements) { list = new ArrayList<>(Arrays.asList(elements)); set = new HashSet<>(Arrays.asList(elements)); } /** * Creates a string...
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task>...
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task> list;//make private array public ToDoList() { //this keyword refers to the current object in a method or constructor this.list = new ArrayList<>(); } public Task[] getSortedList() { Task[] sortedList = new Task[this.list.size()];//.size: gives he number of elements contained in the array //fills array with given values by using a for loop for (int i = 0; i < this.list.size(); i++) { sortedList[i] = this.list.get(i);...
I need to translate my java code into C code. import java.util.Scanner; class CS_Lab3 { public...
I need to translate my java code into C code. import java.util.Scanner; class CS_Lab3 { public static void main( String args[] ) { Scanner input = new Scanner( System.in ); // create array to hold user input int nums[] = new int[10]; int i = 0, truthCount = 0; char result = 'F', result2 = 'F'; // ask user to enter integers System.out.print("Please Enter 10 Different integers: "); // gather input into array for ( i = 0; i <...
Implement a Factory Design Pattern for the code below: MAIN: import java.util.ArrayList; import java.util.List; import java.util.Random;...
Implement a Factory Design Pattern for the code below: MAIN: import java.util.ArrayList; import java.util.List; import java.util.Random; public class Main { public static void main(String[] args) { Character char1 = new Orc("Grumlin"); Character char2 = new Elf("Therae"); int damageDealt = char1.attackEnemy(); System.out.println(char1.name + " has attacked an enemy " + "and dealt " + damageDealt + " damage"); char1.hasCastSpellSkill = true; damageDealt = char1.attackEnemy(); System.out.println(char1.name + " has attacked an enemy " + "and dealt " + damageDealt + " damage");...
Convert this pseudo code program into sentences. import java.util.ArrayList; import java.util.Collections; class Bulgarian {    public...
Convert this pseudo code program into sentences. import java.util.ArrayList; import java.util.Collections; class Bulgarian {    public static void main(String[] args)    {        max_cards=45;        arr->new ArraryList        col=1;        card=0;        left=max_cards;        do{            col->random number            row->new ArrayList;            for i=0 to i<col            {                card++                add card into row            }   ...
Copy class CirclesViewer from Codecheck and complete it. You need to import the graphics package. After...
Copy class CirclesViewer from Codecheck and complete it. You need to import the graphics package. After completed, the program will ask the user to enter an integer between 1 and 10 then draw the specified number of circles. The radius starts at 10 and increments by 10 for each next one. They all touch the line x = 5 at the left and the line y = 10 at the top. Another way to look at this is that the...
Python I am creating a class in python. Here is my code below: import csv import...
Python I am creating a class in python. Here is my code below: import csv import json population = list() with open('PopChange.csv', 'r') as p: reader = csv.reader(p) next(reader) for line in reader: population.append(obj.POP(line)) population.append(obj.POP(line)) class POP: """ Extract the data """ def __init__(self, line): self.data = line # get elements self.id = self.data[0].strip() self.geography = self.data[1].strip() self.targetGeoId = self.data[2].strip() self.targetGeoId2 = self.data[3].strip() self.popApr1 = self.data[4].strip() self.popJul1 = self.data[5].strip() self.changePop = self.data[6].strip() The problem is, I get an error saying:  ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT