Sie sind auf Seite 1von 11

Planning

Day 1 plan (ideas only)


- Create restaurant class
- Creates object with the following parameters:
- Arrays of discount percent on a particular time:
- [ [discount percent, time], [discount percent, time], ... ]
- Maybe use a 2d array
- Methods:
- discountTime(String time, int percentage)

- Create Database class (collects restaurant objects)


- addToDatabase() // adds restaurant to the list
- comparePrice() // compare price between 2 restaurant and returns the cheaper one

3 Files we have to work on

1. Restaurant class
2. Database class (aka test program to add restaurant)
Day 2 plan (wow this should be the plan we’re sticking with)
- Main (driver class)
- BackStage:
- Create an array for collecting all restaurant object
- Define all restaurants and the promotions
- Adds each of the restaurant object into the array

- List all restaurant names


- Ask user to type in the name of their desired restaurant they wanna learn more about
- If the user types in a name of the restaurants
- List all promotion during the day
- If the user types in incorrectly
- Ask if they want to add restaurant

- Class Restaurant
- (Static array called allRestaurant // to gather all restaurant’s data)
- Object’s variables / parameters
- String name
- 2d array of discount% and time
- Optional: list of tags
- Constructor(name, array):
- Set name
- addToArray(array)
- Method
- addToArray(array)
- array.append(this)
- getAllDiscount()
- Print all the discount of this restaurant
- addRestaurantPromotion(time, discount)
- add into the 2d array of the discount list

First progress
- Main (driver class)
- BackStage:
- Create an array for collecting all restaurant object (​O​)
- Define all restaurants and the promotions (​O​)
- Adds each of the restaurant object into the array (​O​)

- Class Restaurant
- Object’s variables / parameters
- String name
- 2d array of discount% and time
- Constructor(name):
- Set name
- Method
- addPromotion(time, discount)
- add into the 2d array of the discount list
- printAll()
- Print all the discount of this restaurant
- *Added* will not print the array that has not been set a promotion
- printPromotion()
- Print the discount at a specific row

Added stuff:
● toTime()
- In case invalid time (12:60, -12:00, 25:00) is inputted, it will justify the user that this is
not valid: so that the time matches
● Used a sleep method from the Thread class

Portal to Repl.it
https://repl.it/@ThanawinBoonpoj/TheProject

Main.java
import​ java​.​util​.​Arrays​;
import​ java​.​lang​.​Thread​;
import​ java​.​util​.​Scanner​;

class​ Main ​{
​public​ ​static​ ​void​ main​(​String​[]​ args​)​ ​throws​ InterruptedException ​{
​// this is how to deal with sleep method bc it always throw an
InterruptedException error

​//=====================================================
​// Set up for restaurant info

Restaurant a ​=​ ​new​ Restaurant​(​"Fuji"​);


a​.​addPromotion​(​12.00​,​ ​50​);
a​.​addPromotion​(​13.30​,​ ​20​);
a​.​addPromotion​(​17.30​,​ ​50​);

Restaurant b ​=​ ​new​ Restaurant​(​"Siao Long Bao"​);


b​.​addPromotion​(​8.00​,​ ​10​);
b​.​addPromotion​(​12.30​,​ ​10​);
b​.​addPromotion​(​18.00​,​ ​40​);

Restaurant c ​=​ ​new​ Restaurant​(​"Kentucky Fried Chicken"​);


c​.​addPromotion​(​1.00​,​ ​5​);
c​.​addPromotion​(​7.30​,​ ​25​);
c​.​addPromotion​(​12.00​,​ ​30​);
c​.​addPromotion​(​15.00​,​ ​10​);

Restaurant d ​=​ ​new​ Restaurant​(​"Santa Fe"​);


d​.​addPromotion​(​11.00​,​ ​10​);
d​.​addPromotion​(​12.00​,​ ​40​);
d​.​addPromotion​(​13.00​,​ ​20​);
d​.​addPromotion​(​15.00​,​ ​30​);

Restaurant allRestaurant​[]​ ​=​ ​new


Restaurant​[​Restaurant​.​totalRestaurant​];
allRestaurant​[​0​]​ ​=​ a​;
allRestaurant​[​1​]​ ​=​ b​;
allRestaurant​[​2​]​ ​=​ c​;
allRestaurant​[​3​]​ ​=​ d​;
​//=====================================================
​// Real Program

​//Greetings
System​.​out​.​println​();
System​.​out​.​println​(​"Good day, my mate!"​);
System​.​out​.​println​(​"Welcome to YEETIGO,"​);
System​.​out​.​println​(​"where you get imaginary discounts in Mr.Task's
class"​);
System​.​out​.​println​();
Thread​.​sleep​(​5000​);

System​.​out​.​println​(​"Here's the ongoing discount we have:"​);


System​.​out​.​println​(​"Format: [time of promotion, discount percent]
\n"​);

​//Print all the restaurant's informations


Thread​.​sleep​(​5000​);
​for​ ​(​int​ i ​=​ ​0​;​ i ​<​ allRestaurant​.​length​;​ i ​++){
System​.​out​.​println​(​allRestaurant​[​i​].​name ​+​ ​" ("​ ​+​ ​(​i​+​1​)​ ​+​ ​"): "​);
allRestaurant​[​i​].​printAll​();
System​.​out​.​println​();
Thread​.​sleep​(​2000​);
​}

​//Ask user for info


System​.​out​.​println​(​"Please select the number in the brackets behind
the restaurant name of the one you wish to receive discount: "​);

Scanner x ​=​ ​new​ Scanner​(​System​.​in​);


​int​ answer1 ​=​ x​.​nextInt​();
Thread​.​sleep​(​500​);
System​.​out​.​println​();

System​.​out​.​println​(​"Please enter the row number of the discount that


you are reserving for : "​);

Scanner y ​=​ ​new​ Scanner​(​System​.​in​);


​int​ answer2 ​=​ y​.​nextInt​();
Thread​.​sleep​(​500​);
System​.​out​.​println​();

​//Confirm Answer
​if​ ​(​answer1 ​-​ ​1​ ​<​ allRestaurant​.​length​){
Thread​.​sleep​(​1000​);
System​.​out​.​println​(​"Detail of your reservation: "​);
System​.​out​.​println​(​"Restaurant: "​ ​+​ allRestaurant​[​answer1 ​-
1​].​name​);
System​.​out​.​println​(​"Discount: "​);
allRestaurant​[​answer1 ​-​ ​1​].​printPromotion​(​answer2​);
Thread​.​sleep​(​1000​);

System​.​out​.​println​();
​}
​else​{
System​.​out​.​println​(​"Invalid number."​);
System​.​out​.​println​(​"Make sure you read the instruction carefully
next time!"​);
​}
Thread​.​sleep​(​500​);

​// Add in your own restaurant


System​.​out​.​println​(​"By the way, do you want to add in your own
restaurant? [y/n]\n "​);

Scanner z ​=​ ​new​ Scanner​(​System​.​in​);


String answer3 ​=​ z​.​nextLine​();

System​.​out​.​println​(​answer3​);

​if​ ​(​answer3​.​equalsIgnoreCase​(​"y"​)){​ ​// in case the user wants to


create a restaurant
System​.​out​.​println​(​"Please enter the name of your restaurant: \n
"​);
Scanner w ​=​ ​new​ Scanner​(​System​.​in​);
String answer4 ​=​ w​.​nextLine​();

Restaurant custom ​=​ ​new​ Restaurant​(​answer4​);

System​.​out​.​println​(​"Please enter the discount percentage: \n "​);

Scanner v ​=​ ​new​ Scanner​(​System​.​in​);


​int​ answer5 ​=​ v​.​nextInt​();

System​.​out​.​println​(​"Please enter the time of your promotion: \n "​);

Scanner q ​=​ ​new​ Scanner​(​System​.​in​);


​double​ answer6 ​=​ q​.​nextDouble​();

​if​ ​(​custom​.​toTime​(​answer6​)){​ ​// if the time is invalid the program


will not initiate the
custom​.​addPromotion​(​answer6​,​ answer5​);
Thread​.​sleep​(​500​);

System​.​out​.​println​(​"This is your restaurant information: "​);


Thread​.​sleep​(​500​);
System​.​out​.​println​(​"Restaurant name: "​ ​+​ custom​.​name​);
System​.​out​.​println​(​"Discount of the restaurant: "​);
custom​.​printPromotion​(​1​);
Thread​.​sleep​(​2000​);

System​.​out​.​println​(​"\n Thank you very much for using YEETIGO!


Good bye, and have a good yoinks!"​);
​}
​else​{
System​.​out​.​println​(​"\n Restaurant registration failed. Please try
again next time! BYE!"​);
​}
w​.​close​();
v​.​close​();
q​.​close​();
​}
​else​{
System​.​out​.​println​(​"That is totally fine! Thank you for using
YEETIGO! Good bye, and have a good yoinks!"​);
​}
x​.​close​();
y​.​close​();
z​.​close​();
​}

Restaurant.java
import​ java​.​util​.​Arrays​;

public​ ​class​ Restaurant​{

String name​;​ ​// the restaurant name


​int​ totalPromotion ​=​ ​0​;​ ​// how many promotion does this restaurant have
​double​ promotion​[][]​ ​=​ ​new​ ​double​[​10​][​2​];​ ​// list of the promotion
​static​ ​int​ totalRestaurant ​=​ ​0​;​ ​// keeping track of the number of the
restaurant
​//No default constructor: We want the user to put everything by
themselves
​public​ Restaurant​(​String name​){
​this​.​name ​=​ name​;​ ​//set the name into input
totalRestaurant ​++;​ ​// increase restauran number
​}

​public​ ​void​ addPromotion​(​double​ time​,​ ​int​ discount​){


​// the if else statement in this part helps in keeping track of
OutOfBound errors that would crash the program
​if​ ​(​totalPromotion ​<​ ​24​){
promotion​[​totalPromotion​][​0​]​ ​=​ time​;​ ​// column 1 is time
promotion​[​totalPromotion​][​1​]​ ​=​ discount​;​ ​// column 2 is discount
totalPromotion​++;​ ​// to increase the total count of promotion
​}
​else​ ​{
System​.​out​.​println​(​"Maximium promotion capacity reached."​);
​}
​}

​public​ ​boolean​ toTime​(​double​ x​)​ ​{​ ​// this is a function made in case
of more performance improvement or added function to the program (eg.
search for promotion using time)
​if​ ​(​x ​<​ ​0​)​ ​{
​//Negative time
System​.​out​.​println​(​"Invalid time: Negative value. Please
re-enter."​);
​return​ ​false​;

​}​ ​else​ ​if​ ​(​x​%​1​ ​>=​ ​0.5999999999999996​)​ ​{


​// time = XX.60 or more
System​.​out​.​println​(​"Invalid time: Minute syntax error. Please
re-enter."​);
​return​ ​false​;

​}​ ​else​ ​if​ ​(​x ​>​ ​24​)​ ​{


​// not 24 hour time
System​.​out​.​println​(​"Invalid time: Hour syntax error. Please
re-enter."​);
​return​ ​false​;

​}​ ​else​{
​return​ ​true​;
​}
​}

​public​ ​void​ printPromotion​(​int​ r​){​ ​// this method is for printing a


specific line of promotion
​if​ ​(​r ​<​ ​10​){​ ​// checking if the inputted number is more than the
capacity of the array itself
​for​(​int​ i​=​0​;​ i​<​promotion​[​r​-​1​].​length​;​ i​++){

​if​ ​(​promotion​[​r​-​1​][​i​]​ ​!=​ ​0.0​)​ ​{​ ​// checking if the promotion is


null
System​.​out​.​print​(​promotion​[​r​-​1​][​i​]);
​if​ ​(​i​<(​promotion​[​r​-​1​].​length​-​1​))​ ​{​ ​// enjoy the beauty of
punctuation
System​.​out​.​print​(​", "​);
​}
​}
​else​{
System​.​out​.​println​(​"There's no promotion at the selected
number"​);
​}
​}
​}
​else​ ​{
System​.​out​.​println​(​"There's no promotion at the selected number"​);
​}
​}
​public​ ​void​ printAll​()​ ​{​ ​// print every row of promotion
​for​(​double​[]​ k ​:​ promotion​)​ ​{
​if​ ​(​Arrays​.​toString​(​k​).​equals​(​"[0.0, 0.0]"​)){​ ​// checking if the
promotion at some point is null
System​.​out​.​print​(​""​);
​}
​else​{
System​.​out​.​println​(​Arrays​.​toString​(​k​));
​}
​}
​}
}

Das könnte Ihnen auch gefallen