2015-03-23 2 views
0

У меня есть система раскассирования и вот мои коды:получать общую сумму в моей системе раскассирования

import javax.swing.*; 
import java.util.Scanner; 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.PrintWriter; 
public class FinalProject { 

    private static final int totalsundae = 0; 

    public static void main(String[] args) { 
     int totaldrinks = 0, totalfries = 0, totalburger = 0, totalsundae = 0; 

     FinalProject m = new FinalProject(); 
     m.Cashiering(); 

    } 

    public void Cashiering() { 
     String menu[] = {"Customer's Name", "Menu", "Pay", "View Order list", "Exit"}; 
     String choice = ""; 
     char ch; 

     do { 
      choice = (String) JOptionPane.showInputDialog(null, "Please select your order", "menu", 1, null, menu, menu[0]); 
      ch = choice.toLowerCase().charAt(0); 
      switch (ch) { 

       case 'c': 
        customer(); 

        break; 

       case 'm': 
        menu2(); 
        break; 
       case 'p': 
        getTotal(); 

        break; 
       case 'v': 
        orderList(); 
        break; 
       case 'e': 
        System.exit(0); 

      } 
     } while (!choice.equals("Close")); 
    } 

    public void customer() { 

     String name = (String) JOptionPane.showInputDialog("WELCOME !!!!! \n\nPLEASE ENTER YOUR NAME: "); 
     try { 
      PrintWriter printW = new PrintWriter(new FileWriter("cash.txt", true)); 
      printW.println("Customer's Name " + "\n" + name); 

      JOptionPane.showMessageDialog(null, "Your Name successfully stored \n\n You can Order Now "); 
      printW.close(); 
     } catch (Exception e) { 

     } 
    } 

    public int burger() { 
     int burger = 0, quantity = 0, totalburger = 0; 
     int choice; 
     String burgnm = ""; 

     String menu = "\nBURGER: \n1.Regular Burger 45 \n2.Burger with cheese---->55 \n3.Burger with egg and cheese----50 \n4.Burger with ham---->60 \n5.Burger with ham and cheese---->70 "; 

     choice = Integer.parseInt(JOptionPane.showInputDialog("Please select: " + menu)); 

     switch (choice) { 
      case 1: 
       burger += 45; 
       burgnm = "Regular Burger - "; 
       quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? ")); 
       break; 
      case 2: 
       burger += 55; 
       burgnm = "Burger with cheese - "; 
       quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? ")); 
       break; 
      case 3: 
       burger += 50; 
       burgnm = "Burger with egg and cheese - "; 
       quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? ")); 
       break; 
      case 4: 
       burger += 60; 
       burgnm = "Burger with ham - "; 
       quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? ")); 
       break; 
      case 5: 
       burger += 70; 
       burgnm = "Burger with ham and cheese - "; 
       quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? ")); 
       break; 
      default: 
       JOptionPane.showMessageDialog(null, "INVALID"); 
     } 

     if (quantity < 0) { 
      JOptionPane.showMessageDialog(null, "INVALID"); 
     } else { 
      totalburger = quantity * burger; 
      burgnm = burgnm + totalburger + "\n"; 
      JOptionPane.showMessageDialog(null, "Total = " + burgnm); 

      try { 
       PrintWriter printW = new PrintWriter(new FileWriter("cash.txt", true)); 
       if (choice == 1) { 
        printW.println("Customer's Order " + "\n" + quantity + burgnm); 
       } else if (choice == 2) 
        printW.println("Customer's Order " + "\n" + quantity + burgnm); 
       else if (choice == 3) 
        printW.println("Customer's Order " + "\n" + quantity + burgnm); 

       else if (choice == 4) 
        printW.println("Customer's Order " + "\n" + quantity + burgnm); 
       else if (choice == 5) 
        printW.println("Customer's Order " + "\n" + quantity + burgnm); 

       JOptionPane.showMessageDialog(null, "Record successfully stored "); 
       printW.close(); 


      } catch (Exception e) { 
      } 
     } 
     return totalburger; 
    } 

    public int Spaghetti() { 
     int choice, quantity = 0, totalspag = 0, spaghetti = 0; 
     String spagnm = null; 

     String menu = "\nSPAGHETTI: \n1.Regular spaghetti---->60 \n2.Large Spaghetti---->70"; 
     choice = Integer.parseInt(JOptionPane.showInputDialog("Please select: " + menu)); 

     switch (choice) { 
      case 1: 
       spaghetti += 60; 
       spagnm = "Regular Spaghetti - "; 
       quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? ")); 
       break; 
      case 2: 
       spaghetti += 70; 
       spagnm = "Large Spaghetti - "; 
       quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? ")); 
       break; 
      default: 
       JOptionPane.showMessageDialog(null, "INVALID"); 
     } 

     if (quantity < 0) { 
      JOptionPane.showMessageDialog(null, "INVALID"); 
     } else { 
      totalspag = quantity * spaghetti; 
      spagnm = spagnm + totalspag + "\n"; 
      JOptionPane.showMessageDialog(null, "Total = " + spagnm); 

      try { 
       PrintWriter printW = new PrintWriter(new FileWriter("cash.txt", true)); 
       if (choice == 1) { 
        printW.println("Customer's Order " + "\n" + quantity + spagnm); 
       } else if (choice == 2) 
        printW.println("Customer's Order " + "\n" + quantity + spagnm); 

       JOptionPane.showMessageDialog(null, "Record successfully stored "); 
       printW.close(); 


      } catch (Exception e) { 
      } 
     } 
     return totalspag; 
    } 

    public int Fries() { 
     int choice, fries = 0, quantity = 0, totalfries = 0; 
     String friesnm = ""; 
     String menu = "\nFRIES: \n1.Regular Fries ----> 35\n2.Medium Fries ----> 45 \n3.LargeFries ----> 55"; 
     choice = Integer.parseInt(JOptionPane.showInputDialog("Please select: " + menu)); 

     switch (choice) { 
      case 1: 
       fries += 35; 
       friesnm = "Regular Fries - "; 
       quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? ")); 
       break; 
      case 2: 
       fries += 45; 
       friesnm = "Medium Fries - "; 
       quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? ")); 
       break; 
      case 3: 
       fries += 55; 
       friesnm = "Large Fries - "; 
       quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? ")); 
       break; 
      default: 
       JOptionPane.showMessageDialog(null, "INVALID"); 
     } 

     if (quantity < 0) { 
      JOptionPane.showMessageDialog(null, "INVALID"); 
     } else { 
      totalfries = quantity * fries; 
      friesnm = friesnm + totalfries + "\n"; 
      JOptionPane.showMessageDialog(null, "Total = " + friesnm); 

      try { 
       PrintWriter printW = new PrintWriter(new FileWriter("cash.txt", true)); 
       if (choice == 1) { 
        printW.println("Customer's Order " + "\n" + quantity + friesnm); 
       } else if (choice == 2) 
        printW.println("Customer's Order " + "\n" + quantity + friesnm); 
       else if (choice == 3) 
        printW.println("Customer's Order " + "\n" + quantity + friesnm); 

       JOptionPane.showMessageDialog(null, "Record successfully stored "); 
       printW.close(); 


      } catch (Exception e) { 
      } 
     } 
     return totalfries; 
    } 

    public int Sundae() { 
     int choice, quantity = 0, sundae = 0, totalsun = 0; 
     String sundaenm = ""; 
     String menu = "\nSUNDAE: \n1.Choco sundae ----> 28 \n2.Caramel sundae ----> 28 \n3.Strawberry sundae ----> 28"; 
     choice = Integer.parseInt(JOptionPane.showInputDialog("Please select: " + menu)); 
     switch (choice) { 
      case 1: 
       sundae += 28; 
       sundaenm = " Choco Sundae -"; 
       quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? ")); 
       break; 
      case 2: 
       sundae += 28; 
       sundaenm = " Caramel Sundae "; 
       quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? ")); 
       break; 
      case 3: 
       sundae += 28; 
       sundaenm = " Strawberry Sundae - "; 
       quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? ")); 
       break; 
      default: 
       JOptionPane.showMessageDialog(null, "INVALID"); 
     } 

     if (quantity < 0) { 
      JOptionPane.showMessageDialog(null, "INVALID"); 
     } else { 
      totalsun = quantity * sundae; 
      sundaenm = sundaenm + totalsun + "\n"; 
      JOptionPane.showMessageDialog(null, "Total = " + sundaenm); 

      try { 
       PrintWriter printW = new PrintWriter(new FileWriter("cash.txt", true)); 
       if (choice == 1) { 
        printW.println("Costumer's Order " + "\n" + quantity + sundaenm); 
       } else if (choice == 2) 
        printW.println("Costumer's Order " + "\n" + quantity + sundaenm); 
       else if (choice == 3) 
        printW.println("Costumer's Order " + "\n" + quantity + sundaenm); 

       JOptionPane.showMessageDialog(null, "Record successfully stored "); 
       printW.close(); 


      } catch (Exception e) { 
      } 
     } 
     return totalsun; 
    } 

    public int Drinks() { 
     int choice, quantity = 0, drinks = 0, totaldrinks = 0; 
     String drinksnm = ""; 
     String menu = "\nDRINKS: \n1.Reg. Coke ----> 25 \n2.Large Coke ----> 35 \n3.Reg.Sprite ----> 28 \n4.Large Sprite ----> 38"; 
     choice = Integer.parseInt(JOptionPane.showInputDialog("Please select: " + menu)); 

     switch (choice) { 
      case 1: 
       drinks += 25; 
       drinksnm = "Regular Coke - "; 
       quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? ")); 
       break; 
      case 2: 
       drinks += 35; 
       drinksnm = "Large Coke - "; 
       quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? ")); 
       break; 
      case 3: 
       drinks += 28; 
       drinksnm = "Regular Sprite - "; 
       quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? ")); 
       break; 
      case 4: 
       drinksnm = "Large Sprite - "; 
       quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? ")); 

       break; 
      default: 
       JOptionPane.showMessageDialog(null, "INVALID"); 
     } 

     if (quantity < 0) { 
      JOptionPane.showMessageDialog(null, "INVALID"); 
     } else { 
      totaldrinks = quantity * drinks; 
      drinksnm = drinksnm + totaldrinks + "\n"; 
      JOptionPane.showMessageDialog(null, "Total = " + drinksnm); 

      try { 
       PrintWriter printW = new PrintWriter(new FileWriter("cash.txt", true)); 
       if (choice == 1) { 
        printW.println("Costumer's Order " + "\n" + quantity + drinksnm); 
       } else if (choice == 2) 
        printW.println("Costumer's Order " + "\n" + quantity + drinksnm); 
       else if (choice == 3) 
        printW.println("Costumer's Order " + "\n" + quantity + drinksnm); 

       else if (choice == 4) 
        printW.println("Costumer's Order " + "\n" + quantity + drinksnm); 


       JOptionPane.showMessageDialog(null, "Record successfully stored "); 
       printW.close(); 


      } catch (Exception e) { 
      } 
     } 
     return totaldrinks; 
    } 

    public void orderList() { 
     try { 
      Scanner in = new Scanner(new FileReader("cash.txt")); 
      String all = "\nWELCOME !!!! \nCASHIERING SYSTEM\n"; 
      while (in.hasNext()) { 
       all = all + in.nextLine() + "\n"; 

      } 
      JOptionPane.showMessageDialog(null, new JTextArea(all)); 
      in.close(); 
     } catch (Exception e) { 
     } 
    } 

    public void getTotal() { 

    } 

    public void menu2() { 
     String menu[] = {"Burger", "Spaghetti", "Fries", "Ice Sundae", "Drinks", "Exit"}; 
     String choice = ""; 
     char ch; 
     int yesorno = 0; 

     do { 
      choice = (String) JOptionPane.showInputDialog(null, "Please select your order", "menu", 1, null, menu, menu[0]); 
      ch = choice.toLowerCase().charAt(0); 
      switch (ch) { 

       case 'b': 
        burger(); 
        yesorno = Integer.parseInt(JOptionPane.showInputDialog("Do you still have any order?? \n 1.Yes \n2.No")); 
        if (yesorno == 1) { 
         menu2(); 

        } else { 
         Cashiering(); 
        } 

        break; 
       case 's': 
        Spaghetti(); 
        yesorno = Integer.parseInt(JOptionPane.showInputDialog("Do you still have any order?? \n 1.Yes \n2.No")); 
        if (yesorno == 1) { 
         menu2(); 

        } else { 
         Cashiering(); 
        } 
        break; 
       case 'f': 
        Fries(); 
        yesorno = Integer.parseInt(JOptionPane.showInputDialog("Do you still have any order?? \n 1.Yes \n2.No")); 
        if (yesorno == 1) { 
         menu2(); 

        } else { 
         Cashiering(); 
        } 

        break; 
       case 'i': 
        Sundae(); 
        yesorno = Integer.parseInt(JOptionPane.showInputDialog("Do you still have any order?? \n 1.Yes \n2.No")); 
        if (yesorno == 1) { 
         menu2(); 

        } else { 
         Cashiering(); 
        } 
        break; 
       case 'd': 
        Drinks(); 
        yesorno = Integer.parseInt(JOptionPane.showInputDialog("Do you still have any order?? \n 1.Yes \n2.No")); 
        if (yesorno == 1) { 
         menu2(); 

        } else { 
         Cashiering(); 
        } 

        break; 
       case 'e': 
        break; 

      } 

     } while (!choice.equals("Close")); 
    } 
} 

моя проблема здесь общая. как я могу получить общую сумму заказа клиента, используя метод? Я действительно новичок в java, поэтому надеюсь, что вы сможете дать несколько простых кодов. большое вам спасибо за вашу помощь!

ответ

0

Используйте переменную totalOrder, чтобы сохранить итоговое значение для этой сессии.

import javax.swing.*; 
import java.util.Scanner; 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.PrintWriter; 

    public class FinalProject { 

private int totalOrder; 
private static final int totalsundae = 0; 

public static void main(String[] args) { 
    int totaldrinks = 0, totalfries = 0, totalburger = 0, totalsundae = 0; 

    FinalProject m = new FinalProject(); 
    m.Cashiering(); 

} 

public void Cashiering() { 
    String menu[] = { "Customer's Name", "Menu", "Pay", "View Order list", 
      "Exit" }; 
    String choice = ""; 
    char ch; 

    do { 
     choice = (String) JOptionPane.showInputDialog(null, 
       "Please select your order", "menu", 1, null, menu, menu[0]); 
     ch = choice.toLowerCase().charAt(0); 
     switch (ch) { 

     case 'c': 
      customer(); 

      break; 

     case 'm': 
      menu2(); 
      break; 
     case 'p': 
      getTotal(); 

      break; 
     case 'v': 
      orderList(); 
      break; 
     case 'e': 
      System.exit(0); 

     } 
    } while (!choice.equals("Close")); 
} 

public void customer() { 

    String name = (String) JOptionPane 
      .showInputDialog("WELCOME !!!!! \n\nPLEASE ENTER YOUR NAME: "); 
    try { 
     PrintWriter printW = new PrintWriter(new FileWriter("cash.txt", 
       true)); 
     printW.println("Customer's Name " + "\n" + name); 

     JOptionPane.showMessageDialog(null, 
       "Your Name successfully stored \n\n You can Order Now "); 
     printW.close(); 
    } catch (Exception e) { 

    } 
} 

public int burger() { 
    int burger = 0, quantity = 0, totalburger = 0; 
    int choice; 
    String burgnm = ""; 

    String menu = "\nBURGER: \n1.Regular Burger 45 \n2.Burger with cheese---->55 \n3.Burger with egg and cheese----50 \n4.Burger with ham---->60 \n5.Burger with ham and cheese---->70 "; 

    choice = Integer.parseInt(JOptionPane.showInputDialog("Please select: " 
      + menu)); 

    switch (choice) { 
    case 1: 
     burger += 45; 
     burgnm = "Regular Burger - "; 
     quantity = Integer.parseInt(JOptionPane 
       .showInputDialog("How many do you want??? ")); 
     break; 
    case 2: 
     burger += 55; 
     burgnm = "Burger with cheese - "; 
     quantity = Integer.parseInt(JOptionPane 
       .showInputDialog("How many do you want??? ")); 
     break; 
    case 3: 
     burger += 50; 
     burgnm = "Burger with egg and cheese - "; 
     quantity = Integer.parseInt(JOptionPane 
       .showInputDialog("How many do you want??? ")); 
     break; 
    case 4: 
     burger += 60; 
     burgnm = "Burger with ham - "; 
     quantity = Integer.parseInt(JOptionPane 
       .showInputDialog("How many do you want??? ")); 
     break; 
    case 5: 
     burger += 70; 
     burgnm = "Burger with ham and cheese - "; 
     quantity = Integer.parseInt(JOptionPane 
       .showInputDialog("How many do you want??? ")); 
     break; 
    default: 
     JOptionPane.showMessageDialog(null, "INVALID"); 
    } 

    if (quantity < 0) { 
     JOptionPane.showMessageDialog(null, "INVALID"); 
    } else { 
     totalburger = quantity * burger; 
     burgnm = burgnm + totalburger + "\n"; 
     JOptionPane.showMessageDialog(null, "Total = " + burgnm); 

     try { 
      PrintWriter printW = new PrintWriter(new FileWriter("cash.txt", 
        true)); 
      if (choice == 1) { 
       printW.println("Customer's Order " + "\n" + quantity 
         + burgnm); 
      } else if (choice == 2) 
       printW.println("Customer's Order " + "\n" + quantity 
         + burgnm); 
      else if (choice == 3) 
       printW.println("Customer's Order " + "\n" + quantity 
         + burgnm); 

      else if (choice == 4) 
       printW.println("Customer's Order " + "\n" + quantity 
         + burgnm); 
      else if (choice == 5) 
       printW.println("Customer's Order " + "\n" + quantity 
         + burgnm); 

      JOptionPane.showMessageDialog(null, 
        "Record successfully stored "); 
      printW.close(); 

     } catch (Exception e) { 
     } 
    } 
    totalOrder = totalOrder + totalburger; 
    return totalburger; 
} 

public int Spaghetti() { 
    int choice, quantity = 0, totalspag = 0, spaghetti = 0; 
    String spagnm = null; 

    String menu = "\nSPAGHETTI: \n1.Regular spaghetti---->60 \n2.Large Spaghetti---->70"; 
    choice = Integer.parseInt(JOptionPane.showInputDialog("Please select: " 
      + menu)); 

    switch (choice) { 
    case 1: 
     spaghetti += 60; 
     spagnm = "Regular Spaghetti - "; 
     quantity = Integer.parseInt(JOptionPane 
       .showInputDialog("How many do you want??? ")); 
     break; 
    case 2: 
     spaghetti += 70; 
     spagnm = "Large Spaghetti - "; 
     quantity = Integer.parseInt(JOptionPane 
       .showInputDialog("How many do you want??? ")); 
     break; 
    default: 
     JOptionPane.showMessageDialog(null, "INVALID"); 
    } 

    if (quantity < 0) { 
     JOptionPane.showMessageDialog(null, "INVALID"); 
    } else { 
     totalspag = quantity * spaghetti; 
     spagnm = spagnm + totalspag + "\n"; 
     JOptionPane.showMessageDialog(null, "Total = " + spagnm); 

     try { 
      PrintWriter printW = new PrintWriter(new FileWriter("cash.txt", 
        true)); 
      if (choice == 1) { 
       printW.println("Customer's Order " + "\n" + quantity 
         + spagnm); 
      } else if (choice == 2) 
       printW.println("Customer's Order " + "\n" + quantity 
         + spagnm); 

      JOptionPane.showMessageDialog(null, 
        "Record successfully stored "); 
      printW.close(); 

     } catch (Exception e) { 
     } 
    } 
    totalOrder = totalOrder + totalspag; 
    return totalspag; 
} 

public int Fries() { 
    int choice, fries = 0, quantity = 0, totalfries = 0; 
    String friesnm = ""; 
    String menu = "\nFRIES: \n1.Regular Fries ----> 35\n2.Medium Fries ----> 45 \n3.LargeFries ----> 55"; 
    choice = Integer.parseInt(JOptionPane.showInputDialog("Please select: " 
      + menu)); 

    switch (choice) { 
    case 1: 
     fries += 35; 
     friesnm = "Regular Fries - "; 
     quantity = Integer.parseInt(JOptionPane 
       .showInputDialog("How many do you want??? ")); 
     break; 
    case 2: 
     fries += 45; 
     friesnm = "Medium Fries - "; 
     quantity = Integer.parseInt(JOptionPane 
       .showInputDialog("How many do you want??? ")); 
     break; 
    case 3: 
     fries += 55; 
     friesnm = "Large Fries - "; 
     quantity = Integer.parseInt(JOptionPane 
       .showInputDialog("How many do you want??? ")); 
     break; 
    default: 
     JOptionPane.showMessageDialog(null, "INVALID"); 
    } 

    if (quantity < 0) { 
     JOptionPane.showMessageDialog(null, "INVALID"); 
    } else { 
     totalfries = quantity * fries; 
     friesnm = friesnm + totalfries + "\n"; 
     JOptionPane.showMessageDialog(null, "Total = " + friesnm); 

     try { 
      PrintWriter printW = new PrintWriter(new FileWriter("cash.txt", 
        true)); 
      if (choice == 1) { 
       printW.println("Customer's Order " + "\n" + quantity 
         + friesnm); 
      } else if (choice == 2) 
       printW.println("Customer's Order " + "\n" + quantity 
         + friesnm); 
      else if (choice == 3) 
       printW.println("Customer's Order " + "\n" + quantity 
         + friesnm); 

      JOptionPane.showMessageDialog(null, 
        "Record successfully stored "); 
      printW.close(); 

     } catch (Exception e) { 
     } 
    } 
    totalOrder = totalOrder + totalfries; 
    return totalfries; 
} 

public int Sundae() { 
    int choice, quantity = 0, sundae = 0, totalsun = 0; 
    String sundaenm = ""; 
    String menu = "\nSUNDAE: \n1.Choco sundae ----> 28 \n2.Caramel sundae ----> 28 \n3.Strawberry sundae ----> 28"; 
    choice = Integer.parseInt(JOptionPane.showInputDialog("Please select: " 
      + menu)); 
    switch (choice) { 
    case 1: 
     sundae += 28; 
     sundaenm = " Choco Sundae -"; 
     quantity = Integer.parseInt(JOptionPane 
       .showInputDialog("How many do you want??? ")); 
     break; 
    case 2: 
     sundae += 28; 
     sundaenm = " Caramel Sundae "; 
     quantity = Integer.parseInt(JOptionPane 
       .showInputDialog("How many do you want??? ")); 
     break; 
    case 3: 
     sundae += 28; 
     sundaenm = " Strawberry Sundae - "; 
     quantity = Integer.parseInt(JOptionPane 
       .showInputDialog("How many do you want??? ")); 
     break; 
    default: 
     JOptionPane.showMessageDialog(null, "INVALID"); 
    } 

    if (quantity < 0) { 
     JOptionPane.showMessageDialog(null, "INVALID"); 
    } else { 
     totalsun = quantity * sundae; 
     sundaenm = sundaenm + totalsun + "\n"; 
     JOptionPane.showMessageDialog(null, "Total = " + sundaenm); 

     try { 
      PrintWriter printW = new PrintWriter(new FileWriter("cash.txt", 
        true)); 
      if (choice == 1) { 
       printW.println("Costumer's Order " + "\n" + quantity 
         + sundaenm); 
      } else if (choice == 2) 
       printW.println("Costumer's Order " + "\n" + quantity 
         + sundaenm); 
      else if (choice == 3) 
       printW.println("Costumer's Order " + "\n" + quantity 
         + sundaenm); 

      JOptionPane.showMessageDialog(null, 
        "Record successfully stored "); 
      printW.close(); 

     } catch (Exception e) { 
     } 
    } 
    totalOrder = totalOrder + totalsun; 
    return totalsun; 
} 

public int Drinks() { 
    int choice, quantity = 0, drinks = 0, totaldrinks = 0; 
    String drinksnm = ""; 
    String menu = "\nDRINKS: \n1.Reg. Coke ----> 25 \n2.Large Coke ----> 35 \n3.Reg.Sprite ----> 28 \n4.Large Sprite ----> 38"; 
    choice = Integer.parseInt(JOptionPane.showInputDialog("Please select: " 
      + menu)); 

    switch (choice) { 
    case 1: 
     drinks += 25; 
     drinksnm = "Regular Coke - "; 
     quantity = Integer.parseInt(JOptionPane 
       .showInputDialog("How many do you want??? ")); 
     break; 
    case 2: 
     drinks += 35; 
     drinksnm = "Large Coke - "; 
     quantity = Integer.parseInt(JOptionPane 
       .showInputDialog("How many do you want??? ")); 
     break; 
    case 3: 
     drinks += 28; 
     drinksnm = "Regular Sprite - "; 
     quantity = Integer.parseInt(JOptionPane 
       .showInputDialog("How many do you want??? ")); 
     break; 
    case 4: 
     drinksnm = "Large Sprite - "; 
     quantity = Integer.parseInt(JOptionPane 
       .showInputDialog("How many do you want??? ")); 

     break; 
    default: 
     JOptionPane.showMessageDialog(null, "INVALID"); 
    } 

    if (quantity < 0) { 
     JOptionPane.showMessageDialog(null, "INVALID"); 
    } else { 
     totaldrinks = quantity * drinks; 
     drinksnm = drinksnm + totaldrinks + "\n"; 
     JOptionPane.showMessageDialog(null, "Total = " + drinksnm); 

     try { 
      PrintWriter printW = new PrintWriter(new FileWriter("cash.txt", 
        true)); 
      if (choice == 1) { 
       printW.println("Costumer's Order " + "\n" + quantity 
         + drinksnm); 
      } else if (choice == 2) 
       printW.println("Costumer's Order " + "\n" + quantity 
         + drinksnm); 
      else if (choice == 3) 
       printW.println("Costumer's Order " + "\n" + quantity 
         + drinksnm); 

      else if (choice == 4) 
       printW.println("Costumer's Order " + "\n" + quantity 
         + drinksnm); 

      JOptionPane.showMessageDialog(null, 
        "Record successfully stored "); 
      printW.close(); 

     } catch (Exception e) { 
     } 
    } 
    totalOrder = totalOrder + totaldrinks; 
    return totaldrinks; 
} 

public void orderList() { 
    try { 
     Scanner in = new Scanner(new FileReader("cash.txt")); 
     String all = "\nWELCOME !!!! \nCASHIERING SYSTEM\n"; 
     while (in.hasNext()) { 
      all = all + in.nextLine() + "\n"; 

     } 
     JOptionPane.showMessageDialog(null, new JTextArea(all)); 
     in.close(); 
    } catch (Exception e) { 
    } 
} 

public void getTotal() { 
    JOptionPane.showMessageDialog(null, "Order Total\n"+ totalOrder + ""); 

} 

public void menu2() { 
    String menu[] = { "Burger", "Spaghetti", "Fries", "Ice Sundae", 
      "Drinks", "Exit" }; 
    String choice = ""; 
    char ch; 
    int yesorno = 0; 

    do { 
     choice = (String) JOptionPane.showInputDialog(null, 
       "Please select your order", "menu", 1, null, menu, menu[0]); 
     ch = choice.toLowerCase().charAt(0); 
     switch (ch) { 

     case 'b': 
      burger(); 
      yesorno = Integer 
        .parseInt(JOptionPane 
          .showInputDialog("Do you still have any order?? \n 1.Yes \n2.No")); 
      if (yesorno == 1) { 
       menu2(); 

      } else { 
       Cashiering(); 
      } 

      break; 
     case 's': 
      Spaghetti(); 
      yesorno = Integer 
        .parseInt(JOptionPane 
          .showInputDialog("Do you still have any order?? \n 1.Yes \n2.No")); 
      if (yesorno == 1) { 
       menu2(); 

      } else { 
       Cashiering(); 
      } 
      break; 
     case 'f': 
      Fries(); 
      yesorno = Integer 
        .parseInt(JOptionPane 
          .showInputDialog("Do you still have any order?? \n 1.Yes \n2.No")); 
      if (yesorno == 1) { 
       menu2(); 

      } else { 
       Cashiering(); 
      } 

      break; 
     case 'i': 
      Sundae(); 
      yesorno = Integer 
        .parseInt(JOptionPane 
          .showInputDialog("Do you still have any order?? \n 1.Yes \n2.No")); 
      if (yesorno == 1) { 
       menu2(); 

      } else { 
       Cashiering(); 
      } 
      break; 
     case 'd': 
      Drinks(); 
      yesorno = Integer 
        .parseInt(JOptionPane 
          .showInputDialog("Do you still have any order?? \n 1.Yes \n2.No")); 
      if (yesorno == 1) { 
       menu2(); 

      } else { 
       Cashiering(); 
      } 

      break; 
     case 'e': 
      break; 

     } 

    } while (!choice.equals("Close")); 
    } 
} 
+0

Я уже пробовал это. но когда я вызываю метод getTotal(), метод, в котором они упорядочиваются, появится снова. то, что я действительно хочу, когда я вызываю метод getTotal(), методы, в которых они заказываются, больше не появятся. – pep

+0

Отредактировано ... – DroidStunter

+0

Сообщите мне, если это сработало для вас. Он должен вернуть общую сумму заказа в MessageDialog. Вы можете изменить его, чтобы отобразить, как вам нужно. :-) – DroidStunter

Смежные вопросы