In: Computer Science
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);
}
}
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 :