/
Form1.cs
158 lines (136 loc) · 5.34 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace AbstractFactory
{
public partial class Form1 : Form
{
string inputFileExtension;
string selectedPath;
string result = string.Empty;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "Please wait! The Menu is being generated based on your selection!";
textBox1.Text = string.Empty;
string restaurantCountry = string.Empty;
string restaurantCategory = string.Empty;
string outputFormat = string.Empty;
string inputReader = string.Empty;
var FD = new System.Windows.Forms.OpenFileDialog();
if (FD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
selectedPath = FD.FileName;
inputFileExtension = Path.GetExtension(FD.FileName);
}
if (britishRadioBtn.Checked)
{
restaurantCountry = "British";
inputReader = inputFileExtension.Replace('.',' ').Trim(); // "xml";
}
else if (americanRadioBtn.Checked)
{
restaurantCountry = "American";
inputReader = inputFileExtension.Replace('.', ' ').Trim(); //"json";
}
if(dinerRadioBtn.Checked)
{
restaurantCategory = "Diner";
}
else if (eveningRadioBtn.Checked)
{
restaurantCategory = "Evening";
}
else if(allDayRadioBtn.Checked)
{
restaurantCategory = "All";
}
if(plainTextRadioBtn.Checked)
{
outputFormat = "txt";
}
else if(htmlRadioBtn.Checked)
{
outputFormat = "html";
}
else if(xmlRadioBtn.Checked)
{
outputFormat = "xml";
}
Decider objDecider = new Decider(inputReader, outputFormat);
string fileExtension = Path.GetExtension(selectedPath);
result = objDecider.getMenuDetail(restaurantCountry, restaurantCategory, selectedPath, fileExtension);
if (!string.IsNullOrEmpty(result))
{
var savePath = Path.Combine(Application.StartupPath, "Menu." + outputFormat);
File.WriteAllText(savePath, result);
label1.Text = string.Format(@"Menu generated and saved. The path is displayed below in the text box. If you want to save the output to another location click the 'Save File' button.");
textBox1.Text = savePath;
}
else
{
label1.Text = "No File generated";
}
}
public class Decider
{
string InputReader = string.Empty;
string OutputFormat = string.Empty;
public Decider(string inputReader, string outputFormat)
{
InputReader = inputReader;
OutputFormat = outputFormat;
}
public string getMenuDetail(string factoryType, string restaurantType, string relativePath, string fileExtension)
{
if (factoryType == "British")
{
IRestaurantFactory factory = new BritishRestaurant();
var readMenu = factory.Reader(InputReader);
var menugen = factory.MenuGenerator();
var menuFormatter = factory.MenuOutputFormatter(OutputFormat);
var inputFile = File.ReadAllText(relativePath);
var data = readMenu.readFile2(inputFile, factoryType);
var genData = menugen.generateMenu(data, restaurantType);
var formattedData = menuFormatter.formatText(genData);
return formattedData.ToString();
}
else if (factoryType == "American")
{
IRestaurantFactory factory = new AmericanRestaurant();
var readMenu = factory.Reader(InputReader);
var menugen = factory.MenuGenerator();
var menuFormatter = factory.MenuOutputFormatter(OutputFormat);
var inputFile = File.ReadAllText(relativePath);
var data = readMenu.readFile2(inputFile, factoryType);
var genData = menugen.generateMenu(data, restaurantType);
var formattedData = menuFormatter.formatText(genData);
return formattedData.ToString();
}
else
{
return string.Empty;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
saveFileDialog1.ShowDialog();
}
private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
string name = saveFileDialog1.FileName;
File.WriteAllText(name, result);
}
}
}