What's new

Closed Basic: java code refactoring - preview to oop

Status
Not open for further replies.

codyscott

Eternal Poster
Joined
Sep 13, 2017
Posts
388
Reaction
454
Points
279
Mga 'tol,

Another code refactoring para mas madali at maayos mag program.
Using Java CLASS (or template, or blueprint). A preview to Object Oriented Programming (OOP).
(To learn more, join SERIOUSLY JAVA group)

****NOTE, save all these 3 files in the same project folder******

***LONG METHOD, hindi naka refactor*********
Save it as "StudentApp1.java"
Code:
/**
 * Refactoring series
 * @author CodyScott for PHCorner
 *
 */

public class StudentApp1 {

    public static void main(String[] args) {
        String[] studentInformation = {"Daniel","De La Cruz","24", "BS Civil Engineering", "University of the Philippines"};
      
        String firstName = studentInformation[0];
        String lastName = studentInformation[1];
        String age = studentInformation[2];
        String course = studentInformation[3];
        String school = studentInformation[4];
      
        System.out.println("STUDENT 1 INFORMATION");
        System.out.println("First Name: " + firstName);
        System.out.println("Last Name: " + lastName);
        System.out.println("Age: " + age);
        System.out.println("Course: " + course);
        System.out.println("School: " + school);

        System.out.println("*************************");
      
        String[] studentInformation2 = {"Mark","Santos","18", "BS Nursing", "Adamson U"};
      
        String firstName2 = studentInformation2[0];
        String lastName2 = studentInformation2[1];
        String age2 = studentInformation2[2];
        String course2 = studentInformation2[3];
        String school2 = studentInformation2[4];
        
        System.out.println("STUDENT 2 INFORMATION");
        System.out.println("First Name: " + firstName2);
        System.out.println("Last Name: " + lastName2);
        System.out.println("Age: " + age2);
        System.out.println("Course: " + course2);
        System.out.println("School: " + school2);
    }

}

***GAGAMIT tayo ng Class or template or blueprint*********
Save it as "StudentTemplate.java"
Code:
public    class StudentTemplate{
        private String firstName;
        private String lastName;
        private String age;
        private String course;
        private String school;
      
        public StudentTemplate(String fName,String lName,String edad,String cors,String skol) {
            this.firstName = fName;
            this.lastName = lName;
            this.age = edad;
            this.course = cors;
            this.school = skol;
        }
      
        public String getFirstName() {
            return firstName;
        }
        public String getLastName() {
            return lastName;
        }
        public String getAge() {
            return age;
        }
        public String getCourse() {
            return course;
        }
        public String getSchool() {
            return school;
        }
    }

***GAGAMITIN NATIN yung CLASS to "refactor"*********
Save it as "StudentApp2.java"
Code:
/**
 * Refactoring series
 * A preview to using Class (Object Oriented Programming)
 * @author CodyScott for PHCorner
 *
 */

public class StudentApp2 {

    public static void main(String[] args) {
        StudentTemplate student1 = new StudentTemplate("Daniel","De La Cruz","24", "BS Civil Engineering", "University of the Philippines");
        System.out.println("STUDENT INFORMATION");
        System.out.println("First Name: " + student1.getFirstName());
        System.out.println("Last Name: " + student1.getLastName());
        System.out.println("Age: " + student1.getAge());
        System.out.println("Course: " + student1.getCourse());
        System.out.println("School: " + student1.getSchool());
      
        System.out.println("*************************");
      
        StudentTemplate student2 = new StudentTemplate("Mark","Santos","18", "BS Nursing", "Adamson U");
        System.out.println("STUDENT INFORMATION");
        System.out.println("First Name: " + student2.getFirstName());
        System.out.println("Last Name: " + student2.getLastName());
        System.out.println("Age: " + student2.getAge());
        System.out.println("Course: " + student2.getCourse());
        System.out.println("School: " + student2.getSchool());      
      
    }

  
}

TAKE NOTE: ****Puwede pa ito i-refactor further by adding toString() method and "setter" methods. Assignment nyo na yan.
 
Carly_Rose
Kung medyo proper OOP ang magiging approach mo, yes, puwede kang gumawa ng "template" class for:

1) Product class
- tapos meron attributes na "productName" at "productPrice".....tapos meron SETTERs and GETTERS.

...then as simple as 2 classes lang yung buong system mo...gawa ka ng Main Application class mo

2) MainApp class
- dito gawa ka ng ArrayList<Product>...ito ang mag i-store ang mga pre-populated na products mo
- dito gawa ka ng isang METHOD na gagawa ng mga "products"....at saka mag-i-store ng products sa ArrayList (dapat ito ang unang tatakbo bago ang lahat para pag nag order na, naka ready na yung ArrayList ng mga products)
- dito gawa ka na ng LOGIC ng system (display available products, ask customer what product to order, ask quantity, do the math, etc. etc)

Yan ang very simple overview.

Umpisahan mo (kahit comments pa lang ang laman, then puwede kitang i-guide)
 
Code:
public class MainApplication
{
    static ArrayList<Product> mgaProduct;
    static String listOfProduct [] = {"Burger_Price 50", "Coke_Price 50", "Fries_Price 40"};
   
    public static void main (String [] args){
       
        mgaListahanNgProduct();
        createProduct();
    }
   
    static void mgaListahanNgProduct(){
        mgaProduct = new ArrayList<Product>();
    }
   
    static void createProduct(){
        for(Product product : mgaProduct){
            mgaProduct.add(product);
        }
    }
}




public class Product
{
    private String productName;
    private int productPrice;
   
    public void setProductName(String productName){
        this.productName = productName;
    }
   
    public void setProductPrice(int productPrice){
        this.productPrice = productPrice;
    }
   
    public String getProductName(){
        return productName;
    }
   
    public int getProductPrice(){
        return productPrice;
    }
}
hindi ko alam ts kung tama ba tong pinag gagawa ko huhu
 
..OK... good start! Carly_Rose

Code:
public class MainApplication
{
    static ArrayList<Product> mgaProduct;  //GOOD

   //kung ganito approach ang gagawin mo...
    static String listOfProduct [] = {"Burger_Price 50", "Coke_Price 50", "Fries_Price 40"};
   //GAWIN MO SIYANG (para madaling mag "split" later.  
   static String listOfProduct [] = {"Burger_Price", "50", "Coke_Price",  "50", "Fries_Price", "40"};

 
    public static void main (String [] args){
      //step 1: initialize mgaProduct

     //step 2: make products and store them inside mgaProduct

     //step 3: display the products
                   create some kind of menu

    //step 4: ask customer his/her order
                 ask quantity

    //step 5: calculate order     
    }
 
   //METHOD(s) for step 1

  //METHOD(s) for step 2

  //METHOD(s) for step 3

  //METHOD(s) for step 4

  //METHOD(s) for step 5
  
}


//this class is GOOD as it is
public class Product
{
    private String productName;
    private int productPrice;
 
    public void setProductName(String productName){
        this.productName = productName;
    }
 
    public void setProductPrice(int productPrice){
        this.productPrice = productPrice;
    }
 
    public String getProductName(){
        return productName;
    }
 
    public int getProductPrice(){
        return productPrice;
    }
}

heto yung pattern.....try your best. Tignan ko kung saan ka madadapa...AND I will try to help you as you go.
;)
 
Step 1 one is GOOD..
Step 2 is wrong....meron akong ginawang method.. pag-aralan mo. And also for price, use doable (not int) if you can.
Step 3 is wrong....since na meron ng mgaProduct arraylist.... just loop inside that and use the GETTER methods of each product object.

step 4 and 5....saka na lang gawin pag nagawa mo na yung step 3 and napag aralan mo na yung step 2.

you are almost there.... kailangan lang i-re-wire ang utak mo ng konti... hang in there!
;)

Code:
public class MainApplication
{
    static ArrayList<Product> mgaProduct;
    static String listOfProduct [] = {"Burger_Price", "50", "Coke_Price",  "50", "Fries_Price", "40"};
    static Scanner askTheCustomer;
    static int price = 0, quantity = 0, total = 0;
 
    public static void main (String [] args){
    
    
        //step 1: initialize mgaProduct   *******//GOOD
        initializeOfProduct();
    

        //step 2: make products and store them inside mgaProduct     ******//WRONG
        production();

        //step 3: display the products
        //create some kind of menu
        displayMenu();
    
        //step 4: ask customer his/her order
        //ask quantity
        System.out.println("Good Day Maam/Sir What Do Like To Order");
        System.out.println("How many?");
        
        //step 5: calculate order
        calculateTheOrderOfCustomer();
    }
 

    //METHOD(s) for step 1
    static void initializeOfProduct(){
        mgaProduct = new ArrayList<Product>();
    }

    //METHOD(s) for step 2     ****//WRONG
    static void production(){
        for(Product product : mgaProduct){
            mgaProduct.add(product);
    }
}
//********************************************
    //CORRECT WAY
    static void production(){
        //1. make a loop to read "listOfProduct" array
        //TAKE NOTE OF the i=i+2
        for(int i=0; i<listOfProduct.length; i=i+2) {
            //2. for every loop, create a new Product
            Product newProduct = new Product();
            newProduct.setName(listOfProduct[i]);
            newProduct.setPrice( Integer.parseInt((listOfProduct[i+1])) );
            //3. and then store that new product into the mgaProduct arraylist
            mgaProduct.add(newProduct);
        }
    }

//**********************************************


    //METHOD(s) for step 3       //***WRONG, kailangan mo ng BASAHIN
YUNG LAMAN NG mgaProduct arrayList (loop).....this is where you can use the GETTER methods
from each CLASS (OOP) object of each product.....try solving this method
    static void displayMenu(){
        System.out.println("Welcome To Burger Ng Bayan \n"); //wrong
        System.out.println("    Menu For Today    "); //wrong
        System.out.println("    1 Burger Price 50"); //wrong
        System.out.println("    2 Coke Price 50"); //wrong
        System.out.println("    3 Fries Price 40");  //wrong

//******use loop to loop inside "mgaProduct" arraylist
    }



    //METHOD(s) for step 4
    static  int getMenuChoices(){
        int choice;
        choice = askTheCustomer.nextInt();
        return choice;
    }

    //METHOD(s) for step 5
    static void calculateTheOrderOfCustomer(){
        total += price * quantity;
    }

}
 
Carly_Rose

Heto ang gamitin mo...mas malinis.
Let's do it one method at a time.

***Code for the Main Application
Code:
import java.util.ArrayList;
import java.util.Scanner;

public class MainApp
{
    static ArrayList<Product> mgaProduct;
    static String[] listOfProduct  = {"Burger_Price", "50", "Coke_Price",  "50", "Fries_Price", "40"};
    static Scanner askTheCustomer;
    static int price = 0, quantity = 0, total = 0;
 
    public static void main (String [] args){
        //step 1
        initializeOfProduct();
        //step 2
        production();
        //step 3
        displayMenu();

       //step 4
      //step 5

    }

    //step 1
    static void initializeOfProduct(){
        mgaProduct = new ArrayList<Product>();
    }


    //step 2
    static void production(){
        //1. make a loop to read "listOfProduct" array
        //TAKE NOTE OF the i=i+2, mag ski-skip siya ng dalawa dahil doon sa listOfProduct array na nasa taas
        for(int i=0; i<listOfProduct.length; i=i+2) {
            //2. for every loop, create a new Product
            Product newProduct = new Product();
            newProduct.setName(listOfProduct[i]);
            newProduct.setPrice( Double.parseDouble( listOfProduct[i+1] )  ) ;
            //3. and then store that new product into the mgaProduct arraylist
            mgaProduct.add(newProduct);
        }
    }

    //METHOD for step 3
    static void displayMenu() {
        System.out.println("WELCOME TO BURGER NG BAYAN");
        System.out.println("****MENU FOR TODAY******");
        for(Product p: mgaProduct) {
             //???????????
            //display name using GETTER and display price using GETTER
            //???????????
        }
    } 
 
    //METHOD(s) for step 4

    //METHOD(s) for step 5
}

***code for the Product Template
Code:
public class Product {
    private String name;
    private double price;
 
    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;
    }
}

try to complete STEP 3
 
Carly_Rose

Heto ang gamitin mo...mas malinis.
Let's do it one method at a time.

***Code for the Main Application
Code:
import java.util.ArrayList;
import java.util.Scanner;

public class MainApp
{
    static ArrayList<Product> mgaProduct;
    static String[] listOfProduct  = {"Burger_Price", "50", "Coke_Price",  "50", "Fries_Price", "40"};
    static Scanner askTheCustomer;
    static int price = 0, quantity = 0, total = 0;
 
    public static void main (String [] args){
        //step 1
        initializeOfProduct();
        //step 2
        production();
        //step 3
        displayMenu();

       //step 4
      //step 5

    }

    //step 1
    static void initializeOfProduct(){
        mgaProduct = new ArrayList<Product>();
    }


    //step 2
    static void production(){
        //1. make a loop to read "listOfProduct" array
        //TAKE NOTE OF the i=i+2, mag ski-skip siya ng dalawa dahil doon sa listOfProduct array na nasa taas
        for(int i=0; i<listOfProduct.length; i=i+2) {
            //2. for every loop, create a new Product
            Product newProduct = new Product();
            newProduct.setName(listOfProduct[i]);
            newProduct.setPrice( Double.parseDouble( listOfProduct[i+1] )  ) ;
            //3. and then store that new product into the mgaProduct arraylist
            mgaProduct.add(newProduct);
        }
    }

    //METHOD for step 3
    static void displayMenu() {
        System.out.println("WELCOME TO BURGER NG BAYAN");
        System.out.println("****MENU FOR TODAY******");
        for(Product p: mgaProduct) {
             //???????????
            //display name using GETTER and display price using GETTER
            //???????????
        }
    }
 
    //METHOD(s) for step 4

    //METHOD(s) for step 5
}

***code for the Product Template
Code:
public class Product {
    private String name;
    private double price;
 
    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;
    }
}

try to complete STEP 3
Sir Ganito ba sa Step 3 yung isang question mark pinag iisipan ko pa eh

Code:
static void displayMenu() {
        System.out.println("WELCOME TO BURGER NG BAYAN");
        System.out.println("****MENU FOR TODAY******");
        for(CodyScottMgaProduct p: mgaProduct) {
            //???????????
           
            //display name using GETTER and display price using GETTER
            //???????????
            System.out.println(p.getName() +p.getPrice());
        }
    }
 
Sir Ganito ba sa Step 3 yung isang question mark pinag iisipan ko pa eh

Code:
static void displayMenu() {
        System.out.println("WELCOME TO BURGER NG BAYAN");
        System.out.println("****MENU FOR TODAY******");
        for(CodyScottMgaProduct p: mgaProduct) {
            //???????????
          
            //display name using GETTER and display price using GETTER
            //???????????
            System.out.println(p.getName() +p.getPrice());
        }
    }
Sir Ganito Ginawa ko
Code:
static void displayMenu() {
        System.out.println("WELCOME TO BURGER NG BAYAN");
        System.out.println("****MENU FOR TODAY******");
        for(CodyScottMgaProduct p: mgaProduct) {
            //???????????
            String order = p.getName();
            //display name using GETTER and display price using GETTER
            //???????????
            //System.out.println(p.getName() +p.getPrice());
            System.out.println(order + p.getPrice());
        }
    }
 
Code:
static void displayMenu() {
        System.out.println("WELCOME TO BURGER NG BAYAN");
        System.out.println("****MENU FOR TODAY******");
        for(Product p: mgaProduct) {
            System.out.println(p.getName() + " = $"+ p.getPrice());   //ito lang ang kailangan mo dito pang display
        }
    }

Take note:
1) Product p = ibig sabihin you are referring to object type Product (ito yung template class)
2) mgaProduct = you are referring to the COLLECTION arraylist na ginawa mo sa steps 1 and 2
3) System.out.println(p.getName() + " = $"+ p.getPrice()) = ikaw na bahal sa formatting ng display ng text

try to run your program and at this point, dapat nag di-display lang siya ng MENU muna.
pagandahin mo na lang yung pag display,,,kunyari lang. :)
 
Moving a little bit ahead to Steps 4 and 5.

Ganito ang sequence.

1) "Ale, isang Burger nga at saka isang Coke"
2) Question 1: Paano ita-type ni Ale yung order sa system (whole name or just number)?
= System should ask: What is the product name:
= System should ask also: Quantity:
= System will store order in some kind of orderArrayList.
****And this is a loop na kada isang loop, it will ask sa end "More order (Y or N)?"
3) Pag tapos na mag order....just loop inside the orderArrayList.....
= display customer's final order (use GETTER here again, kapareho ng display Menu method mo)
= do the math and compute the whole order bill (use GETTER here again)
= accept payment and do the math for change (you can use local variables here like totalBill, totalChange, amountReceived)
= finish order
 
Moving a little bit ahead to Steps 4 and 5.

Ganito ang sequence.

1) "Ale, isang Burger nga at saka isang Coke"
2) Question 1: Paano ita-type ni Ale yung order sa system (whole name or just number)?
= System should ask: What is the product name:
= System should ask also: Quantity:
= System will store order in some kind of orderArrayList.
****And this is a loop na kada isang loop, it will ask sa end "More order (Y or N)?"
3) Pag tapos na mag order....just loop inside the orderArrayList.....
= display customer's final order (use GETTER here again, kapareho ng display Menu method mo)
= do the math and compute the whole order bill (use GETTER here again)
= accept payment and do the math for change (you can use local variables here like totalBill, totalChange, amountReceived)
= finish order
Salamat po dito ts
 
Moving a little bit ahead to Steps 4 and 5.

Ganito ang sequence.

1) "Ale, isang Burger nga at saka isang Coke"
2) Question 1: Paano ita-type ni Ale yung order sa system (whole name or just number)?
= System should ask: What is the product name:
= System should ask also: Quantity:
= System will store order in some kind of orderArrayList.
****And this is a loop na kada isang loop, it will ask sa end "More order (Y or N)?"
3) Pag tapos na mag order....just loop inside the orderArrayList.....
= display customer's final order (use GETTER here again, kapareho ng display Menu method mo)
= do the math and compute the whole order bill (use GETTER here again)
= accept payment and do the math for change (you can use local variables here like totalBill, totalChange, amountReceived)
= finish order
Pasensya na po pala ts sa abala ts penge po ng tips mula sa inyo my dapat ba ako basahin na libro or my dapat ba ako aralin gusto ko po kasi tong pag proprogram na course kaso parang ayaw yata sakin neto parang bf ko lang mas malala pa sa bf ko to ayaw kasi makisama saka ang hirap intindihin
 
Pasensya na po pala ts sa abala ts penge po ng tips mula sa inyo my dapat ba ako basahin na libro or my dapat ba ako aralin gusto ko po kasi tong pag proprogram na course kaso parang ayaw yata sakin neto parang bf ko lang mas malala pa sa bf ko to ayaw kasi makisama saka ang hirap intindihin

Paano ka magsaing?
step 1) ihanda mo ang kaldero
step 2) ihanda mo ang bigas
step 3) ilagay mo ang bigas sa kaldero
step 4) lagyan mo ng tubig ang kaldero na may bigas
step 5) isalang ang kaldero sa rice cooker
step 6) hintayin na maluto
step 7) exit the program

...in programming, ganyan din ang procedure.

Panoorin mo itong video na ito and let me know kung luminaw ang outlook mo sa programming.

https://phc.onl/#forbidden#/DF2XAc07eI0

...wala akong maire-recommend na book or video, pero ang maisa-suggest ko lang ay manood ka ng manood ng mga beginners guide hanggang luminaw ng konti ang programming sa iyo. At this point, it does not matter kung C++ or Java or Javascript....ang concept sa beginners level ay pare-pareho.

Goodluck!

Here's the code para sa system na ginagawa mo
TAKE NOTE: somewhat complete na ito pero wala pa itong mga functions like error checking, what if functions, etc. etc.
Marami pang dapat i-improve. Pero yung flow, nandyan na.

Code:
import java.util.ArrayList;
import java.util.Scanner;

public class MainApp
{
    static ArrayList<Product> mgaProduct;
    static String[] listOfProduct  = {"Burger", "50", "Coke_Price",  "50", "Fries_Price", "40"};
    static ArrayList<Product> orders;
   
    public static void main (String [] args){
        //step 1
        initializeOfProductList();
        //step 2
        createAndStoreProducts();
        //step 3
        displayMenu();
        //step 4
        takeOrder();
        //step 5
        finalizeOrder();
    }

    //step 1
    static void initializeOfProductList(){
        mgaProduct = new ArrayList<Product>();
    }

    //step 2
    static void createAndStoreProducts(){
        //1. make a loop to read "listOfProduct" array
        //TAKE NOTE OF the i=i+2, mag ski-skip siya ng dalawa dahil doon sa listOfProduct array na nasa taas
        for(int i=0; i<listOfProduct.length; i=i+2) {
            //2. for every loop, create a new Product
            Product newProduct = new Product();
            newProduct.setName(listOfProduct[i]);
            newProduct.setPrice( Double.parseDouble( listOfProduct[i+1] )  ) ;
            //3. and then store that new product into the mgaProduct arraylist
            mgaProduct.add(newProduct);
        }
    }
   
    //METHOD(s) for step 3
    static void displayMenu() {
        System.out.println("WELCOME TO BURGER NG BAYAN");
        System.out.println("****MENU FOR TODAY******");
        int counter = 0;
        for(Product p: mgaProduct) {
            System.out.println("Code: " + counter + " - "+ p.getName() + " = $" + p.getPrice());
            counter++;
        }
    }   
   
    //METHOD(s) for step 4
    static void takeOrder() {
        //initialize natin yung paglalagyan ng mga orders
        orders = new ArrayList<>();
       
        System.out.println("\nENTER ORDERS:");
       
        //initialize din natin yung mga kailangan nating variables
        Scanner input = new Scanner(System.in);
        boolean moreOrder = true;
        String answer = "Y";
       
        //this is a loop (hihinto lang siya pag wala ng o-ordering ang customer
        do {
            //tanungin kung anong gusto. Then i-type lang yung number ng product (0,1,2,3....)
            System.out.print("Product: ");
            int productCode = input.nextInt();
            //tanungin kung ilang (1,2,3,4,5....)
            System.out.print("Quantity: ");
            int quantity = input.nextInt();
           
            //base sa quantity, yun ang dami ng product na ilalagay sa order ArrayList
            for(int i=0; i<quantity; i++) {
                orders.add(mgaProduct.get(productCode));
            }
           
            //tanungin kung meron pang order ang customer
            System.out.print("More Orders, Y or N?");
            answer = input.next();
           
            //kung wala na, type "N"...kung meron pa type "Y"
            if(!answer.toLowerCase().equals("Y".toLowerCase())) {
                moreOrder = false;
            }
           
        }while(moreOrder);
        //end of loop
       
        input.close();
       
    }

    //METHOD for step 5
    static void finalizeOrder() {
        double total = 0;
        System.out.println("Final Orders:");
       
        //loop inside orders arraylist
        for(Product p: orders) {
            System.out.println(p.getName() + " = $" + p.getPrice());
            //as you loop, you get each product price and add it to total
            total = total + p.getPrice();
        }
       
        System.out.println("Total Bill: $" + total);
    }

}

//TAKE NOTE: Pag-aralan mo ang error checking. Halimbawa, paano kung imbes na number ang i-type, nai-type ay letter?
//Also, paano kung imbes na 3 pieces, nag iba ang isip ng customer, isa na lang ang order.
//Also, what if nag cancel ng order, wala na lang Burger.... etc etc.
//Pag isipin mo yung mga functions na kakailanganin mo....goodluck
 
Moving a little bit ahead to Steps 4 and 5.

Ganito ang sequence.

1) "Ale, isang Burger nga at saka isang Coke"
2) Question 1: Paano ita-type ni Ale yung order sa system (whole name or just number)?
= System should ask: What is the product name:
= System should ask also: Quantity:
= System will store order in some kind of orderArrayList.
****And this is a loop na kada isang loop, it will ask sa end "More order (Y or N)?"
3) Pag tapos na mag order....just loop inside the orderArrayList.....
= display customer's final order (use GETTER here again, kapareho ng display Menu method mo)
= do the math and compute the whole order bill (use GETTER here again)
= accept payment and do the math for change (you can use local variables here like totalBill, totalChange, amountReceived)
= finish order
Sir Ganito po Ginawa ko Sa Step 4 Tama po Ba?

Code:
import java.util.ArrayList;
import java.util.Scanner;

public class CodyScottOrderingMainApp
{
    static ArrayList<CodyScottMgaProduct> mgaProduct;
    static String[] listOfProduct  =
   
    {" (A1) MgBurger ", " 30.00 ",
    " (A2) Big Mac ", " 139.00 ",
    " (B1) Cheese Burger ", " 35.00 ",
    " (B2) Chicken Burger ", " 50.00 ",
    " (C1) MgNuggets ", " 65.00 ",
    " (C2) MgChicken ", "79.00 ",
    " (D1) MgSpagetti ", " 60.00 ",
    " (D2) MgFries ", " 40.00 ",
    " (E1) Coke ", " 10.00 ",
    " (E2) Sprite ", " 10.00 ",
    " (E3) Royal ", " 10.00 ",
    " (F1) Sundae ", " 25.00 ",
    " (F2) MgFloat ", " 25.00 "};
   
    static ArrayList<String> orderList = new ArrayList<>();
    static Scanner askTheCustomer = new Scanner(System.in);
    static int price = 0, quantity = 0, total = 0;
   

    public static void main (String [] args){
        //step 1
        initializeOfProduct();
        //step 2
        production();
        //step 3
        displayMenu();

        //step 4
        kuninAngOrderNgCustomer();
       
        //step 5

    }

    //step 1
    static void initializeOfProduct(){
        mgaProduct = new ArrayList<CodyScottMgaProduct>();
    }


    //step 2
    static void production(){
        //1. make a loop to read "listOfProduct" array
        //TAKE NOTE OF the i=i+2, mag ski-skip siya ng dalawa dahil doon sa listOfProduct array na nasa taas
        for(int i=0; i<listOfProduct.length; i=i+2) {
            //2. for every loop, create a new Product
            CodyScottMgaProduct newProduct = new CodyScottMgaProduct();
            newProduct.setName(listOfProduct[i]);
            newProduct.setPrice( Double.parseDouble( listOfProduct[i+1] )  ) ;
            //3. and then store that new product into the mgaProduct arraylist
            mgaProduct.add(newProduct);
        }
    }

    //METHOD for step 3
     
    static void displayMenu() {
        System.out.println("WELCOME TO BURGER NG BAYAN\n");
        System.out.println("**********MENU FOR TODAY**********\n");
        for(CodyScottMgaProduct p: mgaProduct) {
            System.out.println(p.getName() + " = $"+ p.getPrice());   //ito lang ang kailangan mo dito pang display
       
        }
       
        System.out.println("\n********************************");
       
    }
       
    //METHOD(s) for step 4
    static void kuninAngOrderNgCustomer(){
        System.out.print("\nEnter Code Order : ");
       
        String inputCode = askTheCustomer.next();
       
        CodyScottMgaProduct orderProduct = new CodyScottMgaProduct();
        orderProduct.setName(inputCode);
       
        inputCode.equals("A1");
        System.out.println("Burger");
       
        System.out.println("\n Enter Quantity");   

    }

    //METHOD(s) for step 5
}
 
Paano ka magsaing?
step 1) ihanda mo ang kaldero
step 2) ihanda mo ang bigas
step 3) ilagay mo ang bigas sa kaldero
step 4) lagyan mo ng tubig ang kaldero na may bigas
step 5) isalang ang kaldero sa rice cooker
step 6) hintayin na maluto
step 7) exit the program

...in programming, ganyan din ang procedure.

Panoorin mo itong video na ito and let me know kung luminaw ang outlook mo sa programming.

You do not have permission to view the full content of this post. Log in or register now.

...wala akong maire-recommend na book or video, pero ang maisa-suggest ko lang ay manood ka ng manood ng mga beginners guide hanggang luminaw ng konti ang programming sa iyo. At this point, it does not matter kung C++ or Java or Javascript....ang concept sa beginners level ay pare-pareho.

Goodluck!

Here's the code para sa system na ginagawa mo
TAKE NOTE: somewhat complete na ito pero wala pa itong mga functions like error checking, what if functions, etc. etc.
Marami pang dapat i-improve. Pero yung flow, nandyan na.

Code:
import java.util.ArrayList;
import java.util.Scanner;

public class MainApp
{
    static ArrayList<Product> mgaProduct;
    static String[] listOfProduct  = {"Burger", "50", "Coke_Price",  "50", "Fries_Price", "40"};
    static ArrayList<Product> orders;
  
    public static void main (String [] args){
        //step 1
        initializeOfProductList();
        //step 2
        createAndStoreProducts();
        //step 3
        displayMenu();
        //step 4
        takeOrder();
        //step 5
        finalizeOrder();
    }

    //step 1
    static void initializeOfProductList(){
        mgaProduct = new ArrayList<Product>();
    }

    //step 2
    static void createAndStoreProducts(){
        //1. make a loop to read "listOfProduct" array
        //TAKE NOTE OF the i=i+2, mag ski-skip siya ng dalawa dahil doon sa listOfProduct array na nasa taas
        for(int i=0; i<listOfProduct.length; i=i+2) {
            //2. for every loop, create a new Product
            Product newProduct = new Product();
            newProduct.setName(listOfProduct[i]);
            newProduct.setPrice( Double.parseDouble( listOfProduct[i+1] )  ) ;
            //3. and then store that new product into the mgaProduct arraylist
            mgaProduct.add(newProduct);
        }
    }
  
    //METHOD(s) for step 3
    static void displayMenu() {
        System.out.println("WELCOME TO BURGER NG BAYAN");
        System.out.println("****MENU FOR TODAY******");
        int counter = 0;
        for(Product p: mgaProduct) {
            System.out.println("Code: " + counter + " - "+ p.getName() + " = $" + p.getPrice());
            counter++;
        }
    }  
  
    //METHOD(s) for step 4
    static void takeOrder() {
        //initialize natin yung paglalagyan ng mga orders
        orders = new ArrayList<>();
      
        System.out.println("\nENTER ORDERS:");
      
        //initialize din natin yung mga kailangan nating variables
        Scanner input = new Scanner(System.in);
        boolean moreOrder = true;
        String answer = "Y";
      
        //this is a loop (hihinto lang siya pag wala ng o-ordering ang customer
        do {
            //tanungin kung anong gusto. Then i-type lang yung number ng product (0,1,2,3....)
            System.out.print("Product: ");
            int productCode = input.nextInt();
            //tanungin kung ilang (1,2,3,4,5....)
            System.out.print("Quantity: ");
            int quantity = input.nextInt();
          
            //base sa quantity, yun ang dami ng product na ilalagay sa order ArrayList
            for(int i=0; i<quantity; i++) {
                orders.add(mgaProduct.get(productCode));
            }
          
            //tanungin kung meron pang order ang customer
            System.out.print("More Orders, Y or N?");
            answer = input.next();
          
            //kung wala na, type "N"...kung meron pa type "Y"
            if(!answer.toLowerCase().equals("Y".toLowerCase())) {
                moreOrder = false;
            }
          
        }while(moreOrder);
        //end of loop
      
        input.close();
      
    }

    //METHOD for step 5
    static void finalizeOrder() {
        double total = 0;
        System.out.println("Final Orders:");
      
        //loop inside orders arraylist
        for(Product p: orders) {
            System.out.println(p.getName() + " = $" + p.getPrice());
            //as you loop, you get each product price and add it to total
            total = total + p.getPrice();
        }
      
        System.out.println("Total Bill: $" + total);
    }

}

//TAKE NOTE: Pag-aralan mo ang error checking. Halimbawa, paano kung imbes na number ang i-type, nai-type ay letter?
//Also, paano kung imbes na 3 pieces, nag iba ang isip ng customer, isa na lang ang order.
//Also, what if nag cancel ng order, wala na lang Burger.... etc etc.
//Pag isipin mo yung mga functions na kakailanganin mo....goodluck
Ts Ang Galing nyo naman po Sana Maging katulad ko din kayo. Mag pag asa kaya ako matuto sa Pag proprogram simple lang yung pina pagawa samen ng prof namen pero hirap na hirap ako pero kahit masakit sa utak nag enenjoy nman po ako. Salamat po Ts.
 
Ts yung binigay nyo po video
Concept of algo po ba tapus si prof wangmolin.
yes, yan yon.... take note of the words "Step by Step" and "to solve a problem" na sinasabi niya.

actually yung pinapagawa sa inyong system ay TOO ADVANCED para sa iyo kung nagsisimula ka pa lang na mag program. And in my personal opinion, this is one of the many signs na HINDI qualified ang iyong teacher (or school) na magturo ng programming (or software development).....or..baka naman hindi ka nakinig sa umpisa at last minute na itong ginagawa mo (no offense intended) .. ;)

just try your best....wala naman isinilang na programmer. Lahat ng programmer dumaan sa pinagdadaanan mo ngayon. It's just a matter of how quick they get the idea of programming and dyan lang nagkakaiba.
 
yes, yan yon.... take note of the words "Step by Step" and "to solve a problem" na sinasabi niya.

actually yung pinapagawa sa inyong system ay TOO ADVANCED para sa iyo kung nagsisimula ka pa lang na mag program. And in my personal opinion, this is one of the many signs na HINDI qualified ang iyong teacher (or school) na magturo ng programming (or software development).....or..baka naman hindi ka nakinig sa umpisa at last minute na itong ginagawa mo (no offense intended) .. ;)

just try your best....wala naman isinilang na programmer. Lahat ng programmer dumaan sa pinagdadaanan mo ngayon. It's just a matter of how quick they get the idea of programming and dyan lang nagkakaiba.
Nakikinig nman po ako ts kaso parang hindi nman pumapasok yung tinuturo sakin kasi parang ang nang yayari para lang kinokopya ko yung ginagawa ng prof namen tapus at the end pag nag pagawa parang ganun pero pag nag code na iba nnman dapat pang hindi ko gayahin yung ginagawa namen at gumawa ako ng sariling kong idea kung pano gawin yun.
 
yes, yan yon.... take note of the words "Step by Step" and "to solve a problem" na sinasabi niya.

actually yung pinapagawa sa inyong system ay TOO ADVANCED para sa iyo kung nagsisimula ka pa lang na mag program. And in my personal opinion, this is one of the many signs na HINDI qualified ang iyong teacher (or school) na magturo ng programming (or software development).....or..baka naman hindi ka nakinig sa umpisa at last minute na itong ginagawa mo (no offense intended) .. ;)

just try your best....wala naman isinilang na programmer. Lahat ng programmer dumaan sa pinagdadaanan mo ngayon. It's just a matter of how quick they get the idea of programming and dyan lang nagkakaiba.
Parang concept mo ts ibang ibang sa mga program namen. How to be you po. Siguro habang nag aaral ka ang talino talino mo. Kasi yung mga teknik mo para ikaw lang ang meron ganyan pag tumitingin ako sa ibang program ang layo pero mas madali talaga unawain yung sayo TS. Pwede bang mag bayad ng tuition fee sayo parang mas marami pa ako matutunan sayo eh
 
Status
Not open for further replies.
Back
Top